|
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.DateTimeFormatterUtil.getFormattedDate; |
10 | 9 | import static org.opensearch.sql.expression.datetime.DateTimeFormatterUtil.getFormattedDateOfToday; |
11 | 10 | import static org.opensearch.sql.expression.datetime.DateTimeFormatterUtil.getFormattedTime; |
12 | 11 |
|
13 | 12 | import java.util.List; |
14 | 13 | import lombok.RequiredArgsConstructor; |
15 | | -import org.apache.calcite.DataContext; |
16 | 14 | import org.apache.calcite.adapter.enumerable.NotNullImplementor; |
17 | 15 | import org.apache.calcite.adapter.enumerable.NullPolicy; |
18 | 16 | import org.apache.calcite.adapter.enumerable.RexToLixTranslator; |
|
24 | 22 | import org.opensearch.sql.calcite.utils.OpenSearchTypeFactory; |
25 | 23 | import org.opensearch.sql.calcite.utils.PPLReturnTypes; |
26 | 24 | import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; |
| 25 | +import org.opensearch.sql.calcite.utils.datetime.DateTimeApplyUtils; |
27 | 26 | import org.opensearch.sql.data.model.ExprStringValue; |
28 | 27 | import org.opensearch.sql.data.model.ExprValue; |
29 | 28 | import org.opensearch.sql.expression.function.FunctionProperties; |
@@ -63,34 +62,45 @@ public Expression implement( |
63 | 62 | SqlTypeName type = |
64 | 63 | OpenSearchTypeFactory.convertRelDataTypeToSqlTypeName( |
65 | 64 | call.getOperands().getFirst().getType()); |
66 | | - String methodName = functionType == SqlTypeName.TIME ? "timeFormat" : "dateFormat"; |
67 | | - return Expressions.call( |
68 | | - DataFormatImplementor.class, |
69 | | - methodName, |
70 | | - translatedOperands.get(0), |
71 | | - Expressions.constant(type), |
72 | | - translatedOperands.get(1), |
73 | | - translator.getRoot()); |
74 | | - } |
| 65 | + Expression functionProperties = |
| 66 | + Expressions.call( |
| 67 | + UserDefinedFunctionUtils.class, "restoreFunctionProperties", translator.getRoot()); |
| 68 | + Expression datetime = |
| 69 | + Expressions.call( |
| 70 | + DateTimeApplyUtils.class, |
| 71 | + "transferInputToExprValue", |
| 72 | + translatedOperands.get(0), |
| 73 | + Expressions.constant(type)); |
| 74 | + Expression format = Expressions.new_(ExprStringValue.class, translatedOperands.get(1)); |
75 | 75 |
|
76 | | - public static String dateFormat( |
77 | | - Object date, SqlTypeName type, String format, DataContext propertyContext) { |
78 | | - FunctionProperties restored = |
79 | | - UserDefinedFunctionUtils.restoreFunctionProperties(propertyContext); |
80 | | - ExprValue candidateValue = transferInputToExprValue(date, type); |
81 | | - if (type == SqlTypeName.TIME) { |
82 | | - return getFormattedDateOfToday( |
83 | | - new ExprStringValue(format), candidateValue, restored.getQueryStartClock()) |
84 | | - .stringValue(); |
| 76 | + if (SqlTypeName.TIME.equals(functionType)) { |
| 77 | + return Expressions.call(DataFormatImplementor.class, "timeFormat", datetime, format); |
| 78 | + } else { |
| 79 | + if (SqlTypeName.TIME.equals(type)) { |
| 80 | + return Expressions.call( |
| 81 | + DataFormatImplementor.class, |
| 82 | + "dateFormatForTime", |
| 83 | + functionProperties, |
| 84 | + format, |
| 85 | + datetime); |
| 86 | + } else { |
| 87 | + return Expressions.call(DataFormatImplementor.class, "dateFormat", datetime, format); |
| 88 | + } |
85 | 89 | } |
86 | | - return getFormattedDate(candidateValue, new ExprStringValue(format)).stringValue(); |
87 | 90 | } |
88 | 91 |
|
89 | | - public static String timeFormat( |
90 | | - Object time, SqlTypeName sqlTypeName, String format, Object propertyContext) { |
91 | | - return getFormattedTime( |
92 | | - transferInputToExprValue(time, sqlTypeName), new ExprStringValue(format)) |
| 92 | + public static String dateFormat(ExprValue date, ExprStringValue format) { |
| 93 | + return getFormattedDate(date, format).stringValue(); |
| 94 | + } |
| 95 | + |
| 96 | + public static String dateFormatForTime( |
| 97 | + FunctionProperties functionProperties, ExprStringValue format, ExprValue time) { |
| 98 | + return getFormattedDateOfToday(format, time, functionProperties.getQueryStartClock()) |
93 | 99 | .stringValue(); |
94 | 100 | } |
| 101 | + |
| 102 | + public static String timeFormat(ExprValue time, ExprStringValue format) { |
| 103 | + return getFormattedTime(time, format).stringValue(); |
| 104 | + } |
95 | 105 | } |
96 | 106 | } |
0 commit comments