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 #pragma once
19 
20 #include <cmath>
21 #include <memory>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "arrow/type.h"
27 #include "gandiva/condition.h"
28 #include "gandiva/decimal_scalar.h"
29 #include "gandiva/expression.h"
30 #include "gandiva/visibility.h"
31 
32 namespace gandiva {
33 
34 /// \brief Tree Builder for a nested expression.
35 class GANDIVA_EXPORT TreeExprBuilder {
36  public:
37   /// \brief create a node on a literal.
38   static NodePtr MakeLiteral(bool value);
39   static NodePtr MakeLiteral(uint8_t value);
40   static NodePtr MakeLiteral(uint16_t value);
41   static NodePtr MakeLiteral(uint32_t value);
42   static NodePtr MakeLiteral(uint64_t value);
43   static NodePtr MakeLiteral(int8_t value);
44   static NodePtr MakeLiteral(int16_t value);
45   static NodePtr MakeLiteral(int32_t value);
46   static NodePtr MakeLiteral(int64_t value);
47   static NodePtr MakeLiteral(float value);
48   static NodePtr MakeLiteral(double value);
49   static NodePtr MakeStringLiteral(const std::string& value);
50   static NodePtr MakeBinaryLiteral(const std::string& value);
51   static NodePtr MakeDecimalLiteral(const DecimalScalar128& value);
52 
53   /// \brief create a node on a null literal.
54   /// returns null if data_type is null or if it's not a supported datatype.
55   static NodePtr MakeNull(DataTypePtr data_type);
56 
57   /// \brief create a node on arrow field.
58   /// returns null if input is null.
59   static NodePtr MakeField(FieldPtr field);
60 
61   /// \brief create a node with a function.
62   /// returns null if return_type is null
63   static NodePtr MakeFunction(const std::string& name, const NodeVector& params,
64                               DataTypePtr return_type);
65 
66   /// \brief create a node with an if-else expression.
67   /// returns null if any of the inputs is null.
68   static NodePtr MakeIf(NodePtr condition, NodePtr then_node, NodePtr else_node,
69                         DataTypePtr result_type);
70 
71   /// \brief create a node with a boolean AND expression.
72   static NodePtr MakeAnd(const NodeVector& children);
73 
74   /// \brief create a node with a boolean OR expression.
75   static NodePtr MakeOr(const NodeVector& children);
76 
77   /// \brief create an expression with the specified root_node, and the
78   /// result written to result_field.
79   /// returns null if the result_field is null.
80   static ExpressionPtr MakeExpression(NodePtr root_node, FieldPtr result_field);
81 
82   /// \brief convenience function for simple function expressions.
83   /// returns null if the out_field is null.
84   static ExpressionPtr MakeExpression(const std::string& function,
85                                       const FieldVector& in_fields, FieldPtr out_field);
86 
87   /// \brief create a condition with the specified root_node
88   static ConditionPtr MakeCondition(NodePtr root_node);
89 
90   /// \brief convenience function for simple function conditions.
91   static ConditionPtr MakeCondition(const std::string& function,
92                                     const FieldVector& in_fields);
93 
94   /// \brief creates an in expression
95   static NodePtr MakeInExpressionInt32(NodePtr node,
96                                        const std::unordered_set<int32_t>& constants);
97 
98   static NodePtr MakeInExpressionInt64(NodePtr node,
99                                        const std::unordered_set<int64_t>& constants);
100 
101   static NodePtr MakeInExpressionDecimal(
102       NodePtr node, std::unordered_set<gandiva::DecimalScalar128>& constants);
103 
104   static NodePtr MakeInExpressionString(NodePtr node,
105                                         const std::unordered_set<std::string>& constants);
106 
107   static NodePtr MakeInExpressionBinary(NodePtr node,
108                                         const std::unordered_set<std::string>& constants);
109 
110   /// \brief creates an in expression for float
111   static NodePtr MakeInExpressionFloat(NodePtr node,
112                                        const std::unordered_set<float>& constants);
113 
114   /// \brief creates an in expression for double
115   static NodePtr MakeInExpressionDouble(NodePtr node,
116                                         const std::unordered_set<double>& constants);
117 
118   /// \brief Date as s/millis since epoch.
119   static NodePtr MakeInExpressionDate32(NodePtr node,
120                                         const std::unordered_set<int32_t>& constants);
121 
122   /// \brief Date as millis/us/ns since epoch.
123   static NodePtr MakeInExpressionDate64(NodePtr node,
124                                         const std::unordered_set<int64_t>& constants);
125 
126   /// \brief Time as s/millis of day
127   static NodePtr MakeInExpressionTime32(NodePtr node,
128                                         const std::unordered_set<int32_t>& constants);
129 
130   /// \brief Time as millis/us/ns of day
131   static NodePtr MakeInExpressionTime64(NodePtr node,
132                                         const std::unordered_set<int64_t>& constants);
133 
134   /// \brief Timestamp as millis since epoch.
135   static NodePtr MakeInExpressionTimeStamp(NodePtr node,
136                                            const std::unordered_set<int64_t>& constants);
137 };
138 
139 }  // namespace gandiva
140