]>
git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.arrow
.gandiva
.expression
;
20 import java
.math
.BigDecimal
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
25 import org
.apache
.arrow
.vector
.types
.pojo
.ArrowType
;
26 import org
.apache
.arrow
.vector
.types
.pojo
.Field
;
29 * Contains helper functions for constructing expression trees.
31 public class TreeBuilder
{
32 private TreeBuilder() {}
35 * Helper functions to create literal constants.
37 public static TreeNode
makeLiteral(Boolean booleanConstant
) {
38 return new BooleanNode(booleanConstant
);
41 public static TreeNode
makeLiteral(Float floatConstant
) {
42 return new FloatNode(floatConstant
);
45 public static TreeNode
makeLiteral(Double doubleConstant
) {
46 return new DoubleNode(doubleConstant
);
49 public static TreeNode
makeLiteral(Integer integerConstant
) {
50 return new IntNode(integerConstant
);
53 public static TreeNode
makeLiteral(Long longConstant
) {
54 return new LongNode(longConstant
);
57 public static TreeNode
makeStringLiteral(String stringConstant
) {
58 return new StringNode(stringConstant
);
61 public static TreeNode
makeBinaryLiteral(byte[] binaryConstant
) {
62 return new BinaryNode(binaryConstant
);
65 public static TreeNode
makeDecimalLiteral(String decimalConstant
, int precision
, int scale
) {
66 return new DecimalNode(decimalConstant
, precision
, scale
);
70 * create a null literal.
72 public static TreeNode
makeNull(ArrowType type
) {
73 return new NullNode(type
);
77 * Invoke this function to create a node representing a field, e.g. a column name.
79 * @param field represents the input argument - includes the name and type of the field
80 * @return Node representing a field
82 public static TreeNode
makeField(Field field
) {
83 return new FieldNode(field
);
87 * Invoke this function to create a node representing a function.
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
94 public static TreeNode
makeFunction(String function
,
95 List
<TreeNode
> children
,
97 return new FunctionNode(function
, children
, retType
);
101 * Invoke this function to create a node representing an if-clause.
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
109 public static TreeNode
makeIf(TreeNode condition
,
113 return new IfNode(condition
, thenNode
, elseNode
, retType
);
117 * Invoke this function to create a node representing an and-clause.
119 * @param nodes Nodes in the 'and' clause.
120 * @return Node representing an and-clause
122 public static TreeNode
makeAnd(List
<TreeNode
> nodes
) {
123 return new AndNode(nodes
);
127 * Invoke this function to create a node representing an or-clause.
129 * @param nodes Nodes in the 'or' clause.
130 * @return Node representing an or-clause
132 public static TreeNode
makeOr(List
<TreeNode
> nodes
) {
133 return new OrNode(nodes
);
137 * Invoke this function to create an expression tree.
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
143 public static ExpressionTree
makeExpression(TreeNode root
,
145 return new ExpressionTree(root
, resultField
);
149 * Short cut to create an expression tree involving a single function, e.g. a+b+c.
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
156 public static ExpressionTree
makeExpression(String function
,
157 List
<Field
> inFields
,
159 List
<TreeNode
> children
= new ArrayList
<TreeNode
>(inFields
.size());
160 for (Field field
: inFields
) {
161 children
.add(makeField(field
));
164 TreeNode root
= makeFunction(function
, children
, resultField
.getType());
165 return makeExpression(root
, resultField
);
169 * Invoke this function to create a condition.
171 * @param root is returned by a call to MakeField, MakeFunction, MakeIf, ..
172 * @return condition referring to the root of an expression tree
174 public static Condition
makeCondition(TreeNode root
) {
175 return new Condition(root
);
179 * Short cut to create an expression tree involving a single function, e.g. a+b+c.
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
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
));
192 TreeNode root
= makeFunction(function
, children
, new ArrowType
.Bool());
193 return makeCondition(root
);
196 public static TreeNode
makeInExpressionInt32(TreeNode resultNode
,
197 Set
<Integer
> intValues
) {
198 return InNode
.makeIntInExpr(resultNode
, intValues
);
201 public static TreeNode
makeInExpressionBigInt(TreeNode resultNode
,
202 Set
<Long
> longValues
) {
203 return InNode
.makeLongInExpr(resultNode
, longValues
);
206 public static TreeNode
makeInExpressionDecimal(TreeNode resultNode
,
207 Set
<BigDecimal
> decimalValues
, Integer precision
, Integer scale
) {
208 return InNode
.makeDecimalInExpr(resultNode
, decimalValues
, precision
, scale
);
211 public static TreeNode
makeInExpressionFloat(TreeNode resultNode
,
212 Set
<Float
> floatValues
) {
213 return InNode
.makeFloatInExpr(resultNode
, floatValues
);
216 public static TreeNode
makeInExpressionDouble(TreeNode resultNode
,
217 Set
<Double
> doubleValues
) {
218 return InNode
.makeDoubleInExpr(resultNode
, doubleValues
);
221 public static TreeNode
makeInExpressionString(TreeNode resultNode
,
222 Set
<String
> stringValues
) {
223 return InNode
.makeStringInExpr(resultNode
, stringValues
);
226 public static TreeNode
makeInExpressionBinary(TreeNode resultNode
,
227 Set
<byte[]> binaryValues
) {
228 return InNode
.makeBinaryInExpr(resultNode
, binaryValues
);