andygrove commented on code in PR #4207: URL: https://github.com/apache/datafusion-comet/pull/4207#discussion_r3190910298
########## spark/src/test/scala/org/apache/comet/CometSetOpWithGroupBySuite.scala: ########## @@ -0,0 +1,64 @@ +/* + * 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 + +import org.apache.spark.sql.{CometTestBase, Row} + +/** + * Regression test for issue #4122: on Spark 4.1 (SPARK-52921), EXCEPT ALL / INTERSECT ALL whose + * sides are themselves GROUP BY aggregates are lowered to a plan where the union inherits a hash + * partitioning from its shuffled children, so the downstream final aggregate skips its shuffle. + * If Comet's columnar Union concatenates partitions it breaks that partitioning invariant and the + * resulting sums/counts collapse two sides into the wrong partitions. + */ +class CometSetOpWithGroupBySuite extends CometTestBase { + + test("issue #4122: EXCEPT ALL with GROUP BY under both sides") { + withTempView("tab3", "tab4") { + sql("""CREATE TEMPORARY VIEW tab3 AS SELECT * FROM VALUES + | (1, 2), (1, 2), (1, 3), (2, 3), (2, 2) AS tab3(k, v)""".stripMargin) + sql("""CREATE TEMPORARY VIEW tab4 AS SELECT * FROM VALUES + | (1, 2), (2, 3), (2, 2), (2, 2), (2, 20) AS tab4(k, v)""".stripMargin) + + val df = sql("""SELECT v FROM tab3 GROUP BY v + |EXCEPT ALL + |SELECT k FROM tab4 GROUP BY k""".stripMargin) + checkAnswer(df, Seq(Row(3))) Review Comment: The query does have some operators that cannot be converted. I updated these tests to check for `CometUnionExec` though ########## spark/src/main/spark-4.1/org/apache/spark/sql/comet/shims/ShimCometUnionExec.scala: ########## @@ -0,0 +1,60 @@ +/* + * 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.shims + +import scala.reflect.ClassTag + +import org.apache.spark.SparkContext +import org.apache.spark.rdd.{RDD, SQLPartitioningAwareUnionRDD} +import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, UnknownPartitioning} + +object ShimCometUnionExec { + + /** + * Unions a sequence of RDDs while preserving the declared output partitioning. Spark 4.1 + * introduced [[org.apache.spark.sql.internal.SQLConf.UNION_OUTPUT_PARTITIONING]] (SPARK-52921), + * which lets [[org.apache.spark.sql.execution.UnionExec]] report a non-trivial output + * partitioning when all children share the same partitioning. Downstream operators may then + * skip an otherwise-required shuffle, so the columnar Union path must honor that contract by + * routing through [[SQLPartitioningAwareUnionRDD]] rather than plain `SparkContext.union`, + * which concatenates partitions and breaks the partitioning invariant. + */ + def unionRDDs[T: ClassTag]( + sc: SparkContext, + rdds: Seq[RDD[T]], + outputPartitioning: Partitioning): RDD[T] = { + outputPartitioning match { + case _: UnknownPartitioning => sc.union(rdds) + case _ => + val numPartitions = outputPartitioning.numPartitions + val nonEmpty = rdds.filter(_.partitions.nonEmpty) + // SQLPartitioningAwareUnionRDD indexes every child at every output partition, so any + // child whose partition count diverges from the declared numPartitions would raise + // ArrayIndexOutOfBoundsException. That would only happen if the declared partitioning + // is stale relative to the RDDs (e.g. children were coalesced by AQE but the reported + // partitioning was not). Fall back to plain concat in that case. + if (nonEmpty.isEmpty || nonEmpty.exists(_.partitions.length != numPartitions)) { + sc.union(rdds) Review Comment: Added -- 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]
