Class TypeCoercionImpl

All Implemented Interfaces:

public class TypeCoercionImpl extends AbstractTypeCoercion
Default implementation of Calcite implicit type cast.
  • Constructor Details

  • Method Details

    • rowTypeCoercion

      public boolean rowTypeCoercion(@Nullable SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType targetType)
      Widen a SqlNode's field type to common type, mainly used for set operations like UNION, INTERSECT and EXCEPT.


             type1, type2  type3       select a, b, c from t1
                \      \      \
               type4  type5  type6              UNION
                /      /      /
             type7  type8  type9       select d, e, f from t2

      For struct type (type1, type2, type3) union type (type4, type5, type6), infer the first result column type type7 as the wider type of type1 and type4, the second column type as the wider type of type2 and type5 and so on.

      scope - Validator scope
      query - Query node to update the field type for
      columnIndex - Target column index
      targetType - Target type to cast to
    • binaryArithmeticCoercion

      public boolean binaryArithmeticCoercion(SqlCallBinding binding)
      Coerces operands in binary arithmetic expressions to NUMERIC types.

      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.

    • binaryArithmeticWithStrings

      protected boolean binaryArithmeticWithStrings(SqlCallBinding binding, RelDataType left, RelDataType right)
      For NUMERIC and STRING operands, cast STRING to data type of the other operand.
    • binaryComparisonCoercion

      public boolean binaryComparisonCoercion(SqlCallBinding binding)
      Coerces operands in binary comparison expressions.


      • For EQUALS(=) operator: 1. If operands are BOOLEAN and NUMERIC, evaluate `1=true` and `0=false` all to be true; 2. If operands are datetime and string, do nothing because the SqlToRelConverter already makes the type coercion;
      • For binary comparison [=, >, >=, <, <=]: try to find the common type, i.e. "1 > '1'" will be converted to "1 > 1";
      • For BETWEEN operator, find the common comparison data type of all the operands, the common type is deduced from left to right, i.e. for expression "A between B and C", finds common comparison type D between A and B then common comparison type E between D and C as the final common type.
    • commonTypeForComparison

      protected @Nullable RelDataType commonTypeForComparison(List<RelDataType> dataTypes)
      Finds the common type for binary comparison when the size of operands dataTypes is more than 2. If there are N(more than 2) operands, finds the common type between two operands from left to right:


         type1     type2    type3
          |         |        |
          +- type4 -+        |
               |             |
               +--- type5 ---+
      For operand data types (type1, type2, type3), deduce the common type type4 from type1 and type2, then common type type5 from type4 and type3.
    • dateTimeStringEquality

      protected boolean dateTimeStringEquality(SqlCallBinding binding, RelDataType left, RelDataType right)
      Datetime and STRING equality: cast STRING type to datetime type, SqlToRelConverter already makes the conversion but we still keep this interface overridable so user can have their custom implementation.
    • booleanEquality

      protected boolean booleanEquality(SqlCallBinding binding, RelDataType left, RelDataType right)
      Casts "BOOLEAN = NUMERIC" to "NUMERIC = NUMERIC". Expressions like 1=`expr` and 0=`expr` can be simplified to `expr` and `not expr`, but this better happens in RexSimplify.

      There are 2 cases that need type coercion here:

      1. Case1: `boolean expr1` = 1 or `boolean expr1` = 0, replace the numeric literal with `true` or `false` boolean literal.
      2. Case2: `boolean expr1` = `numeric expr2`, replace expr1 to `1` or `0` numeric literal.
      For case2, wrap the operand in a cast operator, during sql-to-rel conversion we would convert expression `cast(expr1 as right)` to `case when expr1 then 1 else 0.`
    • caseWhenCoercion

      public boolean caseWhenCoercion(SqlCallBinding callBinding)
      CASE and COALESCE type coercion, collect all the branches types including then operands and else operands to find a common type, then cast the operands to the common type when needed.
    • inOperationCoercion

      public boolean inOperationCoercion(SqlCallBinding binding)
      Handles type coercion for IN operation with or without sub-query.

      See TypeCoercionImpl for default strategies.


      With(Without) sub-query:

      • With sub-query: find the common type through comparing the left hand side (LHS) expression types with corresponding right hand side (RHS) expression derived from the sub-query expression's row type. Wrap the fields of the LHS and RHS in CAST operators if it is needed.
      • Without sub-query: convert the nodes of the RHS to the common type by checking all the argument types and find out the minimum common type that all the arguments can be cast to.

      How to find the common type:

      • For both struct sql types (LHS and RHS), find the common type of every LHS and RHS fields pair:
         (field1, field2, field3)    (field4, field5, field6)
            |        |       |          |       |       |
            +--------+---type1----------+       |       |
                     |       |                  |       |
                     +-------+----type2---------+       |
                             |                          |
      • For both basic sql types(LHS and RHS), find the common type of LHS and RHS nodes.
    • builtinFunctionCoercion

      public boolean builtinFunctionCoercion(SqlCallBinding binding, List<RelDataType> operandTypes, List<SqlTypeFamily> expectedFamilies)
      Description copied from interface: TypeCoercion
      Type coercion with inferred type from passed in arguments and the SqlTypeFamily defined in the checkers, e.g. the FamilyOperandTypeChecker.

      Caution that we do not cast from NUMERIC if desired type family is also SqlTypeFamily.NUMERIC.

      If the FamilyOperandTypeCheckers are subsumed in a CompositeOperandTypeChecker, check them based on their combination order. i.e. If we allow a NUMERIC_NUMERIC OR STRING_NUMERIC family combination and are with arguments (op1: VARCHAR(20), op2: BOOLEAN), try to coerce both op1 and op2 to NUMERIC if the type coercion rules allow it, or else try to coerce op2 to NUMERIC and keep op1 the type as it is.

      This is also very interrelated to the composition predicate for the checkers: if the predicate is AND, we would fail fast if the first family type coercion fails.

      binding - Call binding
      operandTypes - Types of the operands passed in
      expectedFamilies - Expected SqlTypeFamily list by user specified
    • userDefinedFunctionCoercion

      public boolean userDefinedFunctionCoercion(SqlValidatorScope scope, SqlCall call, SqlFunction function)
      Type coercion for user-defined functions (UDFs).
    • querySourceCoercion

      public boolean querySourceCoercion(@Nullable SqlValidatorScope scope, RelDataType sourceRowType, RelDataType targetRowType, SqlNode query)
      Description copied from interface: TypeCoercion
      Coerces the source row expression to target type in an INSERT or UPDATE query.

      If the source and target fields in the same ordinal do not equal sans nullability, try to coerce the source field to target field type.

      scope - Source scope
      sourceRowType - Source row type
      targetRowType - Target row type
      query - The query, either an INSERT or UPDATE