AstBuilder — ANTLR-based SQL Parser
AstBuilder
converts SQL statements into Spark SQL’s relational entities (i.e. data types, Catalyst expressions, logical plans or TableIdentifiers
) using visit callback methods.
AstBuilder
is the AST builder of AbstractSqlParser
(i.e. the base SQL parsing infrastructure in Spark SQL).
Tip
|
Spark SQL supports SQL statements as described in SqlBase.g4. Using the file can tell you (almost) exactly what Spark SQL supports at any given time. “Almost” being that although the grammar accepts a SQL statement it can be reported as not allowed by
|
AstBuilder
is a ANTLR AbstractParseTreeVisitor
(as SqlBaseBaseVisitor
) that is generated from SqlBase.g4 ANTLR grammar for Spark SQL.
Note
|
|
Callback Method | ANTLR rule / labeled alternative | Spark SQL Entity | ||||
---|---|---|---|---|---|---|
|
||||||
|
||||||
|
||||||
|
|
Exists expression |
||||
|
|
|
||||
|
|
First aggregate function expression
|
||||
|
|
Supports multiple comma-separated relations (that all together build a condition-less INNER JOIN) with optional LATERAL VIEW. A relation can be one of the following or a combination thereof:
|
||||
|
|
|
||||
|
|
UnresolvedInlineTable unary logical operator (as the child of SubqueryAlias for
|
||||
|
|
InsertIntoTable (indirectly) A 3-element tuple with a
|
||||
|
|
InsertIntoTable (indirectly) A 3-element tuple with a
In a way,
|
||||
|
A logical operator with a InsertIntoTable (and UnresolvedRelation leaf operator)
|
|||||
|
|
|
||||
|
||||||
|
|
|
||||
|
|
|||||
|
|
LogicalPlan for a |
||||
|
||||||
|
|
|||||
|
|
Takes the named expression and relays to visitNamedExpression |
||||
|
A logical operator with a InsertIntoTable
|
|||||
|
|
SortOrder unevaluable unary expression
|
||||
|
FIRST))? ORDER BY order+=sortItem (‘,’ order+=sortItem)* (ORDER |
SORT) BY sortItem (‘,’ sortItem)*)? |
||||
|
|
LogicalPlan from a single statement
|
||||
|
|
|||||
|
|
|||||
|
||||||
|
|
|||||
|
|
|
Parsing Handler | LogicalPlan Added | ||||
---|---|---|---|---|---|
|
|
||||
|
Generate with a UnresolvedGenerator and join flag turned on for |
||||
|
Hint for
|
||||
|
|
||||
|
Join for a FROM clause and relation alone. The following join types are supported:
The following join criteria are supported:
Joins can be |
||||
|
|||||
|
For transform For regular
|
||||
|
|||||
|
WithWindowDefinition for window aggregates (given Used for withQueryResultClauses and withQuerySpecification with
|
Note
|
AstBuilder belongs to org.apache.spark.sql.catalyst.parser package.
|
Function Examples
The examples are handled by visitFunctionCall.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import spark.sessionState.sqlParser scala> sqlParser.parseExpression("foo()") res0: org.apache.spark.sql.catalyst.expressions.Expression = 'foo() scala> sqlParser.parseExpression("foo() OVER windowSpecRef") res1: org.apache.spark.sql.catalyst.expressions.Expression = unresolvedwindowexpression('foo(), WindowSpecReference(windowSpecRef)) scala> sqlParser.parseExpression("foo() OVER (CLUSTER BY field)") res2: org.apache.spark.sql.catalyst.expressions.Expression = 'foo() windowspecdefinition('field, UnspecifiedFrame) |