-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix](nereids)remove useless cast in in-predicate
- Loading branch information
1 parent
1c3cc77
commit d77a966
Showing
3 changed files
with
118 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
...re/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyInPredicate.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,78 @@ | ||
// 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.AbstractExpressionRewriteRule; | ||
import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; | ||
import org.apache.doris.nereids.trees.expressions.Cast; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.expressions.InPredicate; | ||
import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal; | ||
import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* SimplifyInPredicate | ||
*/ | ||
public class SimplifyInPredicate extends AbstractExpressionRewriteRule { | ||
|
||
public static final SimplifyInPredicate INSTANCE = new SimplifyInPredicate(); | ||
|
||
@Override | ||
public Expression visitInPredicate(InPredicate expr, ExpressionRewriteContext context) { | ||
if (expr.children().size() > 1) { | ||
if (expr.getCompareExpr() instanceof Cast) { | ||
Cast cast = (Cast) expr.getCompareExpr(); | ||
if (cast.child().getDataType().isDateV2Type() | ||
&& expr.child(1) instanceof DateTimeV2Literal) { | ||
List<Expression> literals = expr.children().subList(1, expr.children().size()); | ||
if (literals.stream().allMatch(literal -> literal instanceof DateTimeV2Literal | ||
&& canLosslessConvertToDateV2Literal((DateTimeV2Literal) literal))) { | ||
List<Expression> children = Lists.newArrayList(); | ||
children.add(cast.child()); | ||
literals.stream().forEach( | ||
l -> children.add(convertToDateV2Literal((DateTimeV2Literal) l))); | ||
return expr.withChildren(children); | ||
} | ||
} | ||
} | ||
} | ||
return expr; | ||
} | ||
|
||
/* | ||
derive tree: | ||
DateLiteral | ||
| | ||
+--->DateTimeLiteral | ||
| | | ||
| +----->DateTimeV2Literal | ||
+--->DateV2Literal | ||
*/ | ||
private static boolean canLosslessConvertToDateV2Literal(DateTimeV2Literal literal) { | ||
return (literal.getHour() | literal.getMinute() | literal.getSecond() | ||
| literal.getMicroSecond()) == 0L; | ||
} | ||
|
||
private DateV2Literal convertToDateV2Literal(DateTimeV2Literal literal) { | ||
return new DateV2Literal(literal.getYear(), literal.getMonth(), literal.getDay()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
regression-test/suites/nereids_syntax_p0/test_simplify_in_predicate.groovy
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,38 @@ | ||
// 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. | ||
|
||
suite("test_simplify_in_predicate") { | ||
sql "set enable_nereids_planner=true" | ||
sql 'set enable_fallback_to_original_planner=false;' | ||
sql 'drop table if exists test_simplify_in_predicate_t' | ||
sql """CREATE TABLE IF NOT EXISTS `test_simplify_in_predicate_t` ( | ||
a DATE NOT NULL | ||
) ENGINE=OLAP | ||
UNIQUE KEY (`a`) | ||
DISTRIBUTED BY HASH(`a`) BUCKETS 120 | ||
PROPERTIES ( | ||
"replication_num" = "1", | ||
"in_memory" = "false", | ||
"compression" = "LZ4" | ||
);""" | ||
sql """insert into test_simplify_in_predicate_t values( "2023-06-06" );""" | ||
|
||
explain { | ||
sql "verbose select * from test_simplify_in_predicate_t where a in ('1992-01-31', '1992-02-01', '1992-02-02', '1992-02-03', '1992-02-04');" | ||
notContains "CAST" | ||
} | ||
} |