dossett commented on code in PR #3666:
URL: https://github.com/apache/iceberg-python/pull/3666#discussion_r3770759748
##########
pyiceberg/expressions/visitors.py:
##########
@@ -1985,6 +1970,46 @@ def visit_unbound_predicate(self, predicate:
UnboundPredicate) -> BooleanExpress
return bound
+class ResidualVisitor:
+ """Find residuals for an expression using partition values.
+
+ A residual expression is made by partially evaluating an expression using
partition values.
+ For example, if a table is partitioned by day(utc_timestamp) and is read
with a filter expression
+ utc_timestamp > a and utc_timestamp < b, then there are 4 possible
residual expressions
+ for the partition data, d:
+
+ 1. If d > day(a) and d < day(b), the residual is always true
+ 2. If d == day(a) and d != day(b), the residual is utc_timestamp > a
+ 3. If d == day(b) and d != day(a), the residual is utc_timestamp < b
+ 4. If d == day(a) == day(b), the residual is utc_timestamp > a and
utc_timestamp < b
+ """
+
+ schema: Schema
+ spec: PartitionSpec
+ case_sensitive: bool
+ expr: BooleanExpression
+ partition_schema: Schema
+
+ def __init__(self, schema: Schema, spec: PartitionSpec, case_sensitive:
bool, expr: BooleanExpression) -> None:
+ self.schema = schema
+ self.spec = spec
+ self.case_sensitive = case_sensitive
+ self.expr = expr
+ self.partition_schema = Schema(*spec.partition_type(schema).fields)
+
+ def eval(self, partition_data: Record) -> BooleanExpression:
+ return visit(
+ self.expr,
+ visitor=_ResidualEvaluationVisitor(
Review Comment:
It is recreated in the case of cache misses, but the visitor is fairly
lightweight. There's still a net win because the heaviest work (building the
partition `StructType`) is now done once per partition spec in the now
immutable `ResidualEvaluator`. That's a net savings even when there are no
repeated values and no cache hits, per the performance measurement code I ran
(that's in the third commit).
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]