| 68 | } |
| 69 | |
| 70 | TEST_F(TestDecimal, TestSimple) { |
| 71 | // schema for input fields |
| 72 | constexpr int32_t precision = 36; |
| 73 | constexpr int32_t scale = 18; |
| 74 | auto decimal_type = std::make_shared<arrow::Decimal128Type>(precision, scale); |
| 75 | auto field_a = field("a", decimal_type); |
| 76 | auto field_b = field("b", decimal_type); |
| 77 | auto field_c = field("c", decimal_type); |
| 78 | auto schema = arrow::schema({field_a, field_b, field_c}); |
| 79 | |
| 80 | Decimal128TypePtr add2_type; |
| 81 | auto status = DecimalTypeUtil::GetResultType(DecimalTypeUtil::kOpAdd, |
| 82 | {decimal_type, decimal_type}, &add2_type); |
| 83 | |
| 84 | Decimal128TypePtr output_type; |
| 85 | status = DecimalTypeUtil::GetResultType(DecimalTypeUtil::kOpAdd, |
| 86 | {add2_type, decimal_type}, &output_type); |
| 87 | |
| 88 | // output fields |
| 89 | auto res = field("res0", output_type); |
| 90 | |
| 91 | // build expression : a + b + c |
| 92 | auto node_a = TreeExprBuilder::MakeField(field_a); |
| 93 | auto node_b = TreeExprBuilder::MakeField(field_b); |
| 94 | auto node_c = TreeExprBuilder::MakeField(field_c); |
| 95 | auto add2 = TreeExprBuilder::MakeFunction("add", {node_a, node_b}, add2_type); |
| 96 | auto add3 = TreeExprBuilder::MakeFunction("add", {add2, node_c}, output_type); |
| 97 | auto expr = TreeExprBuilder::MakeExpression(add3, res); |
| 98 | |
| 99 | // Build a projector for the expression. |
| 100 | std::shared_ptr<Projector> projector; |
| 101 | status = Projector::Make(schema, {expr}, TestConfiguration(), &projector); |
| 102 | DCHECK_OK(status); |
| 103 | |
| 104 | // Create a row-batch with some sample data |
| 105 | int num_records = 4; |
| 106 | auto array_a = |
| 107 | MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"1", "2", "3", "4"}, scale), |
| 108 | {false, true, true, true}); |
| 109 | auto array_b = |
| 110 | MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"2", "3", "4", "5"}, scale), |
| 111 | {false, true, true, true}); |
| 112 | auto array_c = |
| 113 | MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"3", "4", "5", "6"}, scale), |
| 114 | {true, true, true, true}); |
| 115 | |
| 116 | // prepare input record batch |
| 117 | auto in_batch = |
| 118 | arrow::RecordBatch::Make(schema, num_records, {array_a, array_b, array_c}); |
| 119 | |
| 120 | auto expected = |
| 121 | MakeArrowArrayDecimal(output_type, MakeDecimalVector({"6", "9", "12", "15"}, scale), |
| 122 | {false, true, true, true}); |
| 123 | |
| 124 | // Evaluate expression |
| 125 | arrow::ArrayVector outputs; |
| 126 | status = projector->Evaluate(*in_batch, pool_, &outputs); |
| 127 | DCHECK_OK(status); |
nothing calls this directly
no test coverage detected