Class SqlValidatorUtil


  • public class SqlValidatorUtil
    extends java.lang.Object
    Utility methods related to validation.
    • Constructor Detail

      • SqlValidatorUtil

        private SqlValidatorUtil()
    • Method Detail

      • getRelOptTable

        public static RelOptTable getRelOptTable​(SqlValidatorNamespace namespace,
                                                 Prepare.CatalogReader catalogReader,
                                                 java.lang.String datasetName,
                                                 boolean[] usedDataset)
        Converts a SqlValidatorScope into a RelOptTable. This is only possible if the scope represents an identifier, such as "sales.emp". Otherwise, returns null.
        Parameters:
        namespace - Namespace
        catalogReader - Schema
        datasetName - Name of sample dataset to substitute, or null to use the regular table
        usedDataset - Output parameter which is set to true if a sample dataset is found; may be null
      • 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 indexes
        targetFields - 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)
      • 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
      • 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 unique
        usedNames - Collection of names already used
        suggester - 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)
        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 strings
        suggester - 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 strings
        caseSensitive - 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 strings
        suggester - How to generate new names if duplicate names are found
        caseSensitive - 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 join
        rightType - Row type of right input to join
        joinType - Type of join
        typeFactory - Type factory
        fieldNameList - List of names of fields; if null, field names are inherited and made unique
        systemFieldList - 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 factory
        leftType - Type of left input to join
        rightType - Type of right input to join, or null for semi-join
        fieldNameList - If not null, overrides the original names of the fields
        systemFieldList - 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)
      • 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)
      • 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-sensitive
        leftRowType - Row type of left input to the join
        rightRowType - 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 or SESSION.

        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.

      • 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.

      • 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 a CalciteSchema.TypeEntry in a given schema whose type has the given name, possibly qualified.
        Parameters:
        rootSchema - root schema
        typeName - 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 a CalciteSchema.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 metadata
        names - 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 returns CalciteSchema nested to the given rootSchema with specified schemaPath.

        Uses the case-sensitivity policy of specified nameMatcher.

        If not found, returns null.

        Parameters:
        rootSchema - root schema
        schemaPath - full schema path of required schema
        nameMatcher - name matcher
        Returns:
        CalciteSchema that corresponds specified schemaPath
      • 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.)

      • 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.)