spark3 spark-sql explain 命令的执行过程

1. SparkSQLDriver

对于每个 SQL 语句,除了 CommandFactory 定义的,如 dfs之外,都创建一个 SparkSQLDriver 对象,然后调用 他的 init方法和 run 方法。

scala 复制代码
override def run(command: String): CommandProcessorResponse = {
    try {
      val substitutorCommand = SQLConf.withExistingConf(context.conf) {
        new VariableSubstitution().substitute(command)
      }
      context.sparkContext.setJobDescription(substitutorCommand)
      val execution = context.sessionState.executePlan(context.sql(command).logicalPlan)
      hiveResponse = SQLExecution.withNewExecutionId(execution) {
        hiveResultString(execution.executedPlan)
      }
      tableSchema = getResultSetSchema(execution)
      new CommandProcessorResponse(0)
    } catch {
        // 
    }
  }

最重要的是

scala 复制代码
val execution = context.sessionState.executePlan(context.sql(command).logicalPlan)

首先执行 context.sql(command)

Context.sql 方法如下

scala 复制代码
def sql(sqlText: String): DataFrame = sparkSession.sql(sqlText)

sparkSession.sql

plan 是解析后的 Unsolved Logical plan.

scala 复制代码
 def sql(sqlText: String): DataFrame = withActive {
    val tracker = new QueryPlanningTracker
    val plan = tracker.measurePhase(QueryPlanningTracker.PARSING) {
      sessionState.sqlParser.parsePlan(sqlText)
    }
    Dataset.ofRows(self, plan, tracker)
  }

DataSet.ofRows

ofRows 的 qe.assertAnalyzed() 对 plan 进行解析

scala 复制代码
 def ofRows(sparkSession: SparkSession, logicalPlan: LogicalPlan, tracker: QueryPlanningTracker)
    : DataFrame = sparkSession.withActive {
    val qe = new QueryExecution(sparkSession, logicalPlan, tracker)
    qe.assertAnalyzed()
    new Dataset[Row](qe, RowEncoder(qe.analyzed.schema))
  }

context.sql(command).logicalPlan 是 DataSet 的 logicalPlan,代码如下:

scala 复制代码
  @transient private[sql] val logicalPlan: LogicalPlan = {
    val plan = queryExecution.commandExecuted
    if (sparkSession.sessionState.conf.getConf(SQLConf.FAIL_AMBIGUOUS_SELF_JOIN_ENABLED)) {
      val dsIds = plan.getTagValue(Dataset.DATASET_ID_TAG).getOrElse(new HashSet[Long])
      dsIds.add(id)
      plan.setTagValue(Dataset.DATASET_ID_TAG, dsIds)
    }
    plan
  }

QueryExecution

DataSet 的 logicalPlan 用到了 queryExecution.commandExecuted 字段,该字段是 lazy 的,第1次用到的时候初始化,里面有用到 analyzed,也是 lazy 的。

analyzed 把 Unsolved Execution Plan 转为 Resolved Execution Plan。 commandExecuted 执行eagerlyExecuteCommands(analyzed)。

第一次访问 commandExecuted 生成 CommandResult 对象,以后再访问DataSet 的 logicalPlan,还是返回 CommandResult 对象。

scala 复制代码
lazy val analyzed: LogicalPlan = executePhase(QueryPlanningTracker.ANALYSIS) {
  // We can't clone `logical` here, which will reset the `_analyzed` flag.
  sparkSession.sessionState.analyzer.executeAndCheck(logical, tracker)
}

lazy val commandExecuted: LogicalPlan = mode match {
  case CommandExecutionMode.NON_ROOT => analyzed.mapChildren(eagerlyExecuteCommands)
  case CommandExecutionMode.ALL => eagerlyExecuteCommands(analyzed)
  case CommandExecutionMode.SKIP => analyzed
}

eagerlyExecuteCommands 返回 CommandResult 对象

scala 复制代码
private def eagerlyExecuteCommands(p: LogicalPlan) = p transformDown {
    case c: Command =>
      val qe = sparkSession.sessionState.executePlan(c, CommandExecutionMode.NON_ROOT)
      val result = SQLExecution.withNewExecutionId(qe, Some(commandExecutionName(c))) {
        qe.executedPlan.executeCollect()
      }
      CommandResult(
        qe.analyzed.output,
        qe.commandExecuted,
        qe.executedPlan,
        result)
    case other => other
  }

SparkSQLDriver.run

继续回到主流程。context.sessionState.executePlan 参数是 CommandResult 对象。

scala 复制代码
val execution = context.sessionState.executePlan(context.sql(command).logicalPlan)
hiveResponse = SQLExecution.withNewExecutionId(execution) {
        hiveResultString(execution.executedPlan)

sessionState.executePlan

默认 mode 为 CommandExecutionMode.ALL。plan 是 CommandResult 对象。

scala 复制代码
 def executePlan(
      plan: LogicalPlan,
      mode: CommandExecutionMode.Value = CommandExecutionMode.ALL): QueryExecution =
    createQueryExecution(plan, mode)
}
scala 复制代码
protected def createQueryExecution:
    (LogicalPlan, CommandExecutionMode.Value) => QueryExecution =
      (plan, mode) => new QueryExecution(session, plan, mode = mode)

所以 val execution = context.sessionState.executePlan(context.sql(command).logicalPlan) 执行后,execution 是 QueryExecution 对象。

SparkSQLDriver.run

scala 复制代码
hiveResponse = SQLExecution.withNewExecutionId(execution) {
  hiveResultString(execution.executedPlan)
}
tableSchema = getResultSetSchema(execution)
相关推荐
Lx3526 小时前
覆盖索引:减少回表查询的关键技巧
后端·sql·mysql
珹洺7 小时前
MyBatis实战指南(七)MyBatis缓存机制
java·数据库·sql·安全·缓存·oracle·mybatis
Kookoos10 小时前
ABP vNext + Spark on Hadoop:实时流处理与微服务融合
hadoop·微服务·spark·.net·abp vnext
是梦终空10 小时前
JAVA毕业设计227—基于SpringBoot+hadoop+spark+Vue的大数据房屋维修系统(源代码+数据库)
hadoop·spring boot·spark·vue·毕业设计·源代码·大数据房屋维修系统
无人赴约的cat18 小时前
【20250607接单】Spark + Scala + IntelliJ 项目的开发环境配置从零教学
大数据·spark·scala
在未来等你19 小时前
SQL进阶之旅 Day 23:事务隔离级别与性能优化
sql·mysql·postgresql·高并发·数据一致性·数据库优化·事务隔离
_Chipen21 小时前
6.10 - 常用 SQL 语句以及知识点
数据库·sql·oracle
青春之我_XP21 小时前
【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)
数据库·sql
zh_199951 天前
Spark 之 入门讲解详细版(1)
大数据·spark·mapreduce·数据库架构·etl·涛思数据·odps
qq_463944861 天前
【Spark征服之路-2.5-Spark-Core编程(一)】
大数据·分布式·spark