AllyNav opened a new pull request, #4607: URL: https://github.com/apache/iceberg-python/pull/4607
Closes #4601 # Rationale for this change Following #4032 (which removed `IN_PREDICATE_LIMIT`), `_InclusiveMetricsEvaluationVisitor.visit_in` and `_ManifestEvalVisitor.visit_in` linearly scan every literal in the `In` predicate against each file's bounds (`O(literals * files)`). For large `In` predicates (e.g. 100k keys) across tables with many files (e.g. 10k data files), planning takes ~13–19 seconds. This change pre-sorts each `BoundIn.value_set` once in `_InclusiveMetricsEvaluator.__init__` and `_ManifestEvalVisitor.__init__` (preserving stateless, thread-safe `eval()` calls) and uses `bisect.bisect_left` in `visit_in` to check bounds in `O(log n)` per file/manifest: - Find the first literal `>= lower_bound` via `bisect_left`. - If no such literal exists, return `ROWS_CANNOT_MATCH`. - If `upper_bound` is present (and not `NaN`) and `sorted_literals[idx] > upper_bound`, return `ROWS_CANNOT_MATCH`. As a side benefit, `_ManifestEvalVisitor.visit_in` now also prunes manifests when all literals lie outside `[lower_bound, upper_bound]` on both sides with no overlap (matching `_InclusiveMetricsEvaluator`). ### Benchmark (10,000 files/manifests × 100,000 `IN` keys) Measured on macOS arm64, Python 3.13.18 (`tests/benchmark/test_in_predicate_benchmark.py`): | Evaluator | Scenario | Matched | `main` | This PR | Speedup | | :--- | :--- | ---: | ---: | ---: | ---: | | `_InclusiveMetricsEvaluator` | `single-file` | 1 | 12,715.11 ms | 129.36 ms | ~98x | | `_InclusiveMetricsEvaluator` | `all-files` | 10,000 | 19,068.30 ms | 146.18 ms | ~130x | | `_ManifestEvalVisitor` | `single-file` | 1 | 13,065.68 ms | 127.20 ms | ~103x | | `_ManifestEvalVisitor` | `all-files` | 10,000 | 149.99 ms | 147.54 ms | ~1.0x | ## Are these changes tested? - Added unit tests in `tests/expressions/test_evaluator.py` and `tests/expressions/test_visitors.py` covering evaluator reuse across multiple files/manifests, `Or`-nested `In` predicates, and missing lower/upper bounds. - Updated `test_integer_in_above_limit` in `tests/expressions/test_visitors.py` to assert the tighter pruning behavior when literals fall outside both bounds. - Added `@pytest.mark.benchmark` tests in `tests/benchmark/test_in_predicate_benchmark.py` covering 10k files/manifests and 100k `IN` keys for both single-file and all-files hit patterns. - Verified with `pytest tests/expressions/` (626 passed), `pytest tests/benchmark/test_in_predicate_benchmark.py -v -s -m benchmark` (4 passed), `ruff check`, `ruff format --check`, `ty check`, and `mypy`. ## Are there any user-facing changes? Faster file and manifest pruning for large `In` predicates during scan planning. No public API changes. -- 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]
