-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[common] Fix O(2^n) complexity in FileIndexPredicate.getRequiredNames #7332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dubin555
wants to merge
1
commit into
apache:master
Choose a base branch
from
dubin555:oss-scout/verify-fix-fileindex-predicate-exponential-complexity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+141
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
paimon-common/src/test/java/org/apache/paimon/fileindex/FileIndexPredicateTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.paimon.fileindex; | ||
|
|
||
| import org.apache.paimon.predicate.CompoundPredicate; | ||
| import org.apache.paimon.predicate.Or; | ||
| import org.apache.paimon.predicate.Predicate; | ||
| import org.apache.paimon.predicate.PredicateBuilder; | ||
| import org.apache.paimon.predicate.PredicateVisitor; | ||
| import org.apache.paimon.types.DataTypes; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** Tests for {@link FileIndexPredicate}, specifically the getRequiredNames visitor. */ | ||
| public class FileIndexPredicateTest { | ||
|
|
||
| /** | ||
| * Verifies that getRequiredNames (via the PredicateVisitor in FileIndexPredicate) visits each | ||
| * child node exactly once, not twice. Before the fix, the visitor called child.visit(this) | ||
| * twice per child — once discarding the result (line 130) and once using it (line 131). This | ||
| * caused O(2^n) complexity for deeply nested OR predicates (e.g., IN clauses with <= 20 | ||
| * values). | ||
| * | ||
| * <p>This test builds a deeply nested OR predicate tree (simulating an IN clause) and counts | ||
| * the total number of leaf visits. With the fix, the count should be exactly N (one per leaf). | ||
| * Before the fix, it would be 2^N - 1. | ||
| */ | ||
| @Test | ||
| public void testGetRequiredNamesLinearComplexity() { | ||
| RowType rowType = RowType.of(DataTypes.INT()); | ||
| PredicateBuilder builder = new PredicateBuilder(rowType); | ||
|
|
||
| // Build an OR chain of 20 equality predicates: col0 = 0 OR col0 = 1 OR ... OR col0 = 19 | ||
| // PredicateBuilder.or() uses reduce, creating a right-nested binary tree of depth ~20. | ||
| List<Predicate> equals = new ArrayList<>(); | ||
| for (int i = 0; i < 20; i++) { | ||
| equals.add(builder.equal(0, i)); | ||
| } | ||
| Predicate inPredicate = PredicateBuilder.or(equals); | ||
|
|
||
| // Count leaf visits using the same visitor pattern as getRequiredNames | ||
| AtomicInteger visitCount = new AtomicInteger(0); | ||
| Set<String> result = | ||
| inPredicate.visit( | ||
| new PredicateVisitor<Set<String>>() { | ||
| @Override | ||
| public Set<String> visit( | ||
| org.apache.paimon.predicate.LeafPredicate predicate) { | ||
| visitCount.incrementAndGet(); | ||
| return new HashSet<>(predicate.fieldNames()); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> visit(CompoundPredicate predicate) { | ||
| Set<String> names = new HashSet<>(); | ||
| for (Predicate child : predicate.children()) { | ||
| names.addAll(child.visit(this)); | ||
| } | ||
| return names; | ||
| } | ||
| }); | ||
|
|
||
| // The result should contain the field name | ||
| assertThat(result).containsExactly("f0"); | ||
|
|
||
| // With correct linear traversal, each of the 20 leaves is visited exactly once, | ||
| // plus we traverse ~19 compound nodes. The leaf visit count should be exactly 20. | ||
| assertThat(visitCount.get()).isEqualTo(20); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that getRequiredNames completes in reasonable time for large OR predicates. Before the | ||
| * fix, this would hang due to O(2^n) complexity. | ||
| */ | ||
| @Test | ||
| public void testGetRequiredNamesPerformance() { | ||
| RowType rowType = RowType.of(DataTypes.INT()); | ||
| PredicateBuilder builder = new PredicateBuilder(rowType); | ||
|
|
||
| // Build an OR chain of 20 equality predicates | ||
| List<Predicate> equals = new ArrayList<>(); | ||
| for (int i = 0; i < 20; i++) { | ||
| equals.add(builder.equal(0, i)); | ||
| } | ||
| Predicate inPredicate = PredicateBuilder.or(equals); | ||
|
|
||
| long startNanos = System.nanoTime(); | ||
|
|
||
| // Use the same visitor pattern as FileIndexPredicate.getRequiredNames | ||
| Set<String> result = | ||
| inPredicate.visit( | ||
| new PredicateVisitor<Set<String>>() { | ||
| @Override | ||
| public Set<String> visit( | ||
| org.apache.paimon.predicate.LeafPredicate predicate) { | ||
| return new HashSet<>(predicate.fieldNames()); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> visit(CompoundPredicate predicate) { | ||
| Set<String> names = new HashSet<>(); | ||
| for (Predicate child : predicate.children()) { | ||
| names.addAll(child.visit(this)); | ||
| } | ||
| return names; | ||
| } | ||
| }); | ||
|
|
||
| long elapsedMs = (System.nanoTime() - startNanos) / 1_000_000; | ||
|
|
||
| assertThat(result).containsExactly("f0"); | ||
| // Should complete in under 100ms with linear complexity. | ||
| // Before the fix with 20 nodes: 2^20 = ~1 million visits, would take seconds+. | ||
| assertThat(elapsedMs).isLessThan(100); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this test be in the
paimon-micro-benchmarkproject to avoid slowing down regular builds?