6868import org .opensearch .sql .data .model .ExprValue ;
6969import org .opensearch .sql .data .type .ExprCoreType ;
7070import org .opensearch .sql .exception .ExpressionEvaluationException ;
71- import org .opensearch .sql .exception .SemanticCheckException ;
7271import org .opensearch .sql .expression .function .BuiltinFunctionName ;
7372import org .opensearch .sql .expression .function .BuiltinFunctionRepository ;
7473import org .opensearch .sql .expression .function .DefaultFunctionResolver ;
@@ -97,6 +96,9 @@ public class DateTimeFunction {
9796 // 32536771199.999999, or equivalent '3001-01-18 23:59:59.999999' UTC
9897 private static final Double MYSQL_MAX_TIMESTAMP = 32536771200d ;
9998
99+ // Mode used for week/week_of_year function by default when no argument is provided
100+ private static final ExprIntegerValue DEFAULT_WEEK_OF_YEAR_MODE = new ExprIntegerValue (0 );
101+
100102 /**
101103 * Register Date and Time Functions.
102104 *
@@ -118,7 +120,8 @@ public void register(BuiltinFunctionRepository repository) {
118120 repository .register (date_sub ());
119121 repository .register (day ());
120122 repository .register (dayName ());
121- repository .register (dayOfMonth ());
123+ repository .register (dayOfMonth (BuiltinFunctionName .DAYOFMONTH ));
124+ repository .register (dayOfMonth (BuiltinFunctionName .DAY_OF_MONTH ));
122125 repository .register (dayOfWeek (BuiltinFunctionName .DAYOFWEEK .getName ()));
123126 repository .register (dayOfWeek (BuiltinFunctionName .DAY_OF_WEEK .getName ()));
124127 repository .register (dayOfYear (BuiltinFunctionName .DAYOFYEAR ));
@@ -453,12 +456,15 @@ private DefaultFunctionResolver dayName() {
453456 /**
454457 * DAYOFMONTH(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31).
455458 */
456- private DefaultFunctionResolver dayOfMonth () {
457- return define (BuiltinFunctionName .DAYOFMONTH .getName (),
459+ private DefaultFunctionResolver dayOfMonth (BuiltinFunctionName name ) {
460+ return define (name .getName (),
461+ implWithProperties (nullMissingHandlingWithProperties (
462+ (functionProperties , arg ) -> DateTimeFunction .dayOfMonthToday (
463+ functionProperties .getQueryStartClock ())), INTEGER , TIME ),
458464 impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , DATE ),
459465 impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , DATETIME ),
460- impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , TIMESTAMP ),
461- impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , STRING )
466+ impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , STRING ),
467+ impl (nullMissingHandling (DateTimeFunction ::exprDayOfMonth ), INTEGER , TIMESTAMP )
462468 );
463469 }
464470
@@ -484,6 +490,9 @@ private DefaultFunctionResolver dayOfWeek(FunctionName name) {
484490 */
485491 private DefaultFunctionResolver dayOfYear (BuiltinFunctionName dayOfYear ) {
486492 return define (dayOfYear .getName (),
493+ implWithProperties (nullMissingHandlingWithProperties ((functionProperties , arg )
494+ -> DateTimeFunction .dayOfYearToday (
495+ functionProperties .getQueryStartClock ())), INTEGER , TIME ),
487496 impl (nullMissingHandling (DateTimeFunction ::exprDayOfYear ), INTEGER , DATE ),
488497 impl (nullMissingHandling (DateTimeFunction ::exprDayOfYear ), INTEGER , DATETIME ),
489498 impl (nullMissingHandling (DateTimeFunction ::exprDayOfYear ), INTEGER , TIMESTAMP ),
@@ -571,6 +580,9 @@ private DefaultFunctionResolver minute_of_day() {
571580 */
572581 private DefaultFunctionResolver month (BuiltinFunctionName month ) {
573582 return define (month .getName (),
583+ implWithProperties (nullMissingHandlingWithProperties ((functionProperties , arg )
584+ -> DateTimeFunction .monthOfYearToday (
585+ functionProperties .getQueryStartClock ())), INTEGER , TIME ),
574586 impl (nullMissingHandling (DateTimeFunction ::exprMonth ), INTEGER , DATE ),
575587 impl (nullMissingHandling (DateTimeFunction ::exprMonth ), INTEGER , DATETIME ),
576588 impl (nullMissingHandling (DateTimeFunction ::exprMonth ), INTEGER , TIMESTAMP ),
@@ -800,10 +812,18 @@ private DefaultFunctionResolver utc_timestamp() {
800812 */
801813 private DefaultFunctionResolver week (BuiltinFunctionName week ) {
802814 return define (week .getName (),
815+ implWithProperties (nullMissingHandlingWithProperties ((functionProperties , arg )
816+ -> DateTimeFunction .weekOfYearToday (
817+ DEFAULT_WEEK_OF_YEAR_MODE ,
818+ functionProperties .getQueryStartClock ())), INTEGER , TIME ),
803819 impl (nullMissingHandling (DateTimeFunction ::exprWeekWithoutMode ), INTEGER , DATE ),
804820 impl (nullMissingHandling (DateTimeFunction ::exprWeekWithoutMode ), INTEGER , DATETIME ),
805821 impl (nullMissingHandling (DateTimeFunction ::exprWeekWithoutMode ), INTEGER , TIMESTAMP ),
806822 impl (nullMissingHandling (DateTimeFunction ::exprWeekWithoutMode ), INTEGER , STRING ),
823+ implWithProperties (nullMissingHandlingWithProperties ((functionProperties , time , modeArg )
824+ -> DateTimeFunction .weekOfYearToday (
825+ modeArg ,
826+ functionProperties .getQueryStartClock ())), INTEGER , TIME , INTEGER ),
807827 impl (nullMissingHandling (DateTimeFunction ::exprWeek ), INTEGER , DATE , INTEGER ),
808828 impl (nullMissingHandling (DateTimeFunction ::exprWeek ), INTEGER , DATETIME , INTEGER ),
809829 impl (nullMissingHandling (DateTimeFunction ::exprWeek ), INTEGER , TIMESTAMP , INTEGER ),
@@ -844,6 +864,20 @@ private DefaultFunctionResolver date_format() {
844864 );
845865 }
846866
867+
868+ private ExprValue dayOfMonthToday (Clock clock ) {
869+ return new ExprIntegerValue (LocalDateTime .now (clock ).getDayOfMonth ());
870+ }
871+
872+ private ExprValue dayOfYearToday (Clock clock ) {
873+ return new ExprIntegerValue (LocalDateTime .now (clock ).getDayOfYear ());
874+ }
875+
876+ private ExprValue weekOfYearToday (ExprValue mode , Clock clock ) {
877+ return new ExprIntegerValue (
878+ CalendarLookup .getWeekNumber (mode .integerValue (), LocalDateTime .now (clock ).toLocalDate ()));
879+ }
880+
847881 /**
848882 * Day of Week implementation for ExprValue when passing in an arguemt of type TIME.
849883 *
@@ -1085,7 +1119,7 @@ private ExprValue exprDayName(ExprValue date) {
10851119 /**
10861120 * Day of Month implementation for ExprValue.
10871121 *
1088- * @param date ExprValue of Date/String type.
1122+ * @param date ExprValue of Date/Datetime/ String/Time/Timestamp type.
10891123 * @return ExprValue.
10901124 */
10911125 private ExprValue exprDayOfMonth (ExprValue date ) {
@@ -1572,6 +1606,10 @@ private ExprValue exprYear(ExprValue date) {
15721606 return new ExprIntegerValue (date .dateValue ().getYear ());
15731607 }
15741608
1609+ private ExprValue monthOfYearToday (Clock clock ) {
1610+ return new ExprIntegerValue (LocalDateTime .now (clock ).getMonthValue ());
1611+ }
1612+
15751613 private LocalDateTime formatNow (Clock clock ) {
15761614 return formatNow (clock , 0 );
15771615 }
0 commit comments