Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions gandiva/proto/gandiva/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions gandiva/src/main/cpp/jni_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TreeNode> conditions;
private final List<TreeNode> results;
private final TreeNode elseNode;
private final ArrowType retType;

CaseNode(
List<TreeNode> conditions, List<TreeNode> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeNode> conditions, List<TreeNode> results, TreeNode elseNode, ArrowType retType) {
return new CaseNode(conditions, results, elseNode, retType);
}

/**
* Invoke this function to create a node representing an and-clause.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TreeNode> conditions = new ArrayList<>();
List<TreeNode> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +34,49 @@

public class TreeBuilderTest {

@Test
public void testFlatCaseSerialization() throws Exception {
ArrowType type = new ArrowType.Int(32, true);
List<TreeNode> conditions = new ArrayList<>();
List<TreeNode> 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);
Expand Down
Loading