public interface SqlValidator
To create an instance of the default validator implementation, call
SqlValidatorUtil.newValidator(org.apache.calcite.sql.SqlOperatorTable, org.apache.calcite.sql.validate.SqlValidatorCatalogReader, org.apache.calcite.rel.type.RelDataTypeFactory, org.apache.calcite.sql.validate.SqlConformance)
.
The validator interface is an instance of the
visitor pattern
.
Implementations
of the SqlNode.validate(org.apache.calcite.sql.validate.SqlValidator, org.apache.calcite.sql.validate.SqlValidatorScope)
method call the validateXxx
method appropriate to the kind of node:
SqlLiteral.validate(SqlValidator, SqlValidatorScope)
calls
validateLiteral(org.apache.calcite.sql.SqlLiteral)
;
SqlCall.validate(SqlValidator, SqlValidatorScope)
calls
validateCall(SqlCall, SqlValidatorScope)
;
The SqlNode.validateExpr(SqlValidator, SqlValidatorScope)
method
is as SqlNode.validate(SqlValidator, SqlValidatorScope)
but is called
when the node is known to be a scalar expression.
In order to resolve names to objects, the validator builds a map of the
structure of the query. This map consists of two types of objects. A
SqlValidatorScope
describes the tables and columns accessible at a
particular point in the query; and a SqlValidatorNamespace
is a
description of a data source used in a query.
There are different kinds of namespace for different parts of the query.
for example IdentifierNamespace
for table names,
SelectNamespace
for SELECT queries,
SetopNamespace
for UNION, EXCEPT
and INTERSECT. A validator is allowed to wrap namespaces in other objects
which implement SqlValidatorNamespace
, so don't try to cast your
namespace or use instanceof
; use
SqlValidatorNamespace.unwrap(Class)
and
SqlValidatorNamespace.isWrapperFor(Class)
instead.
The validator builds the map by making a quick scan over the query when
the root SqlNode
is first provided. Thereafter, it supplies the
correct scope or namespace object when it calls validation methods.
The methods getSelectScope(org.apache.calcite.sql.SqlSelect)
, getFromScope(org.apache.calcite.sql.SqlSelect)
,
getWhereScope(org.apache.calcite.sql.SqlSelect)
, getGroupScope(org.apache.calcite.sql.SqlSelect)
, getHavingScope(org.apache.calcite.sql.SqlSelect)
,
getOrderScope(org.apache.calcite.sql.SqlSelect)
and getJoinScope(org.apache.calcite.sql.SqlNode)
get the correct scope
to resolve
names in a particular clause of a SQL statement.
Modifier and Type | Method and Description |
---|---|
void |
declareCursor(SqlSelect select,
SqlValidatorScope scope)
Declares a SELECT expression as a cursor.
|
java.lang.String |
deriveAlias(SqlNode node,
int ordinal)
Derives an alias for an expression.
|
RelDataType |
deriveConstructorType(SqlValidatorScope scope,
SqlCall call,
SqlFunction unresolvedConstructor,
SqlFunction resolvedConstructor,
java.util.List<RelDataType> argTypes)
Derives the type of a constructor.
|
RelDataType |
deriveType(SqlValidatorScope scope,
SqlNode operand)
Derives the type of a node in a given scope.
|
SqlNode |
expand(SqlNode expr,
SqlValidatorScope scope)
Expands an expression.
|
SqlNode |
expandOrderExpr(SqlSelect select,
SqlNode orderExpr)
Expands an expression in the ORDER BY clause into an expression with the
same semantics as expressions in the SELECT clause.
|
SqlNodeList |
expandStar(SqlNodeList selectList,
SqlSelect query,
boolean includeSystemVars)
Returns a list of expressions, with every occurrence of "*" or
"TABLE.*" expanded.
|
SqlValidatorCatalogReader |
getCatalogReader()
Returns the catalog reader used by this validator.
|
boolean |
getColumnReferenceExpansion() |
SqlConformance |
getConformance()
Returns the dialect of SQL (SQL:2003, etc.) this validator recognizes.
|
NullCollation |
getDefaultNullCollation()
Returns how NULL values should be collated if an ORDER BY item does not
contain NULLS FIRST or NULLS LAST.
|
java.util.List<java.util.List<java.lang.String>> |
getFieldOrigins(SqlNode sqlQuery)
Returns a description of how each field in the row type maps to a
catalog, schema, table and column in the schema.
|
SqlValidatorScope |
getFromScope(SqlSelect select)
Returns a scope containing the objects visible from the FROM clause of a
query.
|
SqlValidatorScope |
getGroupScope(SqlSelect select)
Returns a scope containing the objects visible from the GROUP BY clause
of a query.
|
SqlValidatorScope |
getHavingScope(SqlSelect select)
Returns a scope containing the objects visible from the HAVING clause of
a query.
|
SqlValidatorScope |
getJoinScope(SqlNode node)
Returns a scope containing the objects visible from the ON and USING
sections of a JOIN clause.
|
SqlValidatorScope |
getMatchRecognizeScope(SqlMatchRecognize node)
Returns a scope match recognize clause.
|
SqlValidatorNamespace |
getNamespace(SqlNode node)
Finds the namespace corresponding to a given node.
|
SqlOperatorTable |
getOperatorTable()
Returns the operator table used by this validator.
|
SqlValidatorScope |
getOrderScope(SqlSelect select)
Returns the scope that expressions in the SELECT and HAVING clause of
this query should use.
|
SqlValidatorScope |
getOverScope(SqlNode node)
Returns the scope of an OVER or VALUES node.
|
RelDataType |
getParameterRowType(SqlNode sqlQuery)
Returns a record type that contains the name and type of each parameter.
|
java.lang.String |
getParentCursor(java.lang.String columnListParamName)
Retrieves the name of the parent cursor referenced by a column list
parameter.
|
SelectScope |
getRawSelectScope(SqlSelect select)
Returns the scope for resolving the SELECT, GROUP BY and HAVING clauses.
|
SqlValidatorScope |
getSelectScope(SqlSelect select)
Returns the appropriate scope for validating a particular clause of a
SELECT statement.
|
RelDataTypeFactory |
getTypeFactory()
Returns the type factory used by this validator.
|
RelDataType |
getUnknownType()
Returns an object representing the "unknown" type.
|
RelDataType |
getValidatedNodeType(SqlNode node)
Returns the type assigned to a node by validation.
|
RelDataType |
getValidatedNodeTypeIfKnown(SqlNode node)
Returns the type assigned to a node by validation, or null if unknown.
|
SqlValidatorScope |
getWhereScope(SqlSelect select)
Returns the scope that expressions in the WHERE and GROUP BY clause of
this query should use.
|
SqlValidatorScope |
getWithScope(SqlNode withItem) |
CalciteException |
handleUnresolvedFunction(SqlCall call,
SqlFunction unresolvedFunction,
java.util.List<RelDataType> argTypes,
java.util.List<java.lang.String> argNames)
Handles a call to a function which cannot be resolved.
|
boolean |
isAggregate(SqlNode selectNode)
Deprecated.
|
boolean |
isAggregate(SqlSelect select)
Returns whether a SELECT statement is an aggregation.
|
boolean |
isSystemField(RelDataTypeField field)
Returns whether a field is a system field.
|
SqlCall |
makeNullaryCall(SqlIdentifier id)
If an identifier is a legitimate call to a function that has no
arguments and requires no parentheses (for example "CURRENT_USER"),
returns a call to that function, otherwise returns null.
|
CalciteContextException |
newValidationError(SqlNode node,
Resources.ExInst<SqlValidatorException> e)
Adds "line x, column y" context to a validator exception.
|
void |
popFunctionCall()
Removes the topmost entry from the function call stack.
|
void |
pushFunctionCall()
Pushes a new instance of a function call on to a function call stack.
|
void |
removeValidatedNodeType(SqlNode node)
Removes a node from the set of validated nodes
|
SqlWindow |
resolveWindow(SqlNode windowOrRef,
SqlValidatorScope scope,
boolean populateBounds)
Converts a window specification or window name into a fully-resolved
window specification.
|
void |
setCallRewrite(boolean rewriteCalls)
Enables or disables rewrite of "macro-like" calls such as COALESCE.
|
void |
setColumnReferenceExpansion(boolean expandColumnReferences)
Enables or disables expansion of column references.
|
void |
setDefaultNullCollation(NullCollation nullCollation)
Sets how NULL values should be collated if an ORDER BY item does not
contain NULLS FIRST or NULLS LAST.
|
void |
setIdentifierExpansion(boolean expandIdentifiers)
Enables or disables expansion of identifiers other than column
references.
|
void |
setValidatedNodeType(SqlNode node,
RelDataType type)
Deprecated.
This method should not be in the
SqlValidator
interface. The validator should drive the type-derivation process, and
store nodes' types when they have been derived. |
boolean |
shouldExpandIdentifiers()
Returns expansion of identifiers.
|
SqlNode |
validate(SqlNode topNode)
Validates an expression tree.
|
void |
validateAggregateParams(SqlCall aggCall,
SqlNode filter,
SqlNodeList orderList,
SqlValidatorScope scope)
Validates parameters for aggregate function.
|
void |
validateCall(SqlCall call,
SqlValidatorScope scope)
Validates a call to an operator.
|
void |
validateColumnListParams(SqlFunction function,
java.util.List<RelDataType> argTypes,
java.util.List<SqlNode> operands)
Validates a COLUMN_LIST parameter
|
void |
validateDataType(SqlDataTypeSpec dataType)
Validates a data type expression.
|
void |
validateDelete(SqlDelete delete)
Validates a DELETE statement.
|
void |
validateDynamicParam(SqlDynamicParam dynamicParam)
Validates a dynamic parameter.
|
void |
validateIdentifier(SqlIdentifier id,
SqlValidatorScope scope)
Resolves an identifier to a fully-qualified name.
|
void |
validateInsert(SqlInsert insert)
Validates an INSERT statement.
|
void |
validateIntervalQualifier(SqlIntervalQualifier qualifier)
Validates a
SqlIntervalQualifier |
void |
validateLiteral(SqlLiteral literal)
Validates a literal.
|
void |
validateMatchRecognize(SqlCall pattern)
Validates a MATCH_RECOGNIZE clause.
|
void |
validateMerge(SqlMerge merge)
Validates a MERGE statement.
|
boolean |
validateModality(SqlSelect select,
SqlModality modality,
boolean fail)
Validates that a query is capable of producing a return of given modality
(relational or streaming).
|
SqlNode |
validateParameterizedExpression(SqlNode topNode,
java.util.Map<java.lang.String,RelDataType> nameToTypeMap)
Validates an expression tree.
|
void |
validateQuery(SqlNode node,
SqlValidatorScope scope,
RelDataType targetRowType)
Checks that a query is valid.
|
void |
validateSequenceValue(SqlValidatorScope scope,
SqlIdentifier id) |
void |
validateUpdate(SqlUpdate update)
Validates an UPDATE statement.
|
void |
validateWindow(SqlNode windowOrId,
SqlValidatorScope scope,
SqlCall call)
Validates the right-hand side of an OVER expression.
|
void |
validateWith(SqlWith with,
SqlValidatorScope scope) |
void |
validateWithItem(SqlWithItem withItem) |
SqlConformance getConformance()
SqlConformanceEnum.DEFAULT
.SqlValidatorCatalogReader getCatalogReader()
SqlOperatorTable getOperatorTable()
SqlNode validate(SqlNode topNode)
topNode
- top of expression tree to be validatedSqlNode validateParameterizedExpression(SqlNode topNode, java.util.Map<java.lang.String,RelDataType> nameToTypeMap)
topNode
- top of expression tree to be validatednameToTypeMap
- map of simple name to RelDataType
; used to
resolve SqlIdentifier
referencesvoid validateQuery(SqlNode node, SqlValidatorScope scope, RelDataType targetRowType)
Valid queries include:
SELECT
statement,
UNION
, INTERSECT
,
EXCEPT
)
AS
operator
node
- Query nodescope
- Scope in which the query occurstargetRowType
- Desired row type, must not be null, may be the data
type 'unknown'.java.lang.RuntimeException
- if the query is not validRelDataType getValidatedNodeType(SqlNode node)
node
- the node of interestRelDataType getValidatedNodeTypeIfKnown(SqlNode node)
getValidatedNodeType(org.apache.calcite.sql.SqlNode)
instead.node
- the node of interestvoid validateIdentifier(SqlIdentifier id, SqlValidatorScope scope)
id
- Identifierscope
- Naming scopevoid validateLiteral(SqlLiteral literal)
literal
- Literalvoid validateIntervalQualifier(SqlIntervalQualifier qualifier)
SqlIntervalQualifier
qualifier
- Interval qualifiervoid validateInsert(SqlInsert insert)
insert
- INSERT statementvoid validateUpdate(SqlUpdate update)
update
- UPDATE statementvoid validateDelete(SqlDelete delete)
delete
- DELETE statementvoid validateMerge(SqlMerge merge)
merge
- MERGE statementvoid validateDataType(SqlDataTypeSpec dataType)
dataType
- Data typevoid validateDynamicParam(SqlDynamicParam dynamicParam)
dynamicParam
- Dynamic parametervoid validateWindow(SqlNode windowOrId, SqlValidatorScope scope, SqlCall call)
identifier
referencing a window, or an
inline window specification
.windowOrId
- SqlNode that can be either SqlWindow with all the
components of a window spec or a SqlIdentifier with the
name of a window spec.scope
- Naming scopecall
- the SqlNode if a function call if the window is attached
to one.void validateMatchRecognize(SqlCall pattern)
pattern
- MATCH_RECOGNIZE clausevoid validateCall(SqlCall call, SqlValidatorScope scope)
call
- Operator callscope
- Naming scopevoid validateAggregateParams(SqlCall aggCall, SqlNode filter, SqlNodeList orderList, SqlValidatorScope scope)
aggCall
- Call to aggregate functionfilter
- Filter (FILTER (WHERE)
clause), or nullorderList
- Ordering specification (WITHING GROUP
clause),
or nullscope
- Syntactic scopevoid validateColumnListParams(SqlFunction function, java.util.List<RelDataType> argTypes, java.util.List<SqlNode> operands)
function
- function containing COLUMN_LIST parameterargTypes
- function argumentsoperands
- operands passed into the function call@Nullable SqlCall makeNullaryCall(SqlIdentifier id)
RelDataType deriveType(SqlValidatorScope scope, SqlNode operand)
scope
- Syntactic scopeoperand
- Parse tree nodeNULL
CalciteContextException newValidationError(SqlNode node, Resources.ExInst<SqlValidatorException> e)
Note that the input exception is checked (it derives from
Exception
) and the output exception is unchecked (it derives from
RuntimeException
). This is intentional -- it should remind code
authors to provide context for their validation errors.
node
- The place where the exception occurred, not nulle
- The validation errorboolean isAggregate(SqlSelect select)
SUM(x) OVER w
, don't count.)select
- SELECT statement@Deprecated boolean isAggregate(SqlNode selectNode)
selectNode
- Expression in SELECT clauseSqlWindow resolveWindow(SqlNode windowOrRef, SqlValidatorScope scope, boolean populateBounds)
SELECT sum(x) OVER (PARTITION
BY x ORDER BY y), sum(y) OVER w1, sum(z) OVER (w ORDER BY y) FROM t
WINDOW w AS (PARTITION BY x)
all aggregations have the same
resolved window specification (PARTITION BY x ORDER BY y)
.windowOrRef
- Either the name of a window (a SqlIdentifier
)
or a window specification (a SqlWindow
).scope
- Scope in which to resolve window namespopulateBounds
- Whether to populate bounds. Doing so may alter the
definition of the window. It is recommended that
populate bounds when translating to physical algebra,
but not when validating.java.lang.RuntimeException
- Validation exception if window does not existSqlValidatorNamespace getNamespace(SqlNode node)
For example, in the query SELECT * FROM (SELECT * FROM t), t1 AS
alias
, the both items in the FROM clause have a corresponding
namespace.
node
- Parse tree nodejava.lang.String deriveAlias(SqlNode node, int ordinal)
ordinal
is less than zero, otherwise generates an
alias EXPR$ordinal
.node
- Expressionordinal
- Ordinal of expressionSqlNodeList expandStar(SqlNodeList selectList, SqlSelect query, boolean includeSystemVars)
selectList
- Select clause to be expandedquery
- QueryincludeSystemVars
- Whether to include system variablesSqlValidatorScope getWhereScope(SqlSelect select)
select
- QueryRelDataTypeFactory getTypeFactory()
void setValidatedNodeType(SqlNode node, RelDataType type)
SqlValidator
interface. The validator should drive the type-derivation process, and
store nodes' types when they have been derived.SqlNode
, now that it has been validated.node
- A SQL parse tree node, never nulltype
- Its type; must not be nullvoid removeValidatedNodeType(SqlNode node)
node
- node to be removedRelDataType getUnknownType()
SqlValidatorScope getSelectScope(SqlSelect select)
Consider
SELECT * FROM foo WHERE EXISTS ( SELECT deptno AS x FROM emp JOIN dept ON emp.deptno = dept.deptno WHERE emp.deptno = 5 GROUP BY deptno ORDER BY x)
What objects can be seen in each part of the sub-query?
getFromScope(org.apache.calcite.sql.SqlSelect)
, you can only see 'foo'.
getWhereScope(org.apache.calcite.sql.SqlSelect)
), GROUP BY (getGroupScope(org.apache.calcite.sql.SqlSelect)
),
SELECT (getSelectScope
), and the ON clause of the JOIN
(getJoinScope(org.apache.calcite.sql.SqlNode)
) you can see 'emp', 'dept', and 'foo'.
getOrderScope(org.apache.calcite.sql.SqlSelect)
), you can see the column alias 'x';
and tables 'emp', 'dept', and 'foo'.
select
- SELECT statementSelectScope getRawSelectScope(SqlSelect select)
SelectScope
; if this is an aggregation query, the
AggregatingScope
is stripped away.select
- SELECT statementSqlValidatorScope getFromScope(SqlSelect select)
select
- SELECT statementSqlValidatorScope getJoinScope(SqlNode node)
node
- The item in the FROM clause which contains the ON or USING
expressiongetFromScope(org.apache.calcite.sql.SqlSelect)
SqlValidatorScope getGroupScope(SqlSelect select)
select
- SELECT statementSqlValidatorScope getHavingScope(SqlSelect select)
select
- SELECT statementSqlValidatorScope getOrderScope(SqlSelect select)
select
- SELECT statementSqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node)
node
- Match recognizevoid declareCursor(SqlSelect select, SqlValidatorScope scope)
select
- select expression associated with the cursorscope
- scope of the parent query associated with the cursorvoid pushFunctionCall()
void popFunctionCall()
java.lang.String getParentCursor(java.lang.String columnListParamName)
columnListParamName
- name of the column list parametervoid setIdentifierExpansion(boolean expandIdentifiers)
expandIdentifiers
- new settingvoid setColumnReferenceExpansion(boolean expandColumnReferences)
expandColumnReferences
- new settingboolean getColumnReferenceExpansion()
void setDefaultNullCollation(NullCollation nullCollation)
NullCollation getDefaultNullCollation()
boolean shouldExpandIdentifiers()
void setCallRewrite(boolean rewriteCalls)
rewriteCalls
- new settingRelDataType deriveConstructorType(SqlValidatorScope scope, SqlCall call, SqlFunction unresolvedConstructor, SqlFunction resolvedConstructor, java.util.List<RelDataType> argTypes)
scope
- Scopecall
- CallunresolvedConstructor
- TODOresolvedConstructor
- TODOargTypes
- Types of argumentsCalciteException handleUnresolvedFunction(SqlCall call, SqlFunction unresolvedFunction, java.util.List<RelDataType> argTypes, java.util.List<java.lang.String> argNames)
call
- CallunresolvedFunction
- Overloaded function which is the target of the
callargTypes
- Types of argumentsargNames
- Names of arguments, or null if call by positionSqlNode expandOrderExpr(SqlSelect select, SqlNode orderExpr)
This is made necessary by a couple of dialect 'features':
select
- Select statement which contains ORDER BYorderExpr
- Expression in the ORDER BY clause.SqlNode expand(SqlNode expr, SqlValidatorScope scope)
expr
- Expressionscope
- Scopeboolean isSystemField(RelDataTypeField field)
In the default implementation, always returns false
.
field
- Fieldjava.util.List<java.util.List<java.lang.String>> getFieldOrigins(SqlNode sqlQuery)
The returned list is never null, and has one element for each field in the row type. Each element is a list of four elements (catalog, schema, table, column), or may be null if the column is an expression.
sqlQuery
- QueryRelDataType getParameterRowType(SqlNode sqlQuery)
sqlQuery
- QuerySqlValidatorScope getOverScope(SqlNode node)
node
- Nodeboolean validateModality(SqlSelect select, SqlModality modality, boolean fail)
select
- Querymodality
- Modality (streaming or relational)fail
- Whether to throw a user error if does not support required
modalityvoid validateWith(SqlWith with, SqlValidatorScope scope)
void validateWithItem(SqlWithItem withItem)
void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id)
SqlValidatorScope getWithScope(SqlNode withItem)
Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.