forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.cc
More file actions
808 lines (652 loc) · 34.2 KB
/
Copy pathserver_test.cc
File metadata and controls
808 lines (652 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <memory>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <sqlite3.h>
#include "arrow/array/array_binary.h"
#include "arrow/array/array_nested.h"
#include "arrow/array/array_primitive.h"
#include "arrow/flight/sql/client.h"
#include "arrow/flight/sql/column_metadata.h"
#include "arrow/flight/sql/example/sqlite_server.h"
#include "arrow/flight/sql/example/sqlite_sql_info.h"
#include "arrow/flight/sql/example/sqlite_type_info.h"
#include "arrow/flight/sql/server.h"
#include "arrow/flight/test_util.h"
#include "arrow/flight/types.h"
#include "arrow/record_batch.h"
#include "arrow/scalar.h"
#include "arrow/table.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/gtest_util.h"
using arrow::internal::checked_cast;
namespace arrow::flight::sql {
/// \brief Auxiliary variant visitor used to assert that GetSqlInfo's values are
/// correctly placed on its DenseUnionArray
class SqlInfoDenseUnionValidator {
private:
const DenseUnionScalar& data;
public:
/// \brief Asserts that the current DenseUnionScalar equals to given string value
void operator()(const std::string& string_value) const {
const auto& scalar = checked_cast<const StringScalar&>(*data.value);
ASSERT_EQ(string_value, scalar.ToString());
}
/// \brief Asserts that the current DenseUnionScalar equals to given bool value
void operator()(const bool bool_value) const {
const auto& scalar = checked_cast<const BooleanScalar&>(*data.value);
ASSERT_EQ(bool_value, scalar.value);
}
/// \brief Asserts that the current DenseUnionScalar equals to given int64_t value
void operator()(const int64_t bigint_value) const {
const auto& scalar = checked_cast<const Int64Scalar&>(*data.value);
ASSERT_EQ(bigint_value, scalar.value);
}
/// \brief Asserts that the current DenseUnionScalar equals to given int32_t value
void operator()(const int32_t int32_bitmask) const {
const auto& scalar = checked_cast<const Int32Scalar&>(*data.value);
ASSERT_EQ(int32_bitmask, scalar.value);
}
/// \brief Asserts that the current DenseUnionScalar equals to given string list
void operator()(const std::vector<std::string>& string_list) const {
const auto& array = checked_cast<const StringArray&>(
*(checked_cast<const ListScalar&>(*data.value).value));
ASSERT_EQ(string_list.size(), array.length());
for (size_t index = 0; index < string_list.size(); index++) {
ASSERT_EQ(string_list[index], array.GetString(index));
}
}
/// \brief Asserts that the current DenseUnionScalar equals to given int32 to int32 list
/// map.
void operator()(const std::unordered_map<int32_t, std::vector<int32_t>>&
int32_to_int32_list) const {
const auto& struct_array = checked_cast<const StructArray&>(
*checked_cast<const MapScalar&>(*data.value).value);
const auto& keys = checked_cast<const Int32Array&>(*struct_array.field(0));
const auto& values = checked_cast<const ListArray&>(*struct_array.field(1));
// Assert that the given map has the right size
ASSERT_EQ(int32_to_int32_list.size(), keys.length());
// For each element on given MapScalar, assert it matches the argument
for (int i = 0; i < keys.length(); i++) {
ASSERT_OK_AND_ASSIGN(const auto& key_scalar, keys.GetScalar(i));
int32_t sql_info_id = checked_cast<const Int32Scalar&>(*key_scalar).value;
// Assert the key (SqlInfo id) exists
ASSERT_TRUE(int32_to_int32_list.count(sql_info_id));
const std::vector<int32_t>& expected_int32_list =
int32_to_int32_list.at(sql_info_id);
// Assert the value (int32 list) has the correct size
ASSERT_EQ(expected_int32_list.size(), values.value_length(i));
// For each element on current ListScalar, assert it matches with the argument
for (size_t j = 0; j < expected_int32_list.size(); j++) {
ASSERT_OK_AND_ASSIGN(auto list_item_scalar,
values.values()->GetScalar(values.value_offset(i) + j));
const auto& list_item = checked_cast<const Int32Scalar&>(*list_item_scalar).value;
ASSERT_EQ(expected_int32_list[j], list_item);
}
}
}
explicit SqlInfoDenseUnionValidator(const DenseUnionScalar& data) : data(data) {}
SqlInfoDenseUnionValidator(const SqlInfoDenseUnionValidator&) = delete;
SqlInfoDenseUnionValidator(SqlInfoDenseUnionValidator&&) = delete;
SqlInfoDenseUnionValidator& operator=(const SqlInfoDenseUnionValidator&) = delete;
};
class TestFlightSqlServer : public ::testing::Test {
public:
std::unique_ptr<FlightSqlClient> sql_client;
arrow::Result<int64_t> ExecuteCountQuery(const std::string& query) {
ARROW_ASSIGN_OR_RAISE(auto flight_info, sql_client->Execute({}, query));
ARROW_ASSIGN_OR_RAISE(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ARROW_ASSIGN_OR_RAISE(auto table, stream->ToTable());
const std::shared_ptr<Array>& result_array = table->column(0)->chunk(0);
ARROW_ASSIGN_OR_RAISE(auto count_scalar, result_array->GetScalar(0));
return reinterpret_cast<Int64Scalar&>(*count_scalar).value;
}
protected:
void SetUp() override {
ASSERT_OK_AND_ASSIGN(auto location, Location::ForGrpcTcp("0.0.0.0", 0));
arrow::flight::FlightServerOptions options(location);
ASSERT_OK_AND_ASSIGN(server, example::SQLiteFlightSqlServer::Create());
ASSERT_OK(server->Init(options));
ASSERT_OK_AND_ASSIGN(location, Location::ForGrpcTcp("localhost", server->port()));
ASSERT_OK_AND_ASSIGN(auto client, FlightClient::Connect(location));
sql_client = std::make_unique<FlightSqlClient>(std::move(client));
}
void TearDown() override {
ASSERT_OK(sql_client->Close());
sql_client.reset();
ASSERT_OK(server->Shutdown());
}
private:
std::shared_ptr<arrow::flight::sql::example::SQLiteFlightSqlServer> server;
};
TEST_F(TestFlightSqlServer, TestCommandStatementQuery) {
ASSERT_OK_AND_ASSIGN(auto flight_info,
sql_client->Execute({}, "SELECT * FROM intTable"));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const std::shared_ptr<Schema>& expected_schema =
arrow::schema({arrow::field("id", int64()), arrow::field("keyName", utf8()),
arrow::field("value", int64()), arrow::field("foreignId", int64())});
const auto id_array = ArrayFromJSON(int64(), R"([1, 2, 3, 4, 5])");
const auto keyname_array =
ArrayFromJSON(utf8(), R"(["one", "zero", "negative one", null, "null"])");
const auto value_array = ArrayFromJSON(int64(), R"([1, 0, -1, null, null])");
const auto foreignId_array = ArrayFromJSON(int64(), R"([1, 1, 1, null, null])");
const std::shared_ptr<Table>& expected_table = Table::Make(
expected_schema, {id_array, keyname_array, value_array, foreignId_array});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTables) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
std::string* table_filter_pattern = nullptr;
bool include_schema = false;
std::vector<std::string>* table_types = nullptr;
ASSERT_OK_AND_ASSIGN(
auto flight_info,
sql_client->GetTables(options, catalog, schema_filter_pattern, table_filter_pattern,
include_schema, table_types));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto catalog_name = ArrayFromJSON(utf8(), R"(["main", "main", "main"])");
ASSERT_OK_AND_ASSIGN(auto schema_name, MakeArrayOfNull(utf8(), 3));
const auto table_name =
ArrayFromJSON(utf8(), R"(["foreignTable", "intTable", "sqlite_sequence"])");
const auto table_type = ArrayFromJSON(utf8(), R"(["table", "table", "table"])");
std::shared_ptr<Table> expected_table = Table::Make(
SqlSchema::GetTablesSchema(), {catalog_name, schema_name, table_name, table_type});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTablesWithTableFilter) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
std::string table_filter_pattern = "int%";
bool include_schema = false;
std::vector<std::string>* table_types = nullptr;
ASSERT_OK_AND_ASSIGN(
auto flight_info,
sql_client->GetTables(options, catalog, schema_filter_pattern,
&table_filter_pattern, include_schema, table_types));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto catalog_name = ArrayFromJSON(utf8(), R"(["main"])");
const auto schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto table_type = ArrayFromJSON(utf8(), R"(["table"])");
const std::shared_ptr<Table>& expected_table = Table::Make(
SqlSchema::GetTablesSchema(), {catalog_name, schema_name, table_name, table_type});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTablesWithTableTypesFilter) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
std::string* table_filter_pattern = nullptr;
bool include_schema = false;
std::vector<std::string> table_types{"index"};
ASSERT_OK_AND_ASSIGN(
auto flight_info,
sql_client->GetTables(options, catalog, schema_filter_pattern, table_filter_pattern,
include_schema, &table_types));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
AssertSchemaEqual(SqlSchema::GetTablesSchema(), table->schema());
ASSERT_EQ(table->num_rows(), 0);
}
TEST_F(TestFlightSqlServer, TestCommandGetTablesWithUnexistenceTableTypeFilter) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
std::string* table_filter_pattern = nullptr;
bool include_schema = false;
std::vector<std::string> table_types{"table"};
ASSERT_OK_AND_ASSIGN(
auto flight_info,
sql_client->GetTables(options, catalog, schema_filter_pattern, table_filter_pattern,
include_schema, &table_types));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto catalog_name = ArrayFromJSON(utf8(), R"(["main", "main", "main"])");
const auto schema_name = ArrayFromJSON(utf8(), R"([null, null, null])");
const auto table_name =
ArrayFromJSON(utf8(), R"(["foreignTable", "intTable", "sqlite_sequence"])");
const auto table_type = ArrayFromJSON(utf8(), R"(["table", "table", "table"])");
const std::shared_ptr<Table>& expected_table = Table::Make(
SqlSchema::GetTablesSchema(), {catalog_name, schema_name, table_name, table_type});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTablesWithIncludedSchemas) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
std::string table_filter_pattern = "int%";
bool include_schema = true;
std::vector<std::string>* table_types = nullptr;
ASSERT_OK_AND_ASSIGN(
auto flight_info,
sql_client->GetTables(options, catalog, schema_filter_pattern,
&table_filter_pattern, include_schema, table_types));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const char* db_table_name = "intTable";
const auto catalog_name = ArrayFromJSON(utf8(), R"(["main"])");
const auto schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto table_type = ArrayFromJSON(utf8(), R"(["table"])");
const std::shared_ptr<Schema> schema_table = arrow::schema(
{arrow::field(
"id", int64(), true,
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map()),
arrow::field(
"keyName", utf8(), true,
example::GetColumnMetadata(SQLITE_TEXT, db_table_name).metadata_map()),
arrow::field(
"value", int64(), true,
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map()),
arrow::field(
"foreignId", int64(), true,
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map())});
ASSERT_OK_AND_ASSIGN(auto schema_buffer, ipc::SerializeSchema(*schema_table));
std::shared_ptr<Array> table_schema;
ArrayFromVector<BinaryType, std::string>({schema_buffer->ToString()}, &table_schema);
const std::shared_ptr<Table>& expected_table =
Table::Make(SqlSchema::GetTablesSchemaWithIncludedSchema(),
{catalog_name, schema_name, table_name, table_type, table_schema});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTypeInfo) {
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetXdbcTypeInfo({}));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
auto batch = example::DoGetTypeInfoResult();
ASSERT_OK_AND_ASSIGN(auto expected_table, Table::FromRecordBatches({batch}));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetTypeInfoWithFiltering) {
int data_type = -4;
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetXdbcTypeInfo({}, data_type));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
auto batch = example::DoGetTypeInfoResult(data_type);
ASSERT_OK_AND_ASSIGN(auto expected_table, Table::FromRecordBatches({batch}));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetCatalogs) {
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetCatalogs({}));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
auto expected_table = TableFromJSON(SqlSchema::GetCatalogsSchema(), {R"([["main"]])"});
ASSERT_NO_FATAL_FAILURE(AssertTablesEqual(*expected_table, *table, /*verbose=*/true));
}
TEST_F(TestFlightSqlServer, TestCommandGetDbSchemas) {
FlightCallOptions options = {};
std::string* catalog = nullptr;
std::string* schema_filter_pattern = nullptr;
ASSERT_OK_AND_ASSIGN(auto flight_info,
sql_client->GetDbSchemas(options, catalog, schema_filter_pattern));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
auto expected_table =
TableFromJSON(SqlSchema::GetDbSchemasSchema(), {R"([["main", null]])"});
ASSERT_NO_FATAL_FAILURE(AssertTablesEqual(*expected_table, *table, /*verbose=*/true));
}
TEST_F(TestFlightSqlServer, TestCommandGetTableTypes) {
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetTableTypes({}));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto table_type = ArrayFromJSON(utf8(), R"(["table"])");
const std::shared_ptr<Table>& expected_table =
Table::Make(SqlSchema::GetTableTypesSchema(), {table_type});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandStatementUpdate) {
int64_t result;
ASSERT_OK_AND_ASSIGN(result,
sql_client->ExecuteUpdate(
{},
"INSERT INTO intTable (keyName, value) VALUES "
"('KEYNAME1', 1001), ('KEYNAME2', 1002), ('KEYNAME3', 1003)"));
ASSERT_EQ(3, result);
ASSERT_OK_AND_ASSIGN(result, sql_client->ExecuteUpdate(
{},
"UPDATE intTable SET keyName = 'KEYNAME1' "
"WHERE keyName = 'KEYNAME2' OR keyName = 'KEYNAME3'"));
ASSERT_EQ(2, result);
ASSERT_OK_AND_ASSIGN(
result,
sql_client->ExecuteUpdate({}, "DELETE FROM intTable WHERE keyName = 'KEYNAME1'"));
ASSERT_EQ(3, result);
}
TEST_F(TestFlightSqlServer, TestCommandPreparedStatementQuery) {
ASSERT_OK_AND_ASSIGN(auto prepared_statement,
sql_client->Prepare({}, "SELECT * FROM intTable"));
ASSERT_OK_AND_ASSIGN(auto flight_info, prepared_statement->Execute());
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const char* db_table_name = "intTable";
const std::shared_ptr<Schema>& expected_schema = arrow::schema(
{arrow::field(
"id", int64(),
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map()),
arrow::field(
"keyName", utf8(),
example::GetColumnMetadata(SQLITE_TEXT, db_table_name).metadata_map()),
arrow::field(
"value", int64(),
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map()),
arrow::field(
"foreignId", int64(),
example::GetColumnMetadata(SQLITE_INTEGER, db_table_name).metadata_map())});
const auto id_array = ArrayFromJSON(int64(), R"([1, 2, 3, 4, 5])");
const auto keyname_array =
ArrayFromJSON(utf8(), R"(["one", "zero", "negative one", null, "null"])");
const auto value_array = ArrayFromJSON(int64(), R"([1, 0, -1, null, null])");
const auto foreignId_array = ArrayFromJSON(int64(), R"([1, 1, 1, null, null])");
const std::shared_ptr<Table>& expected_table = Table::Make(
expected_schema, {id_array, keyname_array, value_array, foreignId_array});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandPreparedStatementQueryWithParameterBinding) {
ASSERT_OK_AND_ASSIGN(
auto prepared_statement,
sql_client->Prepare({}, "SELECT * FROM intTable WHERE keyName LIKE ?"));
const std::shared_ptr<Schema>& parameter_schema =
prepared_statement->parameter_schema();
const std::shared_ptr<Schema>& expected_parameter_schema =
arrow::schema({arrow::field("parameter_1", example::GetUnknownColumnDataType())});
ASSERT_NO_FATAL_FAILURE(AssertSchemaEqual(expected_parameter_schema, parameter_schema));
auto record_batch = RecordBatchFromJSON(parameter_schema, R"([ [[0, "%one"]] ])");
ASSERT_OK(prepared_statement->SetParameters(std::move(record_batch)));
ASSERT_OK_AND_ASSIGN(auto flight_info, prepared_statement->Execute());
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const std::shared_ptr<Schema>& expected_schema =
arrow::schema({arrow::field("id", int64()), arrow::field("keyName", utf8()),
arrow::field("value", int64()), arrow::field("foreignId", int64())});
auto expected_table = TableFromJSON(expected_schema, {R"([
[1, "one", 1, 1],
[3, "negative one", -1, 1]
])"});
ASSERT_NO_FATAL_FAILURE(AssertTablesEqual(*expected_table, *table, /*verbose=*/true));
// Set multiple parameters at once
record_batch = RecordBatchFromJSON(
parameter_schema, R"([ [[0, "%one"]], [[0, "%zero"]], [[0, "null"]] ])");
ASSERT_OK(prepared_statement->SetParameters(std::move(record_batch)));
ASSERT_OK_AND_ASSIGN(flight_info, prepared_statement->Execute());
ASSERT_OK_AND_ASSIGN(stream, sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(table, stream->ToTable());
expected_table = TableFromJSON(expected_schema, {R"([
[1, "one", 1, 1],
[3, "negative one", -1, 1],
[2, "zero", 0, 1],
[5, "null", null, null]
])"});
ASSERT_NO_FATAL_FAILURE(AssertTablesEqual(*expected_table, *table, /*verbose=*/true));
// Set a stream of parameters
ASSERT_OK_AND_ASSIGN(
auto reader,
RecordBatchReader::Make({
RecordBatchFromJSON(parameter_schema, R"([ [[0, "%one"]], [[0, "%zero"]] ])"),
RecordBatchFromJSON(parameter_schema, R"([ [[0, "%null%"]] ])"),
}));
ASSERT_OK(prepared_statement->SetParameters(std::move(reader)));
ASSERT_OK_AND_ASSIGN(flight_info, prepared_statement->Execute());
ASSERT_OK_AND_ASSIGN(stream, sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(table, stream->ToTable());
ASSERT_NO_FATAL_FAILURE(AssertTablesEqual(*expected_table, *table, /*verbose=*/true));
}
TEST_F(TestFlightSqlServer, TestCommandPreparedStatementUpdateWithParameterBinding) {
ASSERT_OK_AND_ASSIGN(
auto prepared_statement,
sql_client->Prepare(
{}, "INSERT INTO INTTABLE (keyName, value) VALUES ('new_value', ?)"));
const std::shared_ptr<Schema>& parameter_schema =
prepared_statement->parameter_schema();
const std::shared_ptr<Schema>& expected_parameter_schema =
arrow::schema({arrow::field("parameter_1", example::GetUnknownColumnDataType())});
ASSERT_NO_FATAL_FAILURE(AssertSchemaEqual(expected_parameter_schema, parameter_schema));
auto record_batch = RecordBatchFromJSON(parameter_schema, R"([ [[2, 999]] ])");
ASSERT_OK(prepared_statement->SetParameters(std::move(record_batch)));
ASSERT_OK_AND_EQ(5, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
ASSERT_OK_AND_EQ(1, prepared_statement->ExecuteUpdate());
ASSERT_OK_AND_EQ(6, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
ASSERT_OK_AND_EQ(1, sql_client->ExecuteUpdate(
{}, "DELETE FROM intTable WHERE keyName = 'new_value'"));
ASSERT_OK_AND_EQ(5, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
// Set multiple parameters at once
record_batch = RecordBatchFromJSON(parameter_schema, R"([ [[2, 999]], [[2, 42]] ])");
ASSERT_OK(prepared_statement->SetParameters(std::move(record_batch)));
ASSERT_OK_AND_EQ(2, prepared_statement->ExecuteUpdate());
ASSERT_OK_AND_EQ(7, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
// Set a stream of parameters
ASSERT_OK_AND_ASSIGN(
auto reader,
RecordBatchReader::Make({
RecordBatchFromJSON(parameter_schema, R"([ [[2, 999]], [[2, 42]] ])"),
RecordBatchFromJSON(parameter_schema, R"([ [[2, -1]] ])"),
}));
ASSERT_OK(prepared_statement->SetParameters(std::move(reader)));
ASSERT_OK_AND_EQ(3, prepared_statement->ExecuteUpdate());
ASSERT_OK_AND_EQ(10, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
}
TEST_F(TestFlightSqlServer, TestCommandPreparedStatementUpdate) {
ASSERT_OK_AND_ASSIGN(
auto prepared_statement,
sql_client->Prepare(
{}, "INSERT INTO INTTABLE (keyName, value) VALUES ('new_value', 999)"));
ASSERT_OK_AND_EQ(5, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
ASSERT_OK_AND_EQ(1, prepared_statement->ExecuteUpdate());
ASSERT_OK_AND_EQ(6, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
ASSERT_OK_AND_EQ(1, sql_client->ExecuteUpdate(
{}, "DELETE FROM intTable WHERE keyName = 'new_value'"));
ASSERT_OK_AND_EQ(5, ExecuteCountQuery("SELECT COUNT(*) FROM intTable"));
}
TEST_F(TestFlightSqlServer, TestCommandGetPrimaryKeys) {
FlightCallOptions options = {};
TableRef table_ref = {std::nullopt, std::nullopt, "int%"};
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetPrimaryKeys(options, table_ref));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto key_name = ArrayFromJSON(utf8(), R"([null])");
const auto table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto column_name = ArrayFromJSON(utf8(), R"(["id"])");
const auto key_sequence = ArrayFromJSON(int32(), R"([1])");
const std::shared_ptr<Table>& expected_table = Table::Make(
SqlSchema::GetPrimaryKeysSchema(),
{catalog_name, schema_name, table_name, column_name, key_sequence, key_name});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetImportedKeys) {
FlightCallOptions options = {};
TableRef table_ref = {std::nullopt, std::nullopt, "intTable"};
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetImportedKeys(options, table_ref));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto pk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_table_name = ArrayFromJSON(utf8(), R"(["foreignTable"])");
const auto pk_column_name = ArrayFromJSON(utf8(), R"(["id"])");
const auto fk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto fk_column_name = ArrayFromJSON(utf8(), R"(["foreignId"])");
const auto key_sequence = ArrayFromJSON(int32(), R"([0])");
const auto fk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto update_rule = ArrayFromJSON(uint8(), R"([3])");
const auto delete_rule = ArrayFromJSON(uint8(), R"([3])");
const std::shared_ptr<Table>& expected_table =
Table::Make(SqlSchema::GetImportedKeysSchema(),
{pk_catalog_name, pk_schema_name, pk_table_name, pk_column_name,
fk_catalog_name, fk_schema_name, fk_table_name, fk_column_name,
key_sequence, fk_key_name, pk_key_name, update_rule, delete_rule});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetExportedKeys) {
FlightCallOptions options = {};
TableRef table_ref = {std::nullopt, std::nullopt, "foreignTable"};
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetExportedKeys(options, table_ref));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto pk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_table_name = ArrayFromJSON(utf8(), R"(["foreignTable"])");
const auto pk_column_name = ArrayFromJSON(utf8(), R"(["id"])");
const auto fk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto fk_column_name = ArrayFromJSON(utf8(), R"(["foreignId"])");
const auto key_sequence = ArrayFromJSON(int32(), R"([0])");
const auto fk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto update_rule = ArrayFromJSON(uint8(), R"([3])");
const auto delete_rule = ArrayFromJSON(uint8(), R"([3])");
const std::shared_ptr<Table>& expected_table =
Table::Make(SqlSchema::GetExportedKeysSchema(),
{pk_catalog_name, pk_schema_name, pk_table_name, pk_column_name,
fk_catalog_name, fk_schema_name, fk_table_name, fk_column_name,
key_sequence, fk_key_name, pk_key_name, update_rule, delete_rule});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetCrossReference) {
FlightCallOptions options = {};
TableRef pk_table_ref = {std::nullopt, std::nullopt, "foreignTable"};
TableRef fk_table_ref = {std::nullopt, std::nullopt, "intTable"};
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetCrossReference(
options, pk_table_ref, fk_table_ref));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
const auto pk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_table_name = ArrayFromJSON(utf8(), R"(["foreignTable"])");
const auto pk_column_name = ArrayFromJSON(utf8(), R"(["id"])");
const auto fk_catalog_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_schema_name = ArrayFromJSON(utf8(), R"([null])");
const auto fk_table_name = ArrayFromJSON(utf8(), R"(["intTable"])");
const auto fk_column_name = ArrayFromJSON(utf8(), R"(["foreignId"])");
const auto key_sequence = ArrayFromJSON(int32(), R"([0])");
const auto fk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto pk_key_name = ArrayFromJSON(utf8(), R"([null])");
const auto update_rule = ArrayFromJSON(uint8(), R"([3])");
const auto delete_rule = ArrayFromJSON(uint8(), R"([3])");
const std::shared_ptr<Table>& expected_table =
Table::Make(SqlSchema::GetCrossReferenceSchema(),
{pk_catalog_name, pk_schema_name, pk_table_name, pk_column_name,
fk_catalog_name, fk_schema_name, fk_table_name, fk_column_name,
key_sequence, fk_key_name, pk_key_name, update_rule, delete_rule});
AssertTablesEqual(*expected_table, *table);
}
TEST_F(TestFlightSqlServer, TestCommandGetSqlInfo) {
const auto& sql_info_expected_results = sql::example::GetSqlInfoResultMap();
std::vector<int> sql_info_ids;
sql_info_ids.reserve(sql_info_expected_results.size());
for (const auto& sql_info_expected_result : sql_info_expected_results) {
sql_info_ids.push_back(sql_info_expected_result.first);
}
FlightCallOptions call_options;
ASSERT_OK_AND_ASSIGN(auto flight_info,
sql_client->GetSqlInfo(call_options, sql_info_ids));
ASSERT_OK_AND_ASSIGN(
auto reader, sql_client->DoGet(call_options, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto results, reader->ToTable());
ASSERT_EQ(2, results->num_columns());
ASSERT_EQ(sql_info_ids.size(), results->num_rows());
const auto& col_name = results->column(0);
const auto& col_value = results->column(1);
for (int32_t i = 0; i < col_name->num_chunks(); i++) {
const auto* col_name_chunk_data =
col_name->chunk(i)->data()->GetValuesSafe<int32_t>(1);
const auto& col_value_chunk = col_value->chunk(i);
for (int64_t row = 0; row < col_value->length(); row++) {
ASSERT_OK_AND_ASSIGN(const auto& scalar, col_value_chunk->GetScalar(row));
const SqlInfoDenseUnionValidator validator(
reinterpret_cast<const DenseUnionScalar&>(*scalar));
const auto& expected_result =
sql_info_expected_results.at(col_name_chunk_data[row]);
std::visit(validator, expected_result);
}
}
}
TEST_F(TestFlightSqlServer, TestCommandGetSqlInfoNoInfo) {
FlightCallOptions call_options;
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetSqlInfo(call_options, {999999}));
EXPECT_RAISES_WITH_MESSAGE_THAT(
KeyError, ::testing::HasSubstr("No information for SQL info number 999999"),
sql_client->DoGet(call_options, flight_info->endpoints()[0].ticket));
}
TEST_F(TestFlightSqlServer, CancelQuery) {
// Not supported
ASSERT_OK_AND_ASSIGN(auto flight_info, sql_client->GetSqlInfo({}, {}));
ASSERT_RAISES(NotImplemented, sql_client->CancelQuery({}, *flight_info));
}
TEST_F(TestFlightSqlServer, Transactions) {
ASSERT_OK_AND_ASSIGN(auto handle, sql_client->BeginTransaction({}));
ASSERT_TRUE(handle.is_valid());
ASSERT_NE(handle.transaction_id(), "");
ASSERT_RAISES(NotImplemented, sql_client->BeginSavepoint({}, handle, "savepoint"));
ASSERT_OK_AND_ASSIGN(auto flight_info,
sql_client->Execute({}, "SELECT * FROM intTable", handle));
ASSERT_OK_AND_ASSIGN(auto stream,
sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(auto table, stream->ToTable());
int64_t row_count = table->num_rows();
int64_t result;
ASSERT_OK_AND_ASSIGN(result,
sql_client->ExecuteUpdate(
{},
"INSERT INTO intTable (keyName, value) VALUES "
"('KEYNAME1', 1001), ('KEYNAME2', 1002), ('KEYNAME3', 1003)",
handle));
ASSERT_EQ(3, result);
ASSERT_OK_AND_ASSIGN(flight_info,
sql_client->Execute({}, "SELECT * FROM intTable", handle));
ASSERT_OK_AND_ASSIGN(stream, sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(table, stream->ToTable());
ASSERT_EQ(table->num_rows(), row_count + 3);
ASSERT_OK(sql_client->Rollback({}, handle));
// Commit/rollback invalidate the handle
ASSERT_RAISES(KeyError, sql_client->Rollback({}, handle));
ASSERT_RAISES(KeyError, sql_client->Commit({}, handle));
ASSERT_OK_AND_ASSIGN(flight_info, sql_client->Execute({}, "SELECT * FROM intTable"));
ASSERT_OK_AND_ASSIGN(stream, sql_client->DoGet({}, flight_info->endpoints()[0].ticket));
ASSERT_OK_AND_ASSIGN(table, stream->ToTable());
ASSERT_EQ(table->num_rows(), row_count);
}
} // namespace arrow::flight::sql