3232import org .opensearch .sql .data .model .ExprNullValue ;
3333import org .opensearch .sql .data .model .ExprShortValue ;
3434import org .opensearch .sql .data .model .ExprStringValue ;
35+ import org .opensearch .sql .data .model .ExprValue ;
3536import org .opensearch .sql .data .type .ExprCoreType ;
3637import org .opensearch .sql .data .type .ExprType ;
3738import org .opensearch .sql .expression .function .BuiltinFunctionName ;
@@ -59,6 +60,7 @@ public static void register(BuiltinFunctionRepository repository) {
5960 repository .register (crc32 ());
6061 repository .register (euler ());
6162 repository .register (exp ());
63+ repository .register (expm1 ());
6264 repository .register (floor ());
6365 repository .register (ln ());
6466 repository .register (log ());
@@ -85,6 +87,23 @@ public static void register(BuiltinFunctionRepository repository) {
8587 repository .register (tan ());
8688 }
8789
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+
88107 /**
89108 * Definition of abs() function. The supported signature of abs() function are INT -> INT LONG ->
90109 * LONG FLOAT -> FLOAT DOUBLE -> DOUBLE
@@ -186,15 +205,21 @@ private static DefaultFunctionResolver euler() {
186205 }
187206
188207 /**
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
191210 */
192211 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 );
198223 }
199224
200225 /**
@@ -214,11 +239,8 @@ private static DefaultFunctionResolver floor() {
214239 * ln function is INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
215240 */
216241 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 );
222244 }
223245
224246 /**
@@ -255,23 +277,17 @@ private static DefaultFunctionResolver log() {
255277 * log function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
256278 */
257279 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 );
263282 }
264283
265284 /**
266285 * Definition of log2(x) function. Calculate base-2 logarithm of x The supported signature of log
267286 * function is SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
268287 */
269288 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 );
275291 }
276292
277293 /**
@@ -450,11 +466,8 @@ private static DefaultFunctionResolver round() {
450466 * SHORT/INTEGER/LONG/FLOAT/DOUBLE -> INTEGER
451467 */
452468 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 );
458471 }
459472
460473 /**
@@ -464,12 +477,9 @@ private static DefaultFunctionResolver sign() {
464477 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
465478 */
466479 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 );
473483 }
474484
475485 /**
@@ -479,11 +489,8 @@ private static DefaultFunctionResolver sqrt() {
479489 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
480490 */
481491 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 );
487494 }
488495
489496 /**
@@ -606,11 +613,8 @@ private static DefaultFunctionResolver atan2() {
606613 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
607614 */
608615 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 );
614618 }
615619
616620 /**
@@ -641,11 +645,8 @@ private static DefaultFunctionResolver cot() {
641645 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
642646 */
643647 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 );
649650 }
650651
651652 /**
@@ -655,11 +656,8 @@ private static DefaultFunctionResolver degrees() {
655656 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
656657 */
657658 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 );
663661 }
664662
665663 /**
@@ -669,11 +667,8 @@ private static DefaultFunctionResolver radians() {
669667 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
670668 */
671669 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 );
677672 }
678673
679674 /**
@@ -683,10 +678,7 @@ private static DefaultFunctionResolver sin() {
683678 * INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
684679 */
685680 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 );
691683 }
692684}
0 commit comments