Work FlowThis package contains rules for implicit type coercion, it works during the process of sql validation. The transformation entrance are all kinds of checkers. i.e.
When user do validation for a
SqlNode, and the type coercion is turned on(default), the validator will check the operands/return types of all kinds of operators, if the validation passes through, the validator will just cache the data type (say RelDataType) for the
SqlNodeit has validated;
- If the validation fails, the validator will ask the
TypeCoercionabout if there can make implicit type coercion, if the coercion rules passed, the
TypeCoercioncomponent will replace the
SqlNodewith a casted one, (the node may be an operand of an operator or a column field of a selected row).
TypeCoercionwill then update the inferred type for the casted node and the containing operator/row column type.
- If the coercion rule fails again, the validator will just throw the exception as is before.
For some cases, although the validation passes, we still need the type coercion, e.g. for expression 1 > '1', Calcite will just return false without type coercion, we do type coercion eagerly here: the result expression would be transformed to "1 > cast('1' as int)" and the result would be true.
Conversion ExpressionsThe supported conversion contexts are: Conversion Expressions
Strategies for finding common type:
- If the operator has expected data types, just take them as the desired one. i.e. the UDF.
- If there is no expected data type but data type families are registered, try to coerce operand to the family's default data type, i.e. the String family will have a VARCHAR type.
- If neither expected data type nor families are specified, try to find the tightest common type of the node types, i.e. int and double will return double, the numeric precision does not lose for this case.
- If no tightest common type found, try to find a wider type, i.e. string and int will return int, we allow some precision loss when widening decimal to fractional, or promote to string type.
Types Conversion MatrixSee CalciteImplicitCasts
Interface Summary Interface Description TypeCoercionDefault Strategies to coerce differing types that participate in operations into compatible ones.
Class Summary Class Description AbstractTypeCoercionBase class for all the type coercion rules. TypeCoercionImplDefault implementation of Calcite implicit type cast. TypeCoercionsFactory class for type coercion instantiation of different sql dialects.