Skip to content

Commit 131e5df

Browse files
committed
Refactor: Move type comparison earlier in timestampdiff function implementation
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 5377216 commit 131e5df

1 file changed

Lines changed: 34 additions & 35 deletions

File tree

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

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
package org.opensearch.sql.expression.function.udf.datetime;
77

8-
import static org.opensearch.sql.calcite.utils.datetime.DateTimeApplyUtils.transferInputToExprValue;
98
import static org.opensearch.sql.expression.datetime.DateTimeFunctions.exprTimestampDiff;
109
import static org.opensearch.sql.expression.datetime.DateTimeFunctions.exprTimestampDiffForTimeType;
1110

1211
import java.util.List;
13-
import org.apache.calcite.DataContext;
1412
import org.apache.calcite.adapter.enumerable.NotNullImplementor;
1513
import org.apache.calcite.adapter.enumerable.NullPolicy;
1614
import org.apache.calcite.adapter.enumerable.RexToLixTranslator;
@@ -22,6 +20,7 @@
2220
import org.apache.calcite.sql.type.SqlTypeName;
2321
import org.opensearch.sql.calcite.utils.OpenSearchTypeFactory;
2422
import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils;
23+
import org.opensearch.sql.calcite.utils.datetime.DateTimeApplyUtils;
2524
import org.opensearch.sql.data.model.ExprStringValue;
2625
import org.opensearch.sql.data.model.ExprValue;
2726
import org.opensearch.sql.expression.function.FunctionProperties;
@@ -54,51 +53,51 @@ public static class DiffImplementor implements NotNullImplementor {
5453
@Override
5554
public Expression implement(
5655
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
57-
int startIndex, endIndex;
58-
Expression unit;
5956
// timestampdiff(interval, start, end)
60-
unit = translatedOperands.getFirst();
61-
startIndex = 1;
62-
endIndex = 2;
57+
int startIndex = 1;
58+
int endIndex = 2;
6359
var startType =
6460
OpenSearchTypeFactory.convertRelDataTypeToSqlTypeName(
6561
call.getOperands().get(startIndex).getType());
6662
var endType =
6763
OpenSearchTypeFactory.convertRelDataTypeToSqlTypeName(
6864
call.getOperands().get(endIndex).getType());
6965

70-
return Expressions.call(
71-
DiffImplementor.class,
72-
"diff",
73-
unit,
74-
translatedOperands.get(startIndex),
75-
Expressions.constant(startType),
76-
translatedOperands.get(endIndex),
77-
Expressions.constant(endType),
78-
translator.getRoot());
79-
}
80-
81-
public static Long diff(
82-
String unit,
83-
String start,
84-
SqlTypeName startType,
85-
String end,
86-
SqlTypeName endType,
87-
DataContext propertyContext) {
88-
FunctionProperties restored =
89-
UserDefinedFunctionUtils.restoreFunctionProperties(propertyContext);
90-
ExprValue startTimestamp = transferInputToExprValue(start, startType);
91-
ExprValue endTimestamp = transferInputToExprValue(end, endType);
66+
Expression functionProperties =
67+
Expressions.call(
68+
UserDefinedFunctionUtils.class, "restoreFunctionProperties", translator.getRoot());
69+
Expression unit = Expressions.new_(ExprStringValue.class, translatedOperands.getFirst());
70+
Expression start =
71+
Expressions.call(
72+
DateTimeApplyUtils.class,
73+
"transferInputToExprValue",
74+
translatedOperands.get(startIndex),
75+
Expressions.constant(startType));
76+
Expression end =
77+
Expressions.call(
78+
DateTimeApplyUtils.class,
79+
"transferInputToExprValue",
80+
translatedOperands.get(endIndex),
81+
Expressions.constant(endType));
9282

93-
if (startType == SqlTypeName.TIME || endType == SqlTypeName.TIME) {
94-
return exprTimestampDiffForTimeType(
95-
restored, new ExprStringValue(unit), startTimestamp, endTimestamp)
96-
.longValue();
83+
if (SqlTypeName.TIME.equals(startType) || SqlTypeName.TIME.equals(endType)) {
84+
return Expressions.call(
85+
DiffImplementor.class, "diffForTime", functionProperties, unit, start, end);
9786
}
87+
return Expressions.call(DiffImplementor.class, "diff", unit, start, end);
88+
}
9889

99-
ExprValue diffResult =
100-
exprTimestampDiff(new ExprStringValue(unit), startTimestamp, endTimestamp);
90+
public static long diff(ExprStringValue unit, ExprValue start, ExprValue end) {
91+
ExprValue diffResult = exprTimestampDiff(unit, start, end);
10192
return diffResult.longValue();
10293
}
94+
95+
public static long diffForTime(
96+
FunctionProperties functionProperties,
97+
ExprStringValue unit,
98+
ExprValue start,
99+
ExprValue end) {
100+
return exprTimestampDiffForTimeType(functionProperties, unit, start, end).longValue();
101+
}
103102
}
104103
}

0 commit comments

Comments
 (0)