Spark 之 ProjectExec,

这篇博客深入探讨了Spark SQL中的`ProjectExec`和`RDDScanExec`操作。通过`Testselect10`测试用例展示了如何执行SQL查询,解释了`ProjectExec`如何创建投影并初始化,以及`RDDScanExec`如何从RDD扫描数据并生成内部行。内容涉及到Spark SQL的执行计划、数据处理及底层代码实现。
摘要由CSDN通过智能技术生成
select 10, case

  test("Test select 10") {
    val df = sql("SELECT  10 ")
    df.explain(false)
    df.show()
    df.explain(false)
  }
ProjectExec

./sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala

case class ProjectExec(projectList: Seq[NamedExpression], child: SparkPlan)

执行函数

protected override def doExecute(): RDD[InternalRow] = {
    child.execute().mapPartitionsWithIndexInternal { (index, iter) =>
      val project = UnsafeProjection.create(projectList, child.output)
      project.initialize(index)
      iter.map(project)
    }
  }
/**
   * Returns an UnsafeProjection for given sequence of Expressions, which will be bound to
   * `inputSchema`.
   */
  def create(exprs: Seq[Expression], inputSchema: Seq[Attribute]): UnsafeProjection = {
    create(bindReferences(exprs, inputSchema))
  }

RDDScanExec 饭后的的inputSchema 是空

child.execute().mapPartitionsWithIndexInternal RDDScanExec , 返回只有一个空 row, rowcount 为1

RDDScanExec

./sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala

/** Physical plan node for scanning data from an RDD of InternalRow. */
case class RDDScanExec(
    output: Seq[Attribute],
    rdd: RDD[InternalRow],
    name: String,
    override val outputPartitioning: Partitioning = UnknownPartitioning(0),
    override val outputOrdering: Seq[SortOrder] = Nil) extends LeafExecNode with InputRDDCodegen {
protected override def doExecute(): RDD[InternalRow] = {
    val numOutputRows = longMetric("numOutputRows")
    rdd.mapPartitionsWithIndexInternal { (index, iter) =>
      val proj = UnsafeProjection.create(schema)
      proj.initialize(index)
      iter.map { r =>
        numOutputRows += 1
        proj(r)
      }
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值