aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/BSONEachRowRowInputFormat.cpp
blob: 6536b87e55a5d2176a955a0c2b4cacad7a614e76 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
#include <IO/ReadBufferFromString.h>

#include <Formats/FormatFactory.h>
#include <Formats/FormatSettings.h>
#include <Formats/BSONTypes.h>
#include <Formats/EscapingRuleUtils.h>
#include <Processors/Formats/Impl/BSONEachRowRowInputFormat.h>
#include <IO/ReadHelpers.h>

#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnLowCardinality.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnDecimal.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnMap.h>

#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeFixedString.h>
#include <DataTypes/DataTypeUUID.h>
#include <DataTypes/DataTypeDateTime64.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/DataTypeMap.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/getLeastSupertype.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int INCORRECT_DATA;
    extern const int ILLEGAL_COLUMN;
    extern const int TOO_LARGE_STRING_SIZE;
    extern const int UNKNOWN_TYPE;
}

namespace
{
    enum
    {
        UNKNOWN_FIELD = size_t(-1),
    };
}

BSONEachRowRowInputFormat::BSONEachRowRowInputFormat(
    ReadBuffer & in_, const Block & header_, Params params_, const FormatSettings & format_settings_)
    : IRowInputFormat(header_, in_, std::move(params_))
    , format_settings(format_settings_)
    , prev_positions(header_.columns())
    , types(header_.getDataTypes())
{
    name_map = getPort().getHeader().getNamesToIndexesMap();
}

inline size_t BSONEachRowRowInputFormat::columnIndex(const StringRef & name, size_t key_index)
{
    /// Optimization by caching the order of fields (which is almost always the same)
    /// and a quick check to match the next expected field, instead of searching the hash table.

    if (prev_positions.size() > key_index
        && prev_positions[key_index] != Block::NameMap::const_iterator{}
        && name == prev_positions[key_index]->first)
    {
        return prev_positions[key_index]->second;
    }
    else
    {
        const auto it = name_map.find(name);

        if (it != name_map.end())
        {
            if (key_index < prev_positions.size())
                prev_positions[key_index] = it;

            return it->second;
        }
        else
            return UNKNOWN_FIELD;
    }
}

/// Read the field name. Resulting StringRef is valid only before next read from buf.
static StringRef readBSONKeyName(ReadBuffer & in, String & key_holder)
{
    // This is just an optimization: try to avoid copying the name into key_holder

    if (!in.eof())
    {
        char * next_pos = find_first_symbols<0>(in.position(), in.buffer().end());

        if (next_pos != in.buffer().end())
        {
            StringRef res(in.position(), next_pos - in.position());
            in.position() = next_pos + 1;
            return res;
        }
    }

    key_holder.clear();
    readNullTerminated(key_holder, in);
    return key_holder;
}

static UInt8 readBSONType(ReadBuffer & in)
{
    UInt8 type;
    readBinary(type, in);
    return type;
}

static size_t readBSONSize(ReadBuffer & in)
{
    BSONSizeT size;
    readBinary(size, in);
    return size;
}

template <typename T>
static void readAndInsertInteger(ReadBuffer & in, IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    /// We allow to read any integer into any integer column.
    /// For example we can read BSON Int32 into ClickHouse UInt8.

    if (bson_type == BSONType::INT32)
    {
        UInt32 value;
        readBinary(value, in);
        assert_cast<ColumnVector<T> &>(column).insertValue(static_cast<T>(value));
    }
    else if (bson_type == BSONType::INT64)
    {
        UInt64 value;
        readBinary(value, in);
        assert_cast<ColumnVector<T> &>(column).insertValue(static_cast<T>(value));
    }
    else if (bson_type == BSONType::BOOL)
    {
        UInt8 value;
        readBinary(value, in);
        assert_cast<ColumnVector<T> &>(column).insertValue(static_cast<T>(value));
    }
    else
    {
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into column with type {}",
                        getBSONTypeName(bson_type), data_type->getName());
    }
}

static void readAndInsertIPv4(ReadBuffer & in, IColumn & column, BSONType bson_type)
{
    /// We expect BSON type Int32 as IPv4 value.
    if (bson_type != BSONType::INT32)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON Int32 into column with type IPv4");

    UInt32 value;
    readBinary(value, in);
    assert_cast<ColumnIPv4 &>(column).insertValue(IPv4(value));
}

template <typename T>
static void readAndInsertDouble(ReadBuffer & in, IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != BSONType::DOUBLE)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into column with type {}",
                        getBSONTypeName(bson_type), data_type->getName());

    Float64 value;
    readBinary(value, in);
    assert_cast<ColumnVector<T> &>(column).insertValue(static_cast<T>(value));
}

template <typename DecimalType, BSONType expected_bson_type>
static void readAndInsertSmallDecimal(ReadBuffer & in, IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != expected_bson_type)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into column with type {}",
                        getBSONTypeName(bson_type), data_type->getName());

    DecimalType value;
    readBinary(value, in);
    assert_cast<ColumnDecimal<DecimalType> &>(column).insertValue(value);
}

static void readAndInsertDateTime64(ReadBuffer & in, IColumn & column, BSONType bson_type)
{
    if (bson_type != BSONType::INT64 && bson_type != BSONType::DATETIME)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into DateTime64 column", getBSONTypeName(bson_type));

    DateTime64 value;
    readBinary(value, in);
    assert_cast<DataTypeDateTime64::ColumnType &>(column).insertValue(value);
}

template <typename ColumnType>
static void readAndInsertBigInteger(ReadBuffer & in, IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != BSONType::BINARY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into column with type {}",
                        getBSONTypeName(bson_type), data_type->getName());

    auto size = readBSONSize(in);
    auto subtype = getBSONBinarySubtype(readBSONType(in));
    if (subtype != BSONBinarySubtype::BINARY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON Binary subtype {} into column with type {}",
                        getBSONBinarySubtypeName(subtype), data_type->getName());

    using ValueType = typename ColumnType::ValueType;

    if (size != sizeof(ValueType))
        throw Exception(
            ErrorCodes::INCORRECT_DATA,
            "Cannot parse value of type {}, size of binary data is not equal to the binary size of expected value: {} != {}",
            data_type->getName(),
            size,
            sizeof(ValueType));

    ValueType value;
    readBinary(value, in);
    assert_cast<ColumnType &>(column).insertValue(value);
}

template <bool is_fixed_string>
static void readAndInsertStringImpl(ReadBuffer & in, IColumn & column, size_t size)
{
    if constexpr (is_fixed_string)
    {
        auto & fixed_string_column = assert_cast<ColumnFixedString &>(column);
        size_t n = fixed_string_column.getN();
        if (size > n)
            throw Exception(ErrorCodes::TOO_LARGE_STRING_SIZE, "Too large string for FixedString column");

        auto & data = fixed_string_column.getChars();

        size_t old_size = data.size();
        data.resize_fill(old_size + n);

        try
        {
            in.readStrict(reinterpret_cast<char *>(data.data() + old_size), size);
        }
        catch (...)
        {
            /// Restore column state in case of any exception.
            data.resize_assume_reserved(old_size);
            throw;
        }
    }
    else
    {
        auto & column_string = assert_cast<ColumnString &>(column);
        auto & data = column_string.getChars();
        auto & offsets = column_string.getOffsets();

        size_t old_chars_size = data.size();
        size_t offset = old_chars_size + size + 1;
        offsets.push_back(offset);

        try
        {
            data.resize(offset);
            in.readStrict(reinterpret_cast<char *>(&data[offset - size - 1]), size);
            data.back() = 0;
        }
        catch (...)
        {
            /// Restore column state in case of any exception.
            offsets.pop_back();
            data.resize_assume_reserved(old_chars_size);
            throw;
        }
    }
}

template <bool is_fixed_string>
static void readAndInsertString(ReadBuffer & in, IColumn & column, BSONType bson_type)
{
    if (bson_type == BSONType::STRING || bson_type == BSONType::SYMBOL || bson_type == BSONType::JAVA_SCRIPT_CODE)
    {
        auto size = readBSONSize(in);
        if (size == 0)
            throw Exception(ErrorCodes::INCORRECT_DATA, "Incorrect size of a string (zero) in BSON");
        readAndInsertStringImpl<is_fixed_string>(in, column, size - 1);
        assertChar(0, in);
    }
    else if (bson_type == BSONType::BINARY)
    {
        auto size = readBSONSize(in);
        auto subtype = getBSONBinarySubtype(readBSONType(in));
        if (subtype == BSONBinarySubtype::BINARY || subtype == BSONBinarySubtype::BINARY_OLD)
            readAndInsertStringImpl<is_fixed_string>(in, column, size);
        else
            throw Exception(
                ErrorCodes::ILLEGAL_COLUMN,
                "Cannot insert BSON Binary subtype {} into String column",
                getBSONBinarySubtypeName(subtype));
    }
    else if (bson_type == BSONType::OBJECT_ID)
    {
        readAndInsertStringImpl<is_fixed_string>(in, column, BSON_OBJECT_ID_SIZE);
    }
    else
    {
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into String column", getBSONTypeName(bson_type));
    }
}

static void readAndInsertIPv6(ReadBuffer & in, IColumn & column, BSONType bson_type)
{
    if (bson_type != BSONType::BINARY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into IPv6 column", getBSONTypeName(bson_type));

    auto size = readBSONSize(in);
    auto subtype = getBSONBinarySubtype(readBSONType(in));
    if (subtype != BSONBinarySubtype::BINARY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON Binary subtype {} into IPv6 column", getBSONBinarySubtypeName(subtype));

    if (size != sizeof(IPv6))
        throw Exception(
            ErrorCodes::INCORRECT_DATA,
            "Cannot parse value of type IPv6, size of binary data is not equal to the binary size of IPv6 value: {} != {}",
            size,
            sizeof(IPv6));

    IPv6 value;
    readBinary(value, in);
    assert_cast<ColumnIPv6 &>(column).insertValue(value);
}


static void readAndInsertUUID(ReadBuffer & in, IColumn & column, BSONType bson_type)
{
    if (bson_type != BSONType::BINARY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into UUID column", getBSONTypeName(bson_type));

    auto size = readBSONSize(in);
    auto subtype = getBSONBinarySubtype(readBSONType(in));
    if (subtype != BSONBinarySubtype::UUID && subtype != BSONBinarySubtype::UUID_OLD)
        throw Exception(
            ErrorCodes::ILLEGAL_COLUMN,
            "Cannot insert BSON Binary subtype {} into UUID column",
            getBSONBinarySubtypeName(subtype));

    if (size != sizeof(UUID))
        throw Exception(
            ErrorCodes::INCORRECT_DATA,
            "Cannot parse value of type UUID, size of binary data is not equal to the binary size of UUID value: {} != {}",
            size,
            sizeof(UUID));

    UUID value;
    readBinary(value, in);
    assert_cast<ColumnUUID &>(column).insertValue(value);
}

void BSONEachRowRowInputFormat::readArray(IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != BSONType::ARRAY)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into Array column", getBSONTypeName(bson_type));

    const auto * data_type_array = assert_cast<const DataTypeArray *>(data_type.get());
    const auto & nested_type = data_type_array->getNestedType();
    auto & array_column = assert_cast<ColumnArray &>(column);
    auto & nested_column = array_column.getData();

    size_t document_start = in->count();
    BSONSizeT document_size;
    readBinary(document_size, *in);
    if (document_size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
        throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", document_size);

    while (in->count() - document_start + sizeof(BSON_DOCUMENT_END) != document_size)
    {
        auto nested_bson_type = getBSONType(readBSONType(*in));
        readBSONKeyName(*in, current_key_name);
        readField(nested_column, nested_type, nested_bson_type);
    }

    assertChar(BSON_DOCUMENT_END, *in);
    array_column.getOffsets().push_back(array_column.getData().size());
}

void BSONEachRowRowInputFormat::readTuple(IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != BSONType::ARRAY && bson_type != BSONType::DOCUMENT)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into Tuple column", getBSONTypeName(bson_type));

    /// When BSON type is ARRAY, names in nested document are not useful
    /// (most likely they are just sequential numbers).
    bool use_key_names = bson_type == BSONType::DOCUMENT;

    const auto * data_type_tuple = assert_cast<const DataTypeTuple *>(data_type.get());
    auto & tuple_column = assert_cast<ColumnTuple &>(column);
    size_t read_nested_columns = 0;

    size_t document_start = in->count();
    BSONSizeT document_size;
    readBinary(document_size, *in);
    if (document_size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
        throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", document_size);

    while (in->count() - document_start + sizeof(BSON_DOCUMENT_END) != document_size)
    {
        auto nested_bson_type = getBSONType(readBSONType(*in));
        auto name = readBSONKeyName(*in, current_key_name);

        size_t index = read_nested_columns;
        if (use_key_names)
        {
            auto try_get_index = data_type_tuple->tryGetPositionByName(name.toString());
            if (!try_get_index)
                throw Exception(
                                ErrorCodes::INCORRECT_DATA,
                                "Cannot parse tuple column with type {} from BSON array/embedded document field: "
                                "tuple doesn't have element with name \"{}\"",
                                data_type->getName(),
                                name);
            index = *try_get_index;
        }

        if (index >= data_type_tuple->getElements().size())
            throw Exception(
                            ErrorCodes::INCORRECT_DATA,
                            "Cannot parse tuple column with type {} from BSON array/embedded document field: "
                            "the number of fields BSON document exceeds the number of fields in tuple",
                            data_type->getName());

        readField(tuple_column.getColumn(index), data_type_tuple->getElement(index), nested_bson_type);
        ++read_nested_columns;
    }

    assertChar(BSON_DOCUMENT_END, *in);

    if (read_nested_columns != data_type_tuple->getElements().size())
        throw Exception(
                        ErrorCodes::INCORRECT_DATA,
                        "Cannot parse tuple column with type {} from BSON array/embedded document field, "
                        "the number of fields in tuple and BSON document doesn't match: {} != {}",
                        data_type->getName(),
                        data_type_tuple->getElements().size(),
                        read_nested_columns);
}

void BSONEachRowRowInputFormat::readMap(IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type != BSONType::DOCUMENT)
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot insert BSON {} into Map column", getBSONTypeName(bson_type));

    const auto * data_type_map = assert_cast<const DataTypeMap *>(data_type.get());
    const auto & key_data_type = data_type_map->getKeyType();
    const auto & value_data_type = data_type_map->getValueType();
    auto & column_map = assert_cast<ColumnMap &>(column);
    auto & key_column = column_map.getNestedData().getColumn(0);
    auto & value_column = column_map.getNestedData().getColumn(1);
    auto & offsets = column_map.getNestedColumn().getOffsets();

    size_t document_start = in->count();
    BSONSizeT document_size;
    readBinary(document_size, *in);
    if (document_size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
        throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", document_size);

    while (in->count() - document_start + sizeof(BSON_DOCUMENT_END) != document_size)
    {
        auto nested_bson_type = getBSONType(readBSONType(*in));
        auto name = readBSONKeyName(*in, current_key_name);
        ReadBufferFromMemory buf(name.data, name.size);
        key_data_type->getDefaultSerialization()->deserializeWholeText(key_column, buf, format_settings);
        readField(value_column, value_data_type, nested_bson_type);
    }

    assertChar(BSON_DOCUMENT_END, *in);
    offsets.push_back(key_column.size());
}


bool BSONEachRowRowInputFormat::readField(IColumn & column, const DataTypePtr & data_type, BSONType bson_type)
{
    if (bson_type == BSONType::NULL_VALUE)
    {
        if (data_type->isNullable())
        {
            column.insertDefault();
            return true;
        }

        if (!format_settings.null_as_default)
            throw Exception(ErrorCodes::ILLEGAL_COLUMN,
                            "Cannot insert BSON Null value into non-nullable column with type {}",
                            data_type->getName());

        column.insertDefault();
        return false;
    }

    switch (data_type->getTypeId())
    {
        case TypeIndex::Nullable:
        {
            auto & nullable_column = assert_cast<ColumnNullable &>(column);
            auto & nested_column = nullable_column.getNestedColumn();
            const auto & nested_type = assert_cast<const DataTypeNullable *>(data_type.get())->getNestedType();
            nullable_column.getNullMapColumn().insertValue(0);
            return readField(nested_column, nested_type, bson_type);
        }
        case TypeIndex::LowCardinality:
        {
            auto & lc_column = assert_cast<ColumnLowCardinality &>(column);
            auto tmp_column = lc_column.getDictionary().getNestedColumn()->cloneEmpty();
            const auto & dict_type = assert_cast<const DataTypeLowCardinality *>(data_type.get())->getDictionaryType();
            auto res = readField(*tmp_column, dict_type, bson_type);
            lc_column.insertFromFullColumn(*tmp_column, 0);
            return res;
        }
        case TypeIndex::Enum8: [[fallthrough]];
        case TypeIndex::Int8:
        {
            readAndInsertInteger<Int8>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::UInt8:
        {
            readAndInsertInteger<UInt8>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Enum16: [[fallthrough]];
        case TypeIndex::Int16:
        {
            readAndInsertInteger<Int16>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Date: [[fallthrough]];
        case TypeIndex::UInt16:
        {
            readAndInsertInteger<UInt16>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Date32: [[fallthrough]];
        case TypeIndex::Int32:
        {
            readAndInsertInteger<Int32>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::DateTime: [[fallthrough]];
        case TypeIndex::UInt32:
        {
            readAndInsertInteger<UInt32>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Int64:
        {
            readAndInsertInteger<Int64>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::UInt64:
        {
            readAndInsertInteger<UInt64>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Int128:
        {
            readAndInsertBigInteger<ColumnInt128>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::UInt128:
        {
            readAndInsertBigInteger<ColumnUInt128>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Int256:
        {
            readAndInsertBigInteger<ColumnInt256>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::UInt256:
        {
            readAndInsertBigInteger<ColumnUInt256>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Float32:
        {
            readAndInsertDouble<Float32>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Float64:
        {
            readAndInsertDouble<Float64>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Decimal32:
        {
            readAndInsertSmallDecimal<Decimal32, BSONType::INT32>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Decimal64:
        {
            readAndInsertSmallDecimal<Decimal64, BSONType::INT64>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Decimal128:
        {
            readAndInsertBigInteger<ColumnDecimal<Decimal128>>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Decimal256:
        {
            readAndInsertBigInteger<ColumnDecimal<Decimal256>>(*in, column, data_type, bson_type);
            return true;
        }
        case TypeIndex::DateTime64:
        {
            readAndInsertDateTime64(*in, column, bson_type);
            return true;
        }
        case TypeIndex::FixedString:
        {
            readAndInsertString<true>(*in, column, bson_type);
            return true;
        }
        case TypeIndex::String:
        {
            readAndInsertString<false>(*in, column, bson_type);
            return true;
        }
        case TypeIndex::IPv4:
        {
            readAndInsertIPv4(*in, column, bson_type);
            return true;
        }
        case TypeIndex::IPv6:
        {
            readAndInsertIPv6(*in, column, bson_type);
            return true;
        }
        case TypeIndex::UUID:
        {
            readAndInsertUUID(*in, column, bson_type);
            return true;
        }
        case TypeIndex::Array:
        {
            readArray(column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Tuple:
        {
            readTuple(column, data_type, bson_type);
            return true;
        }
        case TypeIndex::Map:
        {
            readMap(column, data_type, bson_type);
            return true;
        }
        default:
        {
            throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Type {} is not supported for output in BSON format", data_type->getName());
        }
    }
}

static void skipBSONField(ReadBuffer & in, BSONType type)
{
    switch (type)
    {
        case BSONType::DOUBLE:
        {
            in.ignore(sizeof(Float64));
            break;
        }
        case BSONType::BOOL:
        {
            in.ignore(sizeof(UInt8));
            break;
        }
        case BSONType::INT64: [[fallthrough]];
        case BSONType::DATETIME: [[fallthrough]];
        case BSONType::TIMESTAMP:
        {
            in.ignore(sizeof(UInt64));
            break;
        }
        case BSONType::INT32:
        {
            in.ignore(sizeof(Int32));
            break;
        }
        case BSONType::JAVA_SCRIPT_CODE: [[fallthrough]];
        case BSONType::SYMBOL: [[fallthrough]];
        case BSONType::STRING:
        {
            BSONSizeT size;
            readBinary(size, in);
            in.ignore(size);
            break;
        }
        case BSONType::DOCUMENT: [[fallthrough]];
        case BSONType::ARRAY:
        {
            BSONSizeT size;
            readBinary(size, in);
            if (size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
                throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", size);
            in.ignore(size - sizeof(size));
            break;
        }
        case BSONType::BINARY:
        {
            BSONSizeT size;
            readBinary(size, in);
            in.ignore(size + 1);
            break;
        }
        case BSONType::MIN_KEY: [[fallthrough]];
        case BSONType::MAX_KEY: [[fallthrough]];
        case BSONType::UNDEFINED: [[fallthrough]];
        case BSONType::NULL_VALUE:
        {
            break;
        }
        case BSONType::OBJECT_ID:
        {
            in.ignore(BSON_OBJECT_ID_SIZE);
            break;
        }
        case BSONType::REGEXP:
        {
            skipNullTerminated(in);
            skipNullTerminated(in);
            break;
        }
        case BSONType::DB_POINTER:
        {
            BSONSizeT size;
            readBinary(size, in);
            in.ignore(size + BSON_DB_POINTER_SIZE);
            break;
        }
        case BSONType::JAVA_SCRIPT_CODE_W_SCOPE:
        {
            BSONSizeT size;
            readBinary(size, in);
            if (size < sizeof(BSONSizeT))
                throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid java code_w_scope size: {}", size);
            in.ignore(size - sizeof(size));
            break;
        }
        case BSONType::DECIMAL128:
        {
            in.ignore(16);
            break;
        }
    }
}

void BSONEachRowRowInputFormat::skipUnknownField(BSONType type, const String & key_name)
{
    if (!format_settings.skip_unknown_fields)
        throw Exception(ErrorCodes::INCORRECT_DATA, "Unknown field found while parsing BSONEachRow format: {}", key_name);

    skipBSONField(*in, type);
}

void BSONEachRowRowInputFormat::syncAfterError()
{
    /// Skip all remaining bytes in current document
    size_t already_read_bytes = in->count() - current_document_start;
    in->ignore(current_document_size - already_read_bytes);
}

bool BSONEachRowRowInputFormat::readRow(MutableColumns & columns, RowReadExtension & ext)
{
    size_t num_columns = columns.size();

    read_columns.assign(num_columns, false);
    seen_columns.assign(num_columns, false);

    if (in->eof())
        return false;

    size_t key_index = 0;

    current_document_start = in->count();
    readBinary(current_document_size, *in);
    if (current_document_size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
        throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", current_document_size);

    while (in->count() - current_document_start + sizeof(BSON_DOCUMENT_END) != current_document_size)
    {
        auto type = getBSONType(readBSONType(*in));
        auto name = readBSONKeyName(*in, current_key_name);
        auto index = columnIndex(name, key_index);

        if (index == UNKNOWN_FIELD)
        {
            current_key_name.assign(name.data, name.size);
            skipUnknownField(BSONType(type), current_key_name);
        }
        else
        {
            if (seen_columns[index])
                throw Exception(ErrorCodes::INCORRECT_DATA, "Duplicate field found while parsing BSONEachRow format: {}", name);

            seen_columns[index] = true;
            read_columns[index] = readField(*columns[index], types[index], BSONType(type));
        }

        ++key_index;
    }

    assertChar(BSON_DOCUMENT_END, *in);

    const auto & header = getPort().getHeader();
    /// Fill non-visited columns with the default values.
    for (size_t i = 0; i < num_columns; ++i)
        if (!seen_columns[i])
            header.getByPosition(i).type->insertDefaultInto(*columns[i]);

    if (format_settings.defaults_for_omitted_fields)
        ext.read_columns = read_columns;
    else
        ext.read_columns.assign(read_columns.size(), true);

    return true;
}

void BSONEachRowRowInputFormat::resetParser()
{
    IRowInputFormat::resetParser();
    read_columns.clear();
    seen_columns.clear();
    prev_positions.clear();
}

size_t BSONEachRowRowInputFormat::countRows(size_t max_block_size)
{
    size_t num_rows = 0;
    BSONSizeT document_size;
    while (!in->eof() && num_rows < max_block_size)
    {
        readBinary(document_size, *in);
        if (document_size < sizeof(BSONSizeT) + sizeof(BSON_DOCUMENT_END))
            throw Exception(ErrorCodes::INCORRECT_DATA, "Invalid document size: {}", document_size);
        in->ignore(document_size - sizeof(BSONSizeT));
        ++num_rows;
    }

    return num_rows;
}

BSONEachRowSchemaReader::BSONEachRowSchemaReader(ReadBuffer & in_, const FormatSettings & settings_)
    : IRowWithNamesSchemaReader(in_, settings_)
{
}

DataTypePtr BSONEachRowSchemaReader::getDataTypeFromBSONField(BSONType type, bool allow_to_skip_unsupported_types, bool & skip)
{
    switch (type)
    {
        case BSONType::DOUBLE:
        {
            in.ignore(sizeof(Float64));
            return std::make_shared<DataTypeFloat64>();
        }
        case BSONType::BOOL:
        {
            in.ignore(sizeof(UInt8));
            return DataTypeFactory::instance().get("Bool");
        }
        case BSONType::INT64:
        {
            in.ignore(sizeof(Int64));
            return std::make_shared<DataTypeInt64>();
        }
        case BSONType::DATETIME:
        {
            in.ignore(sizeof(Int64));
            return std::make_shared<DataTypeDateTime64>(6, "UTC");
        }
        case BSONType::INT32:
        {
            in.ignore(sizeof(Int32));
            return std::make_shared<DataTypeInt32>();
        }
        case BSONType::SYMBOL: [[fallthrough]];
        case BSONType::JAVA_SCRIPT_CODE: [[fallthrough]];
        case BSONType::STRING:
        {
            BSONSizeT size;
            readBinary(size, in);
            in.ignore(size);
            return std::make_shared<DataTypeString>();
        }
        case BSONType::OBJECT_ID:;
        {
            in.ignore(BSON_OBJECT_ID_SIZE);
            return makeNullable(std::make_shared<DataTypeFixedString>(BSON_OBJECT_ID_SIZE));
        }
        case BSONType::DOCUMENT:
        {
            auto nested_names_and_types = getDataTypesFromBSONDocument(false);
            auto nested_types = nested_names_and_types.getTypes();
            bool types_are_equal = true;
            if (nested_types.empty() || !nested_types[0])
                return nullptr;

            for (size_t i = 1; i != nested_types.size(); ++i)
            {
                if (!nested_types[i])
                    return nullptr;

                types_are_equal &= nested_types[i]->equals(*nested_types[0]);
            }

            if (types_are_equal)
                return std::make_shared<DataTypeMap>(std::make_shared<DataTypeString>(), nested_types[0]);

            return std::make_shared<DataTypeTuple>(std::move(nested_types), nested_names_and_types.getNames());

        }
        case BSONType::ARRAY:
        {
            auto nested_types = getDataTypesFromBSONDocument(false).getTypes();
            bool types_are_equal = true;
            if (nested_types.empty() || !nested_types[0])
                return nullptr;

            for (size_t i = 1; i != nested_types.size(); ++i)
            {
                if (!nested_types[i])
                    return nullptr;

                types_are_equal &= nested_types[i]->equals(*nested_types[0]);
            }

            if (types_are_equal)
                return std::make_shared<DataTypeArray>(nested_types[0]);

            return std::make_shared<DataTypeTuple>(std::move(nested_types));
        }
        case BSONType::BINARY:
        {
            BSONSizeT size;
            readBinary(size, in);
            auto subtype = getBSONBinarySubtype(readBSONType(in));
            in.ignore(size);
            switch (subtype)
            {
                case BSONBinarySubtype::BINARY_OLD: [[fallthrough]];
                case BSONBinarySubtype::BINARY:
                    return std::make_shared<DataTypeString>();
                case BSONBinarySubtype::UUID_OLD: [[fallthrough]];
                case BSONBinarySubtype::UUID:
                    return std::make_shared<DataTypeUUID>();
                default:
                    throw Exception(ErrorCodes::UNKNOWN_TYPE, "BSON binary subtype {} is not supported", getBSONBinarySubtypeName(subtype));
            }
        }
        case BSONType::NULL_VALUE:
        {
            return nullptr;
        }
        default:
        {
            if (!allow_to_skip_unsupported_types)
                throw Exception(ErrorCodes::UNKNOWN_TYPE, "BSON type {} is not supported", getBSONTypeName(type));

            skip = true;
            skipBSONField(in, type);
            return nullptr;
        }
    }
}

NamesAndTypesList BSONEachRowSchemaReader::getDataTypesFromBSONDocument(bool allow_to_skip_unsupported_types)
{
    size_t document_start = in.count();
    BSONSizeT document_size;
    readBinary(document_size, in);
    NamesAndTypesList names_and_types;
    while (in.count() - document_start + sizeof(BSON_DOCUMENT_END) != document_size)
    {
        auto bson_type = getBSONType(readBSONType(in));
        String name;
        readNullTerminated(name, in);
        bool skip = false;
        auto type = getDataTypeFromBSONField(bson_type, allow_to_skip_unsupported_types, skip);
        if (!skip)
            names_and_types.emplace_back(name, type);
    }

    assertChar(BSON_DOCUMENT_END, in);

    return names_and_types;
}

NamesAndTypesList BSONEachRowSchemaReader::readRowAndGetNamesAndDataTypes(bool & eof)
{
    if (in.eof())
    {
        eof = true;
        return {};
    }

    return getDataTypesFromBSONDocument(format_settings.bson.skip_fields_with_unsupported_types_in_schema_inference);
}

void BSONEachRowSchemaReader::transformTypesIfNeeded(DataTypePtr & type, DataTypePtr & new_type)
{
    DataTypes types = {type, new_type};
    /// For example for integer conversion Int32,
    auto least_supertype = tryGetLeastSupertype(types);
    if (least_supertype)
        type = new_type = least_supertype;
}

static std::pair<bool, size_t>
fileSegmentationEngineBSONEachRow(ReadBuffer & in, DB::Memory<> & memory, size_t min_bytes, size_t max_rows)
{
    size_t number_of_rows = 0;

    while (!in.eof() && memory.size() < min_bytes && number_of_rows < max_rows)
    {
        BSONSizeT document_size;
        readBinary(document_size, in);

        if (document_size < sizeof(document_size))
            throw ParsingException(ErrorCodes::INCORRECT_DATA, "Size of BSON document is invalid");

        if (min_bytes != 0 && document_size > 10 * min_bytes)
            throw ParsingException(
                ErrorCodes::INCORRECT_DATA,
                "Size of BSON document is extremely large. Expected not greater than {} bytes, but current is {} bytes per row. Increase "
                "the value setting 'min_chunk_bytes_for_parallel_parsing' or check your data manually, most likely BSON is malformed",
                min_bytes, document_size);

        if (document_size < sizeof(document_size))
            throw ParsingException(ErrorCodes::INCORRECT_DATA, "Size of BSON document is invalid");

        size_t old_size = memory.size();
        memory.resize(old_size + document_size);
        unalignedStore<BSONSizeT>(memory.data() + old_size, document_size);
        in.readStrict(memory.data() + old_size + sizeof(document_size), document_size - sizeof(document_size));
        ++number_of_rows;
    }

    return {!in.eof(), number_of_rows};
}

void registerInputFormatBSONEachRow(FormatFactory & factory)
{
    factory.registerInputFormat(
        "BSONEachRow",
        [](ReadBuffer & buf, const Block & sample, IRowInputFormat::Params params, const FormatSettings & settings)
        { return std::make_shared<BSONEachRowRowInputFormat>(buf, sample, std::move(params), settings); });
    factory.registerFileExtension("bson", "BSONEachRow");
}

void registerFileSegmentationEngineBSONEachRow(FormatFactory & factory)
{
    factory.registerFileSegmentationEngine("BSONEachRow", &fileSegmentationEngineBSONEachRow);
}

void registerBSONEachRowSchemaReader(FormatFactory & factory)
{
    factory.registerSchemaReader("BSONEachRow", [](ReadBuffer & buf, const FormatSettings & settings)
    {
        return std::make_unique<BSONEachRowSchemaReader>(buf, settings);
    });
    factory.registerAdditionalInfoForSchemaCacheGetter("BSONEachRow", [](const FormatSettings & settings)
    {
         String result = getAdditionalFormatInfoForAllRowBasedFormats(settings);
         return result + fmt::format(", skip_fields_with_unsupported_types_in_schema_inference={}",
                                     settings.bson.skip_fields_with_unsupported_types_in_schema_inference);
    });
}

}