scala> sql("EXPLAIN FORMATTED SELECT * FROM myTable").show
org.apache.spark.sql.catalyst.parser.ParseException:
Operation not allowed: EXPLAIN FORMATTED(line 1, pos 0)
== SQL ==
EXPLAIN FORMATTED SELECT * FROM myTable
^^^
at org.apache.spark.sql.catalyst.parser.ParserUtils$.operationNotAllowed(ParserUtils.scala:39)
at org.apache.spark.sql.execution.SparkSqlAstBuilder$$anonfun$visitExplain$1.apply(SparkSqlParser.scala:275)
at org.apache.spark.sql.execution.SparkSqlAstBuilder$$anonfun$visitExplain$1.apply(SparkSqlParser.scala:273)
at org.apache.spark.sql.catalyst.parser.ParserUtils$.withOrigin(ParserUtils.scala:93)
at org.apache.spark.sql.execution.SparkSqlAstBuilder.visitExplain(SparkSqlParser.scala:273)
at org.apache.spark.sql.execution.SparkSqlAstBuilder.visitExplain(SparkSqlParser.scala:53)
at org.apache.spark.sql.catalyst.parser.SqlBaseParser$ExplainContext.accept(SqlBaseParser.java:480)
at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:42)
at org.apache.spark.sql.catalyst.parser.AstBuilder$$anonfun$visitSingleStatement$1.apply(AstBuilder.scala:66)
at org.apache.spark.sql.catalyst.parser.AstBuilder$$anonfun$visitSingleStatement$1.apply(AstBuilder.scala:66)
at org.apache.spark.sql.catalyst.parser.ParserUtils$.withOrigin(ParserUtils.scala:93)
at org.apache.spark.sql.catalyst.parser.AstBuilder.visitSingleStatement(AstBuilder.scala:65)
at org.apache.spark.sql.catalyst.parser.AbstractSqlParser$$anonfun$parsePlan$1.apply(ParseDriver.scala:62)
at org.apache.spark.sql.catalyst.parser.AbstractSqlParser$$anonfun$parsePlan$1.apply(ParseDriver.scala:61)
at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parse(ParseDriver.scala:90)
at org.apache.spark.sql.execution.SparkSqlParser.parse(SparkSqlParser.scala:46)
at org.apache.spark.sql.catalyst.parser.AbstractSqlParser.parsePlan(ParseDriver.scala:61)
at org.apache.spark.sql.SparkSession.sql(SparkSession.scala:617)
... 48 elided
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)* SORT BY sort+=sortItem (',' sort+=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.
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)