sunchao commented on code in PR #6883: URL: https://github.com/apache/datafusion-comet/pull/6883#discussion_r4321740889
########## spark/src/main/scala/org/apache/comet/local/CometLocalResultExec.scala: ########## @@ -0,0 +1,135 @@ +/* + * 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.comet.local + +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicLong + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.SparkException +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.{Attribute, SortOrder, UnsafeRow} +import org.apache.spark.sql.catalyst.plans.physical.Partitioning +import org.apache.spark.sql.execution.{ColumnarToRowExec, SparkPlan, UnaryExecNode} + +/** + * Row result boundary of a local query. Spark's collect encodes and compresses every row of a + * result partition in its task, then decodes them on the driver. A local query has one result + * partition, so that work would run on a single thread. Admission requires an in-process local + * master, so collect and take still run one Spark task (keeping cancellation, job groups and SQL + * metrics) but hand copied rows to the driver in this JVM. + */ +case class CometLocalResultExec(child: SparkPlan) extends UnaryExecNode { + override def output: Seq[Attribute] = child.output + override def outputPartitioning: Partitioning = child.outputPartitioning + override def outputOrdering: Seq[SortOrder] = child.outputOrdering + + override protected def doExecute(): RDD[InternalRow] = child.execute() + + override def executeCollect(): Array[InternalRow] = + if (sparkContext.isLocal) LocalResultHandoff.collect(execute(), -1, maxResultSize) + else super.executeCollect() + + override def executeTake(n: Int): Array[InternalRow] = + if (n <= 0) Array.empty + else if (sparkContext.isLocal) LocalResultHandoff.collect(execute(), n, maxResultSize) Review Comment: [P2] Preserve failures from the partitions Spark requests together in `executeTake`. With `local[2]`, ANSI mode and Comet range/local execution enabled, `spark.range(0, 6, 1, 3).filter("id % 2 = 0").selectExpr("100L DIV (4L - id) AS v").limit(2).collect()` throws `DIVIDE_BY_ZERO` in Spark and ordinary Comet, but returns `[25, 50]` locally. After partition 0 supplies one row, Spark requests partitions 1 and 2 together and fails on partition 2. The local handoff stops after partition 1 supplies the second row, suppressing that failure and turning a failed ANSI query into success. Please preserve Spark's partition-request/error behavior, or delegate `executeTake` to ordinary execution until that behavior is supported. Evidence: Reproduced on Spark 4.1.3/JDK 21 using the exact-head native build and freshly compiled current local JVM sources. `actions.log` records `DIVIDE_BY_ZERO` for Spark and ordinary Comet and `ArraySeq([25], [50])` for local execution. The local plan is `CollectLimit 2 -> CometLocalResult -> ColumnarToRow -> CometLocal(partitions=3)`. SparkPlan.scala in Spark 3.4.3, 3.5.9, 4.0.4, 4.1.3 and 4.2.0 computes the next request with `ceil(1.5 * left * partsScanned / buf.length)` and completes `runJob` for all requested partitions before trimming rows. Probe and output: `/tmp/comet6883-oct20-review-_l0rntl0/ReviewActionProbe.scala` and `/tmp/comet6883-oct20-review-_l0rntl0/actions.log`. -- 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]
