Class TypeCoercionImpl
- All Implemented Interfaces:
TypeCoercion
-
Field Summary
Fields inherited from class org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion
factory, validator
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
binaryArithmeticCoercion
(SqlCallBinding binding) Coerces operands in binary arithmetic expressions to NUMERIC types.protected boolean
binaryArithmeticWithStrings
(SqlCallBinding binding, RelDataType left, RelDataType right) For NUMERIC and STRING operands, cast STRING to data type of the other operand.boolean
binaryComparisonCoercion
(SqlCallBinding binding) Coerces operands in binary comparison expressions.protected boolean
booleanEquality
(SqlCallBinding binding, RelDataType left, RelDataType right) Casts "BOOLEAN = NUMERIC" to "NUMERIC = NUMERIC".boolean
builtinFunctionCoercion
(SqlCallBinding binding, List<RelDataType> operandTypes, List<SqlTypeFamily> expectedFamilies) Type coercion with inferred type from passed in arguments and theSqlTypeFamily
defined in the checkers, e.g.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.protected @Nullable RelDataType
commonTypeForComparison
(List<RelDataType> dataTypes) Finds the common type for binary comparison when the size of operandsdataTypes
is more than 2.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.boolean
inOperationCoercion
(SqlCallBinding binding) Handles type coercion for IN operation with or without sub-query.boolean
querySourceCoercion
(@Nullable SqlValidatorScope scope, RelDataType sourceRowType, RelDataType targetRowType, SqlNode query) Coerces the source row expression to target type in an INSERT or UPDATE query.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.boolean
userDefinedFunctionCoercion
(SqlValidatorScope scope, SqlCall call, SqlFunction function) Type coercion for user-defined functions (UDFs).Methods inherited from class org.apache.calcite.sql.validate.implicit.AbstractTypeCoercion
coerceColumnType, coerceOperandsType, coerceOperandType, coerceStringToArray, commonTypeForBinaryComparison, getTightestCommonType, getWiderTypeFor, getWiderTypeForDecimal, getWiderTypeForTwo, implicitCast, needToCast, updateInferredColumnType, updateInferredType
-
Constructor Details
-
TypeCoercionImpl
-
-
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.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 on.- Parameters:
scope
- Validator scopequery
- Query node to update the field type forcolumnIndex
- Target column indextargetType
- Target type to cast to
-
binaryArithmeticCoercion
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
Coerces operands in binary comparison expressions.Rules:
- 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
Finds the common type for binary comparison when the size of operandsdataTypes
is more than 2. If there are N(more than 2) operands, finds the common type between two operands from left to right:Rules:
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
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 inRexSimplify
.There are 2 cases that need type coercion here:
- Case1: `boolean expr1` = 1 or `boolean expr1` = 0, replace the numeric literal with `true` or `false` boolean literal.
- Case2: `boolean expr1` = `numeric expr2`, replace expr1 to `1` or `0` numeric literal.
-
caseWhenCoercion
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
Handles type coercion for IN operation with or without sub-query.See
TypeCoercionImpl
for default strategies.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, List<RelDataType> operandTypes, List<SqlTypeFamily> expectedFamilies) Description copied from interface:TypeCoercion
Type coercion with inferred type from passed in arguments and theSqlTypeFamily
defined in the checkers, e.g. theFamilyOperandTypeChecker
.Caution that we do not cast from NUMERIC if desired type family is also
SqlTypeFamily.NUMERIC
.If the
FamilyOperandTypeChecker
s are subsumed in aCompositeOperandTypeChecker
, 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.
- Parameters:
binding
- Call bindingoperandTypes
- Types of the operands passed inexpectedFamilies
- 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.
- Parameters:
scope
- Source scopesourceRowType
- Source row typetargetRowType
- Target row typequery
- The query, either an INSERT or UPDATE
-