|
| 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.rules.expression.rules; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.rules.expression.AbstractExpressionRewriteRule; |
| 21 | +import org.apache.doris.nereids.rules.expression.ExpressionRewriteContext; |
| 22 | +import org.apache.doris.nereids.trees.expressions.Cast; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 24 | +import org.apache.doris.nereids.trees.expressions.InPredicate; |
| 25 | +import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal; |
| 26 | +import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal; |
| 27 | + |
| 28 | +import com.google.common.collect.Lists; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +/** |
| 33 | + * SimplifyInPredicate |
| 34 | + */ |
| 35 | +public class SimplifyInPredicate extends AbstractExpressionRewriteRule { |
| 36 | + |
| 37 | + public static final SimplifyInPredicate INSTANCE = new SimplifyInPredicate(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public Expression visitInPredicate(InPredicate expr, ExpressionRewriteContext context) { |
| 41 | + if (expr.children().size() > 1) { |
| 42 | + if (expr.getCompareExpr() instanceof Cast) { |
| 43 | + Cast cast = (Cast) expr.getCompareExpr(); |
| 44 | + if (cast.child().getDataType().isDateV2Type() |
| 45 | + && expr.child(1) instanceof DateTimeV2Literal) { |
| 46 | + List<Expression> literals = expr.children().subList(1, expr.children().size()); |
| 47 | + if (literals.stream().allMatch(literal -> literal instanceof DateTimeV2Literal |
| 48 | + && canLosslessConvertToDateV2Literal((DateTimeV2Literal) literal))) { |
| 49 | + List<Expression> children = Lists.newArrayList(); |
| 50 | + children.add(cast.child()); |
| 51 | + literals.stream().forEach( |
| 52 | + l -> children.add(convertToDateV2Literal((DateTimeV2Literal) l))); |
| 53 | + return expr.withChildren(children); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + return expr; |
| 59 | + } |
| 60 | + |
| 61 | + /* |
| 62 | + derive tree: |
| 63 | + DateLiteral |
| 64 | + | |
| 65 | + +--->DateTimeLiteral |
| 66 | + | | |
| 67 | + | +----->DateTimeV2Literal |
| 68 | + +--->DateV2Literal |
| 69 | + */ |
| 70 | + private static boolean canLosslessConvertToDateV2Literal(DateTimeV2Literal literal) { |
| 71 | + return (literal.getHour() | literal.getMinute() | literal.getSecond() |
| 72 | + | literal.getMicroSecond()) == 0L; |
| 73 | + } |
| 74 | + |
| 75 | + private DateV2Literal convertToDateV2Literal(DateTimeV2Literal literal) { |
| 76 | + return new DateV2Literal(literal.getYear(), literal.getMonth(), literal.getDay()); |
| 77 | + } |
| 78 | +} |
0 commit comments