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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/CombineJoinedAggregates.scala:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.ArrayBuffer
+
+import org.apache.spark.sql.catalyst.expressions.{Alias, And, Attribute, 
AttributeMap, Expression, NamedExpression, Or}
+import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
+import org.apache.spark.sql.catalyst.plans.{Cross, FullOuter, Inner, JoinType, 
LeftOuter, RightOuter}
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Filter, Join, 
LeafNode, LogicalPlan, Project, SerializeFromObject}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{AGGREGATE, JOIN}
+
+/**
+ * This rule eliminates the [[Join]] if all the join side are [[Aggregate]]s 
by combine these
+ * [[Aggregate]]s. This rule also support the nested [[Join]], as long as all 
the join sides for
+ * every [[Join]] are [[Aggregate]]s.
+ *
+ * Note: this rule doesn't following cases:
+ * 1. One of the to be merged two [[Aggregate]]s with child [[Filter]] and the 
other one is not.

Review Comment:
   > Thinking more about it, I think what really matters is the predicate 
selectivity. No filter means an always true predicate.
   > 
   > To be safe, I think we should reuse some code of DPP that determines 
selective predicates, and only merge aggregates if they both have selective 
predicates.
   
   Is that so? I think the performance gain of merging queries comes from the 
fact that we don't need to scan the same data 2 times but we can calculate the 
2 aggregates in 1 run.
   E.g. If we have 2 queries:
   ```
   SELECT sum(a) FROM t WHERE <condition 1>
   ```
   and
   ```
   SELECT sum(b) FROM t WHERE <condition 2>
   ```
   and we want to decide when merging the queries into
   ```
   SELECT
     sum(a) FILTER (WHERE <condition 1>),
     sum(b) FILTER (WHERE <condition 2>)
   FROM t
   WHERE <condition 1> OR <condition 2>
   ```
   make sense then I would say:
   - if the conditions are true literals (or they doesn't exist at all)
   - or the conditions match
   
   then it definitely does make sense to merge them.
   But in the first case the conditions are not selective at all and in the 
second case condition selectivity doesn't matter. (Actually these are the 2 
cases that are already covered by `MergeScalarSubqueries.tryMergePlans()`: 
https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/MergeScalarSubqueries.scala#L275-L286.)
   
   Now, if the conditions differ then it isn't trivial to calculate if scans 
overlap, but I think if both condisions are highly selective then we have less 
chance for scans to overlap, don't we?
   
   In my https://github.com/apache/spark/pull/37630 I used a different 
heuristics to disable merging of aggregates with different filter conditions. 
If the conditions contain any partitioning or bucketing columns then aggregates 
are not merged.



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