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

The following examples show how to use org.apache.calcite.sql.SqlCallBinding#isTypeCoercionEnabled() . 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: CompositeOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  // 1. Check eagerly for binary arithmetic expressions.
  // 2. Check the comparability.
  // 3. Check if the operands have the right type.
  if (callBinding.isTypeCoercionEnabled()) {
    final TypeCoercion typeCoercion = callBinding.getValidator().getTypeCoercion();
    typeCoercion.binaryArithmeticCoercion(callBinding);
  }
  if (check(callBinding)) {
    return true;
  }
  if (!throwOnFailure) {
    return false;
  }
  if (composition == Composition.OR) {
    for (SqlOperandTypeChecker allowedRule : allowedRules) {
      allowedRule.checkOperandTypes(callBinding, true);
    }
  }

  // If no exception thrown, just throw a generic validation
  // signature error.
  throw callBinding.newValidationSignatureError();
}
 
Example 2
Source File: ComparableOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  boolean b = true;
  for (int i = 0; i < nOperands; ++i) {
    RelDataType type = callBinding.getOperandType(i);
    if (!checkType(callBinding, throwOnFailure, type)) {
      b = false;
      break;
    }
  }
  if (b) {
    // Coerce type first.
    if (callBinding.isTypeCoercionEnabled()) {
      TypeCoercion typeCoercion = callBinding.getValidator().getTypeCoercion();
      // For comparison operators, i.e. >, <, =, >=, <=.
      typeCoercion.binaryComparisonCoercion(callBinding);
    }
    b = super.checkOperandTypes(callBinding, false);
  }
  if (!b && throwOnFailure) {
    throw callBinding.newValidationSignatureError();
  }
  return b;
}
 
Example 3
Source File: CompositeOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
private boolean checkWithoutTypeCoercion(SqlCallBinding callBinding) {
  if (!callBinding.isTypeCoercionEnabled()) {
    return false;
  }
  for (SqlOperandTypeChecker rule : allowedRules) {
    if (rule instanceof ImplicitCastOperandTypeChecker) {
      ImplicitCastOperandTypeChecker rule1 = (ImplicitCastOperandTypeChecker) rule;
      if (rule1.checkOperandTypesWithoutTypeCoercion(callBinding, false)) {
        return true;
      }
    }
  }
  return false;
}
 
Example 4
Source File: FamilyOperandTypeChecker.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean checkSingleOperandType(
    SqlCallBinding callBinding,
    SqlNode node,
    int iFormalOperand,
    boolean throwOnFailure) {
  SqlTypeFamily family = families.get(iFormalOperand);
  if (family == SqlTypeFamily.ANY) {
    // no need to check
    return true;
  }
  if (SqlUtil.isNullLiteral(node, false)) {
    if (callBinding.isTypeCoercionEnabled()) {
      return true;
    } else if (throwOnFailure) {
      throw callBinding.getValidator().newValidationError(node,
          RESOURCE.nullIllegal());
    } else {
      return false;
    }
  }
  RelDataType type =
      callBinding.getValidator().deriveType(
          callBinding.getScope(),
          node);
  SqlTypeName typeName = type.getSqlTypeName();

  // Pass type checking for operators if it's of type 'ANY'.
  if (typeName.getFamily() == SqlTypeFamily.ANY) {
    return true;
  }

  if (!family.getTypeNames().contains(typeName)) {
    if (throwOnFailure) {
      throw callBinding.newValidationSignatureError();
    }
    return false;
  }
  return true;
}
 
Example 5
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;
}