Class SqlValidatorUtil
- java.lang.Object
-
- org.apache.calcite.sql.validate.SqlValidatorUtil
-
public class SqlValidatorUtil extends java.lang.Object
Utility methods related to validation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SqlValidatorUtil.DeepCopier
Deprecated.(package private) static class
SqlValidatorUtil.GroupAnalyzer
Builds a list of GROUP BY expressions.static interface
SqlValidatorUtil.Suggester
Suggests candidates for unique names, given the number of attempts so far and the number of expressions in the project list.
-
Field Summary
Fields Modifier and Type Field Description static SqlValidatorUtil.Suggester
ATTEMPT_SUGGESTER
static SqlValidatorUtil.Suggester
EXPR_SUGGESTER
static SqlValidatorUtil.Suggester
F_SUGGESTER
-
Constructor Summary
Constructors Modifier Constructor Description private
SqlValidatorUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static SqlNode
addAlias(SqlNode expr, java.lang.String alias)
Converts an expression "expr" into "expr AS alias".private static void
addFields(java.util.List<RelDataTypeField> fieldList, java.util.List<RelDataType> typeList, java.util.List<java.lang.String> nameList, java.util.Set<java.lang.String> uniqueNames)
private static ImmutableBitSet
analyzeGroupExpr(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, SqlNode groupExpr)
Analyzes a component of a tuple in a GROUPING SETS clause.static void
analyzeGroupItem(SqlValidatorScope scope, 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.private static java.util.List<ImmutableBitSet>
analyzeGroupTuple(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, java.util.List<SqlNode> operandList)
Analyzes a tuple in a GROUPING SETS clause.static void
checkCharsetAndCollateConsistentIfCharType(RelDataType type)
(package private) static void
checkIdentifierListForDuplicates(java.util.List<SqlNode> columnList, SqlValidatorImpl.ValidationErrorFunction validationErrorFunction)
Checks that there are no duplicates in a list ofSqlIdentifier
.private static java.util.List<SqlValidatorNamespace>
children(SqlValidatorScope scope)
(package private) static boolean
containsMonotonic(SelectScope scope, SqlNodeList nodes)
Returns whether any of the given expressions are sorted.static boolean
containsMonotonic(SqlValidatorScope scope)
Returns whether there are any input columns that are sorted.private static void
convertGroupSet(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, com.google.common.collect.ImmutableList.Builder<ImmutableBitSet> builder, SqlNode groupExpr)
Analyzes a GROUPING SETS item in a GROUP BY clause.static RelDataType
createJoinType(RelDataTypeFactory typeFactory, RelDataType leftType, RelDataType rightType, java.util.List<java.lang.String> fieldNameList, java.util.List<RelDataTypeField> systemFieldList)
Returns the type the row which results when two relations are joined.static RelDataType
createTypeFromProjection(RelDataType type, java.util.List<java.lang.String> columnNameList, RelDataTypeFactory typeFactory, boolean caseSensitive)
static com.google.common.collect.ImmutableList<ImmutableBitSet>
cube(java.util.List<ImmutableBitSet> bitSets)
Computes the cube of bit sets.static RelDataType
deriveJoinRowType(RelDataType leftType, RelDataType rightType, JoinRelType joinType, RelDataTypeFactory typeFactory, java.util.List<java.lang.String> fieldNameList, java.util.List<RelDataTypeField> systemFieldList)
Derives the type of a join relational expression.static java.util.List<java.lang.String>
deriveNaturalJoinColumnList(SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType)
Derives the list of column names suitable for NATURAL JOIN.static java.lang.String
getAlias(SqlNode node, int ordinal)
Derives an alias for a node, and invents a mangled identifier if it cannot.static AggregatingSelectScope
getEnclosingAggregateSelectScope(SqlValidatorScope scope)
static SelectScope
getEnclosingSelectScope(SqlValidatorScope scope)
static java.util.List<RelDataTypeField>
getExtendedColumns(RelDataTypeFactory typeFactory, SqlValidatorTable table, SqlNodeList extendedColumns)
Gets a list of extended columns with field indices to the underlying table.static com.google.common.collect.ImmutableMap<java.lang.Integer,RelDataTypeField>
getIndexToFieldMap(java.util.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 ImmutableBitSet
getOrdinalBitSet(RelDataType sourceRowType, java.util.Map<java.lang.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 RelOptTable
getRelOptTable(SqlValidatorNamespace namespace, Prepare.CatalogReader catalogReader, java.lang.String datasetName, boolean[] usedDataset)
Converts aSqlValidatorScope
into aRelOptTable
.private static RelOptTable
getRelOptTable(TableNamespace tableNamespace, Prepare.CatalogReader catalogReader, java.lang.String datasetName, boolean[] usedDataset, java.util.List<RelDataTypeField> extendedFields)
static CalciteSchema
getSchema(CalciteSchema rootSchema, java.lang.Iterable<java.lang.String> schemaPath, SqlNameMatcher nameMatcher)
Finds and returnsCalciteSchema
nested to the given rootSchema with specified schemaPath.static void
getSchemaObjectMonikers(SqlValidatorCatalogReader catalogReader, java.util.List<java.lang.String> names, java.util.List<SqlMoniker> hints)
static CalciteSchema.TableEntry
getTableEntry(SqlValidatorCatalogReader catalogReader, java.util.List<java.lang.String> names)
Finds aCalciteSchema.TableEntry
in a given catalog reader whose table has the given name, possibly qualified.private static CalciteSchema.TableEntry
getTableEntryFrom(CalciteSchema schema, java.lang.String name, boolean caseSensitive)
static RelDataTypeField
getTargetField(RelDataType rowType, RelDataTypeFactory typeFactory, SqlIdentifier id, SqlValidatorCatalogReader catalogReader, RelOptTable table)
Resolve a target column name in the target table.static CalciteSchema.TypeEntry
getTypeEntry(CalciteSchema rootSchema, SqlIdentifier typeName)
Finds aCalciteSchema.TypeEntry
in a given schema whose type has the given name, possibly qualified.static SqlValidatorNamespace
lookup(SqlValidatorScope scope, java.util.List<java.lang.String> names)
Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace.static RelDataTypeField
lookupField(boolean caseSensitive, RelDataType rowType, java.lang.String columnName)
Deprecated.private static int
lookupGroupExpr(SqlValidatorUtil.GroupAnalyzer groupAnalyzer, SqlNode expr)
static java.util.Map<java.lang.String,java.lang.Integer>
mapNameToIndex(java.util.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, SqlConformance conformance)
Factory method forSqlValidator
.private static java.util.List<Pair<SqlIdentifier,SqlDataTypeSpec>>
pairs(SqlNodeList extendedColumns)
Converts a list of extended columns (of the form [name0, type0, name1, type1, ...]) into a list of (name, type) pairs.static com.google.common.collect.ImmutableList<ImmutableBitSet>
rollup(java.util.List<ImmutableBitSet> bitSets)
Computes the rollup of bit sets.static java.lang.String
uniquify(java.lang.String name, java.util.Set<java.lang.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.static java.util.List<java.lang.String>
uniquify(java.util.List<java.lang.String> nameList)
Deprecated.static java.util.List<java.lang.String>
uniquify(java.util.List<java.lang.String> nameList, boolean caseSensitive)
Makes sure that the names in a list are unique.static java.util.List<java.lang.String>
uniquify(java.util.List<java.lang.String> nameList, SqlValidatorUtil.Suggester suggester)
Deprecated.static java.util.List<java.lang.String>
uniquify(java.util.List<java.lang.String> nameList, SqlValidatorUtil.Suggester suggester, boolean caseSensitive)
Makes sure that the names in a list are unique.
-
-
-
Field Detail
-
EXPR_SUGGESTER
public static final SqlValidatorUtil.Suggester EXPR_SUGGESTER
-
F_SUGGESTER
public static final SqlValidatorUtil.Suggester F_SUGGESTER
-
ATTEMPT_SUGGESTER
public static final SqlValidatorUtil.Suggester ATTEMPT_SUGGESTER
-
-
Method Detail
-
getRelOptTable
public static RelOptTable getRelOptTable(SqlValidatorNamespace namespace, Prepare.CatalogReader catalogReader, java.lang.String datasetName, boolean[] 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
-
getRelOptTable
private static RelOptTable getRelOptTable(TableNamespace tableNamespace, Prepare.CatalogReader catalogReader, java.lang.String datasetName, boolean[] usedDataset, java.util.List<RelDataTypeField> extendedFields)
-
getExtendedColumns
public static java.util.List<RelDataTypeField> getExtendedColumns(RelDataTypeFactory typeFactory, SqlValidatorTable table, SqlNodeList extendedColumns)
Gets a list of extended columns with field indices to the underlying table.
-
pairs
private static java.util.List<Pair<SqlIdentifier,SqlDataTypeSpec>> pairs(SqlNodeList extendedColumns)
Converts a list of extended columns (of the form [name0, type0, name1, type1, ...]) into a list of (name, type) pairs.
-
getIndexToFieldMap
public static com.google.common.collect.ImmutableMap<java.lang.Integer,RelDataTypeField> getIndexToFieldMap(java.util.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, java.util.Map<java.lang.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
public static java.util.Map<java.lang.String,java.lang.Integer> mapNameToIndex(java.util.List<RelDataTypeField> fields)
Returns a map from field names to indexes.
-
lookupField
@Deprecated public static RelDataTypeField lookupField(boolean caseSensitive, RelDataType rowType, java.lang.String columnName)
Deprecated.
-
checkCharsetAndCollateConsistentIfCharType
public static void checkCharsetAndCollateConsistentIfCharType(RelDataType type)
-
checkIdentifierListForDuplicates
static void checkIdentifierListForDuplicates(java.util.List<SqlNode> columnList, SqlValidatorImpl.ValidationErrorFunction validationErrorFunction)
Checks that there are no duplicates in a list ofSqlIdentifier
.
-
addAlias
public static SqlNode addAlias(SqlNode expr, java.lang.String alias)
Converts an expression "expr" into "expr AS alias".
-
getAlias
public static java.lang.String getAlias(SqlNode node, int ordinal)
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"
- Returns:
- An alias, if one can be derived; or a synthetic alias "expr$ordinal" if ordinal < 0; otherwise null
-
newValidator
public static SqlValidatorWithHints newValidator(SqlOperatorTable opTab, SqlValidatorCatalogReader catalogReader, RelDataTypeFactory typeFactory, SqlConformance conformance)
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 java.lang.String uniquify(java.lang.String name, java.util.Set<java.lang.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 public static java.util.List<java.lang.String> uniquify(java.util.List<java.lang.String> nameList)
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 java.util.List<java.lang.String> uniquify(java.util.List<java.lang.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 java.util.List<java.lang.String> uniquify(java.util.List<java.lang.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 java.util.List<java.lang.String> uniquify(java.util.List<java.lang.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, RelDataType rightType, JoinRelType joinType, RelDataTypeFactory typeFactory, java.util.List<java.lang.String> fieldNameList, java.util.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, RelDataType rightType, java.util.List<java.lang.String> fieldNameList, java.util.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
-
addFields
private static void addFields(java.util.List<RelDataTypeField> fieldList, java.util.List<RelDataType> typeList, java.util.List<java.lang.String> nameList, java.util.Set<java.lang.String> uniqueNames)
-
getTargetField
public static RelDataTypeField getTargetField(RelDataType rowType, RelDataTypeFactory typeFactory, SqlIdentifier id, SqlValidatorCatalogReader catalogReader, 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
public static SqlValidatorNamespace lookup(SqlValidatorScope scope, java.util.List<java.lang.String> names)
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, java.util.List<java.lang.String> names, java.util.List<SqlMoniker> hints)
-
getEnclosingSelectScope
public static SelectScope getEnclosingSelectScope(SqlValidatorScope scope)
-
getEnclosingAggregateSelectScope
public static AggregatingSelectScope getEnclosingAggregateSelectScope(SqlValidatorScope scope)
-
deriveNaturalJoinColumnList
public static java.util.List<java.lang.String> deriveNaturalJoinColumnList(SqlNameMatcher nameMatcher, RelDataType leftRowType, RelDataType rightRowType)
Derives the list of column names suitable for NATURAL JOIN. These are the columns that occur exactly once on 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, java.util.List<java.lang.String> columnNameList, RelDataTypeFactory typeFactory, boolean caseSensitive)
-
analyzeGroupItem
public static void analyzeGroupItem(SqlValidatorScope scope, 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.
-
convertGroupSet
private static void convertGroupSet(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, com.google.common.collect.ImmutableList.Builder<ImmutableBitSet> builder, SqlNode groupExpr)
Analyzes a GROUPING SETS item in a GROUP BY clause.
-
analyzeGroupTuple
private static java.util.List<ImmutableBitSet> analyzeGroupTuple(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, java.util.List<SqlNode> operandList)
Analyzes a tuple in a GROUPING SETS clause.For example, in
GROUP BY GROUPING SETS ((a, b), a, c)
,(a, b)
is a tuple.Gathers into
groupExprs
the set of distinct expressions being grouped, and returns a bitmap indicating which expressions this tuple is grouping.
-
analyzeGroupExpr
private static ImmutableBitSet analyzeGroupExpr(SqlValidatorScope scope, SqlValidatorUtil.GroupAnalyzer groupAnalyzer, SqlNode groupExpr)
Analyzes a component of a tuple in a GROUPING SETS clause.
-
lookupGroupExpr
private static int lookupGroupExpr(SqlValidatorUtil.GroupAnalyzer groupAnalyzer, SqlNode expr)
-
rollup
public static com.google.common.collect.ImmutableList<ImmutableBitSet> rollup(java.util.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(java.util.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 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 CalciteSchema.TableEntry getTableEntry(SqlValidatorCatalogReader catalogReader, java.util.List<java.lang.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 CalciteSchema getSchema(CalciteSchema rootSchema, java.lang.Iterable<java.lang.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
-
getTableEntryFrom
private static CalciteSchema.TableEntry getTableEntryFrom(CalciteSchema schema, java.lang.String name, boolean caseSensitive)
-
containsMonotonic
public static boolean containsMonotonic(SqlValidatorScope scope)
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.)
-
children
private static java.util.List<SqlValidatorNamespace> children(SqlValidatorScope scope)
-
containsMonotonic
static boolean containsMonotonic(SelectScope scope, SqlNodeList nodes)
Returns whether any of the given expressions 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.)
-
-