Package org.apache.calcite.sql
Class SqlUtil
- java.lang.Object
-
- org.apache.calcite.sql.SqlUtil
-
public abstract class SqlUtil extends java.lang.Object
Contains utility functions related to SQL parsing, all static.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SqlUtil.DatabaseMetaDataInvocationHandler
Handles particularDatabaseMetaData
methods; invocations of other methods will fall through to the base class,BarfingInvocationHandler
, which will throw an error.private static class
SqlUtil.Genealogist
Walks over aSqlNode
tree and returns the ancestry stack when it finds a given node.
-
Constructor Summary
Constructors Constructor Description SqlUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static SqlNode
andExpressions(SqlNode node1, SqlNode node2)
private static RelDataType
bestMatch(java.util.List<SqlFunction> sqlFunctions, int i, RelDataTypePrecedenceList precList)
static SqlLiteral
concatenateLiterals(java.util.List<SqlLiteral> lits)
Concatenates string literals.static RelDataType
createNlsStringType(RelDataTypeFactory typeFactory, NlsString str)
Creates the type of anNlsString
.static java.lang.String
deriveAliasFromOrdinal(int ordinal)
private static java.util.Iterator<SqlOperator>
filterOperatorRoutinesByKind(java.util.Iterator<SqlOperator> routines, SqlKind sqlKind)
private static java.util.Iterator<SqlOperator>
filterRoutinesByParameterCount(java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes)
private static java.util.Iterator<SqlOperator>
filterRoutinesByParameterType(SqlSyntax syntax, java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes, java.util.List<java.lang.String> argNames)
private static java.util.Iterator<SqlOperator>
filterRoutinesByTypePrecedence(SqlSyntax sqlSyntax, java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes)
(package private) static java.util.ArrayList<SqlNode>
flatten(SqlNode node)
private static void
flatten(SqlNode node, java.util.ArrayList<SqlNode> list)
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.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 ofpredicate
within a givenSqlNode
tree.static java.nio.charset.Charset
getCharset(java.lang.String charsetName)
Returns the Java-levelCharset
based on given SQL-level name.static SqlNode
getFromNode(SqlSelect query, int ordinal)
Returns then
th (0-based) input to a join expression.static java.lang.String
getOperatorSignature(SqlOperator op, java.util.List<?> typeList)
Constructs an operator signature from a type list.static SqlNode
getSelectListItem(SqlNode query, int i)
Returns thei
th select-list item of a query.static boolean
isCallTo(SqlNode node, SqlOperator operator)
static boolean
isLiteral(SqlNode node)
Returns whether a node is a literal.static boolean
isLiteral(SqlNode node, boolean allowCast)
Returns whether a node is a literal.static boolean
isLiteralChain(SqlNode node)
Returns whether a node is a literal chain which is used to represent a continued string literal.static boolean
isNull(SqlNode node)
Returns whether a node represents the NULL value or a series of nestedCAST(NULL AS type)
calls.static boolean
isNullLiteral(SqlNode node, boolean allowCast)
Returns whether a node represents the NULL value.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)
Looks up a (possibly overloaded) routine based on name and argument types.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)
Looks up all subject routines matching the given name and argument types.private static java.util.Iterator<SqlOperator>
lookupSubjectRoutinesByName(SqlOperatorTable opTab, SqlIdentifier funcName, SqlSyntax syntax, SqlFunctionCategory category)
static SqlCall
makeCall(SqlOperatorTable opTab, SqlIdentifier id)
If an identifier is a legitimate call to a function which has no arguments and requires no parentheses (for example "CURRENT_USER"), returns a call to that function, otherwise returns null.static boolean
matchRoutinesByParameterCount(SqlOperatorTable opTab, SqlIdentifier funcName, java.util.List<RelDataType> argTypes, SqlFunctionCategory category)
Determines whether there is a routine matching the given name and number of arguments.static CalciteContextException
newContextException(int line, int col, int endLine, int endCol, Resources.ExInst<?> e)
Wraps an exception with context.static CalciteContextException
newContextException(SqlParserPos pos, Resources.ExInst<?> e)
Wraps an exception with context.static CalciteException
newContextException(SqlParserPos pos, Resources.ExInst<?> e, java.lang.String inputText)
Wraps an exception with context.static SqlNode
stripAs(SqlNode node)
If a node is "AS", returns the underlying expression; otherwise returns the node.static SqlNodeList
toNodeList(SqlNode[] operands)
Converts an SqlNode array to a SqlNodeListstatic java.lang.String
translateCharacterSetName(java.lang.String name)
Translates a character set name from a SQL-level name into a Java-level name.static void
unparseBinarySyntax(SqlOperator operator, SqlCall call, SqlWriter writer, int leftPrec, int rightPrec)
static void
unparseFunctionSyntax(SqlOperator operator, SqlWriter writer, SqlCall call)
Unparses a call to an operator which has function syntax.static void
validateCharset(org.apache.calcite.avatica.util.ByteString value, java.nio.charset.Charset charset)
Validate if value can be decoded by given charset.
-
-
-
Method Detail
-
getFromNode
public static SqlNode getFromNode(SqlSelect query, int ordinal)
Returns then
th (0-based) input to a join expression.
-
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 ifallowCast
is true, false otherwise. - For
CAST(CAST(NULL AS type) AS type))
, returns false.
- For
-
isNull
public static boolean isNull(SqlNode node)
Returns whether a node represents the NULL value or a series of nestedCAST(NULL AS type)
calls. For example:isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1)))
returnstrue
.
-
isLiteral
public static boolean isLiteral(SqlNode node, boolean allowCast)
Returns whether a node is a literal.Examples:
- For
CAST(literal AS type)
, returns true ifallowCast
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
- For
-
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 callisNullLiteral(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 operatorwriter
- Writercall
- List of 0 or more operands
-
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 ofSqlLiteral
, 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)
Looks up a (possibly overloaded) routine based on name and argument types.- Parameters:
opTab
- operator table to searchfuncName
- name of function being invokedargTypes
- argument typesargNames
- argument names, or null if call by positioncategory
- whether a function or a procedure. (If a procedure is being invoked, the overload rules are simpler.)- 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)
Looks up all subject routines matching the given name and argument types.- Parameters:
opTab
- operator table to searchfuncName
- name of function being invokedargTypes
- argument typesargNames
- argument names, or null if call by positionsqlSyntax
- the SqlSyntax of the SqlOperator being looked upsqlKind
- the SqlKind of the SqlOperator being looked upcategory
- category of routine to look up- 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)
Determines whether there is a routine matching the given name and number of arguments.- Parameters:
opTab
- operator table to searchfuncName
- name of function being invokedargTypes
- argument typescategory
- category of routine to look up- Returns:
- true if match found
-
lookupSubjectRoutinesByName
private static java.util.Iterator<SqlOperator> lookupSubjectRoutinesByName(SqlOperatorTable opTab, SqlIdentifier funcName, SqlSyntax syntax, SqlFunctionCategory category)
-
filterRoutinesByParameterCount
private static java.util.Iterator<SqlOperator> filterRoutinesByParameterCount(java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes)
-
filterRoutinesByParameterType
private static java.util.Iterator<SqlOperator> filterRoutinesByParameterType(SqlSyntax syntax, java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes, java.util.List<java.lang.String> argNames)
-
filterRoutinesByTypePrecedence
private static java.util.Iterator<SqlOperator> filterRoutinesByTypePrecedence(SqlSyntax sqlSyntax, java.util.Iterator<SqlOperator> routines, java.util.List<RelDataType> argTypes)
- See Also:
SQL:1999 Part 2 Section 9.4
-
bestMatch
private static RelDataType bestMatch(java.util.List<SqlFunction> sqlFunctions, int i, RelDataTypePrecedenceList precList)
-
getSelectListItem
public static SqlNode getSelectListItem(SqlNode query, int i)
Returns thei
th select-list item of a query.
-
makeCall
public static SqlCall makeCall(SqlOperatorTable opTab, SqlIdentifier id)
If an identifier is a legitimate call to a function which has no arguments and requires no parentheses (for example "CURRENT_USER"), returns a call to that function, otherwise returns null.
-
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
- operatortypeList
- list of types to use for operands. Types may be represented asString
,SqlTypeFamily
, or any object with a validObject.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
- operatoropName
- name to use for operatortypeList
- list ofSqlTypeName
orString
to use for operands- Returns:
- constructed signature
-
newContextException
public static CalciteException newContextException(SqlParserPos pos, Resources.ExInst<?> e, java.lang.String inputText)
Wraps an exception with context.
-
newContextException
public static CalciteContextException newContextException(SqlParserPos pos, Resources.ExInst<?> e)
Wraps an exception with context.
-
newContextException
public static CalciteContextException newContextException(int line, int col, int endLine, int endCol, Resources.ExInst<?> e)
Wraps an exception with context.
-
isCallTo
public static boolean isCallTo(SqlNode node, SqlOperator operator)
-
createNlsStringType
public static RelDataType createNlsStringType(RelDataTypeFactory typeFactory, NlsString str)
Creates the type of anNlsString
.The type inherits the The NlsString's
Charset
andSqlCollation
, if they are set, otherwise it gets the system defaults.- Parameters:
typeFactory
- Type factorystr
- 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-levelCharset
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 arraycharset
- 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 ofpredicate
within a givenSqlNode
tree.The first element of the list is
root
, and the last is the node that matchedpredicate
. Throws if no node matches.
-
-