1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #include "gandiva/function_registry_math_ops.h"
19 #include "gandiva/function_registry_common.h"
20 
21 namespace gandiva {
22 
23 #define MATH_UNARY_OPS(name, ALIASES)                           \
24   UNARY_SAFE_NULL_IF_NULL(name, ALIASES, int32, float64),       \
25       UNARY_SAFE_NULL_IF_NULL(name, ALIASES, int64, float64),   \
26       UNARY_SAFE_NULL_IF_NULL(name, ALIASES, uint32, float64),  \
27       UNARY_SAFE_NULL_IF_NULL(name, ALIASES, uint64, float64),  \
28       UNARY_SAFE_NULL_IF_NULL(name, ALIASES, float32, float64), \
29       UNARY_SAFE_NULL_IF_NULL(name, ALIASES, float64, float64)
30 
31 #define MATH_BINARY_UNSAFE(name, ALIASES)                          \
32   BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, int32, float64),       \
33       BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, int64, float64),   \
34       BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, uint32, float64),  \
35       BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, uint64, float64),  \
36       BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, float32, float64), \
37       BINARY_UNSAFE_NULL_IF_NULL(name, ALIASES, float64, float64)
38 
39 #define UNARY_SAFE_NULL_NEVER_BOOL_FN(name, ALIASES) \
40   NUMERIC_BOOL_DATE_TYPES(UNARY_SAFE_NULL_NEVER_BOOL, name, ALIASES)
41 
42 #define BINARY_SAFE_NULL_NEVER_BOOL_FN(name, ALIASES) \
43   NUMERIC_BOOL_DATE_TYPES(BINARY_SAFE_NULL_NEVER_BOOL, name, ALIASES)
44 
GetMathOpsFunctionRegistry()45 std::vector<NativeFunction> GetMathOpsFunctionRegistry() {
46   static std::vector<NativeFunction> math_fn_registry_ = {
47       MATH_UNARY_OPS(cbrt, {}), MATH_UNARY_OPS(exp, {}), MATH_UNARY_OPS(log, {}),
48       MATH_UNARY_OPS(log10, {}),
49 
50       MATH_BINARY_UNSAFE(log, {}),
51 
52       BINARY_SYMMETRIC_SAFE_NULL_IF_NULL(power, {"pow"}, float64),
53 
54       UNARY_SAFE_NULL_NEVER_BOOL_FN(isnull, {}),
55       UNARY_SAFE_NULL_NEVER_BOOL_FN(isnotnull, {}),
56 
57       NUMERIC_TYPES(UNARY_SAFE_NULL_NEVER_BOOL, isnumeric, {}),
58 
59       BINARY_SAFE_NULL_NEVER_BOOL_FN(is_distinct_from, {}),
60       BINARY_SAFE_NULL_NEVER_BOOL_FN(is_not_distinct_from, {}),
61 
62       // decimal functions
63       UNARY_SAFE_NULL_IF_NULL(abs, {}, decimal128, decimal128),
64       UNARY_SAFE_NULL_IF_NULL(ceil, {}, decimal128, decimal128),
65       UNARY_SAFE_NULL_IF_NULL(floor, {}, decimal128, decimal128),
66       UNARY_SAFE_NULL_IF_NULL(round, {}, decimal128, decimal128),
67       UNARY_SAFE_NULL_IF_NULL(truncate, {"trunc"}, decimal128, decimal128),
68       BINARY_GENERIC_SAFE_NULL_IF_NULL(round, {}, decimal128, int32, decimal128),
69       BINARY_GENERIC_SAFE_NULL_IF_NULL(truncate, {"trunc"}, decimal128, int32,
70                                        decimal128),
71 
72       NativeFunction("truncate", {"trunc"}, DataTypeVector{int64(), int32()}, int64(),
73                      kResultNullIfNull, "truncate_int64_int32"),
74       NativeFunction("random", {"rand"}, DataTypeVector{}, float64(), kResultNullNever,
75                      "gdv_fn_random", NativeFunction::kNeedsFunctionHolder),
76       NativeFunction("random", {"rand"}, DataTypeVector{int32()}, float64(),
77                      kResultNullNever, "gdv_fn_random_with_seed",
78                      NativeFunction::kNeedsFunctionHolder)};
79 
80   return math_fn_registry_;
81 }
82 
83 #undef MATH_UNARY_OPS
84 
85 #undef MATH_BINARY_UNSAFE
86 
87 #undef UNARY_SAFE_NULL_NEVER_BOOL_FN
88 
89 #undef BINARY_SAFE_NULL_NEVER_BOOL_FN
90 
91 }  // namespace gandiva
92