Class SqlValidatorUtil
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Deprecated.static class
Flattens any FILTER, WITHIN DISTINCT, WITHIN GROUP surrounding a call to an aggregate function.static interface
Suggests candidates for unique names, given the number of attempts so far and the number of expressions in the project list. -
Field Summary
Modifier and TypeFieldDescriptionstatic final SqlValidatorUtil.Suggester
static final SqlValidatorUtil.Suggester
static final SqlValidatorUtil.Suggester
-
Method Summary
Modifier and TypeMethodDescriptionstatic SqlNode
Converts an expression "expr" into "expr AS alias".static void
adjustTypeForArrayConstructor
(RelDataType componentType, SqlOperatorBinding opBinding) When the array element does not equal the array component type, make explicit casting.static void
adjustTypeForArrayFunctions
(RelDataType targetType, SqlOperatorBinding opBinding, int... indexes) Adjusts the types of specified operands in an array operation to match a given target type.static void
adjustTypeForMapConstructor
(Pair<RelDataType, RelDataType> componentType, SqlOperatorBinding opBinding) When the map key or value does not equal the map component key type or value type, make explicit casting.static @Nullable String
static String
Derives an alias for a node, and invents a mangled identifier if it cannot.static void
analyzeGroupItem
(SqlValidatorScope scope, org.apache.calcite.sql.validate.SqlValidatorUtil.GroupAnalyzer groupAnalyzer, com.google.common.collect.ImmutableList.Builder<com.google.common.collect.ImmutableList<ImmutableBitSet>> topBuilder, SqlNode groupExpr) Analyzes an expression in a GROUP BY clause.static void
static boolean
Returns whether there are any input columns that are sorted.static RelDataType
createJoinType
(RelDataTypeFactory typeFactory, RelDataType leftType, @Nullable RelDataType rightType, @Nullable List<String> fieldNameList, List<RelDataTypeField> systemFieldList) Returns the type the row which results when two relations are joined.static CalciteCatalogReader
createSingleTableCatalogReader
(boolean caseSensitive, String tableName, RelDataTypeFactory typeFactory, RelDataType rowType) Creates a catalog reader that contains a singleTable
with temporary table name and specifiedrowType
.static RelDataType
createTypeFromProjection
(RelDataType type, List<String> columnNameList, RelDataTypeFactory typeFactory, boolean caseSensitive) static com.google.common.collect.ImmutableList<ImmutableBitSet>
cube
(List<ImmutableBitSet> bitSets) Computes the cube of bit sets.static RelDataType
deriveJoinRowType
(RelDataType leftType, @Nullable RelDataType rightType, JoinRelType joinType, RelDataTypeFactory typeFactory, @Nullable List<String> fieldNameList, List<RelDataTypeField> systemFieldList) Derives the type of a join relational expression.deriveNaturalJoinColumnList
(SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType) Derives the list of column names suitable for NATURAL JOIN.Flattens an aggregate call.static @Nullable String
Deprecated.static @Nullable AggregatingSelectScope
static @Nullable SelectScope
static List<RelDataTypeField>
getExtendedColumns
(SqlValidator validator, SqlValidatorTable table, SqlNodeList extendedColumns) Gets a list of extended columns with field indices to the underlying table.static com.google.common.collect.ImmutableMap<Integer,
RelDataTypeField> getIndexToFieldMap
(List<RelDataTypeField> sourceFields, RelDataType targetFields) Gets a map of indexes from the source to fields in the target for the intersecting set of source and target fields.static @Nullable SqlNode
getMeasure
(SqlNode selectItem) Returns the measure expression if a select item is a measure, null otherwise.static ImmutableBitSet
getOrdinalBitSet
(RelDataType sourceRowType, Map<Integer, RelDataTypeField> indexToField) Gets the bit-set to the column ordinals in the source for columns that intersect in the target.static ImmutableBitSet
getOrdinalBitSet
(RelDataType sourceRowType, RelDataType targetRowType) Gets the bit-set to the column ordinals in the source for columns that intersect in the target.static @Nullable RelOptTable
getRelOptTable
(SqlValidatorNamespace namespace, @Nullable Prepare.CatalogReader catalogReader, @Nullable String datasetName, boolean @Nullable [] usedDataset) Converts aSqlValidatorScope
into aRelOptTable
.static @Nullable CalciteSchema
getSchema
(CalciteSchema rootSchema, Iterable<String> schemaPath, SqlNameMatcher nameMatcher) Finds and returnsCalciteSchema
nested to the given rootSchema with specified schemaPath.static void
getSchemaObjectMonikers
(SqlValidatorCatalogReader catalogReader, List<String> names, List<SqlMoniker> hints) static @Nullable CalciteSchema.TableEntry
getTableEntry
(SqlValidatorCatalogReader catalogReader, List<String> names) Finds aCalciteSchema.TableEntry
in a given catalog reader whose table has the given name, possibly qualified.static @Nullable RelDataTypeField
getTargetField
(RelDataType rowType, RelDataTypeFactory typeFactory, SqlIdentifier id, SqlValidatorCatalogReader catalogReader, @Nullable RelOptTable table) Resolve a target column name in the target table.static @Nullable CalciteSchema.TypeEntry
getTypeEntry
(CalciteSchema rootSchema, SqlIdentifier typeName) Finds aCalciteSchema.TypeEntry
in a given schema whose type has the given name, possibly qualified.static boolean
Returns whether a select item is a measure.static SqlValidatorNamespace
lookup
(SqlValidatorScope scope, List<String> names) Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace.static @Nullable RelDataTypeField
lookupField
(boolean caseSensitive, RelDataType rowType, String columnName) Deprecated.static @Nullable SqlOperator
lookupSqlFunctionByID
(SqlOperatorTable opTab, SqlIdentifier funName, @Nullable SqlFunctionCategory funcType) Lookup sql function by sql identifier and function category.mapNameToIndex
(List<RelDataTypeField> fields) Returns a map from field names to indexes.static SqlValidatorWithHints
newValidator
(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory) Deprecated.static SqlValidatorWithHints
newValidator
(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory, SqlValidator.Config config) Factory method forSqlValidator
.static com.google.common.collect.ImmutableList<ImmutableBitSet>
rollup
(List<ImmutableBitSet> bitSets) Computes the rollup of bit sets.static String
uniquify
(@Nullable String name, Set<String> usedNames, SqlValidatorUtil.Suggester suggester) Makes a name distinct from other names which have already been used, adds it to the list, and returns it.Deprecated.Makes sure that the names in a list are unique.uniquify
(List<? extends @Nullable String> nameList, SqlValidatorUtil.Suggester suggester) Deprecated.uniquify
(List<? extends @Nullable String> nameList, SqlValidatorUtil.Suggester suggester, boolean caseSensitive) Makes sure that the names in a list are unique.static Pair<SqlNode,
RelDataType> validateExprWithRowType
(boolean caseSensitive, SqlOperatorTable operatorTable, RelDataTypeFactory typeFactory, RelDataType rowType, SqlNode expr) Validate the sql node with specified base table row type.
-
Field Details
-
EXPR_SUGGESTER
-
F_SUGGESTER
-
ATTEMPT_SUGGESTER
-
-
Method Details
-
getRelOptTable
public static @Nullable RelOptTable getRelOptTable(SqlValidatorNamespace namespace, @Nullable Prepare.CatalogReader catalogReader, @Nullable String datasetName, boolean @Nullable [] usedDataset) Converts aSqlValidatorScope
into aRelOptTable
. This is only possible if the scope represents an identifier, such as "sales.emp". Otherwise, returns null.- Parameters:
namespace
- NamespacecatalogReader
- SchemadatasetName
- Name of sample dataset to substitute, or null to use the regular tableusedDataset
- Output parameter which is set to true if a sample dataset is found; may be null
-
getExtendedColumns
public static List<RelDataTypeField> getExtendedColumns(SqlValidator validator, SqlValidatorTable table, SqlNodeList extendedColumns) Gets a list of extended columns with field indices to the underlying table. -
getIndexToFieldMap
public static com.google.common.collect.ImmutableMap<Integer,RelDataTypeField> getIndexToFieldMap(List<RelDataTypeField> sourceFields, RelDataType targetFields) Gets a map of indexes from the source to fields in the target for the intersecting set of source and target fields.- Parameters:
sourceFields
- The source of column names that determine indexestargetFields
- The target fields to be indexed
-
getOrdinalBitSet
public static ImmutableBitSet getOrdinalBitSet(RelDataType sourceRowType, RelDataType targetRowType) Gets the bit-set to the column ordinals in the source for columns that intersect in the target.- Parameters:
sourceRowType
- The source upon which to ordinate the bit set.targetRowType
- The target to overlay on the source to create the bit set.
-
getOrdinalBitSet
public static ImmutableBitSet getOrdinalBitSet(RelDataType sourceRowType, Map<Integer, RelDataTypeField> indexToField) Gets the bit-set to the column ordinals in the source for columns that intersect in the target.- Parameters:
sourceRowType
- The source upon which to ordinate the bit set.indexToField
- The map of ordinals to target fields.
-
mapNameToIndex
Returns a map from field names to indexes. -
lookupField
@Deprecated public static @Nullable RelDataTypeField lookupField(boolean caseSensitive, RelDataType rowType, String columnName) Deprecated. -
checkCharsetAndCollateConsistentIfCharType
-
addAlias
Converts an expression "expr" into "expr AS alias". -
alias
Derives an alias for a node, and invents a mangled identifier if it cannot.Examples:
- Alias: "1 + 2 as foo" yields "foo"
- Identifier: "foo.bar.baz" yields "baz"
- Anything else yields "expr$ordinal"
- Parameters:
node
- Nodeordinal
- Ordinal in SELECT clause (must be ≥ 0)- Returns:
- An alias, if one can be derived; or a synthetic alias "expr$ordinal"; never null
-
alias
-
getAlias
Deprecated.Derives an alias for a node, and invents a mangled identifier if it cannot. -
newValidator
public static SqlValidatorWithHints newValidator(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory, SqlValidator.Config config) Factory method forSqlValidator
. -
newValidator
@Deprecated public static SqlValidatorWithHints newValidator(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory) Deprecated.Factory method forSqlValidator
, with default conformance. -
uniquify
public static String uniquify(@Nullable String name, Set<String> usedNames, SqlValidatorUtil.Suggester suggester) Makes a name distinct from other names which have already been used, adds it to the list, and returns it.- Parameters:
name
- Suggested name, may not be uniqueusedNames
- Collection of names already usedsuggester
- Base for name when input name is null- Returns:
- Unique name
-
uniquify
Deprecated.Makes sure that the names in a list are unique.Does not modify the input list. Returns the input list if the strings are unique, otherwise allocates a new list. Deprecated in favor of caseSensitive aware version.
- Parameters:
nameList
- List of strings- Returns:
- List of unique strings
-
uniquify
@Deprecated public static List<String> uniquify(List<? extends @Nullable String> nameList, SqlValidatorUtil.Suggester suggester) Deprecated.Makes sure that the names in a list are unique.Does not modify the input list. Returns the input list if the strings are unique, otherwise allocates a new list.
- Parameters:
nameList
- List of stringssuggester
- How to generate new names if duplicate names are found- Returns:
- List of unique strings
-
uniquify
public static List<String> uniquify(List<? extends @Nullable String> nameList, boolean caseSensitive) Makes sure that the names in a list are unique.Does not modify the input list. Returns the input list if the strings are unique, otherwise allocates a new list.
- Parameters:
nameList
- List of stringscaseSensitive
- Whether upper and lower case names are considered distinct- Returns:
- List of unique strings
-
uniquify
public static List<String> uniquify(List<? extends @Nullable String> nameList, SqlValidatorUtil.Suggester suggester, boolean caseSensitive) Makes sure that the names in a list are unique.Does not modify the input list. Returns the input list if the strings are unique, otherwise allocates a new list.
- Parameters:
nameList
- List of stringssuggester
- How to generate new names if duplicate names are foundcaseSensitive
- Whether upper and lower case names are considered distinct- Returns:
- List of unique strings
-
deriveJoinRowType
public static RelDataType deriveJoinRowType(RelDataType leftType, @Nullable RelDataType rightType, JoinRelType joinType, RelDataTypeFactory typeFactory, @Nullable List<String> fieldNameList, List<RelDataTypeField> systemFieldList) Derives the type of a join relational expression.- Parameters:
leftType
- Row type of left input to joinrightType
- Row type of right input to joinjoinType
- Type of jointypeFactory
- Type factoryfieldNameList
- List of names of fields; if null, field names are inherited and made uniquesystemFieldList
- List of system fields that will be prefixed to output row type; typically empty but must not be null- Returns:
- join type
-
createJoinType
public static RelDataType createJoinType(RelDataTypeFactory typeFactory, RelDataType leftType, @Nullable RelDataType rightType, @Nullable List<String> fieldNameList, List<RelDataTypeField> systemFieldList) Returns the type the row which results when two relations are joined.The resulting row type consists of the system fields (if any), followed by the fields of the left type, followed by the fields of the right type. The field name list, if present, overrides the original names of the fields.
- Parameters:
typeFactory
- Type factoryleftType
- Type of left input to joinrightType
- Type of right input to join, or null for semi-joinfieldNameList
- If not null, overrides the original names of the fieldssystemFieldList
- List of system fields that will be prefixed to output row type; typically empty but must not be null- Returns:
- type of row which results when two relations are joined
-
getTargetField
public static @Nullable RelDataTypeField getTargetField(RelDataType rowType, RelDataTypeFactory typeFactory, SqlIdentifier id, SqlValidatorCatalogReader catalogReader, @Nullable RelOptTable table) Resolve a target column name in the target table.- Parameters:
rowType
- the target row typeid
- the target column identifiertable
- the target table or null if it is not a RelOptTable instance- Returns:
- the target field or null if the name cannot be resolved
-
lookup
Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace. The returned namespace, never null, may represent a schema, table, column, etc. -
getSchemaObjectMonikers
public static void getSchemaObjectMonikers(SqlValidatorCatalogReader catalogReader, List<String> names, List<SqlMoniker> hints) -
getEnclosingSelectScope
-
getEnclosingAggregateSelectScope
public static @Nullable AggregatingSelectScope getEnclosingAggregateSelectScope(SqlValidatorScope scope) -
deriveNaturalJoinColumnList
public static List<String> deriveNaturalJoinColumnList(SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType) Derives the list of column names suitable for NATURAL JOIN. These are the columns that occur at least once each side of the join.- Parameters:
nameMatcher
- Whether matches are case-sensitiveleftRowType
- Row type of left input to the joinrightRowType
- Row type of right input to the join- Returns:
- List of columns that occur once on each side
-
createTypeFromProjection
public static RelDataType createTypeFromProjection(RelDataType type, List<String> columnNameList, RelDataTypeFactory typeFactory, boolean caseSensitive) -
analyzeGroupItem
public static void analyzeGroupItem(SqlValidatorScope scope, org.apache.calcite.sql.validate.SqlValidatorUtil.GroupAnalyzer groupAnalyzer, com.google.common.collect.ImmutableList.Builder<com.google.common.collect.ImmutableList<ImmutableBitSet>> topBuilder, SqlNode groupExpr) Analyzes an expression in a GROUP BY clause.It may be an expression, an empty list (), or a call to
GROUPING SETS
,CUBE
,ROLLUP
,TUMBLE
,HOP
orSESSION
.Each group item produces a list of group sets, which are written to
topBuilder
. To find the grouping sets of the query, we will take the cartesian product of the group sets. -
rollup
public static com.google.common.collect.ImmutableList<ImmutableBitSet> rollup(List<ImmutableBitSet> bitSets) Computes the rollup of bit sets.For example,
rollup({0}, {1})
returns({0, 1}, {0}, {})
.Bit sets are not necessarily singletons:
rollup({0, 2}, {3, 5})
returns({0, 2, 3, 5}, {0, 2}, {})
. -
cube
public static com.google.common.collect.ImmutableList<ImmutableBitSet> cube(List<ImmutableBitSet> bitSets) Computes the cube of bit sets.For example,
rollup({0}, {1})
returns({0, 1}, {0}, {})
.Bit sets are not necessarily singletons:
rollup({0, 2}, {3, 5})
returns({0, 2, 3, 5}, {0, 2}, {})
. -
getTypeEntry
public static @Nullable CalciteSchema.TypeEntry getTypeEntry(CalciteSchema rootSchema, SqlIdentifier typeName) Finds aCalciteSchema.TypeEntry
in a given schema whose type has the given name, possibly qualified.- Parameters:
rootSchema
- root schematypeName
- name of the type, may be qualified or fully-qualified- Returns:
- TypeEntry with a table with the given name, or null
-
getTableEntry
public static @Nullable CalciteSchema.TableEntry getTableEntry(SqlValidatorCatalogReader catalogReader, List<String> names) Finds aCalciteSchema.TableEntry
in a given catalog reader whose table has the given name, possibly qualified.Uses the case-sensitivity policy of the specified catalog reader.
If not found, returns null.
- Parameters:
catalogReader
- accessor to the table metadatanames
- Name of table, may be qualified or fully-qualified- Returns:
- TableEntry with a table with the given name, or null
-
getSchema
public static @Nullable CalciteSchema getSchema(CalciteSchema rootSchema, Iterable<String> schemaPath, SqlNameMatcher nameMatcher) Finds and returnsCalciteSchema
nested to the given rootSchema with specified schemaPath.Uses the case-sensitivity policy of specified nameMatcher.
If not found, returns null.
- Parameters:
rootSchema
- root schemaschemaPath
- full schema path of required schemanameMatcher
- name matcher- Returns:
- CalciteSchema that corresponds specified schemaPath
-
containsMonotonic
Returns whether there are any input columns that are sorted.If so, it can be the default ORDER BY clause for a WINDOW specification. (This is an extension to the SQL standard for streaming.)
-
lookupSqlFunctionByID
public static @Nullable SqlOperator lookupSqlFunctionByID(SqlOperatorTable opTab, SqlIdentifier funName, @Nullable SqlFunctionCategory funcType) Lookup sql function by sql identifier and function category.- Parameters:
opTab
- operator table to look upfunName
- function namefuncType
- function category- Returns:
- A sql function if and only if there is one operator matches, else null
-
validateExprWithRowType
public static Pair<SqlNode,RelDataType> validateExprWithRowType(boolean caseSensitive, SqlOperatorTable operatorTable, RelDataTypeFactory typeFactory, RelDataType rowType, SqlNode expr) Validate the sql node with specified base table row type. For "base table", we mean the table that the sql node expression references fields with.- Parameters:
caseSensitive
- whether to match the catalog case-sensitivelyoperatorTable
- operator tabletypeFactory
- type factoryrowType
- the table row type that has fields referenced by the expressionexpr
- the expression to validate- Returns:
- pair of a validated expression sql node and its data type, usually a SqlUnresolvedFunction is converted to a resolved function
-
createSingleTableCatalogReader
public static CalciteCatalogReader createSingleTableCatalogReader(boolean caseSensitive, String tableName, RelDataTypeFactory typeFactory, RelDataType rowType) Creates a catalog reader that contains a singleTable
with temporary table name and specifiedrowType
.Make this method public so that other systems can also use it.
- Parameters:
caseSensitive
- whether to match case sensitivelytableName
- table name to register withtypeFactory
- type factoryrowType
- table row type- Returns:
- the
CalciteCatalogReader
instance
-
flatten
Flattens an aggregate call. -
isMeasure
Returns whether a select item is a measure. -
getMeasure
Returns the measure expression if a select item is a measure, null otherwise.For a measure,
selectItem
will have the formAS(MEASURE(exp), alias)
and this method returnsexp
. -
adjustTypeForArrayConstructor
public static void adjustTypeForArrayConstructor(RelDataType componentType, SqlOperatorBinding opBinding) When the array element does not equal the array component type, make explicit casting.- Parameters:
componentType
- derived array component typeopBinding
- description of call
-
adjustTypeForArrayFunctions
public static void adjustTypeForArrayFunctions(RelDataType targetType, SqlOperatorBinding opBinding, int... indexes) Adjusts the types of specified operands in an array operation to match a given target type. This is particularly useful in the context of SQL operations involving array functions, where it's necessary to ensure that all operands have consistent types for the operation to be valid.This method operates on the assumption that the operands to be adjusted are part of a
SqlCall
, which is bound within aSqlOperatorBinding
. The operands to be cast are identified by their indexes within theoperands
list of theSqlCall
. The method performs a dynamic check to determine if an operand is a basic call to an array. If so, it casts each element within the array to the target type. Otherwise, it casts the operand itself to the target type.Example usage: For an operation like
array_append(array(1,2), cast(2 as tinyint))
, if targetType is double, this method would ensure that the elements of the first array and the second operand are cast to double.- Parameters:
targetType
- The targetRelDataType
to which the operands should be cast.opBinding
- TheSqlOperatorBinding
context, which provides access to theSqlCall
and its operands.indexes
- The indexes of the operands within theSqlCall
that need to be adjusted to the target type.- Throws:
NullPointerException
- iftargetType
isnull
.
-
adjustTypeForMapConstructor
public static void adjustTypeForMapConstructor(Pair<RelDataType, RelDataType> componentType, SqlOperatorBinding opBinding) When the map key or value does not equal the map component key type or value type, make explicit casting.- Parameters:
componentType
- derived map pair component typeopBinding
- description of call
-
alias(SqlNode)
ifordinal
is negative, oralias(SqlNode, int)
ifordinal
is non-negative.