Skip to content

Commit 3ec719d

Browse files
committed
Refactor: change return types of UDF implementations to their actual return types (instead of Object)
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 6b78b84 commit 3ec719d

11 files changed

Lines changed: 45 additions & 43 deletions

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/CurrentFunction.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ public static LocalDateTime getNowFromProperties(FunctionProperties functionProp
8484
return DateTimeFunctions.formatNow(functionProperties.getQueryStartClock());
8585
}
8686

87-
public static Object currentDate(LocalDateTime now) {
88-
return new ExprDateValue(now.toLocalDate()).valueForCalcite();
87+
public static String currentDate(LocalDateTime now) {
88+
return (String) new ExprDateValue(now.toLocalDate()).valueForCalcite();
8989
}
9090

91-
public static Object currentTime(LocalDateTime now) {
92-
return new ExprTimeValue(now.toLocalTime()).valueForCalcite();
91+
public static String currentTime(LocalDateTime now) {
92+
return (String) new ExprTimeValue(now.toLocalTime()).valueForCalcite();
9393
}
9494

95-
public static Object currentTimestamp(LocalDateTime now) {
96-
return new ExprTimestampValue(now).valueForCalcite();
95+
public static String currentTimestamp(LocalDateTime now) {
96+
return (String) new ExprTimestampValue(now).valueForCalcite();
9797
}
9898
}
9999
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/DatetimeFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ public Expression implement(
4747
return Expressions.call(DatetimeImplementor.class, "datetime", translatedOperands);
4848
}
4949

50-
public static Object datetime(String timestamp) {
50+
public static String datetime(String timestamp) {
5151
ExprValue argTimestampExpr = new ExprStringValue(timestamp);
5252
ExprValue datetimeExpr;
5353
datetimeExpr = DateTimeFunctions.exprDateTimeNoTimezone(argTimestampExpr);
54-
return datetimeExpr.valueForCalcite();
54+
return (String) datetimeExpr.valueForCalcite();
5555
}
5656

57-
public static Object datetime(String timestamp, String timezone) {
57+
public static String datetime(String timestamp, String timezone) {
5858
ExprValue timestampExpr = new ExprStringValue(timestamp);
5959
ExprValue datetimeExpr =
6060
DateTimeFunctions.exprDateTime(timestampExpr, new ExprStringValue(timezone));
61-
return datetimeExpr.valueForCalcite();
61+
return (String) datetimeExpr.valueForCalcite();
6262
}
6363
}
6464
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/FromUnixTimeFunction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ public Expression implement(
5656
return Expressions.call(FromUnixTimeImplementor.class, "fromUnixTime", translatedOperands);
5757
}
5858

59-
public static Object fromUnixTime(double unixTime) {
60-
return exprFromUnixTime(new ExprDoubleValue(unixTime)).valueForCalcite();
59+
public static String fromUnixTime(double unixTime) {
60+
return (String) exprFromUnixTime(new ExprDoubleValue(unixTime)).valueForCalcite();
6161
}
6262

63-
public static Object fromUnixTime(double unixTime, String format) {
64-
return exprFromUnixTimeFormat(new ExprDoubleValue(unixTime), new ExprStringValue(format))
65-
.valueForCalcite();
63+
public static String fromUnixTime(double unixTime, String format) {
64+
return (String)
65+
exprFromUnixTimeFormat(new ExprDoubleValue(unixTime), new ExprStringValue(format))
66+
.valueForCalcite();
6667
}
6768
}
6869
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/SecToTimeFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public Expression implement(
5252
Expressions.convert_(translatedOperands.getFirst(), Number.class));
5353
}
5454

55-
public static Object secToTime(Number seconds) {
55+
public static String secToTime(Number seconds) {
5656
ExprValue returnTimeValue;
5757
ExprValue transferredValue = ExprValueUtils.fromObjectValue(seconds);
5858
if (seconds instanceof Long || seconds instanceof Integer) {
5959
returnTimeValue = exprSecToTime(transferredValue);
6060
} else {
6161
returnTimeValue = exprSecToTimeWithNanos(transferredValue);
6262
}
63-
return returnTimeValue.valueForCalcite();
63+
return (String) returnTimeValue.valueForCalcite();
6464
}
6565
}
6666
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/SysdateFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public Expression implement(
5050
return Expressions.call(SysdateImplementor.class, "sysdate", translatedOperands);
5151
}
5252

53-
public static Object sysdate() {
53+
public static String sysdate() {
5454
var localDateTime = DateTimeFunctions.formatNow(Clock.systemDefaultZone(), 0);
55-
return new ExprTimestampValue(localDateTime).valueForCalcite();
55+
return (String) new ExprTimestampValue(localDateTime).valueForCalcite();
5656
}
5757

58-
public static Object sysdate(int precision) {
58+
public static String sysdate(int precision) {
5959
var localDateTime = DateTimeFunctions.formatNow(Clock.systemDefaultZone(), precision);
60-
return new ExprTimestampValue(localDateTime).valueForCalcite();
60+
return (String) new ExprTimestampValue(localDateTime).valueForCalcite();
6161
}
6262
}
6363
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/TimestampAddFunction.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,24 @@ public Expression implement(
8282
}
8383
}
8484

85-
public static Object timestampAddForTimeType(
85+
public static String timestampAddForTimeType(
8686
String addUnit, long amount, ExprValue timestampBase, DataContext propertyContext) {
8787
FunctionProperties restored =
8888
UserDefinedFunctionUtils.restoreFunctionProperties(propertyContext);
8989

90-
return exprTimestampAddForTimeType(
91-
restored.getQueryStartClock(),
92-
new ExprStringValue(addUnit),
93-
new ExprLongValue(amount),
94-
timestampBase)
95-
.valueForCalcite();
90+
return (String)
91+
exprTimestampAddForTimeType(
92+
restored.getQueryStartClock(),
93+
new ExprStringValue(addUnit),
94+
new ExprLongValue(amount),
95+
timestampBase)
96+
.valueForCalcite();
9697
}
9798

98-
public static Object timestampAdd(String addUnit, long amount, ExprValue timestampBase) {
99+
public static String timestampAdd(String addUnit, long amount, ExprValue timestampBase) {
99100
ExprValue returnValue =
100101
exprTimestampAdd(new ExprStringValue(addUnit), new ExprLongValue(amount), timestampBase);
101-
return returnValue.valueForCalcite();
102+
return (String) returnValue.valueForCalcite();
102103
}
103104
}
104105
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/TimestampFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public Expression implement(
5555
}
5656
}
5757

58-
public static Object timestamp(FunctionProperties properties, ExprValue datetime) {
59-
return convertToTimestampValue(datetime, properties).valueForCalcite();
58+
public static String timestamp(FunctionProperties properties, ExprValue datetime) {
59+
return (String) convertToTimestampValue(datetime, properties).valueForCalcite();
6060
}
6161

62-
public static Object timestamp(
62+
public static String timestamp(
6363
FunctionProperties properties, ExprValue datetime, ExprValue addTime) {
6464
ExprValue dateTimeBase = convertToTimestampValue(datetime, properties);
6565
ExprValue addTimeValue = convertToTimestampValue(addTime, properties);
66-
return exprAddTime(properties, dateTimeBase, addTimeValue).valueForCalcite();
66+
return (String) exprAddTime(properties, dateTimeBase, addTimeValue).valueForCalcite();
6767
}
6868
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/ToSecondsFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Expression implement(
5858
}
5959
}
6060

61-
public static Object toSeconds(FunctionProperties properties, ExprValue datetime) {
61+
public static long toSeconds(FunctionProperties properties, ExprValue datetime) {
6262
return switch (datetime.type()) {
6363
case ExprCoreType.DATE, ExprCoreType.TIME, ExprCoreType.TIMESTAMP, ExprCoreType.STRING -> {
6464
ExprValue dateTimeValue = convertToTimestampValue(datetime, properties);

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/UnixTimestampFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public Expression implement(
5656
}
5757
}
5858

59-
public static Object unixTimestamp(FunctionProperties properties) {
59+
public static double unixTimestamp(FunctionProperties properties) {
6060
return unixTimeStamp(properties.getQueryStartClock()).doubleValue();
6161
}
6262

63-
public static Object unixTimestamp(FunctionProperties ignored, ExprValue timestamp) {
63+
public static double unixTimestamp(FunctionProperties ignored, ExprValue timestamp) {
6464
return unixTimeStampOf(timestamp).doubleValue();
6565
}
6666
}

core/src/main/java/org/opensearch/sql/expression/function/udf/datetime/WeekFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ public Expression implement(
5353
return Expressions.call(WeekImplementor.class, "week", list);
5454
}
5555

56-
public static Object week(String date) {
56+
public static int week(String date) {
5757
ExprValue dateValue = new ExprDateValue(date);
58-
return DateTimeFunctions.exprWeekWithoutMode(dateValue).valueForCalcite();
58+
return (int) DateTimeFunctions.exprWeekWithoutMode(dateValue).valueForCalcite();
5959
}
6060

61-
public static Object week(String date, int mode) {
61+
public static int week(String date, int mode) {
6262
ExprValue dateValue = new ExprDateValue(date);
6363
ExprValue modeValue = new ExprIntegerValue(mode);
6464
ExprValue woyExpr = DateTimeFunctions.exprWeek(dateValue, modeValue);
65-
return woyExpr.valueForCalcite();
65+
return (int) woyExpr.valueForCalcite();
6666
}
6767
}
6868
}

0 commit comments

Comments
 (0)