Skip to content

Commit 866f1e7

Browse files
branch-4.0: [Enhancement](func) Add fold constant for PREVIOUS_DAY #60755 (#60762)
Cherry-picked from #60755 Co-authored-by: linrrarity <[email protected]>
1 parent 9817326 commit 866f1e7

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,20 @@ public static Expression nextDay(DateV2Literal date, StringLiteral day) {
12891289
return date.plusDays(daysToAdd);
12901290
}
12911291

1292+
/**
1293+
* date arithmetic function previous_day
1294+
*/
1295+
@ExecFunction(name = "previous_day")
1296+
public static Expression previousDay(DateV2Literal date, StringLiteral day) {
1297+
int dayOfWeek = getDayOfWeek(day.getValue());
1298+
if (dayOfWeek == 0) {
1299+
throw new RuntimeException("Invalid day of week: " + day.getValue());
1300+
}
1301+
int daysToSub = (date.getDayOfWeek() - dayOfWeek + 7) % 7;
1302+
daysToSub = daysToSub == 0 ? 7 : daysToSub;
1303+
return date.plusDays(-daysToSub);
1304+
}
1305+
12921306
/**
12931307
* date transform function sec_to_time
12941308
*/

0 commit comments

Comments
 (0)