|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <arrow/api.h> |
| 19 | +#include <arrow/compute/api.h> |
| 20 | +#include <arrow/engine/api.h> |
| 21 | + |
| 22 | +#include <cstdlib> |
| 23 | +#include <iostream> |
| 24 | +#include <memory> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +namespace eng = arrow::engine; |
| 28 | +namespace cp = arrow::compute; |
| 29 | + |
| 30 | +#define ABORT_ON_FAILURE(expr) \ |
| 31 | + do { \ |
| 32 | + arrow::Status status_ = (expr); \ |
| 33 | + if (!status_.ok()) { \ |
| 34 | + std::cerr << status_.message() << std::endl; \ |
| 35 | + abort(); \ |
| 36 | + } \ |
| 37 | + } while (0); |
| 38 | + |
| 39 | +class IgnoringConsumer : public cp::SinkNodeConsumer { |
| 40 | + public: |
| 41 | + explicit IgnoringConsumer(size_t tag) : tag_{tag} {} |
| 42 | + |
| 43 | + arrow::Status Consume(cp::ExecBatch batch) override { |
| 44 | + // Consume a batch of data |
| 45 | + // (just print its row count to stdout) |
| 46 | + std::cout << "-" << tag_ << " consumed " << batch.length << " rows" << std::endl; |
| 47 | + return arrow::Status::OK(); |
| 48 | + } |
| 49 | + |
| 50 | + arrow::Future<> Finish() override { |
| 51 | + // Signal to the consumer that the last batch has been delivered |
| 52 | + // (we don't do any real work in this consumer so mark it finished immediately) |
| 53 | + // |
| 54 | + // The returned future should only finish when all outstanding tasks have completed |
| 55 | + // (after this method is called Consume is guaranteed not to be called again) |
| 56 | + std::cout << "-" << tag_ << " finished" << std::endl; |
| 57 | + return arrow::Future<>::MakeFinished(); |
| 58 | + } |
| 59 | + |
| 60 | + private: |
| 61 | + // A unique label for instances to help distinguish logging output if a plan has |
| 62 | + // multiple sinks |
| 63 | + // |
| 64 | + // In this example, this is set to the zero-based index of the relation tree in the plan |
| 65 | + size_t tag_; |
| 66 | +}; |
| 67 | + |
| 68 | +arrow::Future<std::shared_ptr<arrow::Buffer>> GetSubstraitFromServer( |
| 69 | + const std::string& filename) { |
| 70 | + // Emulate server interaction by parsing hard coded JSON |
| 71 | + std::string substrait_json = R"({ |
| 72 | + "relations": [ |
| 73 | + {"rel": { |
| 74 | + "read": { |
| 75 | + "base_schema": { |
| 76 | + "struct": { |
| 77 | + "types": [ {"i64": {}}, {"bool": {}} ] |
| 78 | + }, |
| 79 | + "names": ["i", "b"] |
| 80 | + }, |
| 81 | + "local_files": { |
| 82 | + "items": [ |
| 83 | + { |
| 84 | + "uri_file": "file://FILENAME_PLACEHOLDER", |
| 85 | + "format": "FILE_FORMAT_PARQUET" |
| 86 | + } |
| 87 | + ] |
| 88 | + } |
| 89 | + } |
| 90 | + }} |
| 91 | + ], |
| 92 | + "extension_uris": [ |
| 93 | + { |
| 94 | + "extension_uri_anchor": 7, |
| 95 | + "uri": "https://github.com/apache/arrow/blob/master/format/substrait/extension_types.yaml" |
| 96 | + } |
| 97 | + ], |
| 98 | + "extensions": [ |
| 99 | + {"extension_type": { |
| 100 | + "extension_uri_reference": 7, |
| 101 | + "type_anchor": 42, |
| 102 | + "name": "null" |
| 103 | + }}, |
| 104 | + {"extension_type_variation": { |
| 105 | + "extension_uri_reference": 7, |
| 106 | + "type_variation_anchor": 23, |
| 107 | + "name": "u8" |
| 108 | + }}, |
| 109 | + {"extension_function": { |
| 110 | + "extension_uri_reference": 7, |
| 111 | + "function_anchor": 42, |
| 112 | + "name": "add" |
| 113 | + }} |
| 114 | + ] |
| 115 | + })"; |
| 116 | + std::string filename_placeholder = "FILENAME_PLACEHOLDER"; |
| 117 | + substrait_json.replace(substrait_json.find(filename_placeholder), |
| 118 | + filename_placeholder.size(), filename); |
| 119 | + return eng::internal::SubstraitFromJSON("Plan", substrait_json); |
| 120 | +} |
| 121 | + |
| 122 | +int main(int argc, char** argv) { |
| 123 | + if (argc < 2) { |
| 124 | + std::cout << "Please specify a parquet file to scan" << std::endl; |
| 125 | + // Fake pass for CI |
| 126 | + return EXIT_SUCCESS; |
| 127 | + } |
| 128 | + |
| 129 | + // Plans arrive at the consumer serialized in a Buffer, using the binary protobuf |
| 130 | + // serialization of a substrait Plan |
| 131 | + auto maybe_serialized_plan = GetSubstraitFromServer(argv[1]).result(); |
| 132 | + ABORT_ON_FAILURE(maybe_serialized_plan.status()); |
| 133 | + std::shared_ptr<arrow::Buffer> serialized_plan = |
| 134 | + std::move(maybe_serialized_plan).ValueOrDie(); |
| 135 | + |
| 136 | + // Print the received plan to stdout as JSON |
| 137 | + arrow::Result<std::string> maybe_plan_json = |
| 138 | + eng::internal::SubstraitToJSON("Plan", *serialized_plan); |
| 139 | + ABORT_ON_FAILURE(maybe_plan_json.status()); |
| 140 | + std::cout << std::string(50, '#') << " received substrait::Plan:" << std::endl; |
| 141 | + std::cout << maybe_plan_json.ValueOrDie() << std::endl; |
| 142 | + |
| 143 | + // The data sink(s) for plans is/are implicit in substrait plans, but explicit in |
| 144 | + // Arrow. Therefore, deserializing a plan requires a factory for consumers: each |
| 145 | + // time the root of a substrait relation tree is deserialized, an Arrow consumer is |
| 146 | + // constructed into which its batches will be piped. |
| 147 | + std::vector<std::shared_ptr<cp::SinkNodeConsumer>> consumers; |
| 148 | + std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] { |
| 149 | + // All batches produced by the plan will be fed into IgnoringConsumers: |
| 150 | + auto tag = consumers.size(); |
| 151 | + consumers.emplace_back(new IgnoringConsumer{tag}); |
| 152 | + return consumers.back(); |
| 153 | + }; |
| 154 | + |
| 155 | + // Deserialize each relation tree in the substrait plan to an Arrow compute Declaration |
| 156 | + arrow::Result<std::vector<cp::Declaration>> maybe_decls = |
| 157 | + eng::DeserializePlan(*serialized_plan, consumer_factory); |
| 158 | + ABORT_ON_FAILURE(maybe_decls.status()); |
| 159 | + std::vector<cp::Declaration> decls = std::move(maybe_decls).ValueOrDie(); |
| 160 | + |
| 161 | + // It's safe to drop the serialized plan; we don't leave references to its memory |
| 162 | + serialized_plan.reset(); |
| 163 | + |
| 164 | + // Construct an empty plan (note: configure Function registry and ThreadPool here) |
| 165 | + arrow::Result<std::shared_ptr<cp::ExecPlan>> maybe_plan = cp::ExecPlan::Make(); |
| 166 | + ABORT_ON_FAILURE(maybe_plan.status()); |
| 167 | + std::shared_ptr<cp::ExecPlan> plan = std::move(maybe_plan).ValueOrDie(); |
| 168 | + |
| 169 | + // Add decls to plan (note: configure ExecNode registry before this point) |
| 170 | + for (const cp::Declaration& decl : decls) { |
| 171 | + ABORT_ON_FAILURE(decl.AddToPlan(plan.get()).status()); |
| 172 | + } |
| 173 | + |
| 174 | + // Validate the plan and print it to stdout |
| 175 | + ABORT_ON_FAILURE(plan->Validate()); |
| 176 | + std::cout << std::string(50, '#') << " produced arrow::ExecPlan:" << std::endl; |
| 177 | + std::cout << plan->ToString() << std::endl; |
| 178 | + |
| 179 | + // Start the plan... |
| 180 | + std::cout << std::string(50, '#') << " consuming batches:" << std::endl; |
| 181 | + ABORT_ON_FAILURE(plan->StartProducing()); |
| 182 | + |
| 183 | + // ... and wait for it to finish |
| 184 | + ABORT_ON_FAILURE(plan->finished().status()); |
| 185 | + return EXIT_SUCCESS; |
| 186 | +} |
0 commit comments