Class SqlUtil


  • public abstract class SqlUtil
    extends java.lang.Object
    Contains utility functions related to SQL parsing, all static.
    • Constructor Detail

      • SqlUtil

        public SqlUtil()
    • Method Detail

      • flatten

        static java.util.ArrayList<SqlNode> flatten​(SqlNode node)
      • getFromNode

        public static SqlNode getFromNode​(SqlSelect query,
                                          int ordinal)
        Returns the nth (0-based) input to a join expression.
      • flatten

        private static void flatten​(SqlNode node,
                                    java.util.ArrayList<SqlNode> list)
      • toNodeList

        public static SqlNodeList toNodeList​(SqlNode[] operands)
        Converts an SqlNode array to a SqlNodeList
      • isNullLiteral

        public static boolean isNullLiteral​(SqlNode node,
                                            boolean allowCast)
        Returns whether a node represents the NULL value.

        Examples:

        • For SqlLiteral Unknown, returns false.
        • For CAST(NULL AS type), returns true if allowCast is true, false otherwise.
        • For CAST(CAST(NULL AS type) AS type)), returns false.
      • isNull

        public static boolean isNull​(SqlNode node)
        Returns whether a node represents the NULL value or a series of nested CAST(NULL AS type) calls. For example: isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1))) returns true.
      • isLiteral

        public static boolean isLiteral​(SqlNode node,
                                        boolean allowCast)
        Returns whether a node is a literal.

        Examples:

        • For CAST(literal AS type), returns true if allowCast is true, false otherwise.
        • For CAST(CAST(literal AS type) AS type)), returns false.
        Parameters:
        node - The node, never null.
        allowCast - whether to regard CAST(literal) as a literal
        Returns:
        Whether the node is a literal
      • isLiteral

        public static boolean isLiteral​(SqlNode node)
        Returns whether a node is a literal.

        Many constructs which require literals also accept CAST(NULL AS type). This method does not accept casts, so you should call isNullLiteral(org.apache.calcite.sql.SqlNode, boolean) first.

        Parameters:
        node - The node, never null.
        Returns:
        Whether the node is a literal
      • isLiteralChain

        public static boolean isLiteralChain​(SqlNode node)
        Returns whether a node is a literal chain which is used to represent a continued string literal.
        Parameters:
        node - The node, never null.
        Returns:
        Whether the node is a literal chain
      • unparseFunctionSyntax

        public static void unparseFunctionSyntax​(SqlOperator operator,
                                                 SqlWriter writer,
                                                 SqlCall call)
        Unparses a call to an operator which has function syntax.
        Parameters:
        operator - The operator
        writer - Writer
        call - List of 0 or more operands
      • unparseSqlIdentifierSyntax

        public static void unparseSqlIdentifierSyntax​(SqlWriter writer,
                                                      SqlIdentifier identifier,
                                                      boolean asFunctionID)
        Unparse a SqlIdentifier syntax.
        Parameters:
        writer - Writer
        identifier - SqlIdentifier
        asFunctionID - Whether this identifier comes from a SqlFunction
      • unparseBinarySyntax

        public static void unparseBinarySyntax​(SqlOperator operator,
                                               SqlCall call,
                                               SqlWriter writer,
                                               int leftPrec,
                                               int rightPrec)
      • concatenateLiterals

        public static SqlLiteral concatenateLiterals​(java.util.List<SqlLiteral> lits)
        Concatenates string literals.

        This method takes an array of arguments, since pairwise concatenation means too much string copying.

        Parameters:
        lits - an array of SqlLiteral, not empty, all of the same class
        Returns:
        a new SqlLiteral, of that same class, whose value is the string concatenation of the values of the literals
        Throws:
        java.lang.ClassCastException - if the lits are not homogeneous.
        java.lang.ArrayIndexOutOfBoundsException - if lits is an empty array.
      • lookupRoutine

        public static SqlOperator lookupRoutine​(SqlOperatorTable opTab,
                                                SqlIdentifier funcName,
                                                java.util.List<RelDataType> argTypes,
                                                java.util.List<java.lang.String> argNames,
                                                SqlFunctionCategory category,
                                                SqlSyntax syntax,
                                                SqlKind sqlKind,
                                                SqlNameMatcher nameMatcher,
                                                boolean coerce)
        Looks up a (possibly overloaded) routine based on name and argument types.
        Parameters:
        opTab - operator table to search
        funcName - name of function being invoked
        argTypes - argument types
        argNames - argument names, or null if call by position
        category - whether a function or a procedure. (If a procedure is being invoked, the overload rules are simpler.)
        nameMatcher - Whether to look up the function case-sensitively
        coerce - Whether to allow type coercion when do filter routines by parameter types
        Returns:
        matching routine, or null if none found
        See Also:
        SQL:1999 Part 2 Section 10.4
      • filterOperatorRoutinesByKind

        private static java.util.Iterator<SqlOperator> filterOperatorRoutinesByKind​(java.util.Iterator<SqlOperator> routines,
                                                                                    SqlKind sqlKind)
      • lookupSubjectRoutines

        public static java.util.Iterator<SqlOperator> lookupSubjectRoutines​(SqlOperatorTable opTab,
                                                                            SqlIdentifier funcName,
                                                                            java.util.List<RelDataType> argTypes,
                                                                            java.util.List<java.lang.String> argNames,
                                                                            SqlSyntax sqlSyntax,
                                                                            SqlKind sqlKind,
                                                                            SqlFunctionCategory category,
                                                                            SqlNameMatcher nameMatcher,
                                                                            boolean coerce)
        Looks up all subject routines matching the given name and argument types.
        Parameters:
        opTab - operator table to search
        funcName - name of function being invoked
        argTypes - argument types
        argNames - argument names, or null if call by position
        sqlSyntax - the SqlSyntax of the SqlOperator being looked up
        sqlKind - the SqlKind of the SqlOperator being looked up
        category - Category of routine to look up
        nameMatcher - Whether to look up the function case-sensitively
        coerce - Whether to allow type coercion when do filter routine by parameter types
        Returns:
        list of matching routines
        See Also:
        SQL:1999 Part 2 Section 10.4
      • matchRoutinesByParameterCount

        public static boolean matchRoutinesByParameterCount​(SqlOperatorTable opTab,
                                                            SqlIdentifier funcName,
                                                            java.util.List<RelDataType> argTypes,
                                                            SqlFunctionCategory category,
                                                            SqlNameMatcher nameMatcher)
        Determines whether there is a routine matching the given name and number of arguments.
        Parameters:
        opTab - operator table to search
        funcName - name of function being invoked
        argTypes - argument types
        category - category of routine to look up
        nameMatcher - Whether to look up the function case-sensitively
        Returns:
        true if match found
      • filterRoutinesByParameterCount

        private static java.util.Iterator<SqlOperator> filterRoutinesByParameterCount​(java.util.Iterator<SqlOperator> routines,
                                                                                      java.util.List<RelDataType> argTypes)
      • getSelectListItem

        public static SqlNode getSelectListItem​(SqlNode query,
                                                int i)
        Returns the ith select-list item of a query.
      • deriveAliasFromOrdinal

        public static java.lang.String deriveAliasFromOrdinal​(int ordinal)
      • getOperatorSignature

        public static java.lang.String getOperatorSignature​(SqlOperator op,
                                                            java.util.List<?> typeList)
        Constructs an operator signature from a type list.
        Parameters:
        op - operator
        typeList - list of types to use for operands. Types may be represented as String, SqlTypeFamily, or any object with a valid Object.toString() method.
        Returns:
        constructed signature
      • getAliasedSignature

        public static java.lang.String getAliasedSignature​(SqlOperator op,
                                                           java.lang.String opName,
                                                           java.util.List<?> typeList)
        Constructs an operator signature from a type list, substituting an alias for the operator name.
        Parameters:
        op - operator
        opName - name to use for operator
        typeList - list of SqlTypeName or String to use for operands
        Returns:
        constructed signature
      • createNlsStringType

        public static RelDataType createNlsStringType​(RelDataTypeFactory typeFactory,
                                                      NlsString str)
        Creates the type of an NlsString.

        The type inherits the The NlsString's Charset and SqlCollation, if they are set, otherwise it gets the system defaults.

        Parameters:
        typeFactory - Type factory
        str - String
        Returns:
        Type, including collation and charset
      • translateCharacterSetName

        public static java.lang.String translateCharacterSetName​(java.lang.String name)
        Translates a character set name from a SQL-level name into a Java-level name.
        Parameters:
        name - SQL-level name
        Returns:
        Java-level name, or null if SQL-level name is unknown
      • getCharset

        public static java.nio.charset.Charset getCharset​(java.lang.String charsetName)
        Returns the Java-level Charset based on given SQL-level name.
        Parameters:
        charsetName - Sql charset name, must not be null.
        Returns:
        charset, or default charset if charsetName is null.
        Throws:
        java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine
      • validateCharset

        public static void validateCharset​(org.apache.calcite.avatica.util.ByteString value,
                                           java.nio.charset.Charset charset)
        Validate if value can be decoded by given charset.
        Parameters:
        value - nls string in byte array
        charset - charset
        Throws:
        java.lang.RuntimeException - If the given value cannot be represented in the given charset
      • stripAs

        public static SqlNode stripAs​(SqlNode node)
        If a node is "AS", returns the underlying expression; otherwise returns the node.
      • getAncestry

        public static com.google.common.collect.ImmutableList<SqlNode> getAncestry​(SqlNode root,
                                                                                   java.util.function.Predicate<SqlNode> predicate,
                                                                                   java.util.function.Predicate<SqlNode> postPredicate)
        Returns a list of ancestors of predicate within a given SqlNode tree.

        The first element of the list is root, and the last is the node that matched predicate. Throws if no node matches.