Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,9 @@ public static FunctionExpression dayofyear(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFYEAR, expressions);
}

public static FunctionExpression day_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions);
public static FunctionExpression day_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.DAY_OF_YEAR, expressions);
}

public static FunctionExpression from_days(Expression... expressions) {
Expand All @@ -358,8 +359,9 @@ public static FunctionExpression month(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH, expressions);
}

public static FunctionExpression month_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
public static FunctionExpression month_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.MONTH_OF_YEAR, expressions);
}

public static FunctionExpression monthname(Expression... expressions) {
Expand Down Expand Up @@ -398,12 +400,14 @@ public static FunctionExpression to_days(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.TO_DAYS, expressions);
}

public static FunctionExpression week(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK, expressions);
public static FunctionExpression week(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEK, expressions);
}

public static FunctionExpression week_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
public static FunctionExpression week_of_year(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEK_OF_YEAR, expressions);
}

public static FunctionExpression year(Expression... expressions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public class DateTimeFunction {
// 32536771199.999999, or equivalent '3001-01-18 23:59:59.999999' UTC
private static final Double MYSQL_MAX_TIMESTAMP = 32536771200d;

// Mode used for week/week_of_year function by default when no argument is provided
private static final ExprIntegerValue DEFAULT_WEEK_OF_YEAR_MODE = new ExprIntegerValue(0);

/**
* Register Date and Time Functions.
*
Expand Down Expand Up @@ -368,6 +371,8 @@ private DefaultFunctionResolver dayOfWeek() {
*/
private DefaultFunctionResolver dayOfYear(BuiltinFunctionName dayOfYear) {
return define(dayOfYear.getName(),
implWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfYearToday(
functionProperties.getQueryStartClock()), INTEGER, TIME),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfYearToday(
functionProperties.getQueryStartClock()), INTEGER, TIME),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) -> DateTimeFunction.dayOfYearToday(
functionProperties.getQueryStartClock())), INTEGER, TIME),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6465ec8

impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprDayOfYear), INTEGER, TIMESTAMP),
Expand Down Expand Up @@ -441,6 +446,8 @@ private DefaultFunctionResolver minute() {
*/
private DefaultFunctionResolver month(BuiltinFunctionName month) {
return define(month.getName(),
implWithProperties((functionProperties, arg) -> DateTimeFunction.monthOfYearToday(
functionProperties.getQueryStartClock()), INTEGER, TIME),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implWithProperties((functionProperties, arg) -> DateTimeFunction.monthOfYearToday(
functionProperties.getQueryStartClock()), INTEGER, TIME),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) -> DateTimeFunction.monthOfYearToday(
functionProperties.getQueryStartClock())), INTEGER, TIME),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6465ec8

impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprMonth), INTEGER, TIMESTAMP),
Expand Down Expand Up @@ -602,10 +609,16 @@ private DefaultFunctionResolver utc_timestamp() {
*/
private DefaultFunctionResolver week(BuiltinFunctionName week) {
return define(week.getName(),
implWithProperties((functionProperties, arg) -> DateTimeFunction.weekOfYearToday(
DEFAULT_WEEK_OF_YEAR_MODE,
functionProperties.getQueryStartClock()), INTEGER, TIME),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implWithProperties((functionProperties, arg) -> DateTimeFunction.weekOfYearToday(
DEFAULT_WEEK_OF_YEAR_MODE,
functionProperties.getQueryStartClock()), INTEGER, TIME),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, arg) -> DateTimeFunction.weekOfYearToday(
DEFAULT_WEEK_OF_YEAR_MODE,
functionProperties.getQueryStartClock())), INTEGER, TIME),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6465ec8

impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, TIMESTAMP),
impl(nullMissingHandling(DateTimeFunction::exprWeekWithoutMode), INTEGER, STRING),
implWithProperties((functionProperties, time, modeArg) -> DateTimeFunction.weekOfYearToday(
modeArg,
functionProperties.getQueryStartClock()), INTEGER, TIME, INTEGER),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
implWithProperties((functionProperties, time, modeArg) -> DateTimeFunction.weekOfYearToday(
modeArg,
functionProperties.getQueryStartClock()), INTEGER, TIME, INTEGER),
implWithProperties(nullMissingHandlingWithProperties((functionProperties, time, modeArg) -> DateTimeFunction.weekOfYearToday(
modeArg,
functionProperties.getQueryStartClock())), INTEGER, TIME, INTEGER),

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2517b31

impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATE, INTEGER),
impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, DATETIME, INTEGER),
impl(nullMissingHandling(DateTimeFunction::exprWeek), INTEGER, TIMESTAMP, INTEGER),
Expand Down Expand Up @@ -646,6 +659,15 @@ private DefaultFunctionResolver date_format() {
);
}

private ExprValue dayOfYearToday(Clock clock) {
return new ExprIntegerValue((formatNow(clock).getDayOfYear()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid extra actions made in formatNow

Suggested change
return new ExprIntegerValue((formatNow(clock).getDayOfYear()));
return new ExprIntegerValue(LocalDateTime.now(clock).getDayOfYear());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2517b31

}

private ExprValue weekOfYearToday(ExprValue mode, Clock clock) {
return new ExprIntegerValue(
CalendarLookup.getWeekNumber(mode.integerValue(), formatNow(clock).toLocalDate()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CalendarLookup.getWeekNumber(mode.integerValue(), formatNow(clock).toLocalDate()));
CalendarLookup.getWeekNumber(mode.integerValue(), LocalDateTime.now(clock).toLocalDate()));

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2517b31

}

/**
* ADDDATE function implementation for ExprValue.
*
Expand Down Expand Up @@ -1241,6 +1263,10 @@ private ExprValue exprYear(ExprValue date) {
return new ExprIntegerValue(date.dateValue().getYear());
}

private ExprValue monthOfYearToday(Clock clock) {
return new ExprIntegerValue((formatNow(clock).getMonthValue()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new ExprIntegerValue((formatNow(clock).getMonthValue()));
return new ExprIntegerValue(LocalDateTime.now(clock).getMonthValue());

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2517b31

}

private LocalDateTime formatNow(Clock clock) {
return formatNow(clock, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,52 @@ public String toString() {
};
}

/**
* Implementation of a function that takes two argument, returns a value, and
* requires FunctionProperties to complete.
*
* @param function {@link ExprValue} based binary function.
* @param returnType return type.
* @param args1Type first argument type.
* @param args2Type second argument type.
* @return Binary Function Implementation.
*/
public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>>
implWithProperties(
SerializableTriFunction<FunctionProperties, ExprValue, ExprValue, ExprValue> function,
ExprType returnType,
ExprType args1Type,
ExprType args2Type) {

return functionName -> {
FunctionSignature functionSignature =
new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type));
FunctionBuilder functionBuilder =
(functionProperties, arguments) -> new FunctionExpression(functionName, arguments) {
@Override
public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) {
ExprValue arg1 = arguments.get(0).valueOf(valueEnv);
ExprValue arg2 = arguments.get(1).valueOf(valueEnv);
return function.apply(functionProperties, arg1, arg2);
}

@Override
public ExprType type() {
return returnType;
}

@Override
public String toString() {
return String.format("%s(%s)", functionName,
arguments.stream()
.map(Object::toString)
.collect(Collectors.joining(", ")));
}
};
return Pair.of(functionSignature, functionBuilder);
};
}

/**
* No Arg Function Implementation.
*
Expand Down
Loading