| 39 | using gandiva::TreeExprBuilder; |
| 40 | |
| 41 | Status Example() { |
| 42 | //(Doc section: Create expressions) |
| 43 | std::shared_ptr<arrow::Field> field_x_raw = arrow::field("x", arrow::int32()); |
| 44 | std::shared_ptr<Node> field_x = TreeExprBuilder::MakeField(field_x_raw); |
| 45 | std::shared_ptr<Node> literal_3 = TreeExprBuilder::MakeLiteral(3); |
| 46 | std::shared_ptr<arrow::Field> field_result = arrow::field("result", arrow::int32()); |
| 47 | |
| 48 | std::shared_ptr<Node> add_node = |
| 49 | TreeExprBuilder::MakeFunction("add", {field_x, literal_3}, arrow::int32()); |
| 50 | std::shared_ptr<Expression> expression = |
| 51 | TreeExprBuilder::MakeExpression(add_node, field_result); |
| 52 | |
| 53 | std::shared_ptr<Node> less_than_node = |
| 54 | TreeExprBuilder::MakeFunction("less_than", {field_x, literal_3}, arrow::boolean()); |
| 55 | std::shared_ptr<Condition> condition = TreeExprBuilder::MakeCondition(less_than_node); |
| 56 | //(Doc section: Create expressions) |
| 57 | |
| 58 | //(Doc section: Create projector and filter) |
| 59 | std::shared_ptr<arrow::Schema> input_schema = arrow::schema({field_x_raw}); |
| 60 | std::shared_ptr<arrow::Schema> output_schema = arrow::schema({field_result}); |
| 61 | std::shared_ptr<Projector> projector; |
| 62 | Status status; |
| 63 | std::vector<std::shared_ptr<Expression>> expressions = {expression}; |
| 64 | status = Projector::Make(input_schema, expressions, &projector); |
| 65 | ARROW_RETURN_NOT_OK(status); |
| 66 | |
| 67 | std::shared_ptr<Filter> filter; |
| 68 | status = Filter::Make(input_schema, condition, &filter); |
| 69 | ARROW_RETURN_NOT_OK(status); |
| 70 | //(Doc section: Create projector and filter) |
| 71 | |
| 72 | //(Doc section: Evaluate projection) |
| 73 | auto pool = arrow::default_memory_pool(); |
| 74 | int num_records = 4; |
| 75 | arrow::Int32Builder builder; |
| 76 | int32_t values[4] = {1, 2, 3, 4}; |
| 77 | ARROW_RETURN_NOT_OK(builder.AppendValues(values, 4)); |
| 78 | ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::Array> array, builder.Finish()); |
| 79 | auto in_batch = arrow::RecordBatch::Make(input_schema, num_records, {array}); |
| 80 | |
| 81 | arrow::ArrayVector outputs; |
| 82 | status = projector->Evaluate(*in_batch, pool, &outputs); |
| 83 | ARROW_RETURN_NOT_OK(status); |
| 84 | std::shared_ptr<arrow::RecordBatch> result = |
| 85 | arrow::RecordBatch::Make(output_schema, outputs[0]->length(), outputs); |
| 86 | //(Doc section: Evaluate projection) |
| 87 | |
| 88 | std::cout << "Project result:" << std::endl; |
| 89 | std::cout << result->ToString() << std::endl; |
| 90 | |
| 91 | //(Doc section: Evaluate filter) |
| 92 | std::shared_ptr<gandiva::SelectionVector> result_indices; |
| 93 | // Use 16-bit integers for indices. Result can be no longer than input size, |
| 94 | // so use batch num_rows as max_slots. |
| 95 | status = gandiva::SelectionVector::MakeInt16(/*max_slots=*/in_batch->num_rows(), pool, |
| 96 | &result_indices); |
| 97 | ARROW_RETURN_NOT_OK(status); |
| 98 | status = filter->Evaluate(*in_batch, result_indices); |
no test coverage detected