sunchao commented on code in PR #6883:
URL: https://github.com/apache/datafusion-comet/pull/6883#discussion_r4316030934


##########
spark-local/src/main/scala/org/apache/comet/local/CometLocalResultExec.scala:
##########
@@ -0,0 +1,134 @@
+/*
+ * 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)
+    else super.executeTake(n)
+
+  /**
+   * Runs the query as ordinary Comet execution instead. An iterator fetches 
one result partition
+   * at a time to bound what it holds, and a local query has a single 
partition holding the whole
+   * result, so the native block runs as Spark tasks, one per partition, as 
without local
+   * execution.
+   */
+  override def executeToIterator(): Iterator[InternalRow] =

Review Comment:
   [P2] Preserve reverse-partition execution for PySpark `tail`. With ANSI 
mode, `local[2]`, and Comet range/local execution enabled, `spark.range(0, 20, 
1, 2).selectExpr("100L DIV id AS v").tail(1)` should return `Row(v=5)`. PySpark 
calls `tailToPython`, which invokes the inherited `executeTail`. That method 
now reads the single concatenated partition and encounters division by zero at 
`id=0`, whereas Spark and ordinary Comet read only the final source partition. 
Could `executeTail` delegate to the original block, like `executeToIterator`, 
or otherwise retain reverse-partition consumption?
   
   Evidence: Reproduced on Spark 4.1.3/JDK 21 using current-head JVM classes 
and the checksum-verified, source-identical native CI artifact. The probe calls 
`df.queryExecution.executedPlan.executeTail(1)`, exactly the execution method 
used by `Dataset.tailToPython`. Spark and ordinary Comet returned 
`ArraySeq(5)`. Local execution threw `SparkArithmeticException 
[DIVIDE_BY_ZERO]`. Its plan was `CometLocalResult -> ColumnarToRow -> 
CometLocal(partitions=2) -> CometProject -> CometRange`. Spark sources from 
3.4.3 through 4.2.0 confirm the same Python route and reverse partition 
selection. Reproduction and output: 
`/tmp/comet6883-oct19-review-_1702lxj/ReviewTailProbe.scala` and 
`/tmp/comet6883-oct19-review-_1702lxj/probe.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]

Reply via email to