Skip to content

Commit 7ee8edb

Browse files
projjalpraveenbingo
authored andcommitted
ARROW-13429: [C++][Gandiva] Fix Gandiva codegen for if-else expression with binary type
Closes apache#10775 from projjal/fixvarbinary and squashes the following commits: 060b142 <Projjal Chanda> Fixed codegen in if-else with binary type Authored-by: Projjal Chanda <iam@pchanda.com> Signed-off-by: Praveen <praveen@dremio.com>
1 parent 0c7e8b0 commit 7ee8edb

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

cpp/src/gandiva/llvm_generator.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,8 @@ LValuePtr LLVMGenerator::Visitor::BuildIfElse(llvm::Value* condition,
11391139

11401140
LValuePtr ret;
11411141
switch (result_type->id()) {
1142-
case arrow::Type::STRING: {
1142+
case arrow::Type::STRING:
1143+
case arrow::Type::BINARY: {
11431144
llvm::PHINode* result_length;
11441145
result_length = builder->CreatePHI(types->i32_type(), 2, "res_length");
11451146
result_length->addIncoming(then_lvalue->length(), then_bb);

cpp/src/gandiva/tests/binary_test.cc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
// under the License.
1717

1818
#include <gtest/gtest.h>
19+
1920
#include "arrow/memory_pool.h"
2021
#include "arrow/status.h"
21-
22+
#include "gandiva/node.h"
2223
#include "gandiva/projector.h"
2324
#include "gandiva/tests/test_util.h"
2425
#include "gandiva/tree_expr_builder.h"
@@ -86,4 +87,50 @@ TEST_F(TestBinary, TestSimple) {
8687
EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0));
8788
}
8889

90+
TEST_F(TestBinary, TestIfElse) {
91+
// schema for input fields
92+
auto field0 = field("f0", arrow::binary());
93+
auto field1 = field("f1", arrow::binary());
94+
95+
auto schema = arrow::schema({field0, field1});
96+
97+
auto f0 = TreeExprBuilder::MakeField(field0);
98+
auto f1 = TreeExprBuilder::MakeField(field1);
99+
100+
// output fields
101+
auto field_result = field("out", arrow::binary());
102+
103+
// Build expression
104+
auto cond = TreeExprBuilder::MakeFunction("isnotnull", {f0}, arrow::boolean());
105+
auto ifexpr = TreeExprBuilder::MakeIf(cond, f0, f1, arrow::binary());
106+
auto expr = TreeExprBuilder::MakeExpression(ifexpr, field_result);
107+
108+
// Build a projector for the expressions.
109+
std::shared_ptr<Projector> projector;
110+
auto status = Projector::Make(schema, {expr}, TestConfiguration(), &projector);
111+
EXPECT_TRUE(status.ok());
112+
113+
// Create a row-batch with some sample data
114+
int num_records = 4;
115+
auto array_f0 =
116+
MakeArrowArrayBinary({"foo", "hello", "hi", "bye"}, {true, true, true, false});
117+
auto array_f1 =
118+
MakeArrowArrayBinary({"fe", "fi", "fo", "fum"}, {true, true, true, true});
119+
120+
// expected output
121+
auto exp =
122+
MakeArrowArrayBinary({"foo", "hello", "hi", "fum"}, {true, true, true, true});
123+
124+
// prepare input record batch
125+
auto in_batch = arrow::RecordBatch::Make(schema, num_records, {array_f0, array_f1});
126+
127+
// Evaluate expression
128+
arrow::ArrayVector outputs;
129+
status = projector->Evaluate(*in_batch, pool_, &outputs);
130+
EXPECT_TRUE(status.ok());
131+
132+
// Validate results
133+
EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0));
134+
}
135+
89136
} // namespace gandiva

0 commit comments

Comments
 (0)