You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "huaxingao (via GitHub)" <gi...@apache.org> on 2023/03/02 04:30:18 UTC

[GitHub] [iceberg] huaxingao opened a new pull request, #6981: Push down group by for partition columns

huaxingao opened a new pull request, #6981:
URL: https://github.com/apache/iceberg/pull/6981

   Push down min/max/count with group by if group by is on partition columns


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155365348


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/source/SparkScanBuilder.java:
##########
@@ -314,6 +306,66 @@ private boolean metricsModeSupportsAggregatePushDown(List<BoundAggregate<?, ?>>
     return true;
   }
 
+  private boolean pushAggregates(List<BoundAggregate<?, ?>> expressions, Aggregation aggregation) {
+    for (AggregateFunc aggregateFunc : aggregation.aggregateExpressions()) {
+      try {
+        Expression expr = SparkAggregates.convert(aggregateFunc);
+        if (expr != null) {
+          Expression bound = Binder.bind(schema.asStruct(), expr, caseSensitive);
+          expressions.add((BoundAggregate<?, ?>) bound);
+        } else {
+          LOG.info(
+              "Skipping aggregate pushdown: AggregateFunc {} can't be converted to iceberg expression",
+              aggregateFunc);
+          return false;
+        }
+      } catch (Exception e) {
+        LOG.info("Skipping aggregate pushdown: Bind failed for AggregateFunc {}", aggregateFunc, e);
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  private boolean pushGroupBy(
+      List<BoundGroupBy<?, ?>> groupByExpressions, Aggregation aggregation) {
+    for (org.apache.spark.sql.connector.expressions.Expression groupby :
+        aggregation.groupByExpressions()) {
+      try {
+        if (groupby instanceof NamedReference) {
+          Expression groupByExpr =
+              Expressions.groupBy(SparkUtil.toColumnName((NamedReference) groupby));
+
+          if (groupByExpr != null) {
+            Expression bound = Binder.bind(schema.asStruct(), groupByExpr, caseSensitive);
+            if (!ExpressionUtil.isPartitionCol((BoundGroupBy) bound, table)) {
+              return false;
+            }
+
+            groupByExpressions.add((BoundGroupBy) bound);
+          } else {
+            LOG.info(
+                "Skipping aggregate pushdown: groupby {} can't be converted to iceberg expression",
+                groupby);
+            return false;
+          }
+        } else {
+          LOG.info(
+              "Skipping aggregate pushdown: groupby {} can't be converted to iceberg expression",
+              groupby);
+          return false;
+        }
+
+      } catch (Exception e) {

Review Comment:
   This is too broad. Can you make it more specific? We generally don't like to catch `Exception`.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#issuecomment-1924191483

   @amogh-jahagirdar, can you 


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #6981: Push down group by for partition columns
URL: https://github.com/apache/iceberg/pull/6981


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155365155


##########
api/src/main/java/org/apache/iceberg/expressions/CountAggregate.java:
##########
@@ -47,25 +50,40 @@ protected Long countFor(DataFile file) {
   }
 
   @Override
-  public Aggregator<Long> newAggregator() {
-    return new CountAggregator<>(this);
+  public Aggregator<Long> newAggregator(List<BoundGroupBy<?, ?>> groupBys) {
+    return new CountAggregator<>(this, groupBys);
   }
 
   private static class CountAggregator<T> extends NullSafeAggregator<T, Long> {
     private Long count = 0L;
+    private Map<StructLike, Long> countP = Maps.newHashMap();
 
-    CountAggregator(BoundAggregate<T, Long> aggregate) {
-      super(aggregate);
+    CountAggregator(BoundAggregate<T, Long> aggregate, List<BoundGroupBy<?, ?>> groupBys) {
+      super(aggregate, groupBys);
     }
 
     @Override
     protected void update(Long value) {
       count += value;
     }
 
+    @Override
+    protected void update(Long value, StructLike partitionKey) {
+      if (countP.get(partitionKey) == null) {
+        countP.put(partitionKey, value);
+      } else {
+        countP.put(partitionKey, countP.get(partitionKey) + value);
+      }
+    }
+
     @Override
     protected Long current() {
       return count;
     }
+
+    @Override
+    protected Map<StructLike, Long> currentPartition() {

Review Comment:
   Why change the aggregator to be partition aware? It seems like it would be cleaner to leave this class as it was and wrap it. Maybe introduce a `GroupingAggregator` that has a map of aggregators by partition key. That way the aggregators stay roughly the same and the grouping functionality is layered on top.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155365664


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/source/SparkScanBuilder.java:
##########
@@ -314,6 +306,66 @@ private boolean metricsModeSupportsAggregatePushDown(List<BoundAggregate<?, ?>>
     return true;
   }
 
+  private boolean pushAggregates(List<BoundAggregate<?, ?>> expressions, Aggregation aggregation) {
+    for (AggregateFunc aggregateFunc : aggregation.aggregateExpressions()) {
+      try {
+        Expression expr = SparkAggregates.convert(aggregateFunc);
+        if (expr != null) {
+          Expression bound = Binder.bind(schema.asStruct(), expr, caseSensitive);
+          expressions.add((BoundAggregate<?, ?>) bound);
+        } else {
+          LOG.info(
+              "Skipping aggregate pushdown: AggregateFunc {} can't be converted to iceberg expression",
+              aggregateFunc);
+          return false;
+        }
+      } catch (Exception e) {
+        LOG.info("Skipping aggregate pushdown: Bind failed for AggregateFunc {}", aggregateFunc, e);
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  private boolean pushGroupBy(

Review Comment:
   I think this should be `canPushGroupBy` rather than `push` since it returns a boolean.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#issuecomment-1924192337

   Oops, I didn't mean to close this! I want to work on getting it in next


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue closed pull request #6981: Push down group by for partition columns
URL: https://github.com/apache/iceberg/pull/6981


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155366799


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionUtil.java:
##########
@@ -156,6 +158,19 @@ public static boolean selectsPartitions(
         caseSensitive);
   }
 
+  public static boolean isPartitionCol(BoundGroupBy expr, Table table) {
+    return table.specs().values().stream().allMatch(spec -> isPartitionCol(expr, spec));

Review Comment:
   This check seems overly broad. The aggregate can be pushed down if the partition column exists in all specs that have matching data files. The table may start out unpartitioned and have partitions added later, and then have the initial data rewritten. We want to test whether the specs that are actually used meet the requirement and stop aggregation if there's a problem.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155364986


##########
api/src/main/java/org/apache/iceberg/expressions/BoundGroupBy.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * 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.iceberg.expressions;
+
+import org.apache.iceberg.StructLike;
+
+public class BoundGroupBy<T, C> extends GroupBy<BoundTerm<T>> implements Bound<C> {

Review Comment:
   I don't think I quite understand the purpose of `GroupBy` expression classes. Looks like they are used to wrap terms, but why not just use the term directly? Terms can be used to extract values from a record, which is really all that we need.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a diff in pull request #6981: Push down group by for partition columns

Posted by "rdblue (via GitHub)" <gi...@apache.org>.
rdblue commented on code in PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#discussion_r1155366539


##########
api/src/main/java/org/apache/iceberg/expressions/ExpressionUtil.java:
##########
@@ -156,6 +158,19 @@ public static boolean selectsPartitions(
         caseSensitive);
   }
 
+  public static boolean isPartitionCol(BoundGroupBy expr, Table table) {
+    return table.specs().values().stream().allMatch(spec -> isPartitionCol(expr, spec));
+  }
+
+  public static boolean isPartitionCol(BoundGroupBy expr, PartitionSpec spec) {

Review Comment:
   This isn't correct. This will return all partition fields for a given ID, but those fields may include a transform. For example, a field could represent `hours(ts)` where `ts` has id 3. In order to check whether this is a partition column you can group by directly, you'd need to check that the transform is the identity transform.



-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#issuecomment-2313785805

   This pull request has been marked as stale due to 30 days of inactivity. It will be closed in 1 week if no further activity occurs. If you think that’s incorrect or this pull request requires a review, please simply write any comment. If closed, you can revive the PR at any time and @mention a reviewer or discuss it on the dev@iceberg.apache.org list. Thank you for your contributions.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Push down group by for partition columns [iceberg]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#issuecomment-2327661225

   This pull request has been closed due to lack of activity. This is not a judgement on the merit of the PR in any way. It is just a way of keeping the PR queue manageable. If you think that is incorrect, or the pull request requires review, you can revive the PR at any time.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] huaxingao commented on pull request #6981: Push down group by for partition columns

Posted by "huaxingao (via GitHub)" <gi...@apache.org>.
huaxingao commented on PR #6981:
URL: https://github.com/apache/iceberg/pull/6981#issuecomment-1452284032

   cc @rdblue @aokolnychyi 


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org