|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 | import math |
| 18 | +import threading |
18 | 19 | from abc import ABC, abstractmethod |
19 | | -from collections.abc import Callable |
| 20 | +from collections.abc import Callable, Hashable |
20 | 21 | from functools import singledispatch |
21 | 22 | from typing import ( |
22 | 23 | Any, |
|
25 | 26 | TypeVar, |
26 | 27 | ) |
27 | 28 |
|
| 29 | +from cachetools import LRUCache, cached |
| 30 | +from cachetools.keys import hashkey |
| 31 | + |
28 | 32 | from pyiceberg.conversions import from_bytes |
29 | 33 | from pyiceberg.expressions import ( |
30 | 34 | AlwaysFalse, |
@@ -1970,6 +1974,20 @@ def residual_for(self, partition_data: Record) -> BooleanExpression: |
1970 | 1974 | return self.expr |
1971 | 1975 |
|
1972 | 1976 |
|
| 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 | +) |
1973 | 1991 | def residual_evaluator_of( |
1974 | 1992 | spec: PartitionSpec, expr: BooleanExpression, case_sensitive: bool, schema: Schema |
1975 | 1993 | ) -> ResidualEvaluator: |
|
0 commit comments