Skip to content

Commit

Permalink
[fix](nereids) fix months_add/ months_sub/ years_add/years_sub comput…
Browse files Browse the repository at this point in the history
…e wrong result because SimplifyArithmeticComparisonRule (apache#44725)

fix months_add/ months_sub/ years_add/years_sub compute wrong result
because SimplifyArithmeticComparisonRule
introduced by apache#25180
  • Loading branch information
924060929 authored Nov 28, 2024
1 parent d77bfa0 commit 1ae91b8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MonthsAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.MonthsSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.WeeksSub;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearsAdd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.YearsSub;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.util.TypeCoercionUtils;
Expand All @@ -66,10 +62,9 @@ public class SimplifyArithmeticComparisonRule implements ExpressionPatternRuleFa
.put(Add.class, Subtract.class)
.put(Subtract.class, Add.class)
.put(Divide.class, Multiply.class)
.put(YearsSub.class, YearsAdd.class)
.put(YearsAdd.class, YearsSub.class)
.put(MonthsSub.class, MonthsAdd.class)
.put(MonthsAdd.class, MonthsSub.class)
// ATTN: YearsAdd, MonthsAdd can not reverse
// for example, months_add(date '2024-01-31', 1) = date '2024-02-29' can not reverse to
// date '2024-01-31' = months_sub(date '2024-02-29', 1)
.put(WeeksSub.class, WeeksAdd.class)
.put(WeeksAdd.class, WeeksSub.class)
.put(DaysSub.class, DaysAdd.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ void testSimplifyDateTimeComparison() {
FoldConstantRule.INSTANCE
)
));
assertRewriteAfterTypeCoercion("years_add(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2020-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("years_sub(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2022-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("months_add(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2020-12-01 00:00:00')");
assertRewriteAfterTypeCoercion("months_sub(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2021-02-01 00:00:00')");
assertRewriteAfterTypeCoercion("years_add(IA, 1) > '2021-01-01 00:00:00'", "(years_add(cast(IA as DATETIMEV2(0)), 1) > '2021-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("years_sub(IA, 1) > '2021-01-01 00:00:00'", "(years_sub(cast(IA as DATETIMEV2(0)), 1) > '2021-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("months_add(IA, 1) > '2021-01-01 00:00:00'", "(months_add(cast(IA as DATETIMEV2(0)), 1) > '2021-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("months_sub(IA, 1) > '2021-01-01 00:00:00'", "(months_sub(cast(IA as DATETIMEV2(0)), 1) > '2021-01-01 00:00:00')");
assertRewriteAfterTypeCoercion("weeks_add(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2020-12-25 00:00:00')");
assertRewriteAfterTypeCoercion("weeks_sub(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2021-01-08 00:00:00')");
assertRewriteAfterTypeCoercion("days_add(IA, 1) > '2021-01-01 00:00:00'", "(cast(IA as DATETIMEV2(0)) > '2020-12-31 00:00:00')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,15 @@ suite("test_date_function") {


qt_sql_time_value """ select cast(4562632 as time), hour(cast(4562632 as time)) , minute(cast(4562632 as time)) , second(cast(4562632 as time)); """

def test_simplify = {
test {
sql "select months_add(dt, 1) = date '2024-02-29' from (select date '2024-01-31' as dt)a"
result([[true]])
}
test {
sql "select years_add(dt, 1) = date '2025-02-28' from (select date '2024-02-29' as dt)a"
result([[true]])
}
}()
}
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,15 @@ suite("test_date_function") {
qt_sql_varchar1 """ select dt, fmt, unix_timestamp(dt, fmt) as k1 from date_varchar order by k1,dt,fmt; """
qt_sql_varchar1 """ select dt, unix_timestamp(dt, "%Y-%m-%d") as k1 from date_varchar order by k1,dt,fmt; """
qt_sql_varchar1 """ select fmt, unix_timestamp("1990-12-12", fmt) as k1 from date_varchar order by k1,dt,fmt; """

def test_simplify = {
test {
sql "select months_add(dt, 1) = date '2024-02-29' from (select date '2024-01-31' as dt)a"
result([[true]])
}
test {
sql "select years_add(dt, 1) = date '2025-02-28' from (select date '2024-02-29' as dt)a"
result([[true]])
}
}()
}

0 comments on commit 1ae91b8

Please sign in to comment.