Skip to content

Use bisect for In predicates in metrics and manifest evaluators - #4607

Open
AllyNav wants to merge 2 commits into
apache:mainfrom
AllyNav:perf/bisect-in-metrics-evaluator
Open

AllyNav wants to merge 2 commits into
apache:mainfrom
AllyNav:perf/bisect-in-metrics-evaluator

Conversation

@AllyNav

@AllyNav AllyNav commented Dec 3, 2026

Copy link
Copy Markdown

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.

Copilot AI balanced review requested due to automatic review settings December 3, 2026 21:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one @AllyNav, big planning win for large In predicates! I verified the bisect logic matches the old all() filtering (626 expression tests pass, and the 4 benchmark tests pass locally) and the id(value_set) cache is sound since value_set is a cached property held alive by the bound expression tree.

CI is failing on mypy though: pyiceberg/expressions/visitors.py:561: error: Statement is unreachable. BoundIn.__new__ is annotated to return BooleanExpression (not BoundIn), which combined with the pydantic plugin makes mypy treat the And/Or/Not branches after the BoundIn check as unreachable. I verified locally that checking (And, Or) and Not before BoundIn clears the error and all expression tests still pass, left a suggestion below.

Comment thread pyiceberg/expressions/visitors.py Outdated
Comment on lines +558 to +565
if isinstance(node, BoundIn):
value_set = node.value_set
sorted_literals[id(value_set)] = sorted(value_set)
elif isinstance(node, (And, Or)):
stack.append(node.left)
stack.append(node.right)
elif isinstance(node, Not):
stack.append(node.child)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy fails on this block in CI (visitors.py:561: Statement is unreachable). Checking BoundIn last makes mypy happy again:

Suggested change
if isinstance(node, BoundIn):
value_set = node.value_set
sorted_literals[id(value_set)] = sorted(value_set)
elif isinstance(node, (And, Or)):
stack.append(node.left)
stack.append(node.right)
elif isinstance(node, Not):
stack.append(node.child)
if isinstance(node, (And, Or)):
stack.append(node.left)
stack.append(node.right)
elif isinstance(node, Not):
stack.append(node.child)
elif isinstance(node, BoundIn):
value_set = node.value_set
sorted_literals[id(value_set)] = sorted(value_set)

@AllyNav

AllyNav commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks for the review @Fokko! Applied your suggestion and CI is all green now. Just getting started with my open source journey .

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use bisect for In predicates in the metrics evaluators

3 participants