hhr293 commented on code in PR #58424:
URL: https://github.com/apache/spark/pull/58424#discussion_r3948200792


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/RewriteSelfJoinInequalityToAggregate.scala:
##########
@@ -0,0 +1,746 @@
+/*
+ * 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.execution
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.aggregate._
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.IN_SUBQUERY
+import org.apache.spark.sql.catalyst.util.CharVarcharUtils
+import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, 
LogicalRelation}
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types._
+
+/**
+ * Rewrites a self-join with an inequality into GROUP BY + HAVING 
COUNT(DISTINCT) > 1.
+ *
+ * Targets the two uncorrelated InSubquery shapes exercised by TPC-DS Q95:
+ *
+ *   - Pattern A': the subquery top-level InnerJoin is a direct self-join.
+ *   - Pattern A2: the subquery contains an outer InnerJoin with a self-join 
child; only the
+ *     self-join child is replaced with Aggregate and the outer join is 
preserved.
+ *
+ * Both patterns require an existence-only membership context so row-count 
multiplicity from the
+ * original self-join cross-product does not affect semantics. Correlated 
InSubquery expressions are
+ * intentionally fail-closed because the ExprId remapping performed here does 
not rewrite correlated
+ * predicates.
+ *
+ * This rule runs in `extendedOperatorOptimizationRules`, which is part of the 
operator optimization
+ * batch and therefore executes before `RewritePredicateSubquery` turns the 
predicate subquery into
+ * a semi/anti/existence join. It only observes the uncorrelated `InSubquery` 
shape at that phase,
+ * so there is no separate LeftSemi/LeftAnti ("Pattern A") or `Exists` 
handling.
+ *
+ * Both patterns share:
+ *   - [[buildAggregateHavingDistinctGt1]] to construct `Filter(cnt > 1, 
Aggregate)`
+ *   - [[canonicalizeWrapper]] to rebuild a wrapping Project so every equi-key 
reference points to
+ *     the sjLeft-side attribute, with **fresh exprIds** (Spark's SPARK-21835 
style -- no reuse of
+ *     original exprIds), returning an old->new attribute remap for downstream 
rewrite.
+ *
+ * Controlled by 
`spark.sql.optimizer.rewriteSelfJoinInequalityToAggregate.enabled`
+ * (default false, opt-in).
+ */
+object RewriteSelfJoinInequalityToAggregate extends Rule[LogicalPlan] with 
PredicateHelper {

Review Comment:
   I'll make the lifecycle of this opt-in flag explicit in the PR description. 
My current view is that we should only consider enabling it by default after 
adding an appropriate cost guard and validating it on broader real workloads.
   
   After that, we can continue evolving the implementation along the direction 
@sunchao suggested. COUNT(DISTINCT) computes more information than this 
predicate actually needs, so there may be a simpler and more general 
formulation in the future. 



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