@@ -324,6 +324,41 @@ public static DateLiteral timestamp(LiteralExpr arg) throws AnalysisException {
324324 return null ;
325325 }
326326
327+ @ FEFunction (name = "to_monday" , argTypes = {"DATETIME" }, returnType = "DATE" )
328+ public static DateLiteral toMonday (LiteralExpr arg ) {
329+ if (arg instanceof DateLiteral && (arg .getType ().isDate () || arg .getType ().isDatetime ())) {
330+ DateLiteral dateLiteral = ((DateLiteral ) arg );
331+ LocalDateTime dateTime = LocalDateTime .of (
332+ ((int ) dateLiteral .getYear ()), ((int ) dateLiteral .getMonth ()), ((int ) dateLiteral .getDay ()),
333+ 0 , 0 , 0 );
334+ dateTime = toMonday (dateTime );
335+ return new DateLiteral (dateTime .getYear (), dateTime .getMonthValue (), dateTime .getDayOfMonth (), Type .DATE );
336+ }
337+ return null ;
338+ }
339+
340+ @ FEFunction (name = "to_monday" , argTypes = {"DATETIMEV2" }, returnType = "DATEV2" )
341+ public static DateLiteral toMondayV2 (LiteralExpr arg ) {
342+ if (arg instanceof DateLiteral && (arg .getType ().isDateV2 () || arg .getType ().isDatetimeV2 ())) {
343+ DateLiteral dateLiteral = ((DateLiteral ) arg );
344+ LocalDateTime dateTime = LocalDateTime .of (
345+ ((int ) dateLiteral .getYear ()), ((int ) dateLiteral .getMonth ()), ((int ) dateLiteral .getDay ()),
346+ 0 , 0 , 0 );
347+ dateTime = toMonday (dateTime );
348+ return new DateLiteral (dateTime .getYear (), dateTime .getMonthValue (), dateTime .getDayOfMonth (), Type .DATEV2 );
349+ }
350+ return null ;
351+ }
352+
353+ private static LocalDateTime toMonday (LocalDateTime dateTime ) {
354+ LocalDateTime specialUpperBound = LocalDateTime .of (1970 , 1 , 4 , 0 , 0 , 0 );
355+ LocalDateTime specialLowerBound = LocalDateTime .of (1970 , 1 , 1 , 0 , 0 , 0 );
356+ if (dateTime .isAfter (specialUpperBound ) || dateTime .isBefore (specialLowerBound )) {
357+ return dateTime .plusDays (-dateTime .getDayOfWeek ().getValue () + 1 );
358+ }
359+ return specialLowerBound ;
360+ }
361+
327362 /**
328363 ------------------------------------------------------------------------------
329364 */
0 commit comments