Java Code Examples for org.apache.calcite.sql.SqlCallBinding#getOperandCount()

The following examples show how to use org.apache.calcite.sql.SqlCallBinding#getOperandCount() . 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: SqlLiteralChainOperator.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean argTypesValid(SqlCallBinding callBinding) {
  if (callBinding.getOperandCount() < 2) {
    return true; // nothing to compare
  }
  RelDataType firstType = null;
  for (Ord<SqlNode> operand : Ord.zip(callBinding.operands())) {
    RelDataType type =
        callBinding.getValidator().deriveType(
            callBinding.getScope(),
            operand.e);
    if (operand.i == 0) {
      firstType = type;
    } else {
      if (!SqlTypeUtil.sameNamedType(firstType, type)) {
        return false;
      }
    }
  }
  return true;
}
 
Example 2
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypesWithoutTypeCoercion(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (families.size() != callBinding.getOperandCount()) {
    // assume this is an inapplicable sub-rule of a composite rule;
    // don't throw exception.
    return false;
  }

  for (Ord<SqlNode> op : Ord.zip(callBinding.operands())) {
    if (!checkSingleOperandType(
        callBinding,
        op.e,
        op.i,
        throwOnFailure)) {
      return false;
    }
  }
  return true;
}
 
Example 3
Source File: SqlPosixRegexOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  int operandCount = callBinding.getOperandCount();
  if (operandCount != 2 && operandCount != 3) {
    throw new AssertionError(
        "Unexpected number of args to " + callBinding.getCall() + ": " + operandCount);
  }

  RelDataType op1Type = callBinding.getOperandType(0);
  RelDataType op2Type = callBinding.getOperandType(1);

  if (!SqlTypeUtil.isComparable(op1Type, op2Type)) {
    throw new AssertionError(
        "Incompatible first two operand types " + op1Type + " and " + op2Type);
  }

  return SqlTypeUtil.isCharTypeComparable(
      callBinding,
      callBinding.operands().subList(0, 2),
      throwOnFailure);
}
 
Example 4
Source File: SqlJsonObjectFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 5
Source File: SqlPositionFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // check that the two operands are of same type.
  switch (callBinding.getOperandCount()) {
  case 2:
    return OperandTypes.SAME_SAME.checkOperandTypes(
        callBinding, throwOnFailure)
        && super.checkOperandTypes(callBinding, throwOnFailure);

  case 3:
    return OperandTypes.SAME_SAME_INTEGER.checkOperandTypes(
        callBinding, throwOnFailure)
        && super.checkOperandTypes(callBinding, throwOnFailure);
  default:
    throw new AssertionError();
  }
}
 
Example 6
Source File: SqlLiteralChainOperator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean argTypesValid(SqlCallBinding callBinding) {
  if (callBinding.getOperandCount() < 2) {
    return true; // nothing to compare
  }
  RelDataType firstType = null;
  for (Ord<SqlNode> operand : Ord.zip(callBinding.operands())) {
    RelDataType type =
        callBinding.getValidator().deriveType(
            callBinding.getScope(),
            operand.e);
    if (operand.i == 0) {
      firstType = type;
    } else {
      if (!SqlTypeUtil.sameNamedType(firstType, type)) {
        return false;
      }
    }
  }
  return true;
}
 
Example 7
Source File: TypeCoercionImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Coerces operands in binary arithmetic expressions to NUMERIC types.
 *
 * <p>For binary arithmetic operators like [+, -, *, /, %]:
 * If the operand is VARCHAR,
 * coerce it to data type of the other operand if its data type is NUMERIC;
 * If the other operand is DECIMAL,
 * coerce the STRING operand to max precision/scale DECIMAL.
 */
public boolean binaryArithmeticCoercion(SqlCallBinding binding) {
  // Assume the operator has NUMERIC family operand type checker.
  SqlOperator operator = binding.getOperator();
  SqlKind kind = operator.getKind();
  boolean coerced = false;
  // Binary operator
  if (binding.getOperandCount() == 2) {
    final RelDataType type1 = binding.getOperandType(0);
    final RelDataType type2 = binding.getOperandType(1);
    // Special case for datetime + interval or datetime - interval
    if (kind == SqlKind.PLUS || kind == SqlKind.MINUS) {
      if (SqlTypeUtil.isInterval(type1) || SqlTypeUtil.isInterval(type2)) {
        return false;
      }
    }
    // Binary arithmetic operator like: + - * / %
    if (kind.belongsTo(SqlKind.BINARY_ARITHMETIC)) {
      coerced = binaryArithmeticWithStrings(binding, type1, type2);
    }
  }
  return coerced;
}
 
Example 8
Source File: DremioArgChecker.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  if (checkers.size() != callBinding.getOperandCount()) {
    // assume this is an inapplicable sub-rule of a composite rule;
    // don't throw
    return false;
  }

  for (Ord<SqlNode> op : Ord.zip(callBinding.operands())) {
    if (!checkSingleOperandType(
        callBinding,
        op.e,
        op.i,
        throwOnFailure)) {
      return false;
    }
  }
  return true;
}
 
Example 9
Source File: DrillCalciteSqlBetweenOperatorWrapper.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Since Calcite has its rule for type compatibility
 * (see {@link org.apache.calcite.sql.type.SqlTypeUtil#isComparable(org.apache.calcite.rel.type.RelDataType,
 * org.apache.calcite.rel.type.RelDataType)}), which is usually different from Drill's, this method is overridden here to avoid
 * Calcite early terminating the queries.
 */
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
  final List<TypeProtos.MinorType> types = new ArrayList<>();
  for (int i = 0; i < callBinding.getOperandCount(); i++) {
    final TypeProtos.MinorType inMinorType = TypeInferenceUtils.getDrillTypeFromCalciteType(callBinding.getOperandType(i));
    if (inMinorType == TypeProtos.MinorType.LATE) {
      return true;
    }
    types.add(inMinorType);
  }

  final boolean isCompatible = TypeCastRules.getLeastRestrictiveType(types) != null;
  if (!isCompatible && throwOnFailure) {
    throw callBinding.newValidationSignatureError();
  }
  return isCompatible;
}
 
Example 10
Source File: FamilyOperandTypeChecker.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (families.size() != callBinding.getOperandCount()) {
    // assume this is an inapplicable sub-rule of a composite rule;
    // don't throw
    return false;
  }

  for (Ord<SqlNode> op : Ord.zip(callBinding.operands())) {
    if (!checkSingleOperandType(
        callBinding,
        op.e,
        op.i,
        throwOnFailure)) {
      return false;
    }
  }
  return true;
}
 
Example 11
Source File: SqlJsonObjectFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public boolean checkOperandTypes(SqlCallBinding callBinding,
    boolean throwOnFailure) {
  final int count = callBinding.getOperandCount();
  for (int i = 1; i < count; i += 2) {
    RelDataType nameType = callBinding.getOperandType(i);
    if (!SqlTypeUtil.inCharFamily(nameType)) {
      if (throwOnFailure) {
        throw callBinding.newError(RESOURCE.expectedCharacter());
      }
      return false;
    }
    if (nameType.isNullable()) {
      if (throwOnFailure) {
        throw callBinding.newError(
            RESOURCE.argumentMustNotBeNull(
                callBinding.operand(i).toString()));
      }
      return false;
    }
  }
  return true;
}
 
Example 12
Source File: SqlPositionFunction.java    From Bats with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // check that the two operands are of same type.
  switch (callBinding.getOperandCount()) {
  case 2:
    return OperandTypes.SAME_SAME.checkOperandTypes(
        callBinding, throwOnFailure)
        && super.checkOperandTypes(callBinding, throwOnFailure);

  case 3:
    return OperandTypes.SAME_SAME_INTEGER.checkOperandTypes(
        callBinding, throwOnFailure)
        && super.checkOperandTypes(callBinding, throwOnFailure);
  default:
    throw new AssertionError();
  }
}
 
Example 13
Source File: CallBindingCallContext.java    From flink with Apache License 2.0 5 votes vote down vote up
public CallBindingCallContext(
		DataTypeFactory dataTypeFactory,
		FunctionDefinition definition,
		SqlCallBinding binding,
		@Nullable RelDataType outputType) {
	super(
		dataTypeFactory,
		definition,
		binding.getOperator().getNameAsId().toString());

	this.adaptedArguments = binding.operands(); // reorders the operands
	this.argumentDataTypes = new AbstractList<DataType>() {
		@Override
		public DataType get(int pos) {
			final RelDataType relDataType = binding.getValidator().deriveType(
				binding.getScope(),
				adaptedArguments.get(pos));
			final LogicalType logicalType = FlinkTypeFactory.toLogicalType(relDataType);
			return TypeConversions.fromLogicalToDataType(logicalType);
		}

		@Override
		public int size() {
			return binding.getOperandCount();
		}
	};
	this.outputType = convertOutputType(binding, outputType);
}
 
Example 14
Source File: NumericExceptFirstOperandChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
	for (int i = 1; i < callBinding.getOperandCount(); i++) {
		if (!SqlTypeUtil.isNumeric(callBinding.getOperandType(i))) {
			if (!throwOnFailure) {
				return false;
			}
			throw callBinding.newValidationSignatureError();
		}
	}
	return true;
}
 
Example 15
Source File: NumericExceptFirstOperandChecker.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
	for (int i = 1; i < callBinding.getOperandCount(); i++) {
		if (!SqlTypeUtil.isNumeric(callBinding.getOperandType(i))) {
			if (!throwOnFailure) {
				return false;
			}
			throw callBinding.newValidationSignatureError();
		}
	}
	return true;
}
 
Example 16
Source File: SqlLikeOperator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  switch (callBinding.getOperandCount()) {
  case 2:
    if (!OperandTypes.STRING_SAME_SAME.checkOperandTypes(
        callBinding,
        throwOnFailure)) {
      return false;
    }
    break;
  case 3:
    if (!OperandTypes.STRING_SAME_SAME_SAME.checkOperandTypes(
        callBinding,
        throwOnFailure)) {
      return false;
    }

    // calc implementation should
    // enforce the escape character length to be 1
    break;
  default:
    throw new AssertionError("unexpected number of args to "
        + callBinding.getCall() + ": " + callBinding.getOperandCount());
  }

  return SqlTypeUtil.isCharTypeComparable(
      callBinding,
      callBinding.operands(),
      throwOnFailure);
}
 
Example 17
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  if (families.size() != callBinding.getOperandCount()) {
    // assume this is an inapplicable sub-rule of a composite rule;
    // don't throw
    return false;
  }
  for (Ord<SqlNode> op : Ord.zip(callBinding.operands())) {
    if (!checkSingleOperandType(
        callBinding,
        op.e,
        op.i,
        false)) {
      // try to coerce type if it is allowed.
      boolean coerced = false;
      if (callBinding.isTypeCoercionEnabled()) {
        TypeCoercion typeCoercion = callBinding.getValidator().getTypeCoercion();
        ImmutableList.Builder<RelDataType> builder = ImmutableList.builder();
        for (int i = 0; i < callBinding.getOperandCount(); i++) {
          builder.add(callBinding.getOperandType(i));
        }
        ImmutableList<RelDataType> dataTypes = builder.build();
        coerced = typeCoercion.builtinFunctionCoercion(callBinding, dataTypes, families);
      }
      // re-validate the new nodes type.
      for (Ord<SqlNode> op1 : Ord.zip(callBinding.operands())) {
        if (!checkSingleOperandType(
            callBinding,
            op1.e,
            op1.i,
            throwOnFailure)) {
          return false;
        }
      }
      return coerced;
    }
  }
  return true;
}
 
Example 18
Source File: SqlLikeOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  switch (callBinding.getOperandCount()) {
  case 2:
    if (!OperandTypes.STRING_SAME_SAME.checkOperandTypes(
        callBinding,
        throwOnFailure)) {
      return false;
    }
    break;
  case 3:
    if (!OperandTypes.STRING_SAME_SAME_SAME.checkOperandTypes(
        callBinding,
        throwOnFailure)) {
      return false;
    }

    // calc implementation should
    // enforce the escape character length to be 1
    break;
  default:
    throw new AssertionError("unexpected number of args to "
        + callBinding.getCall() + ": " + callBinding.getOperandCount());
  }

  return SqlTypeUtil.isCharTypeComparable(
      callBinding,
      callBinding.operands(),
      throwOnFailure);
}