88import java .time .LocalDateTime ;
99import java .util .List ;
1010import lombok .RequiredArgsConstructor ;
11- import org .apache .calcite .DataContext ;
1211import org .apache .calcite .adapter .enumerable .NotNullImplementor ;
1312import org .apache .calcite .adapter .enumerable .NullPolicy ;
1413import org .apache .calcite .adapter .enumerable .RexToLixTranslator ;
1514import org .apache .calcite .linq4j .tree .Expression ;
1615import org .apache .calcite .linq4j .tree .Expressions ;
1716import org .apache .calcite .rex .RexCall ;
1817import org .apache .calcite .sql .type .SqlReturnTypeInference ;
19- import org .apache .calcite .sql .type .SqlTypeName ;
2018import org .opensearch .sql .calcite .utils .UserDefinedFunctionUtils ;
2119import org .opensearch .sql .data .model .ExprDateValue ;
2220import org .opensearch .sql .data .model .ExprTimeValue ;
2321import org .opensearch .sql .data .model .ExprTimestampValue ;
22+ import org .opensearch .sql .data .type .ExprCoreType ;
23+ import org .opensearch .sql .data .type .ExprType ;
2424import org .opensearch .sql .expression .datetime .DateTimeFunctions ;
2525import org .opensearch .sql .expression .function .FunctionProperties ;
2626import org .opensearch .sql .expression .function .ImplementorUDF ;
3737 * <p>It returns the current date, time, or timestamp based on the specified return type.
3838 */
3939public class CurrentFunction extends ImplementorUDF {
40- private final SqlTypeName returnType ;
40+ private final ExprType returnType ;
4141
42- public CurrentFunction (SqlTypeName returnType ) {
42+ public CurrentFunction (ExprType returnType ) {
4343 super (new CurrentFunctionImplementor (returnType ), NullPolicy .NONE );
4444 this .returnType = returnType ;
4545 }
@@ -48,39 +48,39 @@ public CurrentFunction(SqlTypeName returnType) {
4848 public SqlReturnTypeInference getReturnTypeInference () {
4949 return opBinding ->
5050 switch (returnType ) {
51- case DATE -> UserDefinedFunctionUtils .NULLABLE_DATE_UDT ;
52- case TIME -> UserDefinedFunctionUtils .NULLABLE_TIME_UDT ;
53- case TIMESTAMP -> UserDefinedFunctionUtils .NULLABLE_TIMESTAMP_UDT ;
51+ case ExprCoreType . DATE -> UserDefinedFunctionUtils .NULLABLE_DATE_UDT ;
52+ case ExprCoreType . TIME -> UserDefinedFunctionUtils .NULLABLE_TIME_UDT ;
53+ case ExprCoreType . TIMESTAMP -> UserDefinedFunctionUtils .NULLABLE_TIMESTAMP_UDT ;
5454 default -> throw new IllegalArgumentException ("Unsupported return type: " + returnType );
5555 };
5656 }
5757
5858 @ RequiredArgsConstructor
5959 public static class CurrentFunctionImplementor implements NotNullImplementor {
60- private final SqlTypeName returnType ;
60+ private final ExprType returnType ;
6161
6262 @ Override
6363 public Expression implement (
6464 RexToLixTranslator translator , RexCall call , List <Expression > translatedOperands ) {
6565
6666 String functionName =
6767 switch (returnType ) {
68- case DATE -> "currentDate" ;
69- case TIME -> "currentTime" ;
70- case TIMESTAMP -> "currentTimestamp" ;
68+ case ExprCoreType . DATE -> "currentDate" ;
69+ case ExprCoreType . TIME -> "currentTime" ;
70+ case ExprCoreType . TIMESTAMP -> "currentTimestamp" ;
7171 default -> throw new IllegalArgumentException ("Unsupported return type: " + returnType );
7272 };
7373
74- Expression now =
74+ Expression properties =
7575 Expressions .call (
76- CurrentFunctionImplementor .class , "getNowFromProperties" , translator .getRoot ());
76+ UserDefinedFunctionUtils .class , "restoreFunctionProperties" , translator .getRoot ());
77+ Expression now =
78+ Expressions .call (CurrentFunctionImplementor .class , "getNowFromProperties" , properties );
7779
7880 return Expressions .call (CurrentFunctionImplementor .class , functionName , now );
7981 }
8082
81- public static LocalDateTime getNowFromProperties (DataContext propertyContext ) {
82- FunctionProperties functionProperties =
83- UserDefinedFunctionUtils .restoreFunctionProperties (propertyContext );
83+ public static LocalDateTime getNowFromProperties (FunctionProperties functionProperties ) {
8484 return DateTimeFunctions .formatNow (functionProperties .getQueryStartClock ());
8585 }
8686
0 commit comments