-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Fix](nereids) Fix nullable date literal binding and datediff folding #64127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import org.apache.doris.nereids.trees.expressions.ExecFunction; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal; | ||
| import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral; | ||
|
|
@@ -29,8 +30,6 @@ | |
| import org.apache.doris.nereids.trees.expressions.literal.TimestampTzLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.temporal.ChronoUnit; | ||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
|
|
@@ -879,26 +878,31 @@ public static Expression milliSecondsSub(TimestampTzLiteral date, BigIntLiteral | |
| */ | ||
| @ExecFunction(name = "datediff") | ||
| public static Expression dateDiff(DateV2Literal date1, DateV2Literal date2) { | ||
| return new IntegerLiteral(dateDiff(date1.toJavaDateType(), date2.toJavaDateType())); | ||
| return dateDiff((DateLiteral) date1, date2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "datediff") | ||
| public static Expression dateDiff(DateV2Literal date1, DateTimeV2Literal date2) { | ||
| return new IntegerLiteral(dateDiff(date1.toJavaDateType(), date2.toJavaDateType())); | ||
| return dateDiff((DateLiteral) date1, date2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "datediff") | ||
| public static Expression dateDiff(DateTimeV2Literal date1, DateV2Literal date2) { | ||
| return new IntegerLiteral(dateDiff(date1.toJavaDateType(), date2.toJavaDateType())); | ||
| return dateDiff((DateLiteral) date1, date2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "datediff") | ||
| public static Expression dateDiff(DateTimeV2Literal date1, DateTimeV2Literal date2) { | ||
| return new IntegerLiteral(dateDiff(date1.toJavaDateType(), date2.toJavaDateType())); | ||
| return dateDiff((DateLiteral) date1, date2); | ||
| } | ||
|
|
||
| private static int dateDiff(LocalDateTime date1, LocalDateTime date2) { | ||
| return ((int) ChronoUnit.DAYS.between(date2.toLocalDate(), date1.toLocalDate())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以前的错误在哪?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ChronoUnit 认为0000 年 2 月是闰年,但是我们内部的行为应该是认为0000年是平年, 所以常量折叠如果覆盖到了 0000.2, 就会多一天出来 |
||
| @ExecFunction(name = "datediff") | ||
| public static Expression dateDiff(TimestampTzLiteral date1, TimestampTzLiteral date2) { | ||
| return dateDiff((DateLiteral) date1, date2); | ||
| } | ||
|
|
||
| private static Expression dateDiff(DateLiteral date1, DateLiteral date2) { | ||
| return new IntegerLiteral((int) DateV2Literal.dateDiffInDays(date1, date2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "to_days") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| import org.apache.doris.nereids.trees.expressions.functions.scalar.FromSecond; | ||
| import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateTimeLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateTimeV2Literal; | ||
| import org.apache.doris.nereids.trees.expressions.literal.DateV2Literal; | ||
|
|
@@ -38,6 +39,7 @@ | |
| import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.StringLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.TimeV2Literal; | ||
| import org.apache.doris.nereids.trees.expressions.literal.TimestampTzLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral; | ||
| import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; | ||
| import org.apache.doris.nereids.types.DateTimeV2Type; | ||
|
|
@@ -1109,119 +1111,167 @@ private static Expression fromMicroSecond(long microSecond, int scale) { | |
|
|
||
| @ExecFunction(name = "microseconds_diff") | ||
| public static Expression microsecondsDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MICROS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInMicroSeconds(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "microseconds_diff") | ||
| public static Expression microsecondsDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInMicroSeconds(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "milliseconds_diff") | ||
| public static Expression millisecondsDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MILLIS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInMicroSeconds(t1, t2) / 1000L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "milliseconds_diff") | ||
| public static Expression millisecondsDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInMicroSeconds(t1, t2) / 1000L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "seconds_diff") | ||
| public static Expression secondsDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.SECONDS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "seconds_diff") | ||
| public static Expression secondsDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "seconds_diff") | ||
| public static Expression secondsDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.SECONDS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "seconds_diff") | ||
| public static Expression secondsDiff(DateV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.SECONDS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "seconds_diff") | ||
| public static Expression secondsDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.SECONDS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "minutes_diff") | ||
| public static Expression minutesDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MINUTES.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "minutes_diff") | ||
| public static Expression minutesDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "minutes_diff") | ||
| public static Expression minutesDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MINUTES.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "minutes_diff") | ||
| public static Expression minutesDiff(DateV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MINUTES.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "minutes_diff") | ||
| public static Expression minutesDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MINUTES.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "hours_diff") | ||
| public static Expression hoursDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.HOURS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "hours_diff") | ||
| public static Expression hoursDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "hours_diff") | ||
| public static Expression hoursDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.HOURS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "hours_diff") | ||
| public static Expression hoursDiff(DateV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.HOURS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L / 60L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "hours_diff") | ||
| public static Expression hoursDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.HOURS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return new BigIntLiteral(DateTimeV2Literal.datetimeDiffInSecondsRoundToZeroByMicroSecond(t1, t2) / 60L / 60L); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes |
||
|
|
||
| @ExecFunction(name = "days_diff") | ||
| public static Expression daysDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.DAYS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return daysDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "days_diff") | ||
| public static Expression daysDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return daysDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "days_diff") | ||
| public static Expression daysDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.DAYS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return daysDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "days_diff") | ||
| public static Expression daysDiff(DateV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.DAYS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return daysDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "days_diff") | ||
| public static Expression daysDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.DAYS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return daysDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| private static Expression daysDiff(DateLiteral t1, DateLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.dateDiffInDaysRoundToZeroByTime(t1, t2)); | ||
| } | ||
|
|
||
| @ExecFunction(name = "weeks_diff") | ||
| public static Expression weeksDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.WEEKS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return weeksDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "weeks_diff") | ||
| public static Expression weeksDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return weeksDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "weeks_diff") | ||
| public static Expression weeksDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.WEEKS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return weeksDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "weeks_diff") | ||
| public static Expression weeksDiff(DateV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.WEEKS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return weeksDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| @ExecFunction(name = "weeks_diff") | ||
| public static Expression weeksDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.WEEKS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| return weeksDiff((DateLiteral) t1, t2); | ||
| } | ||
|
|
||
| private static Expression weeksDiff(DateLiteral t1, DateLiteral t2) { | ||
| return new BigIntLiteral(DateTimeV2Literal.dateDiffInDaysRoundToZeroByTime(t1, t2) / 7L); | ||
| } | ||
|
|
||
| @ExecFunction(name = "months_diff") | ||
| public static Expression monthsDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| } | ||
|
|
||
| @ExecFunction(name = "months_diff") | ||
| public static Expression monthsDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| } | ||
|
|
||
| @ExecFunction(name = "months_diff") | ||
| public static Expression monthsDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
|
|
@@ -1242,6 +1292,11 @@ public static Expression quartersDiff(DateTimeV2Literal t1, DateTimeV2Literal t2 | |
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType()) / 3); | ||
| } | ||
|
|
||
| @ExecFunction(name = "quarters_diff") | ||
| public static Expression quartersDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType()) / 3); | ||
| } | ||
|
|
||
| @ExecFunction(name = "quarters_diff") | ||
| public static Expression quartersDiff(DateV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.MONTHS.between(t2.toJavaDateType(), t1.toJavaDateType()) / 3); | ||
|
|
@@ -1252,6 +1307,11 @@ public static Expression yearsDiff(DateTimeV2Literal t1, DateTimeV2Literal t2) { | |
| return new BigIntLiteral(ChronoUnit.YEARS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| } | ||
|
|
||
| @ExecFunction(name = "years_diff") | ||
| public static Expression yearsDiff(TimestampTzLiteral t1, TimestampTzLiteral t2) { | ||
| return new BigIntLiteral(ChronoUnit.YEARS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
| } | ||
|
|
||
| @ExecFunction(name = "years_diff") | ||
| public static Expression yearsDiff(DateTimeV2Literal t1, DateV2Literal t2) { | ||
| return new BigIntLiteral(ChronoUnit.YEARS.between(t2.toJavaDateType(), t1.toJavaDateType())); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个得优化器看下