Skip to content

Commit 6e72f18

Browse files
Add Support For TIME Type in "*_OF_YEAR" Functions (#199) (opensearch-project#1223)
Added Support And Tests For Time Type in day_of_year, week_of_year, month_of_year Functions Signed-off-by: GabeFernandez310 <[email protected]>
1 parent dc5578a commit 6e72f18

File tree

7 files changed

+461
-208
lines changed

7 files changed

+461
-208
lines changed

core/src/main/java/org/opensearch/sql/expression/DSL.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,9 @@ public static FunctionExpression dayofyear(Expression... expressions) {
335335
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFYEAR, expressions);
336336
}
337337

338-
public static FunctionExpression day_of_year(Expression... expressions) {
339-
return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions);
338+
public static FunctionExpression day_of_year(
339+
FunctionProperties functionProperties, Expression... expressions) {
340+
return compile(functionProperties, BuiltinFunctionName.DAY_OF_YEAR, expressions);
340341
}
341342

342343
public static FunctionExpression day_of_week(
@@ -372,8 +373,9 @@ public static FunctionExpression month(Expression... expressions) {
372373
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH, expressions);
373374
}
374375

375-
public static FunctionExpression month_of_year(Expression... expressions) {
376-
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
376+
public static FunctionExpression month_of_year(
377+
FunctionProperties functionProperties, Expression... expressions) {
378+
return compile(functionProperties, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
377379
}
378380

379381
public static FunctionExpression monthname(Expression... expressions) {
@@ -416,12 +418,14 @@ public static FunctionExpression to_days(Expression... expressions) {
416418
return compile(FunctionProperties.None, BuiltinFunctionName.TO_DAYS, expressions);
417419
}
418420

419-
public static FunctionExpression week(Expression... expressions) {
420-
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK, expressions);
421+
public static FunctionExpression week(
422+
FunctionProperties functionProperties, Expression... expressions) {
423+
return compile(functionProperties, BuiltinFunctionName.WEEK, expressions);
421424
}
422425

423-
public static FunctionExpression week_of_year(Expression... expressions) {
424-
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
426+
public static FunctionExpression week_of_year(
427+
FunctionProperties functionProperties, Expression... expressions) {
428+
return compile(functionProperties, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
425429
}
426430

427431
public static FunctionExpression year(Expression... expressions) {

core/src/main/java/org/opensearch/sql/expression/datetime/DateTimeFunction.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public class DateTimeFunction {
8989
// 32536771199.999999, or equivalent '3001-01-18 23:59:59.999999' UTC
9090
private static final Double MYSQL_MAX_TIMESTAMP = 32536771200d;
9191

92+
// Mode used for week/week_of_year function by default when no argument is provided
93+
private static final ExprIntegerValue DEFAULT_WEEK_OF_YEAR_MODE = new ExprIntegerValue(0);
94+
9295
/**
9396
* Register Date and Time Functions.
9497
*
@@ -472,6 +475,9 @@ private DefaultFunctionResolver dayOfWeek(FunctionName name) {
472475
*/
473476
private DefaultFunctionResolver dayOfYear(BuiltinFunctionName dayOfYear) {
474477
return define(dayOfYear.getName(),
478+
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg)
479+
-> DateTimeFunction.dayOfYearToday(
480+
functionProperties.getQueryStartClock())), INTEGER, TIME),
475481
impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATE),
476482
impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATETIME),
477483
impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, TIMESTAMP),
@@ -559,6 +565,9 @@ private DefaultFunctionResolver minute_of_day() {
559565
*/
560566
private DefaultFunctionResolver month(BuiltinFunctionName month) {
561567
return define(month.getName(),
568+
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg)
569+
-> DateTimeFunction.monthOfYearToday(
570+
functionProperties.getQueryStartClock())), INTEGER, TIME),
562571
impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATE),
563572
impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATETIME),
564573
impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, TIMESTAMP),
@@ -783,10 +792,18 @@ private DefaultFunctionResolver utc_timestamp() {
783792
*/
784793
private DefaultFunctionResolver week(BuiltinFunctionName week) {
785794
return define(week.getName(),
795+
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg)
796+
-> DateTimeFunction.weekOfYearToday(
797+
DEFAULT_WEEK_OF_YEAR_MODE,
798+
functionProperties.getQueryStartClock())), INTEGER, TIME),
786799
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATE),
787800
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATETIME),
788801
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, TIMESTAMP),
789802
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, STRING),
803+
implWithProperties(nullMissingHandlingWithProperties((functionProperties, time, modeArg)
804+
-> DateTimeFunction.weekOfYearToday(
805+
modeArg,
806+
functionProperties.getQueryStartClock())), INTEGER, TIME, INTEGER),
790807
impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATE, INTEGER),
791808
impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATETIME, INTEGER),
792809
impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, TIMESTAMP, INTEGER),
@@ -827,6 +844,15 @@ private DefaultFunctionResolver date_format() {
827844
);
828845
}
829846

847+
private ExprValue dayOfYearToday(Clock clock) {
848+
return new ExprIntegerValue(LocalDateTime.now(clock).getDayOfYear());
849+
}
850+
851+
private ExprValue weekOfYearToday(ExprValue mode, Clock clock) {
852+
return new ExprIntegerValue(
853+
CalendarLookup.getWeekNumber(mode.integerValue(), LocalDateTime.now(clock).toLocalDate()));
854+
}
855+
830856
/**
831857
* Day of Week implementation for ExprValue when passing in an arguemt of type TIME.
832858
*
@@ -1519,6 +1545,10 @@ private ExprValue exprYear(ExprValue date) {
15191545
return new ExprIntegerValue(date.dateValue().getYear());
15201546
}
15211547

1548+
private ExprValue monthOfYearToday(Clock clock) {
1549+
return new ExprIntegerValue(LocalDateTime.now(clock).getMonthValue());
1550+
}
1551+
15221552
private LocalDateTime formatNow(Clock clock) {
15231553
return formatNow(clock, 0);
15241554
}

core/src/main/java/org/opensearch/sql/expression/function/FunctionDSL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ public String toString() {
141141
* Implementation of a function that takes two arguments, returns a value, and
142142
* requires FunctionProperties to complete.
143143
*
144-
* @param function {@link ExprValue} based unary function.
144+
* @param function {@link ExprValue} based Binary function.
145145
* @param returnType return type.
146146
* @param args1Type first argument type.
147147
* @param args2Type second argument type.
148-
* @return Unary Function Implementation.
148+
* @return Binary Function Implementation.
149149
*/
150150
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>>
151151
implWithProperties(

0 commit comments

Comments
 (0)