peter-toth commented on code in PR #57576:
URL: https://github.com/apache/spark/pull/57576#discussion_r3690103536


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CombineApproximatePercentiles.scala:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.catalyst.optimizer
+
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, 
AttributeReference, CreateArray, Expression, ExprId, GetArrayItem, 
LeafExpression, Literal, NamedExpression}
+import 
org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, 
AggregateMode, ApproximatePercentile}
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.AGGREGATE
+import org.apache.spark.sql.catalyst.util.GenericArrayData
+import org.apache.spark.sql.types.{ArrayType, DoubleType}
+
+private[optimizer] case class PercentileFusionIdentity(
+    aggregateFunctions: Seq[Expression],
+    mode: AggregateMode,
+    isDistinct: Boolean,
+    filter: Option[Expression],
+    percentageBits: Seq[Long])
+
+/**
+ * Foldable percentage array that retains the original scalar aggregate 
structures in equality.
+ *
+ * Fusion removes those structures from the physical aggregate. Keeping them 
here prevents
+ * subquery or exchange reuse from equating plans that were distinct before 
fusion.
+ */
+private[optimizer] case class PercentileFusionArray(identity: 
PercentileFusionIdentity)

Review Comment:
   You're right, and thanks for the concrete counterexample — `100` and `100L` 
are both `Literal`s with the same evaluated accuracy, so the gate as I wrote it 
doesn't close the hole. Fusion keeping only `first`'s accuracy is what drops 
the distinguishing structure, and literal-ness alone says nothing about that.
   
   For the record, since it's a two-line delta rather than a different idea: 
requiring the group's `accuracyExpression`s to be *structurally identical* on 
top of literal percentages does close it — your branch A then simply doesn't 
fuse, and pairs 1 and 2 of `preserve pre-fusion identity across exchange reuse` 
are already excluded by the literal check. But the cost I flagged stands 
(fusion stops firing when `ConstantFolding` is excluded, so the suite's 
baseline methodology would have to exclude only 
`CombineApproximatePercentiles`), and it's a scope call on a PR that already 
has three approvals. Not pressing it — the identity as written is correct, and 
I'm not re-listing this.
   
   One correction to what I wrote last round: I claimed the `ConstantFolding` 
skip was only exercised by the unit test. That's wrong — `SparkOptimizer` runs 
`ConstantFolding` again in `Batch("Infer window group limit")` 
(`SparkOptimizer.scala:100-105`), which is after this batch, so the skip is 
load-bearing in production.
   



-- 
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