55
66package org .opensearch .sql .calcite .utils ;
77
8+ import java .util .Locale ;
9+ import org .apache .calcite .sql .SqlCallBinding ;
10+ import org .apache .calcite .sql .SqlOperandCountRange ;
11+ import org .apache .calcite .sql .SqlOperator ;
812import org .apache .calcite .sql .type .CompositeOperandTypeChecker ;
913import org .apache .calcite .sql .type .FamilyOperandTypeChecker ;
1014import org .apache .calcite .sql .type .OperandTypes ;
15+ import org .apache .calcite .sql .type .SqlOperandCountRanges ;
1116import org .apache .calcite .sql .type .SqlOperandTypeChecker ;
1217import org .apache .calcite .sql .type .SqlTypeFamily ;
18+ import org .apache .calcite .sql .type .SqlTypeUtil ;
1319import org .opensearch .sql .expression .function .UDFOperandMetadata ;
1420
1521/**
@@ -21,48 +27,7 @@ public class PPLOperandTypes {
2127 // This class is not meant to be instantiated.
2228 private PPLOperandTypes () {}
2329
24- /** List of all scalar type signatures (single parameter each) */
25- private static final java .util .List <java .util .List <org .opensearch .sql .data .type .ExprType >>
26- SCALAR_TYPES =
27- java .util .List .of (
28- // Numeric types
29- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .BYTE ),
30- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .SHORT ),
31- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .INTEGER ),
32- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .LONG ),
33- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .FLOAT ),
34- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .DOUBLE ),
35- // String type
36- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .STRING ),
37- // Boolean type
38- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .BOOLEAN ),
39- // Temporal types
40- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .DATE ),
41- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .TIME ),
42- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .TIMESTAMP ),
43- // Special scalar types
44- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .IP ),
45- java .util .List .of (org .opensearch .sql .data .type .ExprCoreType .BINARY ));
46-
47- /** Helper method to create scalar types with optional integer parameter */
48- private static java .util .List <java .util .List <org .opensearch .sql .data .type .ExprType >>
49- createScalarWithOptionalInteger () {
50- java .util .List <java .util .List <org .opensearch .sql .data .type .ExprType >> result =
51- new java .util .ArrayList <>(SCALAR_TYPES );
52-
53- // Add scalar + integer combinations
54- SCALAR_TYPES .forEach (
55- scalarType ->
56- result .add (
57- java .util .List .of (
58- scalarType .get (0 ), org .opensearch .sql .data .type .ExprCoreType .INTEGER )));
59-
60- return result ;
61- }
62-
6330 public static final UDFOperandMetadata NONE = UDFOperandMetadata .wrap (OperandTypes .family ());
64- public static final UDFOperandMetadata OPTIONAL_ANY =
65- UDFOperandMetadata .wrap (OperandTypes .family (SqlTypeFamily .ANY ).or (OperandTypes .family ()));
6631
6732 public static final UDFOperandMetadata OPTIONAL_INTEGER =
6833 UDFOperandMetadata .wrap (OperandTypes .INTEGER .or (OperandTypes .family ()));
@@ -78,8 +43,6 @@ private PPLOperandTypes() {}
7843 public static final UDFOperandMetadata ANY_OPTIONAL_INTEGER =
7944 UDFOperandMetadata .wrap (
8045 OperandTypes .ANY .or (OperandTypes .family (SqlTypeFamily .ANY , SqlTypeFamily .INTEGER )));
81- public static final SqlOperandTypeChecker ANY_OPTIONAL_TIMESTAMP =
82- OperandTypes .ANY .or (OperandTypes .family (SqlTypeFamily .ANY , SqlTypeFamily .TIMESTAMP ));
8346 public static final UDFOperandMetadata INTEGER_INTEGER =
8447 UDFOperandMetadata .wrap (OperandTypes .INTEGER_INTEGER );
8548 public static final UDFOperandMetadata STRING_STRING =
@@ -137,7 +100,6 @@ private PPLOperandTypes() {}
137100
138101 public static final UDFOperandMetadata WIDTH_BUCKET_OPERAND =
139102 UDFOperandMetadata .wrap (
140-
141103 // 1. Numeric fields: bin age span=10
142104 OperandTypes .family (
143105 SqlTypeFamily .NUMERIC ,
@@ -227,24 +189,13 @@ private PPLOperandTypes() {}
227189 UDFOperandMetadata .wrap (
228190 OperandTypes .DATETIME .or (
229191 OperandTypes .family (SqlTypeFamily .DATETIME , SqlTypeFamily .INTEGER )));
230- public static final UDFOperandMetadata ANY_DATETIME_OR_STRING =
231- UDFOperandMetadata .wrap (
232- OperandTypes .family (SqlTypeFamily .ANY )
233- .or (OperandTypes .family (SqlTypeFamily .ANY , SqlTypeFamily .DATETIME ))
234- .or (OperandTypes .family (SqlTypeFamily .ANY , SqlTypeFamily .STRING )));
235192
236193 public static final UDFOperandMetadata DATETIME_DATETIME =
237194 UDFOperandMetadata .wrap (OperandTypes .family (SqlTypeFamily .DATETIME , SqlTypeFamily .DATETIME ));
238195 public static final UDFOperandMetadata DATETIME_OR_STRING_STRING =
239196 UDFOperandMetadata .wrap (
240197 OperandTypes .family (SqlTypeFamily .DATETIME , SqlTypeFamily .CHARACTER )
241198 .or (OperandTypes .CHARACTER_CHARACTER ));
242- public static final UDFOperandMetadata DATETIME_OR_STRING_DATETIME_OR_STRING =
243- UDFOperandMetadata .wrap (
244- OperandTypes .CHARACTER_CHARACTER
245- .or (OperandTypes .family (SqlTypeFamily .DATETIME , SqlTypeFamily .DATETIME ))
246- .or (OperandTypes .family (SqlTypeFamily .DATETIME , SqlTypeFamily .CHARACTER ))
247- .or (OperandTypes .family (SqlTypeFamily .CHARACTER , SqlTypeFamily .DATETIME )));
248199 public static final UDFOperandMetadata STRING_TIMESTAMP =
249200 UDFOperandMetadata .wrap (
250201 OperandTypes .family (SqlTypeFamily .CHARACTER , SqlTypeFamily .TIMESTAMP ));
@@ -288,12 +239,56 @@ private PPLOperandTypes() {}
288239 * booleans, datetime types, and special scalar types like IP and BINARY. Excludes complex types
289240 * like arrays, structs, and maps.
290241 */
291- public static final UDFOperandMetadata ANY_SCALAR = UDFOperandMetadata .wrapUDT (SCALAR_TYPES );
242+ public static final UDFOperandMetadata SCALAR =
243+ UDFOperandMetadata .wrap (
244+ new SqlOperandTypeChecker () {
245+ @ Override
246+ public boolean checkOperandTypes (SqlCallBinding callBinding , boolean throwOnFailure ) {
247+ if (!getOperandCountRange ().isValidCount (callBinding .getOperandCount ())) {
248+ return false ;
249+ }
250+ return OpenSearchTypeUtil .isScalar (callBinding .getOperandType (0 ));
251+ }
252+
253+ @ Override
254+ public SqlOperandCountRange getOperandCountRange () {
255+ return SqlOperandCountRanges .of (1 );
256+ }
257+
258+ @ Override
259+ public String getAllowedSignatures (SqlOperator op , String opName ) {
260+ return String .format (Locale .ROOT , "%s(<SCALAR>)" , opName );
261+ }
262+ });
292263
293264 /**
294265 * Operand type checker that accepts any scalar type with an optional integer argument. This is
295266 * used for aggregation functions that take a field and an optional limit/size parameter.
296267 */
297- public static final UDFOperandMetadata ANY_SCALAR_OPTIONAL_INTEGER =
298- UDFOperandMetadata .wrapUDT (createScalarWithOptionalInteger ());
268+ public static final UDFOperandMetadata SCALAR_OPTIONAL_INTEGER =
269+ UDFOperandMetadata .wrap (
270+ new SqlOperandTypeChecker () {
271+ @ Override
272+ public boolean checkOperandTypes (SqlCallBinding callBinding , boolean throwOnFailure ) {
273+ if (!getOperandCountRange ().isValidCount (callBinding .getOperandCount ())) {
274+ return false ;
275+ }
276+ boolean valid = OpenSearchTypeUtil .isScalar (callBinding .getOperandType (0 ));
277+ if (callBinding .getOperandCount () == 2 ) {
278+ valid = valid && SqlTypeUtil .isIntType (callBinding .getOperandType (1 ));
279+ }
280+ return valid ;
281+ }
282+
283+ @ Override
284+ public SqlOperandCountRange getOperandCountRange () {
285+ return SqlOperandCountRanges .between (1 , 2 );
286+ }
287+
288+ @ Override
289+ public String getAllowedSignatures (SqlOperator op , String opName ) {
290+ return String .format (
291+ Locale .ROOT , "%s(<SCALAR>), %s(<SCALAR>, <INTEGER>)" , opName , opName );
292+ }
293+ });
299294}
0 commit comments