morrySnow commented on code in PR #11630: URL: https://github.com/apache/doris/pull/11630#discussion_r941967031
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/SimplifyCastRule.java: ########## @@ -0,0 +1,97 @@ +// 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.rewrite.rules; + +import org.apache.doris.nereids.rules.expression.rewrite.AbstractExpressionRewriteRule; +import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRewriteContext; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.StringLiteral; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.IntegralType; +import org.apache.doris.nereids.types.NumericType; + +import java.util.Optional; + + +/** + * Rewrite rule of simplify CAST expression. + * Remove redundant cast like + * - cast(1 as int) -> 1. + * Merge cast like + * - cast(cast(1 as bigint) as string) -> cast(1 as string). + */ +public class SimplifyCastRule extends AbstractExpressionRewriteRule { + + public static SimplifyCastRule INSTANCE = new SimplifyCastRule(); + + @Override + public Expression visitCast(Cast origin, ExpressionRewriteContext context) { + return simplify(origin) + .map(simplifiedExpr -> { + if (simplifiedExpr instanceof Cast) { + return rewrite(simplifiedExpr, context); + } + return simplifiedExpr; + }) + .orElse(origin); + } + + private Optional<Expression> simplify(Cast cast) { + Expression source = cast.left(); + StringLiteral type = cast.right(); + + // remove redundant cast + // CAST(value as type), value is type + if (cast.getDataType().equals(source.getDataType())) { + return Optional.of(source); + } + // CAST(CAST()) -> CAST() + if (source instanceof Cast) { + Cast innerCast = (Cast) source; + if (check(innerCast.getDataType(), cast.getDataType()) + && check(innerCast.getDataType(), source.getDataType())) { + return Optional.of(new Cast(innerCast.left(), type)); + } + return simplify(innerCast).map(inner -> + new Cast(inner, type) + ); + } + + return Optional.empty(); + } + + // It after remove redundant cast. so left.class != right.class + // We forbidden subclass of NumericType convert other subclass of NumericType, + // except subclass of IntegralType can mutual conversion. + private boolean check(DataType left, DataType right) { + // int, bigint ... can mutual conversion. + if (left instanceof IntegralType && right instanceof IntegralType) { + return true; + } + + // We forbidden subclass of NumericType convert to other subclass of NumericType, + if (!(left instanceof NumericType) || (!(right instanceof NumericType))) { + return true; + } + + // It after remove redundant cast. so left.class != right.class + // So we don't need to return left.class == right.class + return false; Review Comment: ```suggestion // We forbidden subclass of NumericType convert to other subclass of NumericType, if (left instanceof NumericType && right instanceof NumericType) { return false; } // It after remove redundant cast. so left.class != right.class // So we don't need to return left.class == right.class return true; ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/rules/SimplifyCastRule.java: ########## @@ -0,0 +1,97 @@ +// 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.rewrite.rules; + +import org.apache.doris.nereids.rules.expression.rewrite.AbstractExpressionRewriteRule; +import org.apache.doris.nereids.rules.expression.rewrite.ExpressionRewriteContext; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.StringLiteral; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.IntegralType; +import org.apache.doris.nereids.types.NumericType; + +import java.util.Optional; + + +/** + * Rewrite rule of simplify CAST expression. + * Remove redundant cast like + * - cast(1 as int) -> 1. + * Merge cast like + * - cast(cast(1 as bigint) as string) -> cast(1 as string). + */ +public class SimplifyCastRule extends AbstractExpressionRewriteRule { + + public static SimplifyCastRule INSTANCE = new SimplifyCastRule(); + + @Override + public Expression visitCast(Cast origin, ExpressionRewriteContext context) { + return simplify(origin) + .map(simplifiedExpr -> { + if (simplifiedExpr instanceof Cast) { + return rewrite(simplifiedExpr, context); + } + return simplifiedExpr; + }) + .orElse(origin); + } + + private Optional<Expression> simplify(Cast cast) { + Expression source = cast.left(); + StringLiteral type = cast.right(); + + // remove redundant cast + // CAST(value as type), value is type + if (cast.getDataType().equals(source.getDataType())) { + return Optional.of(source); + } + // CAST(CAST()) -> CAST() + if (source instanceof Cast) { + Cast innerCast = (Cast) source; + if (check(innerCast.getDataType(), cast.getDataType()) + && check(innerCast.getDataType(), source.getDataType())) { + return Optional.of(new Cast(innerCast.left(), type)); + } + return simplify(innerCast).map(inner -> + new Cast(inner, type) + ); + } + + return Optional.empty(); + } + + // It after remove redundant cast. so left.class != right.class + // We forbidden subclass of NumericType convert other subclass of NumericType, + // except subclass of IntegralType can mutual conversion. + private boolean check(DataType left, DataType right) { + // int, bigint ... can mutual conversion. + if (left instanceof IntegralType && right instanceof IntegralType) { + return true; + } + + // We forbidden subclass of NumericType convert to other subclass of NumericType, Review Comment: add some example to explain why forbidden this situation -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org