Package org.apache.calcite.rel.type
Class RelDataTypeFactoryImpl
- java.lang.Object
-
- org.apache.calcite.rel.type.RelDataTypeFactoryImpl
-
- All Implemented Interfaces:
RelDataTypeFactory
- Direct Known Subclasses:
SqlTypeFactoryImpl
public abstract class RelDataTypeFactoryImpl extends java.lang.Object implements RelDataTypeFactory
Abstract base for implementations ofRelDataTypeFactory
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
RelDataTypeFactoryImpl.JavaType
Type which is based upon a Java class.private static class
RelDataTypeFactoryImpl.Key
Key to the data type cache.-
Nested classes/interfaces inherited from interface org.apache.calcite.rel.type.RelDataTypeFactory
RelDataTypeFactory.Builder, RelDataTypeFactory.FieldInfo, RelDataTypeFactory.FieldInfoBuilder
-
-
Field Summary
Fields Modifier and Type Field Description private static com.google.common.cache.LoadingCache<java.lang.Object,RelDataType>
CACHE
Global cache.private static java.util.Map<java.lang.Class,RelDataTypeFamily>
CLASS_FAMILIES
protected RelDataTypeSystem
typeSystem
-
Constructor Summary
Constructors Modifier Constructor Description protected
RelDataTypeFactoryImpl(RelDataTypeSystem typeSystem)
Creates a type factory.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
addFields(RelDataType type, java.util.List<RelDataTypeField> fieldList)
Adds all fields intype
tofieldList
, renumbering the fields (if necessary) to ensure that their index matches their position in the list.RelDataTypeFactory.FieldInfoBuilder
builder()
Creates aRelDataTypeFactory.FieldInfoBuilder
.protected RelDataType
canonize(RelDataType type)
Registers a type, or returns the existing type if it is already registered.protected RelDataType
canonize(StructKind kind, java.util.List<java.lang.String> names, java.util.List<RelDataType> types)
Looks up a type using a temporary key, and if not present, creates a permanent key and type.private RelDataType
copyRecordType(RelRecordType type, boolean ignoreNullable, boolean nullable)
private RelDataType
copySimpleType(RelDataType type, boolean nullable)
RelDataType
copyType(RelDataType type)
Duplicates a type, making a deep copy.RelDataType
createDecimalProduct(RelDataType type1, RelDataType type2)
Infers the return type of a decimal multiplication.RelDataType
createDecimalQuotient(RelDataType type1, RelDataType type2)
Rules: Let p1, s1 be the precision and scale of the first operand Let p2, s2 be the precision and scale of the second operand Let p, s be the precision and scale of the result Let d be the number of whole digits in the result Then the result type is a decimal with: d = p1 - s1 + s2 s < max(6, s1 + p2 + 1) p = d + s p and s are capped at their maximum valuesRelDataType
createJavaType(java.lang.Class clazz)
Creates a type that corresponds to a Java class.RelDataType
createJoinType(RelDataType... types)
Creates a cartesian product type.RelDataType
createStructType(java.util.List<? extends java.util.Map.Entry<java.lang.String,RelDataType>> fieldList)
Creates a type that represents a structured collection of fieldList, obtaining the field information from a list of (name, type) pairs.RelDataType
createStructType(java.util.List<RelDataType> typeList, java.util.List<java.lang.String> fieldNameList)
Creates a type that represents a structured collection of fields.RelDataType
createStructType(RelDataTypeFactory.FieldInfo fieldInfo)
Creates a type that represents a structured collection of fields, obtaining the field information via a callback.RelDataType
createStructType(StructKind kind, java.util.List<RelDataType> typeList, java.util.List<java.lang.String> fieldNameList)
Creates a type that represents a structured collection of fields, given lists of the names and types of the fields.RelDataType
createTypeWithNullability(RelDataType type, boolean nullable)
Creates a type that is the same as another type but with possibly different nullability.private java.util.List<RelDataTypeFieldImpl>
fieldsOf(java.lang.Class clazz)
java.nio.charset.Charset
getDefaultCharset()
private static java.util.List<RelDataTypeField>
getFieldList(java.util.List<RelDataType> types)
Returns a list of the fields in a list of types.private static void
getTypeList(com.google.common.collect.ImmutableList<RelDataType> inTypes, java.util.List<RelDataType> flatTypes)
Returns a list of all atomic types in a list.RelDataTypeSystem
getTypeSystem()
Returns the type system.static boolean
isJavaType(RelDataType t)
private static RelDataType
keyToType(java.lang.Object k)
RelDataType
leastRestrictive(java.util.List<RelDataType> types)
Returns the most general of a set of types (that is, one type to which they can all be cast), or null if conversion is not possible.protected RelDataType
leastRestrictiveStructuredType(java.util.List<RelDataType> types)
boolean
useDoubleMultiplication(RelDataType type1, RelDataType type2)
Returns whether a decimal multiplication should be implemented by casting arguments to double values.-
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.rel.type.RelDataTypeFactory
createArrayType, createMapType, createMultisetType, createSqlIntervalType, createSqlType, createSqlType, createSqlType, createTypeWithCharsetAndCollation, createUnknownType
-
-
-
-
Field Detail
-
CACHE
private static final com.google.common.cache.LoadingCache<java.lang.Object,RelDataType> CACHE
Global cache. Uses soft values to allow GC.
-
CLASS_FAMILIES
private static final java.util.Map<java.lang.Class,RelDataTypeFamily> CLASS_FAMILIES
-
typeSystem
protected final RelDataTypeSystem typeSystem
-
-
Constructor Detail
-
RelDataTypeFactoryImpl
protected RelDataTypeFactoryImpl(RelDataTypeSystem typeSystem)
Creates a type factory.
-
-
Method Detail
-
keyToType
private static RelDataType keyToType(@Nonnull java.lang.Object k)
-
getTypeSystem
public RelDataTypeSystem getTypeSystem()
Description copied from interface:RelDataTypeFactory
Returns the type system.- Specified by:
getTypeSystem
in interfaceRelDataTypeFactory
- Returns:
- Type system
-
createJavaType
public RelDataType createJavaType(java.lang.Class clazz)
Description copied from interface:RelDataTypeFactory
Creates a type that corresponds to a Java class.- Specified by:
createJavaType
in interfaceRelDataTypeFactory
- Parameters:
clazz
- the Java class used to define the type- Returns:
- canonical Java type descriptor
-
createJoinType
public RelDataType createJoinType(RelDataType... types)
Description copied from interface:RelDataTypeFactory
Creates a cartesian product type.- Specified by:
createJoinType
in interfaceRelDataTypeFactory
- Parameters:
types
- array of types to be joined- Returns:
- canonical join type descriptor
-
createStructType
public RelDataType createStructType(java.util.List<RelDataType> typeList, java.util.List<java.lang.String> fieldNameList)
Description copied from interface:RelDataTypeFactory
Creates a type that represents a structured collection of fields. Shorthand forcreateStructType(StructKind.FULLY_QUALIFIED, typeList, fieldNameList)
.- Specified by:
createStructType
in interfaceRelDataTypeFactory
-
createStructType
public RelDataType createStructType(StructKind kind, java.util.List<RelDataType> typeList, java.util.List<java.lang.String> fieldNameList)
Description copied from interface:RelDataTypeFactory
Creates a type that represents a structured collection of fields, given lists of the names and types of the fields.- Specified by:
createStructType
in interfaceRelDataTypeFactory
- Parameters:
kind
- Name resolution policytypeList
- types of the fieldsfieldNameList
- names of the fields- Returns:
- canonical struct type descriptor
-
createStructType
public RelDataType createStructType(RelDataTypeFactory.FieldInfo fieldInfo)
Description copied from interface:RelDataTypeFactory
Creates a type that represents a structured collection of fields, obtaining the field information via a callback.- Specified by:
createStructType
in interfaceRelDataTypeFactory
- Parameters:
fieldInfo
- callback for field information- Returns:
- canonical struct type descriptor
-
createStructType
public final RelDataType createStructType(java.util.List<? extends java.util.Map.Entry<java.lang.String,RelDataType>> fieldList)
Description copied from interface:RelDataTypeFactory
Creates a type that represents a structured collection of fieldList, obtaining the field information from a list of (name, type) pairs.- Specified by:
createStructType
in interfaceRelDataTypeFactory
- Parameters:
fieldList
- List of (name, type) pairs- Returns:
- canonical struct type descriptor
-
leastRestrictive
public RelDataType leastRestrictive(java.util.List<RelDataType> types)
Description copied from interface:RelDataTypeFactory
Returns the most general of a set of types (that is, one type to which they can all be cast), or null if conversion is not possible. The result may be a new type that is less restrictive than any of the input types, e.g.leastRestrictive(INT, NUMERIC(3, 2))
could beNUMERIC(12, 2)
.- Specified by:
leastRestrictive
in interfaceRelDataTypeFactory
- Parameters:
types
- input types to be combined using union (not null, not empty)- Returns:
- canonical union type descriptor
-
leastRestrictiveStructuredType
protected RelDataType leastRestrictiveStructuredType(java.util.List<RelDataType> types)
-
copySimpleType
private RelDataType copySimpleType(RelDataType type, boolean nullable)
-
copyRecordType
private RelDataType copyRecordType(RelRecordType type, boolean ignoreNullable, boolean nullable)
-
copyType
public RelDataType copyType(RelDataType type)
Description copied from interface:RelDataTypeFactory
Duplicates a type, making a deep copy. Normally, this is a no-op, since canonical type objects are returned. However, it is useful when copying a type from one factory to another.- Specified by:
copyType
in interfaceRelDataTypeFactory
- Parameters:
type
- input type- Returns:
- output type, a new object equivalent to input type
-
createTypeWithNullability
public RelDataType createTypeWithNullability(RelDataType type, boolean nullable)
Description copied from interface:RelDataTypeFactory
Creates a type that is the same as another type but with possibly different nullability. The output type may be identical to the input type. For type systems without a concept of nullability, the return value is always the same as the input.- Specified by:
createTypeWithNullability
in interfaceRelDataTypeFactory
- Parameters:
type
- input typenullable
- true to request a nullable type; false to request a NOT NULL type- Returns:
- output type, same as input type except with specified nullability
-
canonize
protected RelDataType canonize(RelDataType type)
Registers a type, or returns the existing type if it is already registered.- Throws:
java.lang.NullPointerException
- if type is null
-
canonize
protected RelDataType canonize(StructKind kind, java.util.List<java.lang.String> names, java.util.List<RelDataType> types)
Looks up a type using a temporary key, and if not present, creates a permanent key and type.This approach allows us to use a cheap temporary key. A permanent key is more expensive, because it must be immutable and not hold references into other data structures.
-
getFieldList
private static java.util.List<RelDataTypeField> getFieldList(java.util.List<RelDataType> types)
Returns a list of the fields in a list of types.
-
getTypeList
private static void getTypeList(com.google.common.collect.ImmutableList<RelDataType> inTypes, java.util.List<RelDataType> flatTypes)
Returns a list of all atomic types in a list.
-
addFields
private static void addFields(RelDataType type, java.util.List<RelDataTypeField> fieldList)
Adds all fields intype
tofieldList
, renumbering the fields (if necessary) to ensure that their index matches their position in the list.
-
isJavaType
public static boolean isJavaType(RelDataType t)
-
fieldsOf
private java.util.List<RelDataTypeFieldImpl> fieldsOf(java.lang.Class clazz)
-
createDecimalProduct
public RelDataType createDecimalProduct(RelDataType type1, RelDataType type2)
Infers the return type of a decimal multiplication. Decimal multiplication involves at least one decimal operand and requires both operands to have exact numeric types.Implement RelDataTypeFactory with SQL 2003 compliant behavior. Let p1, s1 be the precision and scale of the first operand Let p2, s2 be the precision and scale of the second operand Let p, s be the precision and scale of the result, Then the result type is a decimal with:
- p = p1 + p2
- s = s1 + s2
p and s are capped at their maximum values
- Specified by:
createDecimalProduct
in interfaceRelDataTypeFactory
- Parameters:
type1
- type of the first operandtype2
- type of the second operand- Returns:
- the result type for a decimal multiplication, or null if decimal multiplication should not be applied to the operands.
- See Also:
SQL:2003 Part 2 Section 6.26
-
useDoubleMultiplication
public boolean useDoubleMultiplication(RelDataType type1, RelDataType type2)
Description copied from interface:RelDataTypeFactory
Returns whether a decimal multiplication should be implemented by casting arguments to double values.Pre-condition:
createDecimalProduct(type1, type2) != null
- Specified by:
useDoubleMultiplication
in interfaceRelDataTypeFactory
-
createDecimalQuotient
public RelDataType createDecimalQuotient(RelDataType type1, RelDataType type2)
Rules:- Let p1, s1 be the precision and scale of the first operand
- Let p2, s2 be the precision and scale of the second operand
- Let p, s be the precision and scale of the result
- Let d be the number of whole digits in the result
- Then the result type is a decimal with:
- d = p1 - s1 + s2
- s < max(6, s1 + p2 + 1)
- p = d + s
- p and s are capped at their maximum values
- Specified by:
createDecimalQuotient
in interfaceRelDataTypeFactory
- Parameters:
type1
- type of the first operandtype2
- type of the second operand- Returns:
- the result type for a decimal division, or null if decimal division should not be applied to the operands.
- See Also:
SQL:2003 Part 2 Section 6.26
-
getDefaultCharset
public java.nio.charset.Charset getDefaultCharset()
- Specified by:
getDefaultCharset
in interfaceRelDataTypeFactory
- Returns:
- the default
Charset
for string types
-
builder
public RelDataTypeFactory.FieldInfoBuilder builder()
Description copied from interface:RelDataTypeFactory
Creates aRelDataTypeFactory.FieldInfoBuilder
. But sinceFieldInfoBuilder
is deprecated, we recommend that you use its base classRelDataTypeFactory.Builder
, which is not deprecated.- Specified by:
builder
in interfaceRelDataTypeFactory
-
-