org.apache.calcite.sql.SqlOperatorBinding Java Examples

The following examples show how to use org.apache.calcite.sql.SqlOperatorBinding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlRowOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  // The type of a ROW(e1,e2) expression is a record with the types
  // {e1type,e2type}.  According to the standard, field names are
  // implementation-defined.
  return opBinding.getTypeFactory().createStructType(
      new AbstractList<Map.Entry<String, RelDataType>>() {
        public Map.Entry<String, RelDataType> get(int index) {
          return Pair.of(
              SqlUtil.deriveAliasFromOrdinal(index),
              opBinding.getOperandType(index));
        }

        public int size() {
          return opBinding.getOperandCount();
        }
      });
}
 
Example #2
Source File: SqlAbstractTimeFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 20-Feb-2005: Need to take care of time zones.
  int precision = 0;
  if (opBinding.getOperandCount() == 1) {
    RelDataType type = opBinding.getOperandType(0);
    if (SqlTypeUtil.isNumeric(type)) {
      precision = opBinding.getOperandLiteralValue(0, Integer.class);
    }
  }
  assert precision >= 0;
  if (precision > SqlTypeName.MAX_DATETIME_PRECISION) {
    throw opBinding.newError(
        RESOURCE.argumentMustBeValidPrecision(
            opBinding.getOperator().getName(), 0,
            SqlTypeName.MAX_DATETIME_PRECISION));
  }
  return opBinding.getTypeFactory().createSqlType(typeName, precision);
}
 
Example #3
Source File: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // Here we know all the operands have the same type,
  // which has a size (precision), but not a scale.
  RelDataType ret = opBinding.getOperandType(0);
  SqlTypeName typeName = ret.getSqlTypeName();
  assert typeName.allowsPrecNoScale()
      : "LiteralChain has impossible operand type "
      + typeName;
  int size = 0;
  for (RelDataType type : opBinding.collectOperandTypes()) {
    size += type.getPrecision();
    assert type.getSqlTypeName() == typeName;
  }
  return opBinding.getTypeFactory().createSqlType(typeName, size);
}
 
Example #4
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  boolean isNullable = isNullable(opBinding.collectOperandTypes());

  boolean isScalarString = isScalarStringType(opBinding.getOperandType(0).getSqlTypeName());
  int precision = opBinding.getOperandType(0).getPrecision();

  if (isScalarString && precision != RelDataType.PRECISION_NOT_SPECIFIED) {
    RelDataType sqlType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, precision);
    return opBinding.getTypeFactory().createTypeWithNullability(sqlType, isNullable);
  }

  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      SqlTypeName.VARCHAR,
      isNullable);
}
 
Example #5
Source File: FlinkReturnTypes.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	RelDataType type = super.inferReturnType(opBinding);
	RelDataType newType;
	switch (type.getSqlTypeName()) {
		case CHAR:
			newType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, type.getPrecision());
			break;
		case VARCHAR:
			newType = type;
			break;
		default:
			throw new UnsupportedOperationException("Unsupported type: " + type);
	}
	return opBinding.getTypeFactory().createTypeWithNullability(newType, true);
}
 
Example #6
Source File: FlinkReturnTypes.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
	final RelDataType numType = opBinding.getOperandType(0);
	if (numType.getSqlTypeName() != SqlTypeName.DECIMAL) {
		return numType;
	}
	final BigDecimal lenVal;
	if (opBinding.getOperandCount() == 1) {
		lenVal = BigDecimal.ZERO;
	} else if (opBinding.getOperandCount() == 2) {
		lenVal = getArg1Literal(opBinding); // may return null
	} else {
		throw new AssertionError();
	}
	if (lenVal == null) {
		return numType; //
	}
	// ROUND( decimal(p,s), r )
	final int p = numType.getPrecision();
	final int s = numType.getScale();
	final int r = lenVal.intValueExact();
	DecimalType dt = LogicalTypeMerging.findRoundDecimalType(p, s, r);
	return opBinding.getTypeFactory().createSqlType(
		SqlTypeName.DECIMAL, dt.getPrecision(), dt.getScale());
}
 
Example #7
Source File: ComparableOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Similar functionality to
 * {@link #checkOperandTypes(SqlCallBinding, boolean)}, but not part of the
 * interface, and cannot throw an error.
 */
public boolean checkOperandTypes(
    SqlOperatorBinding operatorBinding, SqlCallBinding callBinding) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (type.getComparability().ordinal() < requiredComparability.ordinal()) {
      b = false;
      break;
    }
  }
  if (b) {
    b = super.checkOperandTypes(operatorBinding, callBinding);
  }
  return b;
}
 
Example #8
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static DrillFuncHolder resolveDrillFuncHolder(final SqlOperatorBinding opBinding,
    final List<DrillFuncHolder> functions, FunctionCall functionCall) {
  final FunctionResolver functionResolver = FunctionResolverFactory.getResolver(functionCall);
  final DrillFuncHolder func = functionResolver.getBestMatch(functions, functionCall);

  // Throw an exception
  // if no DrillFuncHolder matched for the given list of operand types
  if (func == null) {
    StringBuilder operandTypes = new StringBuilder();
    for (int i = 0; i < opBinding.getOperandCount(); ++i) {
      operandTypes.append(opBinding.getOperandType(i).getSqlTypeName());
      if (i < opBinding.getOperandCount() - 1) {
        operandTypes.append(",");
      }
    }

    throw UserException
        .functionError()
        .message(String.format("%s does not support operand types (%s)",
            opBinding.getOperator().getName(),
            operandTypes.toString()))
        .build(logger);
  }
  return func;
}
 
Example #9
Source File: SqlItemOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final RelDataType operandType = opBinding.getOperandType(0);
  switch (operandType.getSqlTypeName()) {
  case ARRAY:
    return typeFactory.createTypeWithNullability(
        operandType.getComponentType(), true);
  case MAP:
    return typeFactory.createTypeWithNullability(operandType.getValueType(),
        true);
  case ANY:
  case DYNAMIC_STAR:
    return typeFactory.createTypeWithNullability(
        typeFactory.createSqlType(SqlTypeName.ANY), true);
  default:
    throw new AssertionError();
  }
}
 
Example #10
Source File: OperatorBindingCallContext.java    From flink with Apache License 2.0 6 votes vote down vote up
public OperatorBindingCallContext(
		DataTypeFactory dataTypeFactory,
		FunctionDefinition definition,
		SqlOperatorBinding binding) {
	super(
		dataTypeFactory,
		definition,
		binding.getOperator().getNameAsId().toString());

	this.binding = binding;
	this.argumentDataTypes = new AbstractList<DataType>() {
		@Override
		public DataType get(int pos) {
			final LogicalType logicalType = FlinkTypeFactory.toLogicalType(binding.getOperandType(pos));
			return TypeConversions.fromLogicalToDataType(logicalType);
		}

		@Override
		public int size() {
			return binding.getOperandCount();
		}
	};
}
 
Example #11
Source File: SqlReturnTypeInferenceChain.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (SqlReturnTypeInference rule : rules) {
    RelDataType ret = rule.inferReturnType(opBinding);
    if (ret != null) {
      return ret;
    }
  }
  return null;
}
 
Example #12
Source File: SameOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected boolean checkOperandTypesImpl(
    SqlOperatorBinding operatorBinding,
    boolean throwOnFailure,
    SqlCallBinding callBinding) {
  int nOperandsActual = nOperands;
  if (nOperandsActual == -1) {
    nOperandsActual = operatorBinding.getOperandCount();
  }
  assert !(throwOnFailure && (callBinding == null));
  RelDataType[] types = new RelDataType[nOperandsActual];
  final List<Integer> operandList =
      getOperandList(operatorBinding.getOperandCount());
  for (int i : operandList) {
    types[i] = operatorBinding.getOperandType(i);
  }
  int prev = -1;
  for (int i : operandList) {
    if (prev >= 0) {
      if (!SqlTypeUtil.isComparable(types[i], types[prev])) {
        if (!throwOnFailure) {
          return false;
        }

        // REVIEW jvs 5-June-2005: Why don't we use
        // newValidationSignatureError() here?  It gives more
        // specific diagnostics.
        throw callBinding.newValidationError(
            RESOURCE.needSameTypeParameter());
      }
    }
    prev = i;
  }
  return true;
}
 
Example #13
Source File: SqlCaseOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 4-June-2005:  can't these be unified?
  if (!(opBinding instanceof SqlCallBinding)) {
    return inferTypeFromOperands(opBinding);
  }
  return inferTypeFromValidator((SqlCallBinding) opBinding);
}
 
Example #14
Source File: SameOperandTypeChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected boolean checkOperandTypesImpl(
    SqlOperatorBinding operatorBinding,
    boolean throwOnFailure,
    SqlCallBinding callBinding) {
  int nOperandsActual = nOperands;
  if (nOperandsActual == -1) {
    nOperandsActual = operatorBinding.getOperandCount();
  }
  assert !(throwOnFailure && (callBinding == null));
  RelDataType[] types = new RelDataType[nOperandsActual];
  final List<Integer> operandList =
      getOperandList(operatorBinding.getOperandCount());
  for (int i : operandList) {
    types[i] = operatorBinding.getOperandType(i);
  }
  int prev = -1;
  for (int i : operandList) {
    if (prev >= 0) {
      if (!SqlTypeUtil.isComparable(types[i], types[prev])) {
        if (!throwOnFailure) {
          return false;
        }

        // REVIEW jvs 5-June-2005: Why don't we use
        // newValidationSignatureError() here?  It gives more
        // specific diagnostics.
        throw callBinding.newValidationError(
            RESOURCE.needSameTypeParameter());
      }
    }
    prev = i;
  }
  return true;
}
 
Example #15
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  if (preserveNullability) {
    return opBinding.getOperandType(0);
  }
  return opBinding.getTypeFactory().createTypeWithNullability(opBinding.getOperandType(0), true);
}
 
Example #16
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  return factory.createTypeWithNullability(
    factory.createSqlType(SqlTypeName.ANY),
    true);
}
 
Example #17
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName type = SqlTypeName.VARBINARY;

  return createCalciteTypeWithNullability(factory, type, opBinding.getOperandType(0).isNullable(), null);
}
 
Example #18
Source File: SqlCaseOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  // REVIEW jvs 4-June-2005:  can't these be unified?
  if (!(opBinding instanceof SqlCallBinding)) {
    return inferTypeFromOperands(
        opBinding.getTypeFactory(),
        opBinding.collectOperandTypes());
  }
  return inferTypeFromValidator((SqlCallBinding) opBinding);
}
 
Example #19
Source File: SqlCaseOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RelDataType inferTypeFromOperands(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory typeFactory = opBinding.getTypeFactory();
  final List<RelDataType> argTypes = opBinding.collectOperandTypes();
  assert (argTypes.size() % 2) == 1 : "odd number of arguments expected: "
      + argTypes.size();
  assert argTypes.size() > 1 : "CASE must have more than 1 argument. Given "
    + argTypes.size() + ", " + argTypes;
  List<RelDataType> thenTypes = new ArrayList<>();
  for (int j = 1; j < (argTypes.size() - 1); j += 2) {
    RelDataType argType = argTypes.get(j);
    if (opBinding instanceof RexCallBinding) {
      final RexCallBinding rexCallBinding = (RexCallBinding) opBinding;
      final RexNode whenNode = rexCallBinding.operands().get(j - 1);
      final RexNode thenNode = rexCallBinding.operands().get(j);
      if (whenNode.getKind() == SqlKind.IS_NOT_NULL && argType.isNullable()) {
        // Type is not nullable if the kind is IS NOT NULL.
        final RexCall isNotNullCall = (RexCall) whenNode;
        if (isNotNullCall.getOperands().get(0).equals(thenNode)) {
          argType = typeFactory.createTypeWithNullability(argType, false);
        }
      }
    }
    thenTypes.add(argType);
  }

  thenTypes.add(Iterables.getLast(argTypes));
  return typeFactory.leastRestrictive(thenTypes);
}
 
Example #20
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      returnType,
      false);
}
 
Example #21
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final boolean isNullable = opBinding
      .getOperandType(0)
      .isNullable();

  RelDataType ret = factory.createTypeWithNullability(
      opBinding.getOperandType(1),
      isNullable);
  if (opBinding instanceof SqlCallBinding) {
    SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    SqlNode operand0 = callBinding.operand(0);

    // dynamic parameters and null constants need their types assigned
    // to them using the type they are casted to.
    if(((operand0 instanceof SqlLiteral)
        && (((SqlLiteral) operand0).getValue() == null))
            || (operand0 instanceof SqlDynamicParam)) {
      callBinding.getValidator().setValidatedNodeType(
          operand0,
          ret);
    }
  }

  return ret;
}
 
Example #22
Source File: MatchReturnTypeInference.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  for (int i = start; i < opBinding.getOperandCount(); i++) {
    RelDataType argType = opBinding.getOperandType(i);
    if (SqlTypeUtil.isOfSameTypeName(typeNames, argType)) {
      return argType;
    }
  }
  return null;
}
 
Example #23
Source File: ComparableOperandTypeChecker.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Similar functionality to
 * {@link #checkOperandTypes(SqlCallBinding, boolean)}, but not part of the
 * interface, and cannot throw an error.
 */
public boolean checkOperandTypes(
    SqlOperatorBinding callBinding) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (type.getComparability().ordinal() < requiredComparability.ordinal()) {
      b = false;
    }
  }
  if (b) {
    b = super.checkOperandTypes(callBinding);
  }
  return b;
}
 
Example #24
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
  public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    return TypeInferenceUtils.createCalciteTypeWithNullability(
        opBinding.getTypeFactory(),
        SqlTypeName.ANY,
        opBinding.getOperandType(0).isNullable());
}
 
Example #25
Source File: SqlBetweenOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  SqlCallBinding callBinding = (SqlCallBinding) opBinding;
  ExplicitOperatorBinding newOpBinding =
      new ExplicitOperatorBinding(
          opBinding,
          collectOperandTypes(
              callBinding.getValidator(),
              callBinding.getScope(),
              callBinding.getCall()));
  return ReturnTypes.BOOLEAN_NULLABLE.inferReturnType(
      newOpBinding);
}
 
Example #26
Source File: SqlMultisetValueConstructor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelDataType inferReturnType(
    SqlOperatorBinding opBinding) {
  RelDataType type =
      getComponentType(
          opBinding.getTypeFactory(),
          opBinding.collectOperandTypes());
  if (null == type) {
    return null;
  }
  return SqlTypeUtil.createMultisetType(
      opBinding.getTypeFactory(),
      type,
      false);
}
 
Example #27
Source File: DrillCalciteSqlAggFunctionWrapper.java    From Bats with Apache License 2.0 5 votes vote down vote up
public DrillCalciteSqlAggFunctionWrapper(
    final SqlAggFunction sqlAggFunction,
    final RelDataType relDataType) {
  this(sqlAggFunction, new SqlReturnTypeInference() {
    @Override
    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
      return relDataType;
    }
  });
}
 
Example #28
Source File: PlugginRepositorySqlReturnTypeInference.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  for (RelDataType type : opBinding.collectOperandTypes()) {
    final TypeProtos.MinorType minorType = TypeInferenceUtils.getMinorTypeFromCalciteType(type);
    if(minorType == TypeProtos.MinorType.LATE) {
      return opBinding.getTypeFactory()
        .createTypeWithNullability(
          opBinding.getTypeFactory().createSqlType(SqlTypeName.ANY),
          true);
    }
  }

  final FunctionCall functionCall = TypeInferenceUtils.convertSqlOperatorBindingToFunctionCall(opBinding);
  final AbstractFunctionHolder funcHolder = registry.getFunction(functionCall);
  if(funcHolder == null) {
    final StringBuilder operandTypes = new StringBuilder();
    for(int j = 0; j < opBinding.getOperandCount(); ++j) {
      operandTypes.append(opBinding.getOperandType(j).getSqlTypeName());
      if(j < opBinding.getOperandCount() - 1) {
        operandTypes.append(",");
      }
    }

    throw UserException
      .functionError()
      .message(String.format("%s does not support operand types (%s)",
        opBinding.getOperator().getName(),
        operandTypes))
      .build(logger);
  }

  return TypeInferenceUtils.createCalciteTypeWithNullability(
    opBinding.getTypeFactory(),
    TypeInferenceUtils.getCalciteTypeFromMinorType(funcHolder.getReturnType(null).toMinorType()),
    true,
    null);
}
 
Example #29
Source File: TypeInferenceUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  if (opBinding instanceof SqlCallBinding && (((SqlCallBinding) opBinding).operand(1) instanceof  SqlNumericLiteral)) {
    int precision = ((SqlNumericLiteral) ((SqlCallBinding) opBinding).operand(1)).intValue(true);
    RelDataType sqlType = opBinding.getTypeFactory().createSqlType(SqlTypeName.VARCHAR, Math.max(precision, 0));
    return opBinding.getTypeFactory().createTypeWithNullability(sqlType, isNullable(opBinding.collectOperandTypes()));
  }

  return createCalciteTypeWithNullability(
      opBinding.getTypeFactory(),
      SqlTypeName.VARCHAR,
      isNullable(opBinding.collectOperandTypes()));

}
 
Example #30
Source File: TypeInferenceUtils.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
  final RelDataTypeFactory factory = opBinding.getTypeFactory();
  final SqlTypeName sqlTypeName = SqlTypeName.VARCHAR;

  for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    if(opBinding.getOperandType(i).isNullable()) {
      return createCalciteTypeWithNullability(factory, sqlTypeName, true, null);
    }
  }

  return createCalciteTypeWithNullability(factory, sqlTypeName, false, null);
}