morrySnow commented on code in PR #9942: URL: https://github.com/apache/incubator-doris/pull/9942#discussion_r894323373
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/RewriteHelper.java: ########## @@ -0,0 +1,57 @@ +// 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; + +import org.apache.doris.nereids.analyzer.UnboundSlot; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Literal; + +import com.google.common.collect.Lists; + +/** + * Expression rewrite helper class. + */ +public class RewriteHelper { + + public static boolean isConstant(Expression expr) { + return expr.isConstant(); + } + + public static Expression convertLiteralToSlot(Expression expr) { + ExpressionRewriter rewriter = new ExpressionRewriter(ConvertLiteralToSlotRule.INSTANCE); + return rewriter.rewrite(expr); + } + + /** + * For test. + * convert literal to slot Review Comment: if just for test, it's better move it to test folder ########## fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rewrite/ExpressionRewriteTest.java: ########## @@ -0,0 +1,79 @@ +// 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; + +import org.apache.doris.catalog.Database; +import org.apache.doris.nereids.parser.SqlParser; +import org.apache.doris.nereids.rules.expression.rewrite.rules.NormalizeExpressionRule; +import org.apache.doris.nereids.rules.expression.rewrite.rules.SimplifyNotExprRule; +import org.apache.doris.nereids.trees.expressions.Expression; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Assert; +import org.junit.Test; + +/** + * all expr rewrite rule test case. + */ +public class ExpressionRewriteTest { + private static final Logger LOG = LogManager.getLogger(Database.class); + + private static final SqlParser PARSER = new SqlParser(); + private ExpressionRewriter rewriter; + + @Test + public void testNotExpressionRewrite() { + rewriter = new ExpressionRewriter(SimplifyNotExprRule.INSTANCE); + + assertRewrite("NOT x > y", "x <= y"); + assertRewrite("NOT x < y", "x >= y"); + assertRewrite("NOT x >= y", "x < y"); + assertRewrite("NOT x <= y", "x > y"); + assertRewrite("NOT x = y", "NOT x = y"); + assertRewrite("NOT NOT x > y", "x > y"); + assertRewrite("NOT NOT NOT x > y", "x <= y"); + } + + @Test + public void testNormalizeExpressionRewrite() { + rewriter = new ExpressionRewriter(NormalizeExpressionRule.INSTANCE); + + assertRewrite("2 > x", "x < 2"); + assertRewrite("2 >= x", "x <= 2"); + assertRewrite("2 < x", "x > 2"); + assertRewrite("2 <= x", "x >= 2"); + assertRewrite("2 = x", "x = 2"); + /* Review Comment: remove it if unused -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
