public abstract class AbstractTypeCoercion extends Object implements TypeCoercion
We make tool methods: coerceOperandType(org.apache.calcite.sql.validate.SqlValidatorScope, org.apache.calcite.sql.SqlCall, int, org.apache.calcite.rel.type.RelDataType), coerceColumnType(org.apache.calcite.sql.validate.SqlValidatorScope, org.apache.calcite.sql.SqlNodeList, int, org.apache.calcite.rel.type.RelDataType),
needToCast(org.apache.calcite.sql.validate.SqlValidatorScope, org.apache.calcite.sql.SqlNode, org.apache.calcite.rel.type.RelDataType), updateInferredType(org.apache.calcite.sql.SqlNode, org.apache.calcite.rel.type.RelDataType), updateInferredColumnType(org.apache.calcite.sql.validate.SqlValidatorScope, org.apache.calcite.sql.SqlNode, int, org.apache.calcite.rel.type.RelDataType)
all overridable by derived classes, you can define system specific type coercion logic.
Caution that these methods may modify the SqlNode tree, you should know what the
effect is when using these methods to customize your type coercion rules.
This class also defines the default implementation of the type widening strategies, see
TypeCoercion doc and methods: getTightestCommonType(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType), getWiderTypeFor(java.util.List<org.apache.calcite.rel.type.RelDataType>, boolean),
getWiderTypeForTwo(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType, boolean), getWiderTypeForDecimal(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType),
commonTypeForBinaryComparison(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType) for the detail strategies.
| Modifier and Type | Field and Description |
|---|---|
protected RelDataTypeFactory |
factory |
protected SqlValidator |
validator |
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
coerceColumnType(@Nullable SqlValidatorScope scope,
SqlNodeList nodeList,
int index,
RelDataType targetType)
Cast column at index
index to target type. |
protected boolean |
coerceOperandsType(@Nullable SqlValidatorScope scope,
SqlCall call,
RelDataType commonType)
Coerce all the operands to
commonType. |
protected boolean |
coerceOperandType(@Nullable SqlValidatorScope scope,
SqlCall call,
int index,
RelDataType targetType)
Cast operand at index
index to target type. |
@Nullable RelDataType |
commonTypeForBinaryComparison(@Nullable RelDataType type1,
@Nullable RelDataType type2)
Determines common type for a comparison operator when one operand is String type and the
other is not.
|
@Nullable RelDataType |
getTightestCommonType(@Nullable RelDataType type1,
@Nullable RelDataType type2)
Case1: type widening with no precision loss.
|
@Nullable RelDataType |
getWiderTypeFor(List<RelDataType> typeList,
boolean stringPromotion)
Similar to
getWiderTypeForTwo(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType, boolean), but can handle
sequence types. |
@Nullable RelDataType |
getWiderTypeForDecimal(@Nullable RelDataType type1,
@Nullable RelDataType type2)
Finds a wider type when one or both types are decimal type.
|
@Nullable RelDataType |
getWiderTypeForTwo(@Nullable RelDataType type1,
@Nullable RelDataType type2,
boolean stringPromotion)
Case2: type widening.
|
@Nullable RelDataType |
implicitCast(RelDataType in,
SqlTypeFamily expected)
Type coercion based on the inferred type from passed in operand
and the
SqlTypeFamily defined in the checkers,
e.g. |
protected boolean |
needToCast(SqlValidatorScope scope,
SqlNode node,
RelDataType toType)
Decide if a SqlNode should be casted to target type, derived class
can override this strategy.
|
protected void |
updateInferredColumnType(SqlValidatorScope scope,
SqlNode query,
int columnIndex,
RelDataType desiredType)
Update inferred row type for a query, i.e.
|
protected void |
updateInferredType(SqlNode node,
RelDataType type)
Update inferred type for a SqlNode.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitbinaryArithmeticCoercion, binaryComparisonCoercion, builtinFunctionCoercion, caseWhenCoercion, inOperationCoercion, querySourceCoercion, rowTypeCoercion, userDefinedFunctionCoercionprotected SqlValidator validator
protected RelDataTypeFactory factory
protected boolean coerceOperandType(@Nullable SqlValidatorScope scope, SqlCall call, int index, RelDataType targetType)
index to target type.
we do this base on the fact that validate happens before type coercion.protected boolean coerceOperandsType(@Nullable SqlValidatorScope scope, SqlCall call, RelDataType commonType)
commonType.scope - Validator scopecall - the callcommonType - common type to coerce toprotected boolean coerceColumnType(@Nullable SqlValidatorScope scope, SqlNodeList nodeList, int index, RelDataType targetType)
index to target type.scope - Validator scope for the node listnodeList - Column node listindex - Index of columntargetType - Target type to cast toprotected boolean needToCast(SqlValidatorScope scope, SqlNode node, RelDataType toType)
protected void updateInferredType(SqlNode node, RelDataType type)
protected void updateInferredColumnType(SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType desiredType)
scope - Validator scopequery - Node to inferred typecolumnIndex - Column index to updatedesiredType - Desired column typepublic @Nullable RelDataType getTightestCommonType(@Nullable RelDataType type1, @Nullable RelDataType type2)
getTightestCommonType in interface TypeCoercionpublic @Nullable RelDataType commonTypeForBinaryComparison(@Nullable RelDataType type1, @Nullable RelDataType type2)
commonTypeForBinaryComparison in interface TypeCoercionpublic @Nullable RelDataType getWiderTypeForTwo(@Nullable RelDataType type1, @Nullable RelDataType type2, boolean stringPromotion)
getTightestCommonType(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType)
is that we allow some precision loss when widening decimal to fractional,
or promote fractional to string type.getWiderTypeForTwo in interface TypeCoercionpublic @Nullable RelDataType getWiderTypeForDecimal(@Nullable RelDataType type1, @Nullable RelDataType type2)
The default implementation depends on the max precision/scale of the type system,
you can override it based on the specific system requirement in
RelDataTypeSystem.
getWiderTypeForDecimal in interface TypeCoercionpublic @Nullable RelDataType getWiderTypeFor(List<RelDataType> typeList, boolean stringPromotion)
getWiderTypeForTwo(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType, boolean), but can handle
sequence types. getWiderTypeForTwo(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType, boolean) doesn't satisfy the associative law,
i.e. (a op b) op c may not equal to a op (b op c). This is only a problem for STRING type or
nested STRING type in collection type like ARRAY. Excluding these types,
getWiderTypeForTwo(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType, boolean) satisfies the associative law. For instance,
(DATE, INTEGER, VARCHAR) should have VARCHAR as the wider common type.getWiderTypeFor in interface TypeCoercionpublic @Nullable RelDataType implicitCast(RelDataType in, SqlTypeFamily expected)
SqlTypeFamily defined in the checkers,
e.g. the FamilyOperandTypeChecker.
Caution that we do not cast from NUMERIC to NUMERIC. See CalciteImplicitCasts for the details.
in - Inferred operand typeexpected - Expected SqlTypeFamily of registered SqlFunctionCopyright © 2012-2022 Apache Software Foundation. All Rights Reserved.