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 2020/08/23 14:06:24 UTC

[GitHub] [iceberg] rymurr commented on a change in pull request #1314: [python] Adding rowgroup evaluator using pyarrow parquet stats

rymurr commented on a change in pull request #1314:
URL: https://github.com/apache/iceberg/pull/1314#discussion_r475222220



##########
File path: python/setup.py
##########
@@ -38,7 +37,7 @@
                       'requests',
                       'retrying',
                       'pandas',
-                      'pyarrow'
+                      'pyarrow>=0.17.0'

Review comment:
       any reason to not use 1.0?

##########
File path: python/iceberg/parquet/parquet_rowgroup_evaluator.py
##########
@@ -0,0 +1,340 @@
+# 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.
+
+# inspiration drawn from the predicate accepts function in Kartothek
+# https://github.com/JDASoftwareGroup/kartothek/blob/master/kartothek/serialization/_parquet.py#L406
+
+from decimal import Decimal
+
+from iceberg.api.expressions import Binder, Expressions, ExpressionVisitors
+from iceberg.api.types import TypeID
+
+MICROSECOND_CONVERSION = 1000000
+
+
+class ParquetRowgroupEvaluator(object):
+    """
+    Evaluator for determining if a pyarrow parquet row-group matches the given expression and index bounds.
+
+    Parameters
+    ----------
+    schema : iceberg.api.Schema
+        An iceberg schema to use for binding the predicate
+    field_name_map: map
+        A map that translates file column names to the current schema
+    unbound : iceberg.api.expressions.UnboundPredicate
+        The unbound predicate to evaluate
+    start : int
+        The start index of the assigned reader
+    end : int
+        The end index of the assigned reader
+    """
+    def __init__(self, schema, field_name_map, unbound, start, end):
+        self.schema = schema
+        self.struct = schema.as_struct()
+        self.field_name_map = field_name_map
+
+        self.expr = None if unbound is None else Binder.bind(self.struct, Expressions.rewrite_not(unbound))
+        self.start = start
+        self.end = end
+        self._visitors = None
+
+    def _visitor(self):
+        if self._visitors is None:
+            self._visitors = ParquetRowgroupEvalVisitor(self.expr, self.schema, self.field_name_map,
+                                                        self.struct, self.start, self.end)
+
+        return self._visitors
+
+    def eval(self, row_group):
+        return self._visitor().eval(row_group)
+
+
+class ParquetRowgroupEvalVisitor(ExpressionVisitors.BoundExpressionVisitor):
+    ROWS_MIGHT_MATCH = True
+    ROWS_CANNOT_MATCH = False
+
+    def __init__(self, expr, schema, field_name_map, struct, start, end):
+        self.expr = expr
+        self.schema = schema
+        self.field_name_map = field_name_map
+        self.struct = struct
+        self.start = start
+        self.end = end
+
+        # row-group stats info
+        self.lower_bounds = None
+        self.upper_bounds = None
+        self.nulls = None
+        self.num_rows = None
+        self.midpoint = None
+        self.parquet_cols = None
+
+    def eval(self, row_group):
+        """
+        Returns a boolean that determines if the given row-group may contain rows
+        for the assigned predicate and read-range[start, end]
+
+        Parameters
+        ----------
+        row_group : pyarrow._parquet.RowGroupMetaData
+            The pyarrow parquet row-group metadata being evaluated
+        Returns
+        -------
+        boolean
+            True if rows for the current evaluator might exist in the row group, false otherwise
+        """
+        if row_group.num_rows <= 0:
+            return ParquetRowgroupEvalVisitor.ROWS_CANNOT_MATCH
+
+        self.get_stats(row_group)
+        self.num_rows = row_group.num_rows
+
+        # if the mid-point of the row-group is not contained by the
+        # start-end range we don't read it
+        if self.start is not None and self.end is not None \
+                and not (self.start <= self.midpoint <= self.end):
+            return ParquetRowgroupEvalVisitor.ROWS_CANNOT_MATCH
+
+        if self.expr is None:
+            return ParquetRowgroupEvalVisitor.ROWS_MIGHT_MATCH
+
+        return ExpressionVisitors.visit(self.expr, self)
+
+    def always_true(self):
+        return ParquetRowgroupEvalVisitor.ROWS_MIGHT_MATCH
+
+    def always_false(self):
+        return ParquetRowgroupEvalVisitor.ROWS_CANNOT_MATCH
+
+    def not_(self, result):
+        return not result
+
+    def and_(self, left_result, right_result):
+        return left_result and right_result
+
+    def or_(self, left_result, right_result):
+        return left_result or right_result
+
+    def is_null(self, ref):
+        id = ref.field_id

Review comment:
       nit: `id` variable hides the `id` builtin. Could we call this `field_id`? 




----------------------------------------------------------------
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:
users@infra.apache.org



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