|
16 | 16 | // under the License. |
17 | 17 |
|
18 | 18 | #include <gtest/gtest.h> |
| 19 | + |
19 | 20 | #include "arrow/memory_pool.h" |
20 | 21 | #include "arrow/status.h" |
21 | | - |
| 22 | +#include "gandiva/node.h" |
22 | 23 | #include "gandiva/projector.h" |
23 | 24 | #include "gandiva/tests/test_util.h" |
24 | 25 | #include "gandiva/tree_expr_builder.h" |
@@ -86,4 +87,50 @@ TEST_F(TestBinary, TestSimple) { |
86 | 87 | EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0)); |
87 | 88 | } |
88 | 89 |
|
| 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 | + |
89 | 136 | } // namespace gandiva |
0 commit comments