public abstract class SqlUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
SqlUtil.DatabaseMetaDataInvocationHandler
Handles particular
DatabaseMetaData 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 a
SqlNode tree and returns the
ancestry stack when it finds a given node. |
Constructor and Description |
---|
SqlUtil() |
Modifier and Type | Method and 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 an
NlsString . |
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 of
predicate within a given
SqlNode tree. |
static java.nio.charset.Charset |
getCharset(java.lang.String charsetName)
Returns the Java-level
Charset based on given SQL-level name. |
static SqlNode |
getFromNode(SqlSelect query,
int ordinal)
Returns the
n 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 the
i 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 nested
CAST(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,
SqlNameMatcher nameMatcher)
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,
SqlNameMatcher nameMatcher)
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,
SqlNameMatcher nameMatcher) |
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.
|
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 SqlNodeList
|
static 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 |
unparseSqlIdentifierSyntax(SqlWriter writer,
SqlIdentifier identifier,
boolean asFunctionID)
Unparse a SqlIdentifier 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.
|
public static SqlNode getFromNode(SqlSelect query, int ordinal)
n
th (0-based) input to a join expression.public static SqlNodeList toNodeList(SqlNode[] operands)
public static boolean isNullLiteral(SqlNode node, boolean allowCast)
Examples:
SqlLiteral
Unknown, returns false.
CAST(NULL AS type)
, returns true if
allowCast
is true, false otherwise.
CAST(CAST(NULL AS type) AS type))
,
returns false.
public static boolean isNull(SqlNode node)
CAST(NULL AS type)
calls. For example:
isNull(CAST(CAST(NULL as INTEGER) AS VARCHAR(1)))
returns true
.public static boolean isLiteral(SqlNode node, boolean allowCast)
Examples:
CAST(literal AS type)
, returns true if
allowCast
is true, false otherwise.
CAST(CAST(literal AS type) AS type))
,
returns false.
node
- The node, never null.allowCast
- whether to regard CAST(literal) as a literalpublic static boolean isLiteral(SqlNode node)
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.
node
- The node, never null.public static boolean isLiteralChain(SqlNode node)
node
- The node, never null.public static void unparseFunctionSyntax(SqlOperator operator, SqlWriter writer, SqlCall call)
operator
- The operatorwriter
- Writercall
- List of 0 or more operandspublic static void unparseSqlIdentifierSyntax(SqlWriter writer, SqlIdentifier identifier, boolean asFunctionID)
writer
- Writeridentifier
- SqlIdentifierasFunctionID
- Whether this identifier comes from a SqlFunctionpublic static void unparseBinarySyntax(SqlOperator operator, SqlCall call, SqlWriter writer, int leftPrec, int rightPrec)
public static SqlLiteral concatenateLiterals(java.util.List<SqlLiteral> lits)
This method takes an array of arguments, since pairwise concatenation means too much string copying.
lits
- an array of SqlLiteral
, not empty, all of the same
classSqlLiteral
, of that same class, whose value is the
string concatenation of the values of the literalsjava.lang.ClassCastException
- if the lits are not homogeneous.java.lang.ArrayIndexOutOfBoundsException
- if lits is an empty array.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)
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.)nameMatcher
- Whether to look up the function case-sensitivelySQL:1999 Part 2 Section 10.4
private static java.util.Iterator<SqlOperator> filterOperatorRoutinesByKind(java.util.Iterator<SqlOperator> routines, SqlKind sqlKind)
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)
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 upnameMatcher
- Whether to look up the function case-sensitivelySQL:1999 Part 2 Section 10.4
public static boolean matchRoutinesByParameterCount(SqlOperatorTable opTab, SqlIdentifier funcName, java.util.List<RelDataType> argTypes, SqlFunctionCategory category, SqlNameMatcher nameMatcher)
opTab
- operator table to searchfuncName
- name of function being invokedargTypes
- argument typescategory
- category of routine to look upnameMatcher
- Whether to look up the function case-sensitivelyprivate static java.util.Iterator<SqlOperator> lookupSubjectRoutinesByName(SqlOperatorTable opTab, SqlIdentifier funcName, SqlSyntax syntax, SqlFunctionCategory category, SqlNameMatcher nameMatcher)
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)
SQL:1999 Part 2 Section 9.4
private static RelDataType bestMatch(java.util.List<SqlFunction> sqlFunctions, int i, RelDataTypePrecedenceList precList)
public static SqlNode getSelectListItem(SqlNode query, int i)
i
th select-list item of a query.public static java.lang.String deriveAliasFromOrdinal(int ordinal)
public static java.lang.String getOperatorSignature(SqlOperator op, java.util.List<?> typeList)
op
- operatortypeList
- list of types to use for operands. Types may be
represented as String
, SqlTypeFamily
, or
any object with a valid Object.toString()
method.public static java.lang.String getAliasedSignature(SqlOperator op, java.lang.String opName, java.util.List<?> typeList)
op
- operatoropName
- name to use for operatortypeList
- list of SqlTypeName
or String
to use for
operandspublic static CalciteException newContextException(SqlParserPos pos, Resources.ExInst<?> e, java.lang.String inputText)
public static CalciteContextException newContextException(SqlParserPos pos, Resources.ExInst<?> e)
public static CalciteContextException newContextException(int line, int col, int endLine, int endCol, Resources.ExInst<?> e)
public static boolean isCallTo(SqlNode node, SqlOperator operator)
public static RelDataType createNlsStringType(RelDataTypeFactory typeFactory, NlsString str)
NlsString
.
The type inherits the The NlsString's Charset
and
SqlCollation
, if they are set, otherwise it gets the system
defaults.
typeFactory
- Type factorystr
- Stringpublic static java.lang.String translateCharacterSetName(java.lang.String name)
name
- SQL-level namepublic static java.nio.charset.Charset getCharset(java.lang.String charsetName)
Charset
based on given SQL-level name.charsetName
- Sql charset name, must not be null.java.nio.charset.UnsupportedCharsetException
- If no support for the named charset
is available in this instance of the Java virtual machinepublic static void validateCharset(org.apache.calcite.avatica.util.ByteString value, java.nio.charset.Charset charset)
value
- nls string in byte arraycharset
- charsetjava.lang.RuntimeException
- If the given value cannot be represented in the
given charsetpublic static SqlNode stripAs(SqlNode node)
public static com.google.common.collect.ImmutableList<SqlNode> getAncestry(SqlNode root, java.util.function.Predicate<SqlNode> predicate, java.util.function.Predicate<SqlNode> postPredicate)
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.
Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.