|
| 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 | +/// Integration test using the Acero backend |
| 19 | + |
| 20 | +#include <memory> |
| 21 | +#include <sstream> |
| 22 | + |
| 23 | +#include <gmock/gmock.h> |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "arrow/array.h" |
| 27 | +#include "arrow/engine/substrait/util.h" |
| 28 | +#include "arrow/flight/server.h" |
| 29 | +#include "arrow/flight/sql/client.h" |
| 30 | +#include "arrow/flight/sql/example/acero_server.h" |
| 31 | +#include "arrow/flight/sql/types.h" |
| 32 | +#include "arrow/flight/types.h" |
| 33 | +#include "arrow/stl_iterator.h" |
| 34 | +#include "arrow/table.h" |
| 35 | +#include "arrow/testing/gtest_util.h" |
| 36 | +#include "arrow/type_fwd.h" |
| 37 | +#include "arrow/util/checked_cast.h" |
| 38 | + |
| 39 | +namespace arrow { |
| 40 | +namespace flight { |
| 41 | +namespace sql { |
| 42 | + |
| 43 | +using arrow::internal::checked_cast; |
| 44 | + |
| 45 | +class TestAcero : public ::testing::Test { |
| 46 | + public: |
| 47 | + void SetUp() override { |
| 48 | + ASSERT_OK_AND_ASSIGN(auto location, Location::ForGrpcTcp("localhost", 0)); |
| 49 | + flight::FlightServerOptions options(location); |
| 50 | + |
| 51 | + ASSERT_OK_AND_ASSIGN(server_, acero_example::MakeAceroServer()); |
| 52 | + ASSERT_OK(server_->Init(options)); |
| 53 | + |
| 54 | + ASSERT_OK_AND_ASSIGN(auto client, FlightClient::Connect(server_->location())); |
| 55 | + client_.reset(new FlightSqlClient(std::move(client))); |
| 56 | + } |
| 57 | + |
| 58 | + void TearDown() override { |
| 59 | + ASSERT_OK(client_->Close()); |
| 60 | + ASSERT_OK(server_->Shutdown()); |
| 61 | + } |
| 62 | + |
| 63 | + protected: |
| 64 | + std::unique_ptr<FlightSqlClient> client_; |
| 65 | + std::unique_ptr<FlightServerBase> server_; |
| 66 | +}; |
| 67 | + |
| 68 | +arrow::Result<std::shared_ptr<Buffer>> MakeSubstraitPlan() { |
| 69 | + ARROW_ASSIGN_OR_RAISE(std::string dir_string, |
| 70 | + arrow::internal::GetEnvVar("PARQUET_TEST_DATA")); |
| 71 | + ARROW_ASSIGN_OR_RAISE(auto dir, |
| 72 | + arrow::internal::PlatformFilename::FromString(dir_string)); |
| 73 | + ARROW_ASSIGN_OR_RAISE(auto filename, dir.Join("binary.parquet")); |
| 74 | + std::string uri = std::string("file://") + filename.ToString(); |
| 75 | + |
| 76 | + // TODO(ARROW-17229): we should use a RootRel here |
| 77 | + std::string json_plan = R"({ |
| 78 | + "relations": [ |
| 79 | + { |
| 80 | + "rel": { |
| 81 | + "read": { |
| 82 | + "base_schema": { |
| 83 | + "struct": { |
| 84 | + "types": [ |
| 85 | + {"binary": {}} |
| 86 | + ] |
| 87 | + }, |
| 88 | + "names": [ |
| 89 | + "foo" |
| 90 | + ] |
| 91 | + }, |
| 92 | + "local_files": { |
| 93 | + "items": [ |
| 94 | + { |
| 95 | + "uri_file": "URI_PLACEHOLDER", |
| 96 | + "parquet": {} |
| 97 | + } |
| 98 | + ] |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + ] |
| 104 | +})"; |
| 105 | + std::string uri_placeholder = "URI_PLACEHOLDER"; |
| 106 | + json_plan.replace(json_plan.find(uri_placeholder), uri_placeholder.size(), uri); |
| 107 | + return engine::SerializeJsonPlan(json_plan); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(TestAcero, GetSqlInfo) { |
| 111 | + FlightCallOptions call_options; |
| 112 | + std::vector<int> sql_info_codes = { |
| 113 | + SqlInfoOptions::SqlInfo::FLIGHT_SQL_SERVER_SUBSTRAIT, |
| 114 | + SqlInfoOptions::SqlInfo::FLIGHT_SQL_SERVER_TRANSACTION, |
| 115 | + }; |
| 116 | + ASSERT_OK_AND_ASSIGN(auto flight_info, |
| 117 | + client_->GetSqlInfo(call_options, sql_info_codes)); |
| 118 | + ASSERT_OK_AND_ASSIGN(auto reader, |
| 119 | + client_->DoGet(call_options, flight_info->endpoints()[0].ticket)); |
| 120 | + ASSERT_OK_AND_ASSIGN(auto results, reader->ToTable()); |
| 121 | + ASSERT_OK_AND_ASSIGN(auto batch, results->CombineChunksToBatch()); |
| 122 | + ASSERT_EQ(2, results->num_rows()); |
| 123 | + std::vector<std::pair<uint32_t, SqlInfoResult>> info; |
| 124 | + const auto& ids = checked_cast<const UInt32Array&>(*batch->column(0)); |
| 125 | + const auto& values = checked_cast<const DenseUnionArray&>(*batch->column(1)); |
| 126 | + for (int64_t i = 0; i < batch->num_rows(); i++) { |
| 127 | + ASSERT_OK_AND_ASSIGN(auto scalar, values.GetScalar(i)); |
| 128 | + if (ids.Value(i) == SqlInfoOptions::SqlInfo::FLIGHT_SQL_SERVER_SUBSTRAIT) { |
| 129 | + ASSERT_EQ(*checked_cast<const DenseUnionScalar&>(*scalar).value, |
| 130 | + BooleanScalar(true)); |
| 131 | + } else if (ids.Value(i) == SqlInfoOptions::SqlInfo::FLIGHT_SQL_SERVER_TRANSACTION) { |
| 132 | + ASSERT_EQ( |
| 133 | + *checked_cast<const DenseUnionScalar&>(*scalar).value, |
| 134 | + Int32Scalar( |
| 135 | + SqlInfoOptions::SqlSupportedTransaction::SQL_SUPPORTED_TRANSACTION_NONE)); |
| 136 | + } else { |
| 137 | + FAIL() << "Unexpected info value: " << ids.Value(i); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +TEST_F(TestAcero, Scan) { |
| 143 | +#ifdef _WIN32 |
| 144 | + GTEST_SKIP() << "ARROW-16392: Substrait File URI not supported for Windows"; |
| 145 | +#endif |
| 146 | + |
| 147 | + FlightCallOptions call_options; |
| 148 | + ASSERT_OK_AND_ASSIGN(auto serialized_plan, MakeSubstraitPlan()); |
| 149 | + |
| 150 | + SubstraitPlan plan{serialized_plan->ToString(), /*version=*/"0.6.0"}; |
| 151 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<FlightInfo> info, |
| 152 | + client_->ExecuteSubstrait(call_options, plan)); |
| 153 | + ipc::DictionaryMemo memo; |
| 154 | + ASSERT_OK_AND_ASSIGN(auto schema, info->GetSchema(&memo)); |
| 155 | + // TODO(ARROW-17229): the scanner "special" fields are still included, strip them |
| 156 | + // manually |
| 157 | + auto fixed_schema = arrow::schema({schema->fields()[0]}); |
| 158 | + ASSERT_NO_FATAL_FAILURE( |
| 159 | + AssertSchemaEqual(fixed_schema, arrow::schema({field("foo", binary())}))); |
| 160 | + |
| 161 | + ASSERT_EQ(1, info->endpoints().size()); |
| 162 | + ASSERT_EQ(0, info->endpoints()[0].locations.size()); |
| 163 | + ASSERT_OK_AND_ASSIGN(auto reader, |
| 164 | + client_->DoGet(call_options, info->endpoints()[0].ticket)); |
| 165 | + ASSERT_OK_AND_ASSIGN(auto reader_schema, reader->GetSchema()); |
| 166 | + ASSERT_NO_FATAL_FAILURE(AssertSchemaEqual(schema, reader_schema)); |
| 167 | + ASSERT_OK_AND_ASSIGN(auto table, reader->ToTable()); |
| 168 | + ASSERT_GT(table->num_rows(), 0); |
| 169 | +} |
| 170 | + |
| 171 | +TEST_F(TestAcero, Update) { |
| 172 | +#ifdef _WIN32 |
| 173 | + GTEST_SKIP() << "ARROW-16392: Substrait File URI not supported for Windows"; |
| 174 | +#endif |
| 175 | + |
| 176 | + FlightCallOptions call_options; |
| 177 | + ASSERT_OK_AND_ASSIGN(auto serialized_plan, MakeSubstraitPlan()); |
| 178 | + SubstraitPlan plan{serialized_plan->ToString(), /*version=*/"0.6.0"}; |
| 179 | + EXPECT_RAISES_WITH_MESSAGE_THAT(NotImplemented, |
| 180 | + ::testing::HasSubstr("Updates are unsupported"), |
| 181 | + client_->ExecuteSubstraitUpdate(call_options, plan)); |
| 182 | +} |
| 183 | + |
| 184 | +TEST_F(TestAcero, Prepare) { |
| 185 | +#ifdef _WIN32 |
| 186 | + GTEST_SKIP() << "ARROW-16392: Substrait File URI not supported for Windows"; |
| 187 | +#endif |
| 188 | + |
| 189 | + FlightCallOptions call_options; |
| 190 | + ASSERT_OK_AND_ASSIGN(auto serialized_plan, MakeSubstraitPlan()); |
| 191 | + SubstraitPlan plan{serialized_plan->ToString(), /*version=*/"0.6.0"}; |
| 192 | + ASSERT_OK_AND_ASSIGN(auto prepared_statement, |
| 193 | + client_->PrepareSubstrait(call_options, plan)); |
| 194 | + ASSERT_NE(prepared_statement->dataset_schema(), nullptr); |
| 195 | + ASSERT_EQ(prepared_statement->parameter_schema(), nullptr); |
| 196 | + |
| 197 | + auto fixed_schema = arrow::schema({prepared_statement->dataset_schema()->fields()[0]}); |
| 198 | + ASSERT_NO_FATAL_FAILURE( |
| 199 | + AssertSchemaEqual(fixed_schema, arrow::schema({field("foo", binary())}))); |
| 200 | + |
| 201 | + EXPECT_RAISES_WITH_MESSAGE_THAT(NotImplemented, |
| 202 | + ::testing::HasSubstr("Updates are unsupported"), |
| 203 | + prepared_statement->ExecuteUpdate()); |
| 204 | + |
| 205 | + ASSERT_OK_AND_ASSIGN(std::unique_ptr<FlightInfo> info, prepared_statement->Execute()); |
| 206 | + ASSERT_EQ(1, info->endpoints().size()); |
| 207 | + ASSERT_EQ(0, info->endpoints()[0].locations.size()); |
| 208 | + ASSERT_OK_AND_ASSIGN(auto reader, |
| 209 | + client_->DoGet(call_options, info->endpoints()[0].ticket)); |
| 210 | + ASSERT_OK_AND_ASSIGN(auto reader_schema, reader->GetSchema()); |
| 211 | + ASSERT_NO_FATAL_FAILURE( |
| 212 | + AssertSchemaEqual(prepared_statement->dataset_schema(), reader_schema)); |
| 213 | + ASSERT_OK_AND_ASSIGN(auto table, reader->ToTable()); |
| 214 | + ASSERT_GT(table->num_rows(), 0); |
| 215 | + |
| 216 | + ASSERT_OK(prepared_statement->Close()); |
| 217 | +} |
| 218 | + |
| 219 | +TEST_F(TestAcero, Transactions) { |
| 220 | +#ifdef _WIN32 |
| 221 | + GTEST_SKIP() << "ARROW-16392: Substrait File URI not supported for Windows"; |
| 222 | +#endif |
| 223 | + |
| 224 | + FlightCallOptions call_options; |
| 225 | + ASSERT_OK_AND_ASSIGN(auto serialized_plan, MakeSubstraitPlan()); |
| 226 | + Transaction handle("fake-id"); |
| 227 | + SubstraitPlan plan{serialized_plan->ToString(), /*version=*/"0.6.0"}; |
| 228 | + |
| 229 | + EXPECT_RAISES_WITH_MESSAGE_THAT(NotImplemented, |
| 230 | + ::testing::HasSubstr("Transactions are unsupported"), |
| 231 | + client_->ExecuteSubstrait(call_options, plan, handle)); |
| 232 | + EXPECT_RAISES_WITH_MESSAGE_THAT(NotImplemented, |
| 233 | + ::testing::HasSubstr("Transactions are unsupported"), |
| 234 | + client_->PrepareSubstrait(call_options, plan, handle)); |
| 235 | +} |
| 236 | + |
| 237 | +} // namespace sql |
| 238 | +} // namespace flight |
| 239 | +} // namespace arrow |
0 commit comments