Fokko commented on code in PR #2695:
URL: https://github.com/apache/iceberg-python/pull/2695#discussion_r2651022901
##########
pyiceberg/expressions/visitors.py:
##########
@@ -1970,11 +1975,37 @@ def residual_for(self, partition_data: Record) ->
BooleanExpression:
return self.expr
-def residual_evaluator_of(
+_DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE = 128
+
+
+def _residual_evaluator_cache_key(
+ spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool,
schema: Schema
+) -> tuple[Hashable, ...]:
+ return hashkey(spec.spec_id, repr(expr), case_sensitive, schema.schema_id)
+
+
+@cached(
+ cache=LRUCache(maxsize=_DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE),
+ key=_residual_evaluator_cache_key,
+ lock=threading.RLock(),
+)
+def _cached_residual_evaluator_template(
spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool,
schema: Schema
) -> ResidualEvaluator:
return (
UnpartitionedResidualEvaluator(schema=schema, expr=expr)
if spec.is_unpartitioned()
else ResidualEvaluator(spec=spec, expr=expr, schema=schema,
case_sensitive=case_sensitive)
)
+
+
+def residual_evaluator_of(
+ spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool,
schema: Schema
+) -> ResidualEvaluator:
+ """Create a residual evaluator.
+
+ Always returns a fresh evaluator instance because evaluators are stateful
+ (they set `self.struct` during evaluation) and may be used from multiple
+ threads.
+ """
+ return copy.copy(_cached_residual_evaluator_template(spec=spec, expr=expr,
case_sensitive=case_sensitive, schema=schema))
Review Comment:
Nice, thanks for adding the test 👍
--
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]