parthchandra commented on code in PR #838: URL: https://github.com/apache/datafusion-comet/pull/838#discussion_r1720376085
########## spark/src/main/scala/org/apache/spark/sql/comet/CometColumnarToRowExec.scala: ########## @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.spark.sql.comet + +import org.apache.comet.vector.CometVector + +import scala.collection.JavaConverters._ +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, SortOrder, UnsafeProjection} +import org.apache.spark.sql.catalyst.plans.physical.Partitioning +import org.apache.spark.sql.execution.{ColumnarToRowTransition, SparkPlan} +import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics} +import org.apache.spark.util.Utils + +/** + * This is currently an identical copy of Spark's ColumnarToRowExec except for removing the + * code-gen features. + * + * This is moved into the Comet repo as the first step towards refactoring this to make the + * interactions with CometVector more efficient to avoid some JNI overhead. + */ +case class CometColumnarToRowExec(child: SparkPlan) extends ColumnarToRowTransition { + // supportsColumnar requires to be only called on driver side, see also SPARK-37779. + assert(Utils.isInRunningSparkTask || child.supportsColumnar) + + override def output: Seq[Attribute] = child.output + + override def outputPartitioning: Partitioning = child.outputPartitioning + + override def outputOrdering: Seq[SortOrder] = child.outputOrdering + + override lazy val metrics: Map[String, SQLMetric] = Map( + "numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows"), + "numInputBatches" -> SQLMetrics.createMetric(sparkContext, "number of input batches")) + + override def doExecute(): RDD[InternalRow] = { + val numOutputRows = longMetric("numOutputRows") + val numInputBatches = longMetric("numInputBatches") + // This avoids calling `output` in the RDD closure, so that we don't need to include the entire + // plan (this) in the closure. + val localOutput = this.output + child.executeColumnar().mapPartitionsInternal { batches => + val toUnsafe = UnsafeProjection.create(localOutput, localOutput) + batches.flatMap { batch => + numInputBatches += 1 + numOutputRows += batch.numRows() + + // TODO add code that is optimized for dealing with CometVector + // but we can do something like ... + for (i <- 0 until batch.numCols()) { + val cv = batch.column(i).asInstanceOf[CometVector] + // cv.importVector Review Comment: The current implementation accesses each value from the underlying buffer via JNI. This is reasonably quick for integers (and floating point too perhaps, though I have not verified that). But for byte buffers this is a performance killer. Based on experiments with decimals in CometVector, we have seen that copying the entire buffer in one call and avoiding the JNI calls makes things much faster at the cost of increased memory usage. The increase memory usage is something to keep in mind. We might have to experiment with varying the default batch size to get the right mix. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
