godfreyhe commented on a change in pull request #10271: [FLINK-14874] 
[table-planner-blink] add local aggregate to solve data skew for ROLLUP/CUBE 
case
URL: https://github.com/apache/flink/pull/10271#discussion_r349980293
 
 

 ##########
 File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/EnforceLocalAggRuleBase.scala
 ##########
 @@ -0,0 +1,197 @@
+/*
+ * 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.flink.table.planner.plan.rules.physical.batch
+
+import org.apache.flink.table.api.TableException
+import org.apache.flink.table.planner.calcite.FlinkTypeFactory
+import org.apache.flink.table.planner.plan.`trait`.FlinkRelDistribution
+import org.apache.flink.table.planner.plan.nodes.FlinkConventions
+import 
org.apache.flink.table.planner.plan.nodes.physical.batch.{BatchExecExchange, 
BatchExecExpand, BatchExecGroupAggregateBase, BatchExecHashAggregate, 
BatchExecLocalHashAggregate, BatchExecLocalSortAggregate, 
BatchExecSortAggregate}
+import org.apache.flink.table.planner.plan.utils.{AggregateUtil, 
FlinkRelOptUtil}
+import 
org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType
+
+import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall, RelOptRuleOperand}
+import org.apache.calcite.rel.RelNode
+import org.apache.calcite.rex.RexUtil
+import org.apache.calcite.tools.RelBuilder
+import org.apache.calcite.util.Util
+
+import scala.collection.JavaConversions._
+
+/**
+  * Planner rule that writes one phase aggregate to two phase aggregate,
+  * when the following conditions are met:
+  * 1. there is no local aggregate,
+  * 2. the aggregate has non-empty grouping and two phase aggregate strategy 
is enabled,
+  * 3. the input is [[BatchExecExpand]] and there is at least one expand row
+  * which the columns for grouping are all constant.
+  */
+abstract class EnforceLocalAggRuleBase(
+    operand: RelOptRuleOperand,
+    description: String)
+  extends RelOptRule(operand, description)
+  with BatchExecAggRuleBase {
+
+  protected def getBatchExecExpand(call: RelOptRuleCall): BatchExecExpand
+
+  override def matches(call: RelOptRuleCall): Boolean = {
+    val agg: BatchExecGroupAggregateBase = call.rel(0)
+    val expand = getBatchExecExpand(call)
+
+    val tableConfig = FlinkRelOptUtil.getTableConfigFromContext(agg)
+    val aggFunctions = agg.getAggCallToAggFunction.map(_._2).toArray
+    val enableTwoPhaseAgg = isTwoPhaseAggWorkable(aggFunctions, tableConfig)
+
+    val grouping = agg.getGrouping
+    // if all group columns in a expand row are constant, this row will be 
shuffled to
+    // a single node. (shuffle keys are grouping)
+    // add local aggregate to greatly reduce the output data
+    val hasConstantRow = expand.projects.exists {
+      project =>
+        val groupingColumns = grouping.map(i => project.get(i))
+        groupingColumns.forall(RexUtil.isConstant)
+    }
+
+    grouping.nonEmpty && enableTwoPhaseAgg && hasConstantRow
+  }
+
+  protected def createLocalAgg(
 
 Review comment:
   actually, the logic of creating `localAgg` can be partially reused, and the 
logic of creating `globalAgg` can hardly be reused (calling the constructor 
directly)

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to