public abstract class SqlUtil extends 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. |
| Modifier and Type | Field and Description |
|---|---|
static String |
GENERATED_EXPR_ALIAS_PREFIX
Prefix for generated column aliases.
|
| Constructor and Description |
|---|
SqlUtil() |
| Modifier and Type | Method and Description |
|---|---|
static SqlNode |
andExpressions(@Nullable SqlNode node1,
SqlNode node2)
Returns the AND of two expressions.
|
static RelNode |
attachRelHint(HintStrategyTable hintStrategies,
List<RelHint> hints,
Hintable rel)
Attach the
hints to rel with specified hint strategies. |
static SqlLiteral |
concatenateLiterals(List<SqlLiteral> lits)
Concatenates string literals.
|
static SqlNode |
createCall(SqlOperator op,
SqlParserPos pos,
List<SqlNode> operands)
Creates a call to an operator.
|
static RelDataType |
createNlsStringType(RelDataTypeFactory typeFactory,
NlsString str)
Creates the type of an
NlsString. |
static String |
deriveAliasFromOrdinal(int ordinal) |
static String |
getAliasedSignature(SqlOperator op,
String opName,
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,
Predicate<SqlNode> predicate,
Predicate<SqlNode> postPredicate)
Returns a list of ancestors of
predicate within a given
SqlNode tree. |
static Charset |
getCharset(String charsetName)
Returns the Java-level
Charset based on given SQL-level name. |
static SqlNode |
getFromNode(SqlSelect query,
int ordinal)
Returns the
nth (0-based) input to a join expression. |
static String |
getOperatorSignature(SqlOperator op,
List<?> typeList)
Constructs an operator signature from a type list.
|
static List<RelHint> |
getRelHint(HintStrategyTable hintStrategies,
@Nullable SqlNodeList sqlHints)
Returns an immutable list of
RelHint from sql hints, with a given
inherit path from the root node. |
static SqlNode |
getSelectListItem(SqlNode query,
int i)
Returns the
ith select-list item of a query. |
static boolean |
isCallTo(SqlNode node,
SqlOperator operator)
|
static boolean |
isGeneratedAlias(String alias)
Whether the alias is generated by calcite.
|
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(@Nullable SqlNode node,
boolean allowCast)
Returns whether a node represents the NULL value.
|
static @Nullable SqlOperator |
lookupRoutine(SqlOperatorTable opTab,
RelDataTypeFactory typeFactory,
SqlIdentifier funcName,
List<RelDataType> argTypes,
@Nullable List<String> argNames,
@Nullable SqlFunctionCategory category,
SqlSyntax syntax,
SqlKind sqlKind,
SqlNameMatcher nameMatcher,
boolean coerce)
Looks up a (possibly overloaded) routine based on name and argument
types.
|
static Iterator<SqlOperator> |
lookupSubjectRoutines(SqlOperatorTable opTab,
RelDataTypeFactory typeFactory,
SqlIdentifier funcName,
List<RelDataType> argTypes,
@Nullable List<String> argNames,
SqlSyntax sqlSyntax,
SqlKind sqlKind,
@Nullable SqlFunctionCategory category,
SqlNameMatcher nameMatcher,
boolean coerce)
Looks up all subject routines matching the given name and argument types.
|
static boolean |
matchRoutinesByParameterCount(SqlOperatorTable opTab,
SqlIdentifier funcName,
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,
org.apache.calcite.runtime.Resources.ExInst<?> e)
Wraps an exception with context.
|
static CalciteContextException |
newContextException(SqlParserPos pos,
org.apache.calcite.runtime.Resources.ExInst<?> e)
Wraps an exception with context.
|
static CalciteException |
newContextException(SqlParserPos pos,
org.apache.calcite.runtime.Resources.ExInst<?> e,
String inputText)
Wraps an exception with context.
|
static @PolyNull SqlNode |
stripAs(@PolyNull SqlNode node)
If a node is "AS", returns the underlying expression; otherwise returns
the node.
|
static SqlNodeList |
stripListAs(SqlNodeList nodeList)
Modifies a list of nodes, removing AS from each if present.
|
static SqlNodeList |
toNodeList(SqlNode[] operands)
Converts a SqlNode array to a SqlNodeList.
|
static @Nullable String |
translateCharacterSetName(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)
Deprecated.
|
static void |
unparseFunctionSyntax(SqlOperator operator,
SqlWriter writer,
SqlCall call,
boolean ordered)
Unparses a call to an operator that 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,
Charset charset)
Validate if value can be decoded by given charset.
|
public static final String GENERATED_EXPR_ALIAS_PREFIX
public static SqlNode andExpressions(@Nullable SqlNode node1, SqlNode node2)
If node1 is null, returns node2.
Flattens if either node is an AND.
public static SqlNode getFromNode(SqlSelect query, int ordinal)
nth (0-based) input to a join expression.public static SqlNodeList toNodeList(SqlNode[] operands)
public static boolean isNullLiteral(@Nullable 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.@Deprecated public static void unparseFunctionSyntax(SqlOperator operator, SqlWriter writer, SqlCall call)
public static void unparseFunctionSyntax(SqlOperator operator, SqlWriter writer, SqlCall call, boolean ordered)
operator - The operatorwriter - Writercall - List of 0 or more operandsordered - Whether argument list may end with ORDER BYpublic 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(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 literalsClassCastException - if the lits are not homogeneous.ArrayIndexOutOfBoundsException - if lits is an empty array.public static @Nullable SqlOperator lookupRoutine(SqlOperatorTable opTab, RelDataTypeFactory typeFactory, SqlIdentifier funcName, List<RelDataType> argTypes, @Nullable List<String> argNames, @Nullable SqlFunctionCategory category, SqlSyntax syntax, SqlKind sqlKind, SqlNameMatcher nameMatcher, boolean coerce)
opTab - operator table to searchtypeFactory - Type factoryfuncName - 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-sensitivelycoerce - Whether to allow type coercion when do filter routines
by parameter typesSQL:1999 Part 2 Section 10.4public static Iterator<SqlOperator> lookupSubjectRoutines(SqlOperatorTable opTab, RelDataTypeFactory typeFactory, SqlIdentifier funcName, List<RelDataType> argTypes, @Nullable List<String> argNames, SqlSyntax sqlSyntax, SqlKind sqlKind, @Nullable SqlFunctionCategory category, SqlNameMatcher nameMatcher, boolean coerce)
opTab - operator table to searchtypeFactory - Type factoryfuncName - 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-sensitivelycoerce - Whether to allow type coercion when do filter routine
by parameter typesSQL:1999 Part 2 Section 10.4public static boolean matchRoutinesByParameterCount(SqlOperatorTable opTab, SqlIdentifier funcName, 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-sensitivelypublic static SqlNode getSelectListItem(SqlNode query, int i)
ith select-list item of a query.public static String deriveAliasFromOrdinal(int ordinal)
public static boolean isGeneratedAlias(String alias)
alias - not nullpublic static String getOperatorSignature(SqlOperator op, 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 String getAliasedSignature(SqlOperator op, String opName, List<?> typeList)
op - operatoropName - name to use for operatortypeList - list of SqlTypeName or String to use for
operandspublic static CalciteException newContextException(SqlParserPos pos, org.apache.calcite.runtime.Resources.ExInst<?> e, String inputText)
public static CalciteContextException newContextException(SqlParserPos pos, org.apache.calcite.runtime.Resources.ExInst<?> e)
public static CalciteContextException newContextException(int line, int col, int endLine, int endCol, org.apache.calcite.runtime.Resources.ExInst<?> e)
public static boolean isCallTo(SqlNode node, SqlOperator operator)
public static RelDataType createNlsStringType(RelDataTypeFactory typeFactory, NlsString str)
NlsString.
The type inherits the NlsString's Charset and
SqlCollation, if they are set, otherwise it gets the system
defaults.
typeFactory - Type factorystr - Stringpublic static @Nullable String translateCharacterSetName(String name)
name - SQL-level namepublic static Charset getCharset(String charsetName)
Charset based on given SQL-level name.charsetName - Sql charset name, must not be null.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,
Charset charset)
value - nls string in byte arraycharset - charsetRuntimeException - If the given value cannot be represented in the
given charsetpublic static @PolyNull SqlNode stripAs(@PolyNull SqlNode node)
public static SqlNodeList stripListAs(SqlNodeList nodeList)
stripAs(org.apache.calcite.sql.SqlNode)public static com.google.common.collect.ImmutableList<SqlNode> getAncestry(SqlNode root, Predicate<SqlNode> predicate, 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.
public static List<RelHint> getRelHint(HintStrategyTable hintStrategies, @Nullable SqlNodeList sqlHints)
RelHint from sql hints, with a given
inherit path from the root node.
The inherit path would be empty list.
hintStrategies - The hint strategies to validate the sql hintssqlHints - The sql hints nodesRelHint listpublic static RelNode attachRelHint(HintStrategyTable hintStrategies, List<RelHint> hints, Hintable rel)
hints to rel with specified hint strategies.hintStrategies - The strategies to filter the hintshints - The original hints to be attachedrel if there are any hints can be attached given
the hint strategies, or the original node if such hints don't existpublic static SqlNode createCall(SqlOperator op, SqlParserPos pos, List<SqlNode> operands)
Deals with the fact the AND and OR are binary.
Copyright © 2012-2022 Apache Software Foundation. All Rights Reserved.