You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/11/23 19:39:12 UTC

[GitHub] [iceberg] huaxingao commented on a diff in pull request #6252: push down min/max/count to iceberg

huaxingao commented on code in PR #6252:
URL: https://github.com/apache/iceberg/pull/6252#discussion_r1030816722


##########
spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/source/SparkPushedDownAggregateUtil.java:
##########
@@ -0,0 +1,373 @@
+/*
+ * 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.spark.source;
+
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+import java.util.List;
+import org.apache.iceberg.MetadataTableType;
+import org.apache.iceberg.MetricsConfig;
+import org.apache.iceberg.MetricsModes;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.expressions.AggregateUtil;
+import org.apache.iceberg.expressions.BoundAggregate;
+import org.apache.iceberg.expressions.Expression;
+import org.apache.iceberg.expressions.ExpressionVisitors;
+import org.apache.iceberg.expressions.Literal;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.spark.SparkTableUtil;
+import org.apache.iceberg.types.Conversions;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.types.Decimal;
+import scala.collection.JavaConverters;
+
+/** Helper methods for working with Spark aggregate push down. */
+public class SparkPushedDownAggregateUtil {
+
+  private SparkPushedDownAggregateUtil() {}
+
+  public static boolean metricsModeSupportsAggregatePushDown(
+      Table table, List<Expression> aggregates) {
+    MetricsConfig config = MetricsConfig.forTable(table);
+    for (Expression aggregate : aggregates) {
+      String colName = AggregateUtil.getAggregateColumnName(aggregate);
+      if (!colName.equals("*")) {
+        MetricsModes.MetricsMode mode = config.columnMode(colName);
+        if (mode.toString().equals("none")) {
+          return false;
+        } else if (mode.toString().equals("counts")) {
+          if (aggregate.op() == Expression.Operation.MAX
+              || aggregate.op() == Expression.Operation.MIN) {
+            return false;
+          }
+        } else if (mode.toString().contains("truncate")) {
+          if (AggregateUtil.getAggregateType(aggregate).typeId() == Type.TypeID.STRING) {
+            if (aggregate.op() == Expression.Operation.MAX
+                || aggregate.op() == Expression.Operation.MIN) {
+              return false;
+            }
+          }
+        }
+      }
+    }
+
+    return true;
+  }
+
+  public static InternalRow[] constructInternalRowForPushedDownAggregate(
+      SparkSession spark, Table table, List<Expression> aggregates, List<Integer> indexInTable) {
+    List<Object> valuesInSparkInternalRow = Lists.newArrayList();
+    Row[] row = SparkPushedDownAggregateUtil.getStatisticRow(spark, table);
+    for (int index = 0; index < aggregates.size(); index++) {
+      Expression aggregate = aggregates.get(index);
+      Type type = AggregateUtil.getAggregateType(aggregate);
+      valuesInSparkInternalRow.add(
+          SparkPushedDownAggregateUtil.getAggregateValue(
+              aggregate, row, indexInTable.get(index), type));
+    }
+
+    InternalRow[] rows = new InternalRow[1];
+    rows[0] = InternalRow.fromSeq(JavaConverters.asScalaBuffer(valuesInSparkInternalRow).toSeq());
+    return rows;
+  }
+
+  public static Row[] getStatisticRow(SparkSession spark, Table table) {
+    Dataset<Row> metadataRows =
+        SparkTableUtil.loadMetadataTable(spark, table, MetadataTableType.DATA_FILES);
+    Dataset dataset =
+        metadataRows.selectExpr(
+            "lower_bounds", "upper_bounds", "record_count", "null_value_counts");

Review Comment:
   I have thought about using something like 
   ```
   metadataRows.select(functions.max(functions.col("upper_bounds.1")))
   ```
   but the values in the statistics such as `upper_bounds` are in byte array. I don't think I can get the correct max/min values. 
   For example, if I have 3.333 and 5.555, the corresponding values in statistics are [-33, 79, 83, 64] and [-113, -62, -79, 64], if I get max, I will get [-33, 79, 83, 64] which is 3.333. That's why i  select `lower_bounds`, `upper_bounds` and get the max/min by myself.



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