Skip to content

Commit 1bccb5c

Browse files
committed
refactor(expressions): replace custom residual evaluator LRU with cachetools
1 parent b0a7878 commit 1bccb5c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

pyiceberg/expressions/visitors.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
import math
18+
import threading
1819
from abc import ABC, abstractmethod
19-
from collections.abc import Callable
20+
from collections.abc import Callable, Hashable
2021
from functools import singledispatch
2122
from typing import (
2223
Any,
@@ -25,6 +26,9 @@
2526
TypeVar,
2627
)
2728

29+
from cachetools import LRUCache, cached
30+
from cachetools.keys import hashkey
31+
2832
from pyiceberg.conversions import from_bytes
2933
from pyiceberg.expressions import (
3034
AlwaysFalse,
@@ -1970,6 +1974,20 @@ def residual_for(self, partition_data: Record) -> BooleanExpression:
19701974
return self.expr
19711975

19721976

1977+
_DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE = 128
1978+
1979+
1980+
def _residual_evaluator_cache_key(
1981+
spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema
1982+
) -> tuple[Hashable, ...]:
1983+
return hashkey(spec.spec_id, repr(expr), case_sensitive, schema.schema_id)
1984+
1985+
1986+
@cached(
1987+
cache=LRUCache(maxsize=_DEFAULT_RESIDUAL_EVALUATOR_CACHE_SIZE),
1988+
key=_residual_evaluator_cache_key,
1989+
lock=threading.RLock(),
1990+
)
19731991
def residual_evaluator_of(
19741992
spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema
19751993
) -> ResidualEvaluator:

0 commit comments

Comments
 (0)