paimon icon indicating copy to clipboard operation
paimon copied to clipboard

[common] Fix O(2^n) complexity in FileIndexPredicate.getRequiredNames

Open dubin555 opened this issue 9 months ago • 2 comments

Purpose

Linked issue: close #7230

FileIndexPredicate.getRequiredNames() calls child.visit(this) twice per child in its CompoundPredicate visitor — once discarding the result, then again to collect it. Since PredicateBuilder.or() produces right-nested binary trees via reduce(), this doubles work at each tree level, resulting in O(2^n) time complexity.

For an IN clause with 20 values (which produces a nested OR tree of depth 19), this means ~1,048,576 leaf visits instead of 20. In production, queries with moderately sized IN clauses hang indefinitely.

The fix removes the redundant child.visit(this) call (line 130), matching the correct pattern already used in PredicateVisitor.FieldNameCollector.

The bug was introduced in ebdfa02bd ("[hotfix] Correct visitors for TransformPredicate"), which refactored the visitor to handle TransformPredicate and accidentally left the duplicate call.

Tests

  • FileIndexPredicateTest.testGetRequiredNamesLinearComplexity() — builds a 20-element OR chain, counts leaf visits via AtomicInteger. Asserts exactly 20 visits (linear). Before fix: 1,048,575 visits (exponential).
  • FileIndexPredicateTest.testGetRequiredNamesPerformance() — builds a 20-element OR chain, asserts completion within 100ms.
  • FileIndexPredicateTest.testGetRequiredNamesBasic() — verifies correctness: all field names are collected from a compound predicate.
  • FileIndexPredicateTest.testGetRequiredNamesSinglePredicate() — verifies single leaf predicate returns the correct field name.

API and Format

No.

Documentation

No.

Generative AI tooling

Generated-by: Claude Code 1.0.33

dubin555 avatar Mar 02 '26 13:03 dubin555

Good point! The testGetRequiredNamesPerformance test with System.nanoTime() assertions is indeed fragile and could be flaky on slower CI machines.

I've removed it — the testGetRequiredNamesLinearComplexity test already verifies the fix by asserting the exact visit count (20 leaf visits instead of 2^20-1), which is a deterministic correctness check rather than a timing-based one. That should be sufficient as a regression test.

dubin555 avatar Mar 12 '26 22:03 dubin555

Please fix compile error.

JingsongLi avatar May 24 '26 02:05 JingsongLi