改写SparkSql执行计划之sparksql开发新语法

改写SparkSql执行计划

SparkSql开发新的语法支持:

首先需要改动ANTLR4文件(在SqlBase.g4中添加文法),重新生成词法分析器(SqlBaseLexer)、语法分析器(SqlBaseParser)和访问者类(SqlBaseVisitor接口与SqlBaseBaseVisitor类),然后在AstBuilder一系列类中添加访问逻辑,最后添加执行逻辑。

修改ANTLR4文件

添加访问逻辑

扩展包dsql下:

DSQLAstBuilder继承了ANTLR4生成的默认SqlBaseBaseVisitor,用于生成SQL对应的抽象语法树AST(Unresolved LogicalPlan)

DSQLsqlAstBuilder继承了DSQLAstBuilder,并在其基础上添加了DDL的访问操作,主要在DSQLsqlParser中调用。

如:

DSQLSqlParser#visitCreateTableHeader中,调用了DSQLAstBuilder#visitTableIdentifier。

本篇以addDataSource为例:

class DSQLSqlParser(conf: SQLConf) extends AbstractDSQLParser {
  /** Get the builder (visitor) which converts a ParseTree into an AST. */  
  override val astBuilder = new DSQLSqlAstBuilder(conf)

  private val substitutor = new VariableSubstitution(conf)

  protected override def parse[T](command: String)(toResult: SqlBaseParser => T): T = {
    super.parse(substitutor.substitute(command))(toResult)
  }
}

DSQLSqlAstBuilder#visitAddDatasource:得到Unresolved LogicalPlan

  /**
   * Create a [[DSQLAddDatasourceCommand]] logical plan
   */
  override def visitAddDatasource(ctx: AddDatasourceContext): LogicalPlan = withOrigin(ctx) {
    val properties = visitPropertyKeyValues(ctx.tablePropertyList)
    DSQLAddDatasourceCommand(ctx.identifier().getText, properties.updated(TEMP_FLAG, "true"))
  }

DSQLSqlAstBuilder#visitPropertyKeyValues;DSQLSqlAstBuilder#visitTablePropertyList等访问方法:

  /**
   * Parse a key-value map from a [[TablePropertyListContext]], assuming all values are specified.
   */
  private def visitPropertyKeyValues(ctx: TablePropertyListContext): Map[String, String] = {
    val props = visitTablePropertyList(ctx)
    val badKeys = props.collect { case (key, null) => key }
    if (badKeys.nonEmpty) {
      operationNotAllowed(
        s"Values must be specified for key(s): ${badKeys.mkString("[", ",", "]")}",
        ctx)
    }
    props
  }

  /**
   * Convert a table property list into a key-value map.
   * This should be called through [[visitPropertyKeyValues]] or [[visitPropertyKeys]].
   */
  override def visitTableProper
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值