| 41 | }; |
| 42 | |
| 43 | TEST_F(TestUtf8, TestSimple) { |
| 44 | // schema for input fields |
| 45 | auto field_a = field("a", utf8()); |
| 46 | auto schema = arrow::schema({field_a}); |
| 47 | |
| 48 | // output fields |
| 49 | auto res_1 = field("res1", int32()); |
| 50 | auto res_2 = field("res2", boolean()); |
| 51 | auto res_3 = field("res3", int32()); |
| 52 | |
| 53 | // build expressions. |
| 54 | // octet_length(a) |
| 55 | // octet_length(a) == bit_length(a) / 8 |
| 56 | // length(a) |
| 57 | auto expr_a = TreeExprBuilder::MakeExpression("octet_length", {field_a}, res_1); |
| 58 | |
| 59 | auto node_a = TreeExprBuilder::MakeField(field_a); |
| 60 | auto octet_length = TreeExprBuilder::MakeFunction("octet_length", {node_a}, int32()); |
| 61 | auto literal_8 = TreeExprBuilder::MakeLiteral((int32_t)8); |
| 62 | auto bit_length = TreeExprBuilder::MakeFunction("bit_length", {node_a}, int32()); |
| 63 | auto div_8 = TreeExprBuilder::MakeFunction("divide", {bit_length, literal_8}, int32()); |
| 64 | auto is_equal = |
| 65 | TreeExprBuilder::MakeFunction("equal", {octet_length, div_8}, boolean()); |
| 66 | auto expr_b = TreeExprBuilder::MakeExpression(is_equal, res_2); |
| 67 | auto expr_c = TreeExprBuilder::MakeExpression("length", {field_a}, res_3); |
| 68 | |
| 69 | // Build a projector for the expressions. |
| 70 | std::shared_ptr<Projector> projector; |
| 71 | auto status = |
| 72 | Projector::Make(schema, {expr_a, expr_b, expr_c}, TestConfiguration(), &projector); |
| 73 | EXPECT_TRUE(status.ok()) << status.message(); |
| 74 | |
| 75 | // Create a row-batch with some sample data |
| 76 | int num_records = 5; |
| 77 | auto array_a = MakeArrowArrayUtf8({"foo", "hello", "bye", "hi", "मदन"}, |
| 78 | {true, true, false, true, true}); |
| 79 | |
| 80 | // expected output |
| 81 | auto exp_1 = MakeArrowArrayInt32({3, 5, 0, 2, 9}, {true, true, false, true, true}); |
| 82 | auto exp_2 = MakeArrowArrayBool({true, true, false, true, true}, |
| 83 | {true, true, false, true, true}); |
| 84 | auto exp_3 = MakeArrowArrayInt32({3, 5, 0, 2, 3}, {true, true, false, true, true}); |
| 85 | |
| 86 | // prepare input record batch |
| 87 | auto in_batch = arrow::RecordBatch::Make(schema, num_records, {array_a}); |
| 88 | |
| 89 | // Evaluate expression |
| 90 | arrow::ArrayVector outputs; |
| 91 | status = projector->Evaluate(*in_batch, pool_, &outputs); |
| 92 | EXPECT_TRUE(status.ok()); |
| 93 | |
| 94 | // Validate results |
| 95 | EXPECT_ARROW_ARRAY_EQUALS(exp_1, outputs.at(0)); |
| 96 | EXPECT_ARROW_ARRAY_EQUALS(exp_2, outputs.at(1)); |
| 97 | EXPECT_ARROW_ARRAY_EQUALS(exp_3, outputs.at(2)); |
| 98 | } |
| 99 | |
| 100 | TEST_F(TestUtf8, TestLiteral) { |
nothing calls this directly
no test coverage detected