Skip to content

Commit 5ed06bb

Browse files
authored
branch-3.1: [fix](nereids) allow merge project for java udf (#57102)
1 parent 57cde86 commit 5ed06bb

14 files changed

Lines changed: 118 additions & 22 deletions

File tree

fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/PushDownFilterThroughProject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Plan visitPhysicalFilter(PhysicalFilter<? extends Plan> filter, CascadesC
4545
PhysicalProject<? extends Plan> project = (PhysicalProject<? extends Plan>) child;
4646
Map<Slot, Expression> childAlias = project.getAliasToProducer();
4747
if (filter.getInputSlots().stream().map(childAlias::get).filter(Objects::nonNull)
48-
.anyMatch(Expression::containsNonfoldable)) {
48+
.anyMatch(Expression::containsUniqueFunction)) {
4949
return filter;
5050
}
5151
PhysicalFilter<? extends Plan> newFilter = filter.withConjunctsAndChild(

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyConflictCompound.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private Expression rewrite(CompoundPredicate compoundPredicate) {
6060
// ie, predicate contains expression 'expression' and 'not expression'
6161
Map<Expression, Pair<Boolean, Boolean>> exprExistMarks = Maps.newHashMap();
6262
for (Expression child : flatten) {
63-
if (!child.containsNonfoldable()) {
63+
if (!child.containsUniqueFunction()) {
6464
if (child instanceof CompoundPredicate) {
6565
Expression newChild = rewrite((CompoundPredicate) child);
6666
if (!child.equals(newChild)) {

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownFilterThroughProject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ private static Pair<Set<Expression>, Set<Expression>> splitConjunctsByChildOutpu
113113
Set<Expression> remainPredicates = Sets.newLinkedHashSet();
114114
for (Expression conjunct : conjuncts) {
115115
Set<Slot> conjunctSlots = conjunct.getInputSlots();
116-
// If filter contains non-foldable expression, it can push down, for example:
116+
// If filter contains unique function, it can push down, for example:
117117
// `filter(a + random(1, 10) > 1) -> project(a)` => `project(a) -> filter(a + random(1, 10) > 1)`.
118-
// If filter slot is alias and its expression contains non-foldable expression, it can't push down, example:
118+
// If filter slot is alias and its expression contains unique function, it can't push down, example:
119119
// `filter(a > 1) -> project(b + random(1, 10) as a)`, if push down filter, it got
120120
// `project(b + random(1, 10) as a) -> filter(b + random(1, 10) > 1)`, it contains two distinct RANDOM.
121121
if (childOutputs.containsAll(conjunctSlots)
122122
&& conjunctSlots.stream().map(childAlias::get).filter(Objects::nonNull)
123-
.noneMatch(Expression::containsNonfoldable)) {
123+
.noneMatch(Expression::containsUniqueFunction)) {
124124
pushDownPredicates.add(conjunct);
125125
} else {
126126
remainPredicates.add(conjunct);

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
2929
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
3030
import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda;
31+
import org.apache.doris.nereids.trees.expressions.functions.scalar.UniqueFunction;
3132
import org.apache.doris.nereids.trees.expressions.literal.Literal;
3233
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
3334
import org.apache.doris.nereids.trees.expressions.shape.LeafExpression;
@@ -422,6 +423,10 @@ public boolean isKeyColumnFromTable() {
422423
&& ((SlotReference) this).getOriginalColumn().get().isKey();
423424
}
424425

426+
public boolean containsUniqueFunction() {
427+
return containsType(UniqueFunction.class);
428+
}
429+
425430
/** containsNullLiteralChildren */
426431
public boolean containsNullLiteralChildren() {
427432
return getOrInitMutableState("CONTAINS_NULL_LITERAL_CHILDREN", () -> {

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/ExpressionTrait.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,13 @@ default String toSql() throws UnboundException {
7373
throw new UnboundException("sql");
7474
}
7575

76-
default boolean foldable() {
77-
return true;
78-
}
79-
8076
/**
81-
* Identify the expression is containing non-foldable expr or not
77+
* foldable() mainly use in fold expression. Udf and UniqueFunction are not foldable.
78+
* But if want to check an expression contains non-idempotent, such as `rand()`, `uuid()`, etc.,
79+
* you should use Expression::containsUniqueFunction instead.
8280
*/
83-
default boolean containsNonfoldable() {
84-
return anyMatch(expr -> !((ExpressionTrait) expr).foldable());
81+
default boolean foldable() {
82+
return true;
8583
}
8684

8785
/**

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Random.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* ScalarFunction 'random'. This class is generated by GenerateFunction.
3636
*/
3737
public class Random extends ScalarFunction
38-
implements ExplicitlyCastableSignature {
38+
implements ExplicitlyCastableSignature, UniqueFunction {
3939

4040
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
4141
FunctionSignature.ret(DoubleType.INSTANCE).args(),

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/RandomBytes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* ScalarFunction 'random_bytes'. This class is generated by GenerateFunction.
3636
*/
3737
public class RandomBytes extends ScalarFunction
38-
implements ExplicitlyCastableSignature, PropagateNullable {
38+
implements ExplicitlyCastableSignature, PropagateNullable, UniqueFunction {
3939

4040
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
4141
FunctionSignature.ret(StringType.INSTANCE).args(IntegerType.INSTANCE),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.expressions.functions.scalar;
19+
20+
/**
21+
* Unique function include random like functions. Two calls to the function with the same arguments
22+
* will return different results.
23+
*/
24+
public interface UniqueFunction {
25+
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Uuid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* ScalarFunction 'uuid'. This class is generated by GenerateFunction.
3333
*/
3434
public class Uuid extends ScalarFunction
35-
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable {
35+
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable, UniqueFunction {
3636

3737
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
3838
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT).args()

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/UuidNumeric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* ScalarFunction 'uuid_numeric'. This class is generated by GenerateFunction.
3333
*/
3434
public class UuidNumeric extends ScalarFunction
35-
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable {
35+
implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable, UniqueFunction {
3636

3737
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
3838
FunctionSignature.ret(LargeIntType.INSTANCE).args()

0 commit comments

Comments
 (0)