public class TypeCoercionImpl extends AbstractTypeCoercion
factory, validator| Constructor and Description |
|---|
TypeCoercionImpl(RelDataTypeFactory typeFactory,
SqlValidator validator) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
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 the
SqlTypeFamily 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 operands
dataTypes 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).
|
coerceColumnType, coerceOperandsType, coerceOperandType, commonTypeForBinaryComparison, getTightestCommonType, getWiderTypeFor, getWiderTypeForDecimal, getWiderTypeForTwo, implicitCast, needToCast, updateInferredColumnType, updateInferredTypepublic TypeCoercionImpl(RelDataTypeFactory typeFactory, SqlValidator validator)
public boolean rowTypeCoercion(@Nullable SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType targetType)
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.scope - Validator scopequery - Query node to update the field type forcolumnIndex - Target column indextargetType - Target type to cast topublic boolean binaryArithmeticCoercion(SqlCallBinding binding)
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.
protected boolean binaryArithmeticWithStrings(SqlCallBinding binding, RelDataType left, RelDataType right)
public boolean binaryComparisonCoercion(SqlCallBinding binding)
Rules:
protected @Nullable RelDataType commonTypeForComparison(List<RelDataType> dataTypes)
dataTypes 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.protected boolean dateTimeStringEquality(SqlCallBinding binding, RelDataType left, RelDataType right)
protected boolean booleanEquality(SqlCallBinding binding, RelDataType left, RelDataType right)
RexSimplify.
There are 2 cases that need type coercion here:
public boolean caseWhenCoercion(SqlCallBinding callBinding)
public boolean inOperationCoercion(SqlCallBinding binding)
See TypeCoercionImpl for default strategies.
STRATEGIES
With(Without) sub-query:
How to find the common type:
(field1, field2, field3) (field4, field5, field6)
| | | | | |
+--------+---type1----------+ | |
| | | |
+-------+----type2---------+ |
| |
+-------------type3--------+
public boolean builtinFunctionCoercion(SqlCallBinding binding, List<RelDataType> operandTypes, List<SqlTypeFamily> expectedFamilies)
TypeCoercionSqlTypeFamily 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 bindingoperandTypes - Types of the operands passed inexpectedFamilies - Expected SqlTypeFamily list by user specifiedpublic boolean userDefinedFunctionCoercion(SqlValidatorScope scope, SqlCall call, SqlFunction function)
public boolean querySourceCoercion(@Nullable SqlValidatorScope scope, RelDataType sourceRowType, RelDataType targetRowType, SqlNode query)
TypeCoercionIf 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 scopesourceRowType - Source row typetargetRowType - Target row typequery - The query, either an INSERT or UPDATECopyright © 2012-2022 Apache Software Foundation. All Rights Reserved.