Skip to content

Commit c3ad7c9

Browse files
committed
all
1 parent 5aee624 commit c3ad7c9

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,22 @@ public static Expression microsecond(DateTimeV2Literal date) {
214214
*/
215215
@ExecFunction(name = "dayofyear", argTypes = {"DATE"}, returnType = "SMALLINT")
216216
public static Expression dayOfYear(DateLiteral date) {
217-
return new SmallIntLiteral((short) date.toJavaDateType().getDayOfYear());
217+
return new SmallIntLiteral((short) date.getDayOfYear());
218218
}
219219

220220
@ExecFunction(name = "dayofyear", argTypes = {"DATETIME"}, returnType = "SMALLINT")
221221
public static Expression dayOfYear(DateTimeLiteral date) {
222-
return new SmallIntLiteral((short) date.toJavaDateType().getDayOfYear());
222+
return new SmallIntLiteral((short) date.getDayOfYear());
223223
}
224224

225225
@ExecFunction(name = "dayofyear", argTypes = {"DATEV2"}, returnType = "SMALLINT")
226226
public static Expression dayOfYear(DateV2Literal date) {
227-
return new SmallIntLiteral((short) date.toJavaDateType().getDayOfYear());
227+
return new SmallIntLiteral((short) date.getDayOfYear());
228228
}
229229

230230
@ExecFunction(name = "dayofyear", argTypes = {"DATETIMEV2"}, returnType = "SMALLINT")
231231
public static Expression dayOfYear(DateTimeV2Literal date) {
232-
return new SmallIntLiteral((short) date.toJavaDateType().getDayOfYear());
232+
return new SmallIntLiteral((short) date.getDayOfYear());
233233
}
234234

235235
/**
@@ -260,22 +260,22 @@ public static Expression dayOfMonth(DateTimeV2Literal date) {
260260
*/
261261
@ExecFunction(name = "dayofweek", argTypes = {"DATE"}, returnType = "TINYINT")
262262
public static Expression dayOfWeek(DateLiteral date) {
263-
return new TinyIntLiteral((byte) (date.toJavaDateType().getDayOfWeek().getValue() % 7 + 1));
263+
return new TinyIntLiteral((byte) (date.getDayOfWeek() % 7 + 1));
264264
}
265265

266266
@ExecFunction(name = "dayofweek", argTypes = {"DATETIME"}, returnType = "TINYINT")
267267
public static Expression dayOfWeek(DateTimeLiteral date) {
268-
return new TinyIntLiteral((byte) (date.toJavaDateType().getDayOfWeek().getValue() % 7 + 1));
268+
return new TinyIntLiteral((byte) (date.getDayOfWeek() % 7 + 1));
269269
}
270270

271271
@ExecFunction(name = "dayofweek", argTypes = {"DATEV2"}, returnType = "TINYINT")
272272
public static Expression dayOfWeek(DateV2Literal date) {
273-
return new TinyIntLiteral((byte) (date.toJavaDateType().getDayOfWeek().getValue() % 7 + 1));
273+
return new TinyIntLiteral((byte) (date.getDayOfWeek() % 7 + 1));
274274
}
275275

276276
@ExecFunction(name = "dayofweek", argTypes = {"DATETIMEV2"}, returnType = "TINYINT")
277277
public static Expression dayOfWeek(DateTimeV2Literal date) {
278-
return new TinyIntLiteral((byte) (date.toJavaDateType().getDayOfWeek().getValue() % 7 + 1));
278+
return new TinyIntLiteral((byte) (date.getDayOfWeek() % 7 + 1));
279279
}
280280

281281
private static int distanceToFirstDayOfWeek(LocalDateTime dateTime) {
@@ -853,18 +853,53 @@ public static Expression yearWeek(LocalDateTime localDateTime, int mode) {
853853
}
854854
}
855855

856+
/**
857+
*
858+
* @param dateTime
859+
* @return
860+
*/
856861
@ExecFunction(name = "weekofyear", argTypes = {"DATETIMEV2"}, returnType = "TINYINT")
857862
public static Expression weekOfYear(DateTimeV2Literal dateTime) {
863+
if (dateTime.getYear() == 0 && dateTime.getDayOfWeek() == 1) {
864+
if (dateTime.getMonth() == 1 && dateTime.getDay() == 2) {
865+
return new TinyIntLiteral((byte) 1);
866+
}
867+
return new TinyIntLiteral(
868+
(byte) (dateTime.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()) + 1));
869+
}
858870
return new TinyIntLiteral((byte) dateTime.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()));
859871
}
860872

873+
/**
874+
*
875+
* @param dateTime
876+
* @return
877+
*/
861878
@ExecFunction(name = "weekofyear", argTypes = {"DATETIME"}, returnType = "TINYINT")
862879
public static Expression weekOfYear(DateTimeLiteral dateTime) {
880+
if (dateTime.getYear() == 0 && dateTime.getDayOfWeek() == 1) {
881+
if (dateTime.getMonth() == 1 && dateTime.getDay() == 2) {
882+
return new TinyIntLiteral((byte) 1);
883+
}
884+
return new TinyIntLiteral(
885+
(byte) (dateTime.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()) + 1));
886+
}
863887
return new TinyIntLiteral((byte) dateTime.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()));
864888
}
865889

890+
/**
891+
*
892+
* @param date
893+
* @return
894+
*/
866895
@ExecFunction(name = "weekofyear", argTypes = {"DATEV2"}, returnType = "TINYINT")
867896
public static Expression weekOfYear(DateV2Literal date) {
897+
if (date.getYear() == 0 && date.getDayOfWeek() == 1) {
898+
if (date.getMonth() == 1 && date.getDay() == 2) {
899+
return new TinyIntLiteral((byte) 1);
900+
}
901+
return new TinyIntLiteral((byte) (date.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()) + 1));
902+
}
868903
return new TinyIntLiteral((byte) date.toJavaDateType().get(WeekFields.ISO.weekOfWeekBasedYear()));
869904
}
870905

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/literal/DateLiteral.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ public long getDay() {
417417
return day;
418418
}
419419

420+
public int getDayOfYear() {
421+
if (year == 0 && month == 3 && (day == 1 || day == 2)) {
422+
return toJavaDateType().getDayOfYear() - 1;
423+
}
424+
return toJavaDateType().getDayOfYear();
425+
}
426+
427+
public int getDayOfWeek() {
428+
if (year == 0 && (month == 1 || (month == 2 && day <= 28))) {
429+
// shift right with 1 offset
430+
return toJavaDateType().getDayOfWeek().getValue() % 7 + 1;
431+
}
432+
return toJavaDateType().getDayOfWeek().getValue();
433+
}
434+
420435
public Expression plusDays(long days) {
421436
return fromJavaDateType(toJavaDateType().plusDays(days));
422437
}

regression-test/data/correctness_p0/test_cast_date_decimal.out

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ true
1515
2012-03-12
1616

1717
-- !sql6 --
18-
\N
18+
\N \N \N \N
19+
20+
-- !sql7 --
21+
\N \N \N \N
1922

regression-test/suites/correctness_p0/test_cast_date_decimal.groovy

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ suite("test_cast_date_decimal") {
3737
"""
3838

3939
qt_sql6 """
40-
select cast('0000-02-29' as date);
40+
select cast('0000-02-29' as date), cast('0000-02-29' as datetime), cast('00000229' as date), cast('0000-02-29 12:12:12.123' as datetime);
41+
"""
42+
43+
qt_sql7 """
44+
select /*+SET_VAR(debug_skip_fold_constant=true)*/ cast('0000-02-29' as date), cast('0000-02-29' as datetime), cast('00000229' as date), cast('0000-02-29 12:12:12.123' as datetime);
4145
"""
4246
}

0 commit comments

Comments
 (0)