[ 
https://issues.apache.org/jira/browse/BEAM-8794?focusedWorklogId=347559&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-347559
 ]

ASF GitHub Bot logged work on BEAM-8794:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 21/Nov/19 17:58
            Start Date: 21/Nov/19 17:58
    Worklog Time Spent: 10m 
      Work Description: 11moon11 commented on pull request #10180: [BEAM-8794] 
Conditional aggregate project merge
URL: https://github.com/apache/beam/pull/10180#discussion_r349234940
 
 

 ##########
 File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/impl/rule/BeamAggregateProjectMergeRule.java
 ##########
 @@ -0,0 +1,93 @@
+/*
+ * 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.beam.sdk.extensions.sql.impl.rule;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.apache.beam.sdk.extensions.sql.impl.rel.BeamIOSourceRel;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.RelOptRuleCall;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.plan.volcano.RelSubset;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.RelNode;
+import org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.SingleRel;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Aggregate;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Filter;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.Project;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.core.RelFactories;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.rel.rules.AggregateProjectMergeRule;
+import 
org.apache.beam.vendor.calcite.v1_20_0.org.apache.calcite.tools.RelBuilderFactory;
+
+/**
+ * This rule is essentially a wrapper around Calcite's {@code 
AggregateProjectMergeRule}. In the
+ * case when an underlying IO supports project push-down it is more efficient 
to not merge {@code
+ * Project} with an {@code Aggregate}, leaving it for the {@code 
BeamIOPUshDownRule}.
+ */
+public class BeamAggregateProjectMergeRule extends AggregateProjectMergeRule {
+  public static final AggregateProjectMergeRule INSTANCE =
+      new BeamAggregateProjectMergeRule(
+          Aggregate.class, Project.class, RelFactories.LOGICAL_BUILDER);
+
+  public BeamAggregateProjectMergeRule(
+      Class<? extends Aggregate> aggregateClass,
+      Class<? extends Project> projectClass,
+      RelBuilderFactory relBuilderFactory) {
+    super(aggregateClass, projectClass, relBuilderFactory);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final Project project = call.rel(1);
+    BeamIOSourceRel io = getUnderlyingIO(new HashSet<>(), project);
+
+    // Only perform AggregateProjectMergeRule when IO is not present or 
project push-down is not
+    // supported.
+    if (io == null || !io.getBeamSqlTable().supportsProjects().isSupported()) {
+      super.onMatch(call);
+    }
+  }
+
+  /**
+   * Following scenarios are possible:<br>
+   * 1) Aggregate <- Project <- IO.<br>
+   * 2) Aggregate <- Project <- Chain of Project/Filter <- IO.<br>
+   * 3) Aggregate <- Project <- Something else.<br>
+   * 4) Aggregate <- Project <- Chain of Project/Filter <- Something else.
+   *
+   * @param parent project that matched this rule.
+   * @return {@code BeamIOSourceRel} when it is present or null when some 
other {@code RelNode} is
+   *     present.
+   */
+  private BeamIOSourceRel getUnderlyingIO(Set<RelNode> visitedNodes, SingleRel 
parent) {
+    // No need to look at the same node more than once.
+    if (visitedNodes.contains(parent)) {
+      return null;
+    }
+    visitedNodes.add(parent);
+    List<RelNode> nodes = ((RelSubset) parent.getInput()).getRelList();
+
+    for (RelNode node : nodes) {
+      if (node instanceof Filter || node instanceof Project) {
+        return getUnderlyingIO(visitedNodes, (SingleRel) node);
 
 Review comment:
   Added a check to return from that code path only when an IO is found.
 
----------------------------------------------------------------
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:
[email protected]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 347559)
    Time Spent: 1h 50m  (was: 1h 40m)

> Projects should be handled by an IOPushDownRule before applying 
> AggregateProjectMergeRule
> -----------------------------------------------------------------------------------------
>
>                 Key: BEAM-8794
>                 URL: https://issues.apache.org/jira/browse/BEAM-8794
>             Project: Beam
>          Issue Type: Improvement
>          Components: dsl-sql
>            Reporter: Kirill Kozlov
>            Assignee: Kirill Kozlov
>            Priority: Major
>          Time Spent: 1h 50m
>  Remaining Estimate: 0h
>
> It is more efficient to push-down projected fields at an IO level (vs merging 
> with an Aggregate), when supported.
> When running queries like:
> {code:java}
> select SUM(score) as total_score from <TABLE> group by name{code}
> Projects get merged with an aggregate, as a result Calc (after an 
> IOSourceRel) projects all fields and BeamIOPushDown rule does know what 
> fields can be dropped, thus not dropping any.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to