66package org .opensearch .sql .expression .function .udf .datetime ;
77
88import static org .opensearch .sql .calcite .utils .UserDefinedFunctionUtils .*;
9- import static org .opensearch .sql .calcite .utils .datetime .DateTimeApplyUtils .transferInputToExprValue ;
109import static org .opensearch .sql .expression .datetime .DateTimeFunctions .exprWeekday ;
1110import static org .opensearch .sql .expression .datetime .DateTimeFunctions .formatNow ;
1211
1312import java .util .List ;
14- import org .apache .calcite .DataContext ;
1513import org .apache .calcite .adapter .enumerable .NotNullImplementor ;
1614import org .apache .calcite .adapter .enumerable .NullPolicy ;
1715import org .apache .calcite .adapter .enumerable .RexToLixTranslator ;
2018import org .apache .calcite .rex .RexCall ;
2119import org .apache .calcite .sql .type .SqlReturnTypeInference ;
2220import org .apache .calcite .sql .type .SqlTypeName ;
21+ import org .opensearch .sql .calcite .utils .OpenSearchTypeFactory ;
2322import org .opensearch .sql .calcite .utils .PPLReturnTypes ;
23+ import org .opensearch .sql .calcite .utils .UserDefinedFunctionUtils ;
24+ import org .opensearch .sql .calcite .utils .datetime .DateTimeApplyUtils ;
25+ import org .opensearch .sql .data .model .ExprValue ;
2426import org .opensearch .sql .expression .function .FunctionProperties ;
2527import org .opensearch .sql .expression .function .ImplementorUDF ;
2628
@@ -47,19 +49,34 @@ public SqlReturnTypeInference getReturnTypeInference() {
4749 public static class WeekdayImplementor implements NotNullImplementor {
4850 @ Override
4951 public Expression implement (
50- RexToLixTranslator rexToLixTranslator , RexCall rexCall , List <Expression > list ) {
51- List <Expression > newList = addTypeAndContext (list , rexCall , rexToLixTranslator .getRoot ());
52- return Expressions .call (WeekdayFunction .class , "weekday" , newList );
52+ RexToLixTranslator rexToLixTranslator , RexCall rexCall , List <Expression > operands ) {
53+ Expression functionProperties =
54+ Expressions .call (
55+ UserDefinedFunctionUtils .class ,
56+ "restoreFunctionProperties" ,
57+ rexToLixTranslator .getRoot ());
58+ SqlTypeName dateType =
59+ OpenSearchTypeFactory .convertRelDataTypeToSqlTypeName (
60+ rexCall .getOperands ().getFirst ().getType ());
61+ Expression date =
62+ Expressions .call (
63+ DateTimeApplyUtils .class ,
64+ "transferInputToExprValue" ,
65+ operands .getFirst (),
66+ Expressions .constant (dateType ));
67+
68+ if (SqlTypeName .TIME .equals (dateType )) {
69+ return Expressions .call (WeekdayImplementor .class , "weekdayForTime" , functionProperties );
70+ }
71+ return Expressions .call (WeekdayImplementor .class , "weekday" , date );
72+ }
73+
74+ public static int weekday (ExprValue date ) {
75+ return exprWeekday (date ).integerValue ();
5376 }
54- }
5577
56- public static Object weekday (Object date , SqlTypeName dateType , DataContext propertyContext ) {
57- FunctionProperties restored = restoreFunctionProperties (propertyContext );
58- if (dateType == SqlTypeName .TIME ) {
59- // PPL Weekday returns 0 ~ 6; java.time.DayOfWeek returns 1 ~ 7.
60- return formatNow (restored .getQueryStartClock ()).getDayOfWeek ().getValue () - 1 ;
61- } else {
62- return exprWeekday (transferInputToExprValue (date , dateType )).integerValue ();
78+ public static int weekdayForTime (FunctionProperties functionProperties ) {
79+ return formatNow (functionProperties .getQueryStartClock ()).getDayOfWeek ().getValue () - 1 ;
6380 }
6481 }
6582}
0 commit comments