diff --git a/gandiva/proto/gandiva/types.proto b/gandiva/proto/gandiva/types.proto index 4ce342681d..a7b39546c7 100644 --- a/gandiva/proto/gandiva/types.proto +++ b/gandiva/proto/gandiva/types.proto @@ -117,6 +117,13 @@ message AndNode { repeated TreeNode args = 1; } +message CaseNode { + repeated TreeNode conditions = 1; + repeated TreeNode results = 2; + optional TreeNode elseNode = 3; + optional ExtGandivaType returnType = 4; +} + message OrNode { repeated TreeNode args = 1; } @@ -168,6 +175,7 @@ message TreeNode { optional IfNode ifNode = 6; optional AndNode andNode = 7; optional OrNode orNode = 8; + optional CaseNode caseNode = 9; // literals optional NullNode nullNode = 11; diff --git a/gandiva/src/main/cpp/jni_common.cc b/gandiva/src/main/cpp/jni_common.cc index 2851250072..50cd8a75af 100644 --- a/gandiva/src/main/cpp/jni_common.cc +++ b/gandiva/src/main/cpp/jni_common.cc @@ -319,6 +319,19 @@ NodePtr ProtoTypeToIfNode(const gandiva::types::IfNode& node) { return TreeExprBuilder::MakeIf(cond, then_node, else_node, return_type); } +NodePtr ProtoTypeToCaseNode(const gandiva::types::CaseNode& node) { + NodeVector conditions; + NodeVector results; + for (const auto&condition : node.conditions()) { + conditions.push_back(ProtoTypeToNode(condition)); + } + for (const auto&result : node.results()) { + results.push_back(ProtoTypeToNode(result)); + } + return TreeExprBuilder::MakeCase(conditions, results, ProtoTypeToNode(node.elsenode()), + ProtoTypeToDataType(node.returntype())); +} + NodePtr ProtoTypeToAndNode(const gandiva::types::AndNode& node) { NodeVector children; @@ -436,6 +449,10 @@ NodePtr ProtoTypeToNode(const gandiva::types::TreeNode& node) { return ProtoTypeToFnNode(node.fnnode()); } + if (node.has_casenode()) { + return ProtoTypeToCaseNode(node.casenode()); + } + if (node.has_ifnode()) { return ProtoTypeToIfNode(node.ifnode()); } diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/expression/CaseNode.java b/gandiva/src/main/java/org/apache/arrow/gandiva/expression/CaseNode.java new file mode 100644 index 0000000000..7747943199 --- /dev/null +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/expression/CaseNode.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.arrow.gandiva.expression; + +import java.util.List; +import java.util.Objects; +import org.apache.arrow.gandiva.exceptions.GandivaException; +import org.apache.arrow.gandiva.ipc.GandivaTypes; +import org.apache.arrow.vector.types.pojo.ArrowType; + +/** An ordered CASE expression without a nested ELSE chain. */ +class CaseNode implements TreeNode { + private final List conditions; + private final List results; + private final TreeNode elseNode; + private final ArrowType retType; + + CaseNode( + List conditions, List results, TreeNode elseNode, ArrowType retType) { + if (conditions.isEmpty() || conditions.size() != results.size()) { + throw new IllegalArgumentException("CASE requires matching conditions and results"); + } + this.conditions = List.copyOf(conditions); + this.results = List.copyOf(results); + this.elseNode = Objects.requireNonNull(elseNode); + this.retType = Objects.requireNonNull(retType); + } + + @Override + public GandivaTypes.TreeNode toProtobuf() throws GandivaException { + GandivaTypes.CaseNode.Builder caseNode = GandivaTypes.CaseNode.newBuilder(); + for (int i = 0; i < conditions.size(); i++) { + caseNode.addConditions(conditions.get(i).toProtobuf()); + caseNode.addResults(results.get(i).toProtobuf()); + } + caseNode.setElseNode(elseNode.toProtobuf()); + caseNode.setReturnType(ArrowTypeHelper.arrowTypeToProtobuf(retType)); + return GandivaTypes.TreeNode.newBuilder().setCaseNode(caseNode).build(); + } +} diff --git a/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java b/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java index 96838e5ad1..ce7ddc9772 100644 --- a/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java +++ b/gandiva/src/main/java/org/apache/arrow/gandiva/expression/TreeBuilder.java @@ -101,6 +101,12 @@ public static TreeNode makeIf( return new IfNode(condition, thenNode, elseNode, retType); } + /** Creates an ordered CASE expression with first-match semantics. */ + public static TreeNode makeCase( + List conditions, List results, TreeNode elseNode, ArrowType retType) { + return new CaseNode(conditions, results, elseNode, retType); + } + /** * Invoke this function to create a node representing an and-clause. * diff --git a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java index f2d626f7ab..1d07a4bb5e 100644 --- a/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java +++ b/gandiva/src/test/java/org/apache/arrow/gandiva/evaluator/ProjectorTest.java @@ -380,6 +380,38 @@ public void testMakeProjectorValidationError() throws InterruptedException { assertTrue(exceptionThrown); } + @Test + public void testFlatCaseBeyondProtobufRecursionLimit() throws Exception { + Field input = Field.nullable("a", int32); + TreeNode a = TreeBuilder.makeField(input); + List conditions = new ArrayList<>(); + List results = new ArrayList<>(); + int branches = 5000; + for (int i = 0; i < branches; i++) { + conditions.add( + TreeBuilder.makeFunction( + "equal", List.of(a, TreeBuilder.makeLiteral(i)), new ArrowType.Bool())); + results.add(TreeBuilder.makeLiteral(i + 10)); + } + TreeNode node = TreeBuilder.makeCase(conditions, results, TreeBuilder.makeNull(int32), int32); + ExpressionTree expression = TreeBuilder.makeExpression(node, Field.nullable("out", int32)); + Schema schema = new Schema(List.of(input)); + Projector projector = Projector.make(schema, List.of(expression)); + try (ArrowBuf validity = buf(new byte[] {(byte) 15}); + ArrowBuf values = intBuf(new int[] {0, 2500, 4999, 5000, 0}); + ArrowRecordBatch batch = + new ArrowRecordBatch(5, List.of(new ArrowFieldNode(5, 1)), List.of(validity, values)); + IntVector output = new IntVector("out", allocator)) { + output.allocateNew(5); + projector.evaluate(batch, List.of(output)); + assertEquals(List.of(10, 2510, 5009), List.of(output.get(0), output.get(1), output.get(2))); + assertTrue(output.isNull(3)); + assertTrue(output.isNull(4)); + } finally { + projector.close(); + } + } + @Test public void testEvaluate() throws GandivaException, Exception { Field a = Field.nullable("a", int32); diff --git a/gandiva/src/test/java/org/apache/arrow/gandiva/expression/TreeBuilderTest.java b/gandiva/src/test/java/org/apache/arrow/gandiva/expression/TreeBuilderTest.java index 63b31f1fb3..5aa70ca951 100644 --- a/gandiva/src/test/java/org/apache/arrow/gandiva/expression/TreeBuilderTest.java +++ b/gandiva/src/test/java/org/apache/arrow/gandiva/expression/TreeBuilderTest.java @@ -18,8 +18,10 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import com.google.protobuf.CodedInputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -32,6 +34,49 @@ public class TreeBuilderTest { + @Test + public void testFlatCaseSerialization() throws Exception { + ArrowType type = new ArrowType.Int(32, true); + List conditions = new ArrayList<>(); + List results = new ArrayList<>(); + GandivaTypes.CaseNode.Builder expected = GandivaTypes.CaseNode.newBuilder(); + for (int i = 0; i < 5000; i++) { + TreeNode condition = TreeBuilder.makeLiteral(i % 2 == 0); + TreeNode result = TreeBuilder.makeLiteral(i); + conditions.add(condition); + results.add(result); + expected.addConditions(condition.toProtobuf()); + expected.addResults(result.toProtobuf()); + } + TreeNode fallback = TreeBuilder.makeNull(type); + TreeNode node = TreeBuilder.makeCase(conditions, results, fallback, type); + conditions.clear(); + results.clear(); + expected.setElseNode(fallback.toProtobuf()); + expected.setReturnType(ArrowTypeHelper.arrowTypeToProtobuf(type)); + GandivaTypes.TreeNode expectedTree = + GandivaTypes.TreeNode.newBuilder().setCaseNode(expected).build(); + CodedInputStream input = CodedInputStream.newInstance(node.toProtobuf().toByteArray()); + input.setRecursionLimit(8); + assertEquals(expectedTree, GandivaTypes.TreeNode.parseFrom(input)); + } + + @Test + public void testFlatCaseRejectsMismatchedArms() { + TreeNode fallback = TreeBuilder.makeLiteral(0); + assertThrows( + IllegalArgumentException.class, + () -> TreeBuilder.makeCase(List.of(), List.of(), fallback, new ArrowType.Int(32, true))); + assertThrows( + IllegalArgumentException.class, + () -> + TreeBuilder.makeCase( + List.of(TreeBuilder.makeLiteral(true)), + List.of(), + fallback, + new ArrowType.Int(32, true))); + } + @Test public void testMakeLiteral() throws GandivaException { TreeNode n = TreeBuilder.makeLiteral(Boolean.TRUE);