32
32
import org .opensearch .sql .data .model .ExprNullValue ;
33
33
import org .opensearch .sql .data .model .ExprShortValue ;
34
34
import org .opensearch .sql .data .model .ExprStringValue ;
35
+ import org .opensearch .sql .data .model .ExprValue ;
35
36
import org .opensearch .sql .data .type .ExprCoreType ;
36
37
import org .opensearch .sql .data .type .ExprType ;
37
38
import org .opensearch .sql .expression .function .BuiltinFunctionName ;
@@ -59,6 +60,7 @@ public static void register(BuiltinFunctionRepository repository) {
59
60
repository .register (crc32 ());
60
61
repository .register (euler ());
61
62
repository .register (exp ());
63
+ repository .register (expm1 ());
62
64
repository .register (floor ());
63
65
repository .register (ln ());
64
66
repository .register (log ());
@@ -85,6 +87,23 @@ public static void register(BuiltinFunctionRepository repository) {
85
87
repository .register (tan ());
86
88
}
87
89
90
+ /**
91
+ * Base function for math functions with similar formats that return DOUBLE.
92
+ *
93
+ * @param functionName BuiltinFunctionName of math function.
94
+ * @param formula lambda function of math formula.
95
+ * @param returnType data type return type of the calling function
96
+ * @return DefaultFunctionResolver for math functions.
97
+ */
98
+ private static DefaultFunctionResolver baseMathFunction (
99
+ FunctionName functionName , SerializableFunction <ExprValue ,
100
+ ExprValue > formula , ExprCoreType returnType ) {
101
+ return FunctionDSL .define (functionName ,
102
+ ExprCoreType .numberTypes ().stream ().map (type -> FunctionDSL .impl (
103
+ FunctionDSL .nullMissingHandling (formula ),
104
+ returnType , type )).collect (Collectors .toList ()));
105
+ }
106
+
88
107
/**
89
108
* Definition of abs() function. The supported signature of abs() function are INT -> INT LONG ->
90
109
* LONG FLOAT -> FLOAT DOUBLE -> DOUBLE
@@ -186,15 +205,21 @@ private static DefaultFunctionResolver euler() {
186
205
}
187
206
188
207
/**
189
- * Definition of exp(x) function. Calculate exponent function e to the x The supported signature
190
- * of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
208
+ * Definition of exp(x) function. Calculate exponent function e to the x
209
+ * The supported signature of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
191
210
*/
192
211
private static DefaultFunctionResolver exp () {
193
- return FunctionDSL .define (BuiltinFunctionName .EXP .getName (),
194
- ExprCoreType .numberTypes ().stream ()
195
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
196
- v -> new ExprDoubleValue (Math .exp (v .doubleValue ()))),
197
- type , DOUBLE )).collect (Collectors .toList ()));
212
+ return baseMathFunction (BuiltinFunctionName .EXP .getName (),
213
+ v -> new ExprDoubleValue (Math .exp (v .doubleValue ())), DOUBLE );
214
+ }
215
+
216
+ /**
217
+ * Definition of expm1(x) function. Calculate exponent function e to the x, minus 1
218
+ * The supported signature of exp function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
219
+ */
220
+ private static DefaultFunctionResolver expm1 () {
221
+ return baseMathFunction (BuiltinFunctionName .EXPM1 .getName (),
222
+ v -> new ExprDoubleValue (Math .expm1 (v .doubleValue ())), DOUBLE );
198
223
}
199
224
200
225
/**
@@ -214,11 +239,8 @@ private static DefaultFunctionResolver floor() {
214
239
* ln function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
215
240
*/
216
241
private static DefaultFunctionResolver ln () {
217
- return FunctionDSL .define (BuiltinFunctionName .LN .getName (),
218
- ExprCoreType .numberTypes ().stream ()
219
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
220
- v -> new ExprDoubleValue (Math .log (v .doubleValue ()))),
221
- type , DOUBLE )).collect (Collectors .toList ()));
242
+ return baseMathFunction (BuiltinFunctionName .LN .getName (),
243
+ v -> new ExprDoubleValue (Math .log (v .doubleValue ())), DOUBLE );
222
244
}
223
245
224
246
/**
@@ -255,23 +277,17 @@ private static DefaultFunctionResolver log() {
255
277
* log function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
256
278
*/
257
279
private static DefaultFunctionResolver log10 () {
258
- return FunctionDSL .define (BuiltinFunctionName .LOG10 .getName (),
259
- ExprCoreType .numberTypes ().stream ()
260
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
261
- v -> new ExprDoubleValue (Math .log10 (v .doubleValue ()))),
262
- type , DOUBLE )).collect (Collectors .toList ()));
280
+ return baseMathFunction (BuiltinFunctionName .LOG10 .getName (),
281
+ v -> new ExprDoubleValue (Math .log10 (v .doubleValue ())), DOUBLE );
263
282
}
264
283
265
284
/**
266
285
* Definition of log2(x) function. Calculate base-2 logarithm of x The supported signature of log
267
286
* function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
268
287
*/
269
288
private static DefaultFunctionResolver log2 () {
270
- return FunctionDSL .define (BuiltinFunctionName .LOG2 .getName (),
271
- ExprCoreType .numberTypes ().stream ()
272
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
273
- v -> new ExprDoubleValue (Math .log (v .doubleValue ()) / Math .log (2 ))), DOUBLE , type ))
274
- .collect (Collectors .toList ()));
289
+ return baseMathFunction (BuiltinFunctionName .LOG2 .getName (),
290
+ v -> new ExprDoubleValue (Math .log (v .doubleValue ()) / Math .log (2 )), DOUBLE );
275
291
}
276
292
277
293
/**
@@ -450,11 +466,8 @@ private static DefaultFunctionResolver round() {
450
466
* SHORT/INTEGER/LONG/FLOAT/DOUBLE -> INTEGER
451
467
*/
452
468
private static DefaultFunctionResolver sign () {
453
- return FunctionDSL .define (BuiltinFunctionName .SIGN .getName (),
454
- ExprCoreType .numberTypes ().stream ()
455
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
456
- v -> new ExprIntegerValue (Math .signum (v .doubleValue ()))),
457
- INTEGER , type )).collect (Collectors .toList ()));
469
+ return baseMathFunction (BuiltinFunctionName .SIGN .getName (),
470
+ v -> new ExprIntegerValue (Math .signum (v .doubleValue ())), INTEGER );
458
471
}
459
472
460
473
/**
@@ -464,12 +477,9 @@ private static DefaultFunctionResolver sign() {
464
477
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
465
478
*/
466
479
private static DefaultFunctionResolver sqrt () {
467
- return FunctionDSL .define (BuiltinFunctionName .SQRT .getName (),
468
- ExprCoreType .numberTypes ().stream ()
469
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
470
- v -> v .doubleValue () < 0 ? ExprNullValue .of () :
471
- new ExprDoubleValue (Math .sqrt (v .doubleValue ()))),
472
- DOUBLE , type )).collect (Collectors .toList ()));
480
+ return baseMathFunction (BuiltinFunctionName .SQRT .getName (),
481
+ v -> v .doubleValue () < 0 ? ExprNullValue .of () :
482
+ new ExprDoubleValue (Math .sqrt (v .doubleValue ())), DOUBLE );
473
483
}
474
484
475
485
/**
@@ -479,11 +489,8 @@ private static DefaultFunctionResolver sqrt() {
479
489
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
480
490
*/
481
491
private static DefaultFunctionResolver cbrt () {
482
- return FunctionDSL .define (BuiltinFunctionName .CBRT .getName (),
483
- ExprCoreType .numberTypes ().stream ()
484
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
485
- v -> new ExprDoubleValue (Math .cbrt (v .doubleValue ()))),
486
- DOUBLE , type )).collect (Collectors .toList ()));
492
+ return baseMathFunction (BuiltinFunctionName .CBRT .getName (),
493
+ v -> new ExprDoubleValue (Math .cbrt (v .doubleValue ())), DOUBLE );
487
494
}
488
495
489
496
/**
@@ -606,11 +613,8 @@ private static DefaultFunctionResolver atan2() {
606
613
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
607
614
*/
608
615
private static DefaultFunctionResolver cos () {
609
- return FunctionDSL .define (BuiltinFunctionName .COS .getName (),
610
- ExprCoreType .numberTypes ().stream ()
611
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
612
- v -> new ExprDoubleValue (Math .cos (v .doubleValue ()))),
613
- DOUBLE , type )).collect (Collectors .toList ()));
616
+ return baseMathFunction (BuiltinFunctionName .COS .getName (),
617
+ v -> new ExprDoubleValue (Math .cos (v .doubleValue ())), DOUBLE );
614
618
}
615
619
616
620
/**
@@ -641,11 +645,8 @@ private static DefaultFunctionResolver cot() {
641
645
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
642
646
*/
643
647
private static DefaultFunctionResolver degrees () {
644
- return FunctionDSL .define (BuiltinFunctionName .DEGREES .getName (),
645
- ExprCoreType .numberTypes ().stream ()
646
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
647
- v -> new ExprDoubleValue (Math .toDegrees (v .doubleValue ()))),
648
- type , DOUBLE )).collect (Collectors .toList ()));
648
+ return baseMathFunction (BuiltinFunctionName .DEGREES .getName (),
649
+ v -> new ExprDoubleValue (Math .toDegrees (v .doubleValue ())), DOUBLE );
649
650
}
650
651
651
652
/**
@@ -655,11 +656,8 @@ private static DefaultFunctionResolver degrees() {
655
656
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
656
657
*/
657
658
private static DefaultFunctionResolver radians () {
658
- return FunctionDSL .define (BuiltinFunctionName .RADIANS .getName (),
659
- ExprCoreType .numberTypes ().stream ()
660
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
661
- v -> new ExprDoubleValue (Math .toRadians (v .doubleValue ()))),
662
- DOUBLE , type )).collect (Collectors .toList ()));
659
+ return baseMathFunction (BuiltinFunctionName .RADIANS .getName (),
660
+ v -> new ExprDoubleValue (Math .toRadians (v .doubleValue ())), DOUBLE );
663
661
}
664
662
665
663
/**
@@ -669,11 +667,8 @@ private static DefaultFunctionResolver radians() {
669
667
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
670
668
*/
671
669
private static DefaultFunctionResolver sin () {
672
- return FunctionDSL .define (BuiltinFunctionName .SIN .getName (),
673
- ExprCoreType .numberTypes ().stream ()
674
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
675
- v -> new ExprDoubleValue (Math .sin (v .doubleValue ()))),
676
- DOUBLE , type )).collect (Collectors .toList ()));
670
+ return baseMathFunction (BuiltinFunctionName .SIN .getName (),
671
+ v -> new ExprDoubleValue (Math .sin (v .doubleValue ())), DOUBLE );
677
672
}
678
673
679
674
/**
@@ -683,10 +678,7 @@ private static DefaultFunctionResolver sin() {
683
678
* INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
684
679
*/
685
680
private static DefaultFunctionResolver tan () {
686
- return FunctionDSL .define (BuiltinFunctionName .TAN .getName (),
687
- ExprCoreType .numberTypes ().stream ()
688
- .map (type -> FunctionDSL .impl (FunctionDSL .nullMissingHandling (
689
- v -> new ExprDoubleValue (Math .tan (v .doubleValue ()))),
690
- DOUBLE , type )).collect (Collectors .toList ()));
681
+ return baseMathFunction (BuiltinFunctionName .TAN .getName (),
682
+ v -> new ExprDoubleValue (Math .tan (v .doubleValue ())), DOUBLE );
691
683
}
692
684
}
0 commit comments