-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtable_reader_test.cpp
More file actions
3858 lines (3362 loc) · 176 KB
/
Copy pathtable_reader_test.cpp
File metadata and controls
3858 lines (3362 loc) · 176 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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 "format_v2/table_reader.h"
#include <arrow/api.h>
#include <arrow/io/api.h>
#include <gtest/gtest.h>
#include <parquet/api/reader.h>
#include <parquet/arrow/writer.h>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <memory>
#include <optional>
#include <string>
#include <typeinfo>
#include <vector>
#include "common/consts.h"
#include "core/assert_cast.h"
#include "core/block/block.h"
#include "core/column/column_array.h"
#include "core/column/column_const.h"
#include "core/column/column_map.h"
#include "core/column/column_nullable.h"
#include "core/column/column_string.h"
#include "core/column/column_struct.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_map.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_number.h"
#include "core/data_type/data_type_string.h"
#include "core/data_type/data_type_struct.h"
#include "exprs/runtime_filter_expr.h"
#include "exprs/vectorized_fn_call.h"
#include "exprs/vexpr.h"
#include "exprs/vliteral.h"
#include "exprs/vslot_ref.h"
#include "gen_cpp/Exprs_types.h"
#include "gen_cpp/ExternalTableSchema_types.h"
#include "gen_cpp/PlanNodes_types.h"
#include "io/io_common.h"
#include "runtime/runtime_profile.h"
#include "runtime/runtime_state.h"
#include "storage/segment/condition_cache.h"
namespace doris::format {
namespace {
std::vector<int32_t> projection_ids(const std::vector<LocalColumnIndex>& projections) {
std::vector<int32_t> ids;
ids.reserve(projections.size());
for (const auto& projection : projections) {
ids.push_back(projection.index);
}
return ids;
}
TEST(LocalColumnIndexTest, MergeUnionsPartialChildrenAndFullProjectionDominates) {
LocalColumnIndex target {.index = 10, .project_all_children = false};
target.children.push_back({.index = 1});
target.children.push_back({.index = 2, .project_all_children = false});
target.children.back().children.push_back({.index = 20});
LocalColumnIndex source {.index = 10, .project_all_children = false};
source.children.push_back({.index = 2, .project_all_children = false});
source.children.back().children.push_back({.index = 21});
source.children.push_back({.index = 3});
ASSERT_TRUE(merge_local_column_index(&target, source).ok());
ASSERT_FALSE(target.project_all_children);
ASSERT_EQ(std::vector<int32_t>({1, 2, 3}), projection_ids(target.children));
ASSERT_FALSE(target.children[1].project_all_children);
ASSERT_EQ(std::vector<int32_t>({20, 21}), projection_ids(target.children[1].children));
ASSERT_TRUE(target.children[2].project_all_children);
LocalColumnIndex full_source {.index = 10};
ASSERT_TRUE(merge_local_column_index(&target, full_source).ok());
ASSERT_TRUE(target.project_all_children);
ASSERT_TRUE(target.children.empty());
}
TEST(LocalColumnIndexTest, FindsProjectedChildren) {
LocalColumnIndex projection {.index = 10, .project_all_children = false};
projection.children.push_back({.index = 1});
projection.children.push_back({.index = 2});
EXPECT_TRUE(is_full_projection(nullptr));
EXPECT_FALSE(is_full_projection(&projection));
EXPECT_TRUE(is_partial_projection(&projection));
ASSERT_NE(find_child_projection(&projection, 2), nullptr);
EXPECT_EQ(find_child_projection(&projection, 2)->local_id(), 2);
EXPECT_EQ(find_child_projection(&projection, 3), nullptr);
EXPECT_TRUE(is_child_projected(nullptr, 3));
EXPECT_TRUE(is_child_projected(&projection, 1));
EXPECT_FALSE(is_child_projected(&projection, 3));
}
TEST(LocalColumnIndexTest, ProjectColumnDefinitionMatchesChildrenByLocalId) {
auto int_type = std::make_shared<DataTypeInt32>();
auto string_type = std::make_shared<DataTypeString>();
ColumnDefinition field;
field.identifier = Field::create_field<TYPE_INT>(5);
field.name = "root";
field.type =
std::make_shared<DataTypeStruct>(DataTypes {int_type, string_type}, Strings {"a", "b"});
ColumnDefinition a_child;
a_child.identifier = Field::create_field<TYPE_INT>(10);
a_child.local_id = 0;
a_child.name = "a";
a_child.type = int_type;
ColumnDefinition b_child;
b_child.identifier = Field::create_field<TYPE_INT>(20);
b_child.local_id = 1;
b_child.name = "b";
b_child.type = string_type;
field.children = {
a_child,
b_child,
};
LocalColumnIndex projection {.index = 5, .project_all_children = false};
projection.children.push_back({.index = 1});
ColumnDefinition projected_field;
ASSERT_TRUE(project_column_definition(field, projection, &projected_field).ok());
ASSERT_EQ(projected_field.children.size(), 1);
EXPECT_EQ(projected_field.children[0].get_identifier_field_id(), 20);
EXPECT_EQ(projected_field.children[0].name, "b");
const auto* projected_type =
assert_cast<const DataTypeStruct*>(remove_nullable(projected_field.type).get());
ASSERT_EQ(projected_type->get_elements().size(), 1);
EXPECT_EQ(projected_type->get_element_name(0), "b");
EXPECT_TRUE(projected_type->get_element(0)->equals(*string_type));
}
TEST(LocalColumnIndexTest, ProjectColumnDefinitionKeepsFileChildOrder) {
auto int_type = std::make_shared<DataTypeInt32>();
auto string_type = std::make_shared<DataTypeString>();
ColumnDefinition a_child;
a_child.identifier = Field::create_field<TYPE_INT>(10);
a_child.local_id = 0;
a_child.name = "a";
a_child.type = int_type;
ColumnDefinition b_child;
b_child.identifier = Field::create_field<TYPE_INT>(20);
b_child.local_id = 1;
b_child.name = "b";
b_child.type = string_type;
ColumnDefinition field;
field.identifier = Field::create_field<TYPE_INT>(5);
field.name = "root";
field.type =
std::make_shared<DataTypeStruct>(DataTypes {int_type, string_type}, Strings {"a", "b"});
field.children = {a_child, b_child};
LocalColumnIndex projection {.index = 5, .project_all_children = false};
projection.children.push_back({.index = 1});
projection.children.push_back({.index = 0});
ColumnDefinition projected_field;
ASSERT_TRUE(project_column_definition(field, projection, &projected_field).ok());
ASSERT_EQ(projected_field.children.size(), 2);
EXPECT_EQ(projected_field.children[0].name, "a");
EXPECT_EQ(projected_field.children[1].name, "b");
const auto* projected_type =
assert_cast<const DataTypeStruct*>(remove_nullable(projected_field.type).get());
ASSERT_EQ(projected_type->get_elements().size(), 2);
EXPECT_EQ(projected_type->get_element_name(0), "a");
EXPECT_EQ(projected_type->get_element_name(1), "b");
}
VExprSPtr table_int32_slot_ref(int slot_id, int column_id, const std::string& column_name) {
const auto nullable_int_type = make_nullable(std::make_shared<DataTypeInt32>());
return VSlotRef::create_shared(slot_id, column_id, slot_id, nullable_int_type, column_name);
}
VExprSPtr table_int32_literal(int32_t value) {
return VLiteral::create_shared(std::make_shared<DataTypeInt32>(),
Field::create_field<TYPE_INT>(value));
}
TExprNode table_function_node(const std::string& function_name, const DataTypePtr& return_type,
const std::vector<DataTypePtr>& arg_types,
TExprNodeType::type node_type,
TExprOpcode::type opcode = TExprOpcode::INVALID_OPCODE,
bool short_circuit_evaluation = false) {
TFunctionName fn_name;
fn_name.__set_function_name(function_name);
TFunction fn;
fn.__set_name(fn_name);
fn.__set_binary_type(TFunctionBinaryType::BUILTIN);
std::vector<TTypeDesc> thrift_arg_types;
thrift_arg_types.reserve(arg_types.size());
for (const auto& arg_type : arg_types) {
thrift_arg_types.push_back(arg_type->to_thrift());
}
fn.__set_arg_types(thrift_arg_types);
fn.__set_ret_type(return_type->to_thrift());
fn.__set_has_var_args(false);
TExprNode node;
node.__set_node_type(node_type);
node.__set_opcode(opcode);
node.__set_type(return_type->to_thrift());
node.__set_fn(fn);
node.__set_num_children(static_cast<int16_t>(arg_types.size()));
node.__set_is_nullable(return_type->is_nullable());
if (short_circuit_evaluation) {
node.__set_short_circuit_evaluation(true);
}
return node;
}
VExprSPtr create_expr_from_node(const TExprNode& node) {
VExprSPtr expr;
auto status = VExpr::create_expr(node, expr);
DORIS_CHECK(status.ok()) << status.to_string();
return expr;
}
VExprSPtr table_function_expr(const std::string& function_name, const DataTypePtr& return_type,
const std::vector<DataTypePtr>& arg_types,
TExprNodeType::type node_type = TExprNodeType::FUNCTION_CALL,
TExprOpcode::type opcode = TExprOpcode::INVALID_OPCODE) {
const auto node = table_function_node(function_name, return_type, arg_types, node_type, opcode);
return VectorizedFnCall::create_shared(node);
}
VExprSPtr table_int32_greater_than_expr(int slot_id, int column_id, int32_t value) {
const auto int_type = std::make_shared<DataTypeInt32>();
const auto nullable_int_type = make_nullable(int_type);
auto expr = table_function_expr("gt", make_nullable(std::make_shared<DataTypeUInt8>()),
{nullable_int_type, int_type}, TExprNodeType::BINARY_PRED,
TExprOpcode::GT);
expr->add_child(table_int32_slot_ref(slot_id, column_id, "id"));
expr->add_child(table_int32_literal(value));
return expr;
}
VExprSPtr runtime_filter_wrapper_expr(VExprSPtr impl) {
TExprNode node;
node.__set_node_type(TExprNodeType::SLOT_REF);
node.__set_type(std::make_shared<DataTypeUInt8>()->to_thrift());
node.__set_num_children(1);
return RuntimeFilterExpr::create_shared(node, std::move(impl), 0, false, /*filter_id=*/1);
}
class NullableArrayBigintDefaultExpr final : public VExpr {
public:
explicit NullableArrayBigintDefaultExpr(DataTypePtr data_type)
: _name("single_element_groups") {
_data_type = std::move(data_type);
}
const std::string& expr_name() const override { return _name; }
bool is_constant() const override { return false; }
Status execute_column_impl(VExprContext*, const Block*, const Selector* selector, size_t count,
ColumnPtr& result_column) const override {
DCHECK(selector == nullptr || selector->size() == count);
auto values = ColumnInt64::create();
auto offsets = ColumnArray::ColumnOffsets::create();
auto null_map = ColumnUInt8::create();
for (size_t i = 0; i < count; ++i) {
values->insert_value(7);
offsets->insert_value(static_cast<Int64>(i + 1));
null_map->insert_value(0);
}
auto array_column = ColumnArray::create(std::move(values), std::move(offsets));
result_column = ColumnNullable::create(std::move(array_column), std::move(null_map));
return Status::OK();
}
private:
std::string _name;
};
class TableReaderMaterializeTestHelper final : public TableReader {
public:
using TableReader::_materialize_map_mapping_column;
};
VExprSPtr table_int32_sum_expr(int left_slot_id, int left_column_id, int right_slot_id,
int right_column_id) {
const auto int_type = std::make_shared<DataTypeInt32>();
const auto nullable_int_type = make_nullable(int_type);
auto expr =
table_function_expr("add", nullable_int_type, {nullable_int_type, nullable_int_type});
expr->add_child(table_int32_slot_ref(left_slot_id, left_column_id, "id"));
expr->add_child(table_int32_slot_ref(right_slot_id, right_column_id, "score"));
return expr;
}
VExprSPtr table_int32_sum_greater_than_expr(int left_slot_id, int left_column_id, int right_slot_id,
int right_column_id, int32_t value) {
const auto int_type = std::make_shared<DataTypeInt32>();
const auto nullable_int_type = make_nullable(int_type);
auto expr = table_function_expr("gt", make_nullable(std::make_shared<DataTypeUInt8>()),
{nullable_int_type, int_type}, TExprNodeType::BINARY_PRED,
TExprOpcode::GT);
expr->add_child(
table_int32_sum_expr(left_slot_id, left_column_id, right_slot_id, right_column_id));
expr->add_child(table_int32_literal(value));
return expr;
}
VExprSPtr table_condition_function_expr(const std::string& function_name, bool short_circuit) {
const auto int_type = std::make_shared<DataTypeInt32>();
std::vector<DataTypePtr> arg_types;
if (function_name == "if") {
arg_types = {std::make_shared<DataTypeUInt8>(), int_type, int_type};
} else {
arg_types = {int_type, int_type};
}
auto expr = create_expr_from_node(
table_function_node(function_name, int_type, arg_types, TExprNodeType::FUNCTION_CALL,
TExprOpcode::INVALID_OPCODE, short_circuit));
if (function_name == "if") {
expr->add_child(table_int32_greater_than_expr(0, 0, 0));
expr->add_child(table_int32_literal(1));
expr->add_child(table_int32_literal(0));
} else {
expr->add_child(table_int32_slot_ref(0, 0, "id"));
expr->add_child(table_int32_literal(0));
}
return expr;
}
VExprSPtr table_case_expr(bool short_circuit) {
const auto int_type = std::make_shared<DataTypeInt32>();
TCaseExpr case_node;
case_node.__set_has_case_expr(false);
case_node.__set_has_else_expr(true);
TExprNode node;
node.__set_node_type(TExprNodeType::CASE_EXPR);
node.__set_type(int_type->to_thrift());
node.__set_is_nullable(false);
node.__set_num_children(3);
node.__set_case_expr(case_node);
if (short_circuit) {
node.__set_short_circuit_evaluation(true);
}
auto expr = create_expr_from_node(node);
expr->add_child(table_int32_greater_than_expr(0, 0, 0));
expr->add_child(table_int32_literal(1));
expr->add_child(table_int32_literal(0));
return expr;
}
TEST(CloneTableExprTreeTest, ClonesConditionalExpressions) {
const std::vector<VExprSPtr> expressions {
table_condition_function_expr("if", false),
table_condition_function_expr("if", true),
table_condition_function_expr("ifnull", false),
table_condition_function_expr("ifnull", true),
table_condition_function_expr("coalesce", false),
table_condition_function_expr("coalesce", true),
table_case_expr(false),
table_case_expr(true),
};
for (const auto& expr : expressions) {
VExprSPtr cloned;
const auto status = clone_table_expr_tree(expr, &cloned);
ASSERT_TRUE(status.ok()) << expr->debug_string() << ": " << status.to_string();
ASSERT_NE(cloned, nullptr);
const auto* original_expr = expr.get();
const auto* cloned_expr = cloned.get();
EXPECT_TRUE(typeid(*original_expr) == typeid(*cloned_expr))
<< expr->expr_name() << " cloned as " << typeid(*cloned_expr).name();
EXPECT_EQ(expr->expr_name(), cloned->expr_name());
EXPECT_EQ(expr->get_num_children(), cloned->get_num_children());
EXPECT_NE(original_expr, cloned_expr);
}
}
// Scenario: cloning a VectorizedFnCall whose return type is complex must not reconstruct the expr
// from TExprNode, because DataTypeFactory rejects nested types through the primitive-type path.
TEST(CloneTableExprTreeTest, ClonesVectorizedFnCallWithComplexReturnType) {
const auto int_type = std::make_shared<DataTypeInt32>();
const auto string_type = std::make_shared<DataTypeString>();
const auto struct_type =
std::make_shared<DataTypeStruct>(DataTypes {int_type, string_type}, Strings {"a", "b"});
const auto array_type = std::make_shared<DataTypeArray>(struct_type);
auto expr = table_function_expr("element_at", struct_type, {array_type, int_type});
expr->add_child(VSlotRef::create_shared(0, 0, -1, array_type, "array_of_struct"));
expr->add_child(table_int32_literal(1));
VExprSPtr cloned;
const auto status = clone_table_expr_tree(expr, &cloned);
ASSERT_TRUE(status.ok()) << status.to_string();
ASSERT_NE(cloned, nullptr);
EXPECT_EQ(cloned->expr_name(), expr->expr_name());
EXPECT_TRUE(cloned->data_type()->equals(*struct_type));
EXPECT_EQ(cloned->get_num_children(), 2);
EXPECT_NE(cloned.get(), expr.get());
}
std::shared_ptr<arrow::Array> finish_array(arrow::ArrayBuilder* builder) {
std::shared_ptr<arrow::Array> array;
EXPECT_TRUE(builder->Finish(&array).ok());
return array;
}
std::shared_ptr<arrow::Array> build_int32_array(const std::vector<int32_t>& values) {
arrow::Int32Builder builder;
for (const auto value : values) {
EXPECT_TRUE(builder.Append(value).ok());
}
return finish_array(&builder);
}
std::shared_ptr<arrow::Array> build_string_array(const std::vector<std::string>& values) {
arrow::StringBuilder builder;
for (const auto& value : values) {
EXPECT_TRUE(builder.Append(value).ok());
}
return finish_array(&builder);
}
void write_parquet_file(const std::string& file_path, int32_t id, const std::string& value) {
auto schema = arrow::schema({
arrow::field("id", arrow::int32(), false),
arrow::field("value", arrow::utf8(), false),
});
auto table = arrow::Table::Make(schema, {build_int32_array({id}), build_string_array({value})});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder builder;
builder.version(::parquet::ParquetVersion::PARQUET_2_6);
builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
builder.compression(::parquet::Compression::UNCOMPRESSED);
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out, 1,
builder.build()));
}
void write_struct_parquet_file(const std::string& file_path, int32_t id) {
auto struct_type = arrow::struct_({arrow::field("id", arrow::int32(), false)});
arrow::StructBuilder builder(
struct_type, arrow::default_memory_pool(),
{std::make_shared<arrow::Int32Builder>(arrow::default_memory_pool())});
auto* id_builder = assert_cast<arrow::Int32Builder*>(builder.field_builder(0));
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(id_builder->Append(id).ok());
auto schema = arrow::schema({
arrow::field("s", struct_type, false),
});
auto table = arrow::Table::Make(schema, {finish_array(&builder)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder writer_builder;
writer_builder.version(::parquet::ParquetVersion::PARQUET_2_6);
writer_builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
writer_builder.compression(::parquet::Compression::UNCOMPRESSED);
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out, 1,
writer_builder.build()));
}
void write_struct_parquet_file(const std::string& file_path, const std::vector<int32_t>& ids,
int64_t row_group_size = -1) {
auto struct_type = arrow::struct_({arrow::field("id", arrow::int32(), false)});
arrow::StructBuilder builder(
struct_type, arrow::default_memory_pool(),
{std::make_shared<arrow::Int32Builder>(arrow::default_memory_pool())});
auto* id_builder = assert_cast<arrow::Int32Builder*>(builder.field_builder(0));
for (const auto id : ids) {
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(id_builder->Append(id).ok());
}
auto schema = arrow::schema({
arrow::field("s", struct_type, false),
});
auto table = arrow::Table::Make(schema, {finish_array(&builder)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder writer_builder;
writer_builder.version(::parquet::ParquetVersion::PARQUET_2_6);
writer_builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
writer_builder.compression(::parquet::Compression::UNCOMPRESSED);
const auto write_row_group_size =
row_group_size > 0 ? row_group_size : static_cast<int64_t>(ids.size());
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out,
write_row_group_size,
writer_builder.build()));
}
void write_struct_with_nullable_child_parquet_file(const std::string& file_path) {
auto struct_type = arrow::struct_({
arrow::field("id", arrow::int32(), false),
arrow::field("note", arrow::utf8(), true),
});
std::vector<std::shared_ptr<arrow::ArrayBuilder>> field_builders;
auto id_builder = std::make_unique<arrow::Int32Builder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(id_builder)));
auto note_builder = std::make_unique<arrow::StringBuilder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(note_builder)));
arrow::StructBuilder builder(struct_type, arrow::default_memory_pool(),
std::move(field_builders));
auto* struct_id_builder = assert_cast<arrow::Int32Builder*>(builder.field_builder(0));
auto* struct_note_builder = assert_cast<arrow::StringBuilder*>(builder.field_builder(1));
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(struct_id_builder->Append(7).ok());
EXPECT_TRUE(struct_note_builder->Append("seven").ok());
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(struct_id_builder->Append(8).ok());
EXPECT_TRUE(struct_note_builder->AppendNull().ok());
auto schema = arrow::schema({
arrow::field("s", struct_type, false),
});
auto table = arrow::Table::Make(schema, {finish_array(&builder)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder writer_builder;
writer_builder.version(::parquet::ParquetVersion::PARQUET_2_6);
writer_builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
writer_builder.compression(::parquet::Compression::UNCOMPRESSED);
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out, 2,
writer_builder.build()));
}
void write_list_struct_parquet_file(const std::string& file_path) {
auto struct_type = arrow::struct_(
{arrow::field("a", arrow::int32(), false), arrow::field("b", arrow::int32(), false)});
std::vector<std::shared_ptr<arrow::ArrayBuilder>> field_builders;
auto a_array_builder = std::make_unique<arrow::Int32Builder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(a_array_builder)));
auto b_array_builder = std::make_unique<arrow::Int32Builder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(b_array_builder)));
auto struct_builder = std::make_shared<arrow::StructBuilder>(
struct_type, arrow::default_memory_pool(), std::move(field_builders));
auto list_type = arrow::list(arrow::field("element", struct_type, true));
arrow::ListBuilder builder(arrow::default_memory_pool(), struct_builder, list_type);
auto* a_builder = assert_cast<arrow::Int32Builder*>(struct_builder->field_builder(0));
auto* b_builder = assert_cast<arrow::Int32Builder*>(struct_builder->field_builder(1));
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(struct_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(10).ok());
EXPECT_TRUE(b_builder->Append(11).ok());
EXPECT_TRUE(struct_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(20).ok());
EXPECT_TRUE(b_builder->Append(21).ok());
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(struct_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(30).ok());
EXPECT_TRUE(b_builder->Append(31).ok());
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(struct_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(40).ok());
EXPECT_TRUE(b_builder->Append(41).ok());
auto schema = arrow::schema({
arrow::field("xs", list_type, false),
});
auto table = arrow::Table::Make(schema, {finish_array(&builder)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder writer_builder;
writer_builder.version(::parquet::ParquetVersion::PARQUET_2_6);
writer_builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
writer_builder.compression(::parquet::Compression::UNCOMPRESSED);
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out, 3,
writer_builder.build()));
}
void write_map_struct_parquet_file(const std::string& file_path) {
auto key_builder = std::make_shared<arrow::Int32Builder>();
auto struct_type = arrow::struct_(
{arrow::field("a", arrow::int32(), false), arrow::field("b", arrow::utf8(), false)});
std::vector<std::shared_ptr<arrow::ArrayBuilder>> field_builders;
auto a_array_builder = std::make_unique<arrow::Int32Builder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(a_array_builder)));
auto b_array_builder = std::make_unique<arrow::StringBuilder>();
field_builders.push_back(std::shared_ptr<arrow::ArrayBuilder>(std::move(b_array_builder)));
auto value_builder = std::make_shared<arrow::StructBuilder>(
struct_type, arrow::default_memory_pool(), std::move(field_builders));
auto map_type = arrow::map(arrow::int32(), arrow::field("value", struct_type, false));
arrow::MapBuilder builder(arrow::default_memory_pool(), key_builder, value_builder, map_type);
auto* a_builder = assert_cast<arrow::Int32Builder*>(value_builder->field_builder(0));
auto* b_builder = assert_cast<arrow::StringBuilder*>(value_builder->field_builder(1));
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(key_builder->Append(1).ok());
EXPECT_TRUE(value_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(10).ok());
EXPECT_TRUE(b_builder->Append("ma").ok());
EXPECT_TRUE(key_builder->Append(2).ok());
EXPECT_TRUE(value_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(20).ok());
EXPECT_TRUE(b_builder->Append("mb").ok());
EXPECT_TRUE(builder.Append().ok());
EXPECT_TRUE(key_builder->Append(3).ok());
EXPECT_TRUE(value_builder->Append().ok());
EXPECT_TRUE(a_builder->Append(30).ok());
EXPECT_TRUE(b_builder->Append("mc").ok());
EXPECT_TRUE(builder.AppendEmptyValue().ok());
auto schema = arrow::schema({
arrow::field("kv", map_type, false),
});
auto table = arrow::Table::Make(schema, {finish_array(&builder)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder writer_builder;
writer_builder.version(::parquet::ParquetVersion::PARQUET_2_6);
writer_builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
writer_builder.compression(::parquet::Compression::UNCOMPRESSED);
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out, 3,
writer_builder.build()));
}
void write_int_pair_parquet_file(const std::string& file_path, const std::vector<int32_t>& ids,
const std::vector<int32_t>& scores,
const std::vector<std::string>& values,
int64_t row_group_size = -1) {
const auto id_metadata = arrow::key_value_metadata({"PARQUET:field_id"}, {"0"});
const auto score_metadata = arrow::key_value_metadata({"PARQUET:field_id"}, {"1"});
const auto value_metadata = arrow::key_value_metadata({"PARQUET:field_id"}, {"2"});
auto schema = arrow::schema({
arrow::field("id", arrow::int32(), false)->WithMetadata(id_metadata),
arrow::field("score", arrow::int32(), false)->WithMetadata(score_metadata),
arrow::field("value", arrow::utf8(), false)->WithMetadata(value_metadata),
});
auto table = arrow::Table::Make(schema, {build_int32_array(ids), build_int32_array(scores),
build_string_array(values)});
auto file_result = arrow::io::FileOutputStream::Open(file_path);
ASSERT_TRUE(file_result.ok()) << file_result.status();
std::shared_ptr<arrow::io::FileOutputStream> out = *file_result;
::parquet::WriterProperties::Builder builder;
builder.version(::parquet::ParquetVersion::PARQUET_2_6);
builder.data_page_version(::parquet::ParquetDataPageVersion::V2);
builder.compression(::parquet::Compression::UNCOMPRESSED);
const auto write_row_group_size =
row_group_size > 0 ? row_group_size : static_cast<int64_t>(ids.size());
PARQUET_THROW_NOT_OK(::parquet::arrow::WriteTable(*table, arrow::default_memory_pool(), out,
write_row_group_size, builder.build()));
}
Block build_table_block(const std::vector<ColumnDefinition>& columns) {
Block block;
for (const auto& column : columns) {
block.insert({column.type->create_column(), column.type, column.name});
}
return block;
}
const IColumn& expect_not_null_nullable_nested_column(const IColumn& column) {
if (!column.is_nullable()) {
return column;
}
const auto& nullable_column = assert_cast<const ColumnNullable&>(column);
for (const auto is_null : nullable_column.get_null_map_data()) {
EXPECT_EQ(is_null, 0);
}
return nullable_column.get_nested_column();
}
void expect_nullable_column_all_null(const IColumn& column) {
const auto full_column = column.convert_to_full_column_if_const();
const auto& nullable_column = assert_cast<const ColumnNullable&>(*full_column);
for (const auto is_null : nullable_column.get_null_map_data()) {
EXPECT_EQ(is_null, 1);
}
}
const IColumn& expect_not_null_table_column(const Block& block, size_t position) {
return expect_not_null_nullable_nested_column(*block.get_by_position(position).column);
}
ColumnDefinition make_table_column(int32_t id, const std::string& name, const DataTypePtr& type);
void expect_int32_column_values(const IColumn& column,
const std::vector<int32_t>& expected_values) {
const auto full_column = column.convert_to_full_column_if_const();
const auto& nested_column = expect_not_null_nullable_nested_column(*full_column);
const auto& values = assert_cast<const ColumnInt32&>(nested_column).get_data();
ASSERT_EQ(values.size(), expected_values.size());
for (size_t row = 0; row < expected_values.size(); ++row) {
EXPECT_EQ(values[row], expected_values[row]);
}
}
SplitReadOptions build_split_options(const std::string& file_path) {
SplitReadOptions options;
options.current_range.__set_path(file_path);
options.current_range.__set_file_size(
static_cast<int64_t>(std::filesystem::file_size(file_path)));
return options;
}
void set_table_level_row_count(SplitReadOptions* split_options, int64_t row_count) {
split_options->current_range.__isset.table_format_params = true;
split_options->current_range.table_format_params.__isset.table_level_row_count = true;
split_options->current_range.table_format_params.table_level_row_count = row_count;
}
int64_t parquet_column_start_offset(const ::parquet::ColumnChunkMetaData& column_metadata) {
return column_metadata.has_dictionary_page()
? static_cast<int64_t>(column_metadata.dictionary_page_offset())
: static_cast<int64_t>(column_metadata.data_page_offset());
}
SplitReadOptions build_split_options_for_row_group_mid(const std::string& file_path,
int row_group_idx) {
auto options = build_split_options(file_path);
auto reader = ::parquet::ParquetFileReader::OpenFile(file_path, false);
auto metadata = reader->metadata();
auto row_group_metadata = metadata->RowGroup(row_group_idx);
auto first_column = row_group_metadata->ColumnChunk(0);
auto last_column = row_group_metadata->ColumnChunk(row_group_metadata->num_columns() - 1);
const int64_t row_group_start_offset = parquet_column_start_offset(*first_column);
const int64_t row_group_end_offset =
parquet_column_start_offset(*last_column) + last_column->total_compressed_size();
const int64_t row_group_mid_offset =
row_group_start_offset + (row_group_end_offset - row_group_start_offset) / 2;
options.current_range.__set_start_offset(row_group_mid_offset);
options.current_range.__set_size(1);
return options;
}
DataTypePtr make_table_test_type(const DataTypePtr& type, bool nullable_root = true) {
DORIS_CHECK(type != nullptr);
const auto nested_type = remove_nullable(type);
DataTypePtr result;
if (const auto* struct_type = typeid_cast<const DataTypeStruct*>(nested_type.get())) {
DataTypes child_types;
child_types.reserve(struct_type->get_elements().size());
for (const auto& child_type : struct_type->get_elements()) {
child_types.push_back(make_table_test_type(child_type));
}
result = std::make_shared<DataTypeStruct>(child_types, struct_type->get_element_names());
} else if (const auto* array_type = typeid_cast<const DataTypeArray*>(nested_type.get())) {
result = std::make_shared<DataTypeArray>(
make_table_test_type(array_type->get_nested_type()));
} else if (const auto* map_type = typeid_cast<const DataTypeMap*>(nested_type.get())) {
result = std::make_shared<DataTypeMap>(make_table_test_type(map_type->get_key_type()),
make_table_test_type(map_type->get_value_type()));
} else {
result = nested_type;
}
return nullable_root ? make_nullable(result) : result;
}
ColumnDefinition make_table_column(int32_t id, const std::string& name, const DataTypePtr& type) {
ColumnDefinition column;
if (id >= 0) {
column.identifier = Field::create_field<TYPE_INT>(id);
}
column.name = name;
// TableReader tests model external table scan descriptors. Those table columns are nullable
// even when the Parquet file field itself is required, so keep the test schema aligned with
// the real scan contract at the construction boundary.
column.type = make_table_test_type(type);
return column;
}
ColumnDefinition make_file_column(int32_t id, const std::string& name, const DataTypePtr& type) {
ColumnDefinition field;
field.identifier = Field::create_field<TYPE_INT>(id);
field.local_id = id;
field.name = name;
field.type = make_table_test_type(type);
return field;
}
schema::external::TFieldPtr external_schema_field(std::string name, int32_t id,
std::vector<std::string> aliases = {}) {
auto field = std::make_shared<schema::external::TField>();
field->__set_name(std::move(name));
field->__set_id(id);
if (!aliases.empty()) {
field->__set_name_mapping(std::move(aliases));
}
schema::external::TFieldPtr field_ptr;
field_ptr.field_ptr = std::move(field);
field_ptr.__isset.field_ptr = true;
return field_ptr;
}
schema::external::TFieldPtr external_array_field(std::string name, int32_t id,
schema::external::TFieldPtr item_field,
std::vector<std::string> aliases = {}) {
auto field = external_schema_field(std::move(name), id, std::move(aliases));
schema::external::TArrayField array_field;
array_field.__set_item_field(std::move(item_field));
field.field_ptr->nestedField.__set_array_field(std::move(array_field));
field.field_ptr->__isset.nestedField = true;
return field;
}
schema::external::TFieldPtr external_map_field(std::string name, int32_t id,
schema::external::TFieldPtr key_field,
schema::external::TFieldPtr value_field,
std::vector<std::string> aliases = {}) {
auto field = external_schema_field(std::move(name), id, std::move(aliases));
schema::external::TMapField map_field;
map_field.__set_key_field(std::move(key_field));
map_field.__set_value_field(std::move(value_field));
field.field_ptr->nestedField.__set_map_field(std::move(map_field));
field.field_ptr->__isset.nestedField = true;
return field;
}
schema::external::TFieldPtr external_struct_field(std::string name, int32_t id,
std::vector<schema::external::TFieldPtr> fields,
std::vector<std::string> aliases = {}) {
auto field = external_schema_field(std::move(name), id, std::move(aliases));
schema::external::TStructField struct_field;
struct_field.__set_fields(std::move(fields));
field.field_ptr->nestedField.__set_struct_field(std::move(struct_field));
field.field_ptr->__isset.nestedField = true;
return field;
}
schema::external::TSchema external_schema(int64_t schema_id,
std::vector<schema::external::TFieldPtr> fields) {
schema::external::TStructField root_field;
root_field.__set_fields(std::move(fields));
schema::external::TSchema schema;
schema.__set_schema_id(schema_id);
schema.__set_root_field(std::move(root_field));
return schema;
}
ColumnDefinition make_nullable_column_definition(ColumnDefinition column) {
column.type = make_table_test_type(column.type);
for (auto& child : column.children) {
child = make_nullable_column_definition(std::move(child));
}
return column;
}
MutableColumnPtr make_not_null_nullable_column(MutableColumnPtr nested_column) {
auto null_map = ColumnUInt8::create();
for (size_t i = 0; i < nested_column->size(); ++i) {
null_map->insert_value(0);
}
return ColumnNullable::create(std::move(nested_column), std::move(null_map));
}
class TableReaderCharVarcharTestHelper final : public TableReader {
public:
using TableReader::_should_truncate_char_or_varchar_column;
using TableReader::_truncate_char_or_varchar_column;
};
class TableReaderCastTestHelper final : public TableReader {
public:
using TableReader::_cast_column_to_type;
};
TEST(TableReaderTest, TruncateCharOrVarcharPredicateOnlyAppliesToParquetStringWidthMismatch) {
ColumnMapping mapping;
mapping.table_type = std::make_shared<DataTypeString>(3, TYPE_VARCHAR);
mapping.file_type = std::make_shared<DataTypeString>(10, TYPE_VARCHAR);
EXPECT_TRUE(TableReaderCharVarcharTestHelper::_should_truncate_char_or_varchar_column(mapping));
mapping.file_type = std::make_shared<DataTypeString>(2, TYPE_VARCHAR);
EXPECT_FALSE(
TableReaderCharVarcharTestHelper::_should_truncate_char_or_varchar_column(mapping));
mapping.file_type = std::make_shared<DataTypeString>();
EXPECT_TRUE(TableReaderCharVarcharTestHelper::_should_truncate_char_or_varchar_column(mapping));
mapping.file_type = std::make_shared<DataTypeInt32>();
EXPECT_TRUE(TableReaderCharVarcharTestHelper::_should_truncate_char_or_varchar_column(mapping));
mapping.table_type = std::make_shared<DataTypeString>();
EXPECT_FALSE(
TableReaderCharVarcharTestHelper::_should_truncate_char_or_varchar_column(mapping));
}
TEST(TableReaderTest, TruncateCharOrVarcharColumnKeepsNullMap) {
auto nested = ColumnString::create();
nested->insert_data("abcdef", 6);
nested->insert_data("xyz", 3);
auto null_map = ColumnUInt8::create();
null_map->insert_value(0);
null_map->insert_value(1);
auto type = make_nullable(std::make_shared<DataTypeString>(3, TYPE_VARCHAR));
Block block;
block.insert({ColumnNullable::create(std::move(nested), std::move(null_map)), type, "v"});
TableReaderCharVarcharTestHelper::_truncate_char_or_varchar_column(&block, 0, 3);
ASSERT_EQ(block.columns(), 1);
ASSERT_EQ(block.rows(), 2);
const auto* nullable_column =
assert_cast<const ColumnNullable*>(block.get_by_position(0).column.get());
EXPECT_EQ(nullable_column->get_nested_column().get_data_at(0).to_string(), "abc");
EXPECT_FALSE(nullable_column->is_null_at(0));
EXPECT_TRUE(nullable_column->is_null_at(1));
}
void set_name_identifiers(std::vector<ColumnDefinition>* columns);
void set_name_identifier(ColumnDefinition* column) {
DORIS_CHECK(column != nullptr);
column->identifier = Field::create_field<TYPE_STRING>(column->name);
set_name_identifiers(&column->children);
}
void set_name_identifiers(std::vector<ColumnDefinition>* columns) {
DORIS_CHECK(columns != nullptr);
for (auto& column : *columns) {
set_name_identifier(&column);
}
}
VExprContextSPtr prepared_conjunct(RuntimeState* state, const VExprSPtr& expr) {
auto ctx = VExprContext::create_shared(expr);
auto status = ctx->prepare(state, RowDescriptor());
EXPECT_TRUE(status.ok()) << status;
status = ctx->open(state);
EXPECT_TRUE(status.ok()) << status;
return ctx;
}
struct FakeFileReaderState {
int init_count = 0;
int open_count = 0;
int close_count = 0;
int64_t total_rows = 2;
int64_t aggregate_count = -1;
bool eof_with_first_batch = true;
bool inject_delete_conjunct = false;
bool stop_during_aggregate = false;
std::shared_ptr<FileScanRequest> last_request;
std::shared_ptr<ConditionCacheContext> condition_cache_ctx;
std::shared_ptr<io::IOContext> io_ctx;
};
class FakeFileReader final : public FileReader {
public:
FakeFileReader(std::shared_ptr<io::FileSystemProperties>& system_properties,
std::unique_ptr<io::FileDescription>& file_description,
std::vector<ColumnDefinition> schema, std::shared_ptr<FakeFileReaderState> state)
: FileReader(system_properties, file_description, state->io_ctx, nullptr),
_schema(std::move(schema)),
_state(std::move(state)) {}
Status init(RuntimeState* state) override {
(void)state;
++_state->init_count;
_eof = false;
return Status::OK();
}