Package org.apache.calcite.sql.validate.implicit


package org.apache.calcite.sql.validate.implicit

SQL Implicit Type Cast

Work Flow

This 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. AssignableOperandTypeChecker, ComparableOperandTypeChecker CompositeOperandTypeChecker, FamilyOperandTypeChecker, SameOperandTypeChecker, SetopOperandTypeChecker.

  • 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 SqlNode it has validated;
  • If the validation fails, the validator will ask the TypeCoercion about if there can make implicit type coercion, if the coercion rules passed, the TypeCoercion component will replace the SqlNode with a casted one, (the node may be an operand of an operator or a column field of a selected row).
  • TypeCoercion will 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 SQL Contexts

The 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. INTEGER and DOUBLE will return DOUBLE, the numeric precision does not lose for this case.
  • If no tightest common type is 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 Conversion Matrix

See CalciteImplicitCasts.