|
5 | 5 |
|
6 | 6 | package org.opensearch.sql.expression.function.udf.datetime; |
7 | 7 |
|
8 | | -import static org.opensearch.sql.calcite.utils.datetime.DateTimeApplyUtils.transferInputToExprValue; |
9 | 8 | import static org.opensearch.sql.expression.datetime.DateTimeFunctions.exprTimestampDiff; |
10 | 9 | import static org.opensearch.sql.expression.datetime.DateTimeFunctions.exprTimestampDiffForTimeType; |
11 | 10 |
|
12 | 11 | import java.util.List; |
13 | | -import org.apache.calcite.DataContext; |
14 | 12 | import org.apache.calcite.adapter.enumerable.NotNullImplementor; |
15 | 13 | import org.apache.calcite.adapter.enumerable.NullPolicy; |
16 | 14 | import org.apache.calcite.adapter.enumerable.RexToLixTranslator; |
|
22 | 20 | import org.apache.calcite.sql.type.SqlTypeName; |
23 | 21 | import org.opensearch.sql.calcite.utils.OpenSearchTypeFactory; |
24 | 22 | import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; |
| 23 | +import org.opensearch.sql.calcite.utils.datetime.DateTimeApplyUtils; |
25 | 24 | import org.opensearch.sql.data.model.ExprStringValue; |
26 | 25 | import org.opensearch.sql.data.model.ExprValue; |
27 | 26 | import org.opensearch.sql.expression.function.FunctionProperties; |
@@ -54,51 +53,51 @@ public static class DiffImplementor implements NotNullImplementor { |
54 | 53 | @Override |
55 | 54 | public Expression implement( |
56 | 55 | RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { |
57 | | - int startIndex, endIndex; |
58 | | - Expression unit; |
59 | 56 | // timestampdiff(interval, start, end) |
60 | | - unit = translatedOperands.getFirst(); |
61 | | - startIndex = 1; |
62 | | - endIndex = 2; |
| 57 | + int startIndex = 1; |
| 58 | + int endIndex = 2; |
63 | 59 | var startType = |
64 | 60 | OpenSearchTypeFactory.convertRelDataTypeToSqlTypeName( |
65 | 61 | call.getOperands().get(startIndex).getType()); |
66 | 62 | var endType = |
67 | 63 | OpenSearchTypeFactory.convertRelDataTypeToSqlTypeName( |
68 | 64 | call.getOperands().get(endIndex).getType()); |
69 | 65 |
|
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)); |
92 | 82 |
|
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); |
97 | 86 | } |
| 87 | + return Expressions.call(DiffImplementor.class, "diff", unit, start, end); |
| 88 | + } |
98 | 89 |
|
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); |
101 | 92 | return diffResult.longValue(); |
102 | 93 | } |
| 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 | + } |
103 | 102 | } |
104 | 103 | } |
0 commit comments