Class AbstractTypeCoercion
- All Implemented Interfaces:
TypeCoercion
- Direct Known Subclasses:
TypeCoercionImpl
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.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
coerceColumnType
(@Nullable SqlValidatorScope scope, SqlNodeList nodeList, int index, RelDataType targetType) Cast column at indexindex
to target type.protected boolean
coerceOperandsType
(@Nullable SqlValidatorScope scope, SqlCall call, RelDataType commonType) Coerce all the operands tocommonType
.protected boolean
coerceOperandType
(@Nullable SqlValidatorScope scope, SqlCall call, int index, RelDataType targetType) Cast operand at indexindex
to target type.protected Boolean
coerceStringToArray
(SqlCall call, SqlNode operand, int index, RelDataType fromType, RelDataType targetType) Coerce STRING type to ARRAY 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 togetWiderTypeForTwo(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 theSqlTypeFamily
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.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.calcite.sql.validate.implicit.TypeCoercion
binaryArithmeticCoercion, binaryComparisonCoercion, builtinFunctionCoercion, caseWhenCoercion, inOperationCoercion, querySourceCoercion, rowTypeCoercion, userDefinedFunctionCoercion
-
Field Details
-
validator
-
factory
-
-
Method Details
-
coerceOperandType
protected boolean coerceOperandType(@Nullable SqlValidatorScope scope, SqlCall call, int index, RelDataType targetType) Cast operand at indexindex
to target type. we do this base on the fact that validate happens before type coercion. -
coerceOperandsType
protected boolean coerceOperandsType(@Nullable SqlValidatorScope scope, SqlCall call, RelDataType commonType) Coerce all the operands tocommonType
.- Parameters:
scope
- Validator scopecall
- the callcommonType
- common type to coerce to
-
coerceColumnType
protected boolean coerceColumnType(@Nullable SqlValidatorScope scope, SqlNodeList nodeList, int index, RelDataType targetType) Cast column at indexindex
to target type.- Parameters:
scope
- Validator scope for the node listnodeList
- Column node listindex
- Index of columntargetType
- Target type to cast to
-
needToCast
Decide if a SqlNode should be casted to target type, derived class can override this strategy. -
updateInferredType
Update inferred type for a SqlNode. -
updateInferredColumnType
protected void updateInferredColumnType(SqlValidatorScope scope, SqlNode query, int columnIndex, RelDataType desiredType) Update inferred row type for a query, i.e. SqlCall that returns struct type or SqlSelect.- Parameters:
scope
- Validator scopequery
- Node to inferred typecolumnIndex
- Column index to updatedesiredType
- Desired column type
-
getTightestCommonType
public @Nullable RelDataType getTightestCommonType(@Nullable RelDataType type1, @Nullable RelDataType type2) Case1: type widening with no precision loss. Find the tightest common type of two types that might be used in binary expression.- Specified by:
getTightestCommonType
in interfaceTypeCoercion
- Returns:
- tightest common type, i.e. INTEGER + DECIMAL(10, 2) returns DECIMAL(10, 2)
-
commonTypeForBinaryComparison
public @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. For date + timestamp operands, use timestamp as common type, i.e. Timestamp(2017-01-01 00:00 ...) > Date(2018) evaluates to be false.- Specified by:
commonTypeForBinaryComparison
in interfaceTypeCoercion
-
getWiderTypeForTwo
public @Nullable RelDataType getWiderTypeForTwo(@Nullable RelDataType type1, @Nullable RelDataType type2, boolean stringPromotion) Case2: type widening. The main difference withgetTightestCommonType(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.- Specified by:
getWiderTypeForTwo
in interfaceTypeCoercion
-
getWiderTypeForDecimal
public @Nullable RelDataType getWiderTypeForDecimal(@Nullable RelDataType type1, @Nullable RelDataType type2) Finds a wider type when one or both types are decimal type. If the wider decimal type's precision/scale exceeds system limitation, this rule will truncate the decimal type to the max precision/scale. For decimal and fractional types, returns a decimal type which has the higher precision of the two.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
.- Specified by:
getWiderTypeForDecimal
in interfaceTypeCoercion
-
getWiderTypeFor
Similar togetWiderTypeForTwo(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.- Specified by:
getWiderTypeFor
in interfaceTypeCoercion
-
implicitCast
Type coercion based on the inferred type from passed in operand and theSqlTypeFamily
defined in the checkers, e.g. theFamilyOperandTypeChecker
.Caution that we do not cast from NUMERIC to NUMERIC. See CalciteImplicitCasts for the details.
- Parameters:
in
- Inferred operand typeexpected
- ExpectedSqlTypeFamily
of registered SqlFunction- Returns:
- common type of implicit cast, null if we do not find any
-
coerceStringToArray
protected Boolean coerceStringToArray(SqlCall call, SqlNode operand, int index, RelDataType fromType, RelDataType targetType) Coerce STRING type to ARRAY type.
-