]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / gandiva / src / main / java / org / apache / arrow / gandiva / expression / TreeBuilder.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * 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, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.gandiva.expression;
19
20 import java.math.BigDecimal;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.arrow.vector.types.pojo.ArrowType;
26 import org.apache.arrow.vector.types.pojo.Field;
27
28 /**
29 * Contains helper functions for constructing expression trees.
30 */
31 public class TreeBuilder {
32 private TreeBuilder() {}
33
34 /**
35 * Helper functions to create literal constants.
36 */
37 public static TreeNode makeLiteral(Boolean booleanConstant) {
38 return new BooleanNode(booleanConstant);
39 }
40
41 public static TreeNode makeLiteral(Float floatConstant) {
42 return new FloatNode(floatConstant);
43 }
44
45 public static TreeNode makeLiteral(Double doubleConstant) {
46 return new DoubleNode(doubleConstant);
47 }
48
49 public static TreeNode makeLiteral(Integer integerConstant) {
50 return new IntNode(integerConstant);
51 }
52
53 public static TreeNode makeLiteral(Long longConstant) {
54 return new LongNode(longConstant);
55 }
56
57 public static TreeNode makeStringLiteral(String stringConstant) {
58 return new StringNode(stringConstant);
59 }
60
61 public static TreeNode makeBinaryLiteral(byte[] binaryConstant) {
62 return new BinaryNode(binaryConstant);
63 }
64
65 public static TreeNode makeDecimalLiteral(String decimalConstant, int precision, int scale) {
66 return new DecimalNode(decimalConstant, precision, scale);
67 }
68
69 /**
70 * create a null literal.
71 */
72 public static TreeNode makeNull(ArrowType type) {
73 return new NullNode(type);
74 }
75
76 /**
77 * Invoke this function to create a node representing a field, e.g. a column name.
78 *
79 * @param field represents the input argument - includes the name and type of the field
80 * @return Node representing a field
81 */
82 public static TreeNode makeField(Field field) {
83 return new FieldNode(field);
84 }
85
86 /**
87 * Invoke this function to create a node representing a function.
88 *
89 * @param function Name of the function, e.g. add
90 * @param children The arguments to the function
91 * @param retType The type of the return value of the operator
92 * @return Node representing a function
93 */
94 public static TreeNode makeFunction(String function,
95 List<TreeNode> children,
96 ArrowType retType) {
97 return new FunctionNode(function, children, retType);
98 }
99
100 /**
101 * Invoke this function to create a node representing an if-clause.
102 *
103 * @param condition Node representing the condition
104 * @param thenNode Node representing the if-block
105 * @param elseNode Node representing the else-block
106 * @param retType Return type of the node
107 * @return Node representing an if-clause
108 */
109 public static TreeNode makeIf(TreeNode condition,
110 TreeNode thenNode,
111 TreeNode elseNode,
112 ArrowType retType) {
113 return new IfNode(condition, thenNode, elseNode, retType);
114 }
115
116 /**
117 * Invoke this function to create a node representing an and-clause.
118 *
119 * @param nodes Nodes in the 'and' clause.
120 * @return Node representing an and-clause
121 */
122 public static TreeNode makeAnd(List<TreeNode> nodes) {
123 return new AndNode(nodes);
124 }
125
126 /**
127 * Invoke this function to create a node representing an or-clause.
128 *
129 * @param nodes Nodes in the 'or' clause.
130 * @return Node representing an or-clause
131 */
132 public static TreeNode makeOr(List<TreeNode> nodes) {
133 return new OrNode(nodes);
134 }
135
136 /**
137 * Invoke this function to create an expression tree.
138 *
139 * @param root is returned by a call to MakeField, MakeFunction, or MakeIf
140 * @param resultField represents the return value of the expression
141 * @return ExpressionTree referring to the root of an expression tree
142 */
143 public static ExpressionTree makeExpression(TreeNode root,
144 Field resultField) {
145 return new ExpressionTree(root, resultField);
146 }
147
148 /**
149 * Short cut to create an expression tree involving a single function, e.g. a+b+c.
150 *
151 * @param function Name of the function, e.g. add()
152 * @param inFields In arguments to the function
153 * @param resultField represents the return value of the expression
154 * @return ExpressionTree referring to the root of an expression tree
155 */
156 public static ExpressionTree makeExpression(String function,
157 List<Field> inFields,
158 Field resultField) {
159 List<TreeNode> children = new ArrayList<TreeNode>(inFields.size());
160 for (Field field : inFields) {
161 children.add(makeField(field));
162 }
163
164 TreeNode root = makeFunction(function, children, resultField.getType());
165 return makeExpression(root, resultField);
166 }
167
168 /**
169 * Invoke this function to create a condition.
170 *
171 * @param root is returned by a call to MakeField, MakeFunction, MakeIf, ..
172 * @return condition referring to the root of an expression tree
173 */
174 public static Condition makeCondition(TreeNode root) {
175 return new Condition(root);
176 }
177
178 /**
179 * Short cut to create an expression tree involving a single function, e.g. a+b+c.
180 *
181 * @param function Name of the function, e.g. add()
182 * @param inFields In arguments to the function
183 * @return condition referring to the root of an expression tree
184 */
185 public static Condition makeCondition(String function,
186 List<Field> inFields) {
187 List<TreeNode> children = new ArrayList<>(inFields.size());
188 for (Field field : inFields) {
189 children.add(makeField(field));
190 }
191
192 TreeNode root = makeFunction(function, children, new ArrowType.Bool());
193 return makeCondition(root);
194 }
195
196 public static TreeNode makeInExpressionInt32(TreeNode resultNode,
197 Set<Integer> intValues) {
198 return InNode.makeIntInExpr(resultNode, intValues);
199 }
200
201 public static TreeNode makeInExpressionBigInt(TreeNode resultNode,
202 Set<Long> longValues) {
203 return InNode.makeLongInExpr(resultNode, longValues);
204 }
205
206 public static TreeNode makeInExpressionDecimal(TreeNode resultNode,
207 Set<BigDecimal> decimalValues, Integer precision, Integer scale) {
208 return InNode.makeDecimalInExpr(resultNode, decimalValues, precision, scale);
209 }
210
211 public static TreeNode makeInExpressionFloat(TreeNode resultNode,
212 Set<Float> floatValues) {
213 return InNode.makeFloatInExpr(resultNode, floatValues);
214 }
215
216 public static TreeNode makeInExpressionDouble(TreeNode resultNode,
217 Set<Double> doubleValues) {
218 return InNode.makeDoubleInExpr(resultNode, doubleValues);
219 }
220
221 public static TreeNode makeInExpressionString(TreeNode resultNode,
222 Set<String> stringValues) {
223 return InNode.makeStringInExpr(resultNode, stringValues);
224 }
225
226 public static TreeNode makeInExpressionBinary(TreeNode resultNode,
227 Set<byte[]> binaryValues) {
228 return InNode.makeBinaryInExpr(resultNode, binaryValues);
229 }
230 }