-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature](nereids) in predicate extract non constant expressions (#46794
) Problem Summary: if an in predicate contains non-literal, backend process it will reduce performance. so we need to extract the non constant from the in predicate. this pr add an expression rewrite rule InPredicateExtractNonConstant, it will extract all the non-constant out of the in predicate. for example: ``` k1 in (k2, k3 + 3, 1, 2, 3 + 3) => k1 in (1, 2, 3 + 3) or k1 = k2 or k1 = k3 + 1 ```
- Loading branch information
Showing
10 changed files
with
164 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
77 changes: 77 additions & 0 deletions
77
...n/java/org/apache/doris/nereids/rules/expression/rules/InPredicateExtractNonConstant.java
This file contains 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,77 @@ | ||
// 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.doris.nereids.rules.expression.rules; | ||
|
||
import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher; | ||
import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory; | ||
import org.apache.doris.nereids.rules.expression.ExpressionRuleType; | ||
import org.apache.doris.nereids.trees.expressions.EqualTo; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.expressions.InPredicate; | ||
import org.apache.doris.nereids.util.ExpressionUtils; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Sets; | ||
import org.apache.hadoop.util.Lists; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Extract non-constant of InPredicate, For example: | ||
* where k1 in (k2, k3, 10, 20, 30) ==> where k1 in (10, 20, 30) or k1 = k2 or k1 = k3. | ||
* It's because backend handle in predicate which contains none-constant column will reduce performance. | ||
*/ | ||
public class InPredicateExtractNonConstant implements ExpressionPatternRuleFactory { | ||
public static final InPredicateExtractNonConstant INSTANCE = new InPredicateExtractNonConstant(); | ||
|
||
@Override | ||
public List<ExpressionPatternMatcher<? extends Expression>> buildRules() { | ||
return ImmutableList.of( | ||
matchesType(InPredicate.class) | ||
.when(inPredicate -> inPredicate.getOptions().size() | ||
<= InPredicateDedup.REWRITE_OPTIONS_MAX_SIZE) | ||
.then(this::rewrite) | ||
.toRule(ExpressionRuleType.IN_PREDICATE_EXTRACT_NON_CONSTANT) | ||
); | ||
} | ||
|
||
private Expression rewrite(InPredicate inPredicate) { | ||
Set<Expression> nonConstants = Sets.newLinkedHashSetWithExpectedSize(inPredicate.arity()); | ||
for (Expression option : inPredicate.getOptions()) { | ||
if (!option.isConstant()) { | ||
nonConstants.add(option); | ||
} | ||
} | ||
if (nonConstants.isEmpty()) { | ||
return inPredicate; | ||
} | ||
Expression key = inPredicate.getCompareExpr(); | ||
List<Expression> disjunctions = Lists.newArrayListWithExpectedSize(inPredicate.getOptions().size()); | ||
List<Expression> constants = inPredicate.getOptions().stream().filter(Expression::isConstant) | ||
.collect(Collectors.toList()); | ||
if (!constants.isEmpty()) { | ||
disjunctions.add(ExpressionUtils.toInPredicateOrEqualTo(key, constants)); | ||
} | ||
for (Expression option : nonConstants) { | ||
disjunctions.add(new EqualTo(key, option)); | ||
} | ||
return ExpressionUtils.or(disjunctions); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
47 changes: 47 additions & 0 deletions
47
...va/org/apache/doris/nereids/rules/expression/rules/InPredicateExtractNonConstantTest.java
This file contains 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,47 @@ | ||
// 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.doris.nereids.rules.expression.rules; | ||
|
||
import org.apache.doris.nereids.sqltest.SqlTestBase; | ||
import org.apache.doris.nereids.util.PlanChecker; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class InPredicateExtractNonConstantTest extends SqlTestBase { | ||
@Test | ||
public void testExtractNonConstant() { | ||
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION"); | ||
String sql = "select * from T1 where id in (score, score, score + 100)"; | ||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalFilter().when(f -> f.getPredicate().toString().equals( | ||
"OR[(id#0 = score#1),(id#0 = (score#1 + 100))]" | ||
))); | ||
|
||
sql = "select * from T1 where id in (score, score + 10, score + score, score, 10, 20, 30, 100 + 200)"; | ||
PlanChecker.from(connectContext) | ||
.analyze(sql) | ||
.rewrite() | ||
.matches( | ||
logicalFilter().when(f -> f.getPredicate().toString().equals( | ||
"OR[id#0 IN (20, 10, 300, 30),(id#0 = score#1),(id#0 = (score#1 + 10)),(id#0 = (score#1 + score#1))]" | ||
))); | ||
} | ||
} |