Class SqlTypeCoercionRule

java.lang.Object
org.apache.calcite.sql.type.SqlTypeCoercionRule
All Implemented Interfaces:
SqlTypeMappingRule

public class SqlTypeCoercionRule extends Object implements SqlTypeMappingRule
Rules that determine whether a type is castable from another type.

These rules specify the conversion matrix with explicit CAST.

The implicit type coercion matrix should be a sub-set of this explicit one. We do not define an implicit type coercion matrix, instead we have specific coercion rules for all kinds of SQL contexts which actually define the "matrix".

To add a new implementation to this class, follow these steps:

  1. Initialize a SqlTypeMappingRules.Builder instance with default mappings of INSTANCE.
  2. Modify the mappings with the Builder.
  3. Construct a new SqlTypeCoercionRule instance with method instance(Map).
  4. Set the SqlTypeCoercionRule instance into the SqlValidator.

The code snippet below illustrates how to implement a customized instance.


     // Initialize a Builder instance with the default mappings.
     Builder builder = SqlTypeMappingRules.builder();
     builder.addAll(SqlTypeCoercionRules.instance().getTypeMapping());

     // Do the tweak, for example, if we want to add a rule to allow
     // coerce BOOLEAN to TIMESTAMP.
     builder.add(SqlTypeName.TIMESTAMP,
         builder.copyValues(SqlTypeName.TIMESTAMP)
             .add(SqlTypeName.BOOLEAN).build());

     // Initialize a SqlTypeCoercionRules with the new builder mappings.
     SqlTypeCoercionRules typeCoercionRules = SqlTypeCoercionRules.instance(builder.map);

     // Set the SqlTypeCoercionRules instance into the SqlValidator.
     SqlValidator.Config validatorConf ...;
     validatorConf.withTypeCoercionRules(typeCoercionRules);
     // Use this conf to initialize the SqlValidator.