Class TypeCoercionImpl

    • Constructor Detail

      • TypeCoercionImpl

        public TypeCoercionImpl​(SqlValidator validator)
    • Method Detail

      • rowTypeCoercion

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

        Rules:

        
               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 forth.
        Parameters:
        scope - validator scope
        query - query node to update the field type for
        columnIndex - target column index
        targetType - target type to cast to
        Returns:
        true if any type coercion actually happens.
      • binaryArithmeticCoercion

        public boolean binaryArithmeticCoercion​(SqlCallBinding binding)
        Coerce operands in binary arithmetic expressions to Numeric types.

        Rules:

        • For binary arithmetic operators like [+, -, *, /, %]: 1. If the operand is VARCHAR type, coerce it to data type of the other operand if its data type is NUMERIC.
        • 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 comparision [=, >, >=, <, <=]: try to find the common type, i.e. "1 > '1'" will be converted to "1 > 1".
        • Some single agg functions: coerce string operand to DECIMAL type.
      • binaryArithmeticWithStrings

        protected boolean binaryArithmeticWithStrings​(SqlCallBinding binding,
                                                      RelDataType left,
                                                      RelDataType right)
        For numeric and string operands, cast string to data type of the other operand.
      • dateTimeStringEquality

        protected boolean dateTimeStringEquality​(SqlCallBinding binding,
                                                 RelDataType left,
                                                 RelDataType right)
        Datetime and string equality: cast string type to datetime type, SqlToRelConverter already make 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)
        Cast "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 when 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 if it is needed.
      • inOperationCoercion

        public boolean inOperationCoercion​(SqlCallBinding binding)
        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---------+       |
                               |                          |
                               +-------------type3--------+
          
        • For both basic sql types(LHS and RHS), find the common type of LHS and RHS nodes.
      • builtinFunctionCoercion

        public boolean builtinFunctionCoercion​(SqlCallBinding binding,
                                               java.util.List<RelDataType> operandTypes,
                                               java.util.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 allows a (numeric, numeric) OR (string, numeric) family but with arguments (op1, op2) of types (varchar(20), boolean), try to coerce op1 to numeric 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.

        Parameters:
        binding - Call binding
        operandTypes - Types of the operands passed in
        expectedFamilies - Expected SqlTypeFamily list by user specified
        Returns:
        true if we successfully do any implicit cast
      • userDefinedFunctionCoercion

        public boolean userDefinedFunctionCoercion​(SqlValidatorScope scope,
                                                   SqlCall call,
                                                   SqlFunction function)
        Type coercion for user defined functions(UDFs).
        Returns:
        true if any operands is coerced