关注 spark技术分享,
撸spark源码 玩spark最佳实践

AbstractSqlParser — Base SQL Parsing Infrastructure

AbstractSqlParser — Base SQL Parsing Infrastructure

AbstractSqlParser is the base of ParserInterfaces that use an AstBuilder to parse SQL statements and convert them to Spark SQL entities, i.e. DataType, StructType, Expression, LogicalPlan and TableIdentifier.

AbstractSqlParser is the foundation of the SQL parsing infrastructure.

Table 1. AbstractSqlParser Contract
Method Description

astBuilder

AstBuilder for parsing SQL statements.

Used in all the parse methods, i.e. parseDataType, parseExpression, parsePlan, parseTableIdentifier, and parseTableSchema.

Table 2. AbstractSqlParser’s Implementations
Name Description

SparkSqlParser

The default SQL parser in SessionState available as sqlParser property.

CatalystSqlParser

Creates a DataType or a StructType (schema) from their canonical string representation.

Setting Up SqlBaseLexer and SqlBaseParser for Parsing — parse Method

parse sets up a proper ANTLR parsing infrastructure with SqlBaseLexer and SqlBaseParser (which are the ANTLR-specific classes of Spark SQL that are auto-generated at build time from the SqlBase.g4 grammar).

Tip
Review the definition of ANTLR grammar for Spark SQL in sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4.

Internally, parse first prints out the following INFO message to the logs:

Tip
Enable INFO logging level for the custom AbstractSqlParser, i.e. SparkSqlParser or CatalystSqlParser, to see the above INFO message.

parse then creates and sets up a SqlBaseLexer and SqlBaseParser that in turn passes the latter on to the input toResult function where the parsing finally happens.

Note
parse uses SLL prediction mode for parsing first before falling back to LL mode.

In case of parsing errors, parse reports a ParseException.

Note
parse is used in all the parse methods, i.e. parseDataType, parseExpression, parsePlan, parseTableIdentifier, and parseTableSchema.

parseDataType Method

Note
parseDataType is part of ParserInterface Contract to…​FIXME.

parseDataType…​FIXME

parseExpression Method

Note
parseExpression is part of ParserInterface Contract to…​FIXME.

parseExpression…​FIXME

parseFunctionIdentifier Method

Note
parseFunctionIdentifier is part of ParserInterface Contract to…​FIXME.

parseFunctionIdentifier…​FIXME

parseTableIdentifier Method

Note
parseTableIdentifier is part of ParserInterface Contract to…​FIXME.

parseTableIdentifier…​FIXME

parseTableSchema Method

Note
parseTableSchema is part of ParserInterface Contract to…​FIXME.

parseTableSchema…​FIXME

parsePlan Method

Note
parsePlan is part of ParserInterface Contract to…​FIXME.

parsePlan creates a LogicalPlan for a given SQL textual statement.

Internally, parsePlan builds a SqlBaseParser and requests AstBuilder to parse a single SQL statement.

If a SQL statement could not be parsed, parsePlan throws a ParseException:

赞(0) 打赏
未经允许不得转载:spark技术分享 » AbstractSqlParser — Base SQL Parsing Infrastructure
分享到: 更多 (0)

关注公众号:spark技术分享

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏