aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/udf/arrow/block_reader.h
blob: d7863329ee4a928abb991cfe71e48d6091db2ecf (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
#pragma once

#include "block_item.h"
#include "block_io_buffer.h"
#include "util.h"
#include <arrow/datum.h>

#include <yql/essentials/public/udf/udf_type_inspection.h>
#include <yql/essentials/public/udf/udf_value_builder.h>

namespace NYql {
namespace NUdf {

class IBlockReader : private TNonCopyable {
public:
    virtual ~IBlockReader() = default;
    // result will reference to Array/Scalar internals and will be valid until next call to GetItem/GetScalarItem
    virtual TBlockItem GetItem(const arrow::ArrayData& data, size_t index) = 0;
    virtual TBlockItem GetScalarItem(const arrow::Scalar& scalar) = 0;

    virtual ui64 GetDataWeight(const arrow::ArrayData& data) const = 0;
    virtual ui64 GetDataWeight(TBlockItem item) const = 0;
    virtual ui64 GetDefaultValueWeight() const = 0;

    virtual void SaveItem(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const = 0;
    virtual void SaveScalarItem(const arrow::Scalar& scalar, TOutputBuffer& out) const = 0;
};

struct TBlockItemSerializeProps {
    TMaybe<ui32> MaxSize = 0; // maximum size each block item can occupy in TOutputBuffer
                              // (will be undefined for dynamic object like string)
    bool IsFixed = true;      // true if each block item takes fixed size
};

template<typename T, bool Nullable, typename TDerived> 
class TFixedSizeBlockReaderBase : public IBlockReader {
public:
    TBlockItem GetItem(const arrow::ArrayData& data, size_t index) final {
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return {};
            }
        }
        return static_cast<TDerived*>(this)->MakeBlockItem(data.GetValues<T>(1)[index]);
    }

    TBlockItem GetScalarItem(const arrow::Scalar& scalar) final {
        using namespace arrow::internal;

        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return {};
            }
        }

        if constexpr(std::is_same_v<T, NYql::NDecimal::TInt128>) {
            auto& fixedScalar = checked_cast<const arrow::FixedSizeBinaryScalar&>(scalar);
            T value; memcpy((void*)&value, fixedScalar.value->data(), sizeof(T));
            return static_cast<TDerived*>(this)->MakeBlockItem(value);
        } else {
            return static_cast<TDerived*>(this)->MakeBlockItem(
                *static_cast<const T*>(checked_cast<const PrimitiveScalarBase&>(scalar).data())
            );
        }
    }

    ui64 GetDataWeight(const arrow::ArrayData& data) const final {
        if constexpr (Nullable) {
            return (1 + sizeof(T)) * data.length;
        }
        return sizeof(T) * data.length;
    }

    ui64 GetDataWeight(TBlockItem item) const final {
        Y_UNUSED(item);
        return GetDefaultValueWeight();
    }

    ui64 GetDefaultValueWeight() const final {
        if constexpr (Nullable) {
            return 1 + sizeof(T);
        }
        return sizeof(T);
    }

    void SaveItem(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const final {
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        out.PushNumber(data.GetValues<T>(1)[index]);
    }

    void SaveScalarItem(const arrow::Scalar& scalar, TOutputBuffer& out) const final {
        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        if constexpr(std::is_same_v<T, NYql::NDecimal::TInt128>) {
            auto& fixedScalar = arrow::internal::checked_cast<const arrow::FixedSizeBinaryScalar&>(scalar);
            T value; memcpy((void*)&value, fixedScalar.value->data(), sizeof(T));
            out.PushNumber(value);
        } else {
            out.PushNumber(*static_cast<const T*>(arrow::internal::checked_cast<const arrow::internal::PrimitiveScalarBase&>(scalar).data()));
        }
    }
};

template<typename T, bool Nullable>
class TFixedSizeBlockReader : public TFixedSizeBlockReaderBase<T, Nullable, TFixedSizeBlockReader<T, Nullable>> {
public:
    TBlockItem MakeBlockItem(const T& item) const {
        return TBlockItem(item);
    }
};

template<bool Nullable>
class TResourceBlockReader : public TFixedSizeBlockReaderBase<TUnboxedValuePod, Nullable, TResourceBlockReader<Nullable>> {
public:
    TBlockItem MakeBlockItem(const TUnboxedValuePod& pod) const {
        TBlockItem item;
        std::memcpy(item.GetRawPtr(), pod.GetRawPtr(), sizeof(TBlockItem));
        return item;
    }
};

template<typename TStringType, bool Nullable, NKikimr::NUdf::EDataSlot TOriginal = NKikimr::NUdf::EDataSlot::String>
class TStringBlockReader final : public IBlockReader {
public:
    using TOffset = typename TStringType::offset_type;

    TBlockItem GetItem(const arrow::ArrayData& data, size_t index) final {
        Y_DEBUG_ABORT_UNLESS(data.buffers.size() == 3);
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return {};
            }
        }

        const TOffset* offsets = data.GetValues<TOffset>(1);
        const char* strData = data.GetValues<char>(2, 0);

        std::string_view str(strData + offsets[index], offsets[index + 1] - offsets[index]);
        return TBlockItem(str);
    }

    TBlockItem GetScalarItem(const arrow::Scalar& scalar) final {
        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return {};
            }
        }

        auto buffer = arrow::internal::checked_cast<const arrow::BaseBinaryScalar&>(scalar).value;
        std::string_view str(reinterpret_cast<const char*>(buffer->data()), buffer->size());
        return TBlockItem(str);
    }

    ui64 GetDataWeight(const arrow::ArrayData& data) const final {
        ui64 size = 0;
        if constexpr (Nullable) {
            size += data.length;
        }
        size += data.buffers[2] ? data.buffers[2]->size() : 0;
        return size;
    }

    ui64 GetDataWeight(TBlockItem item) const final {
        if constexpr (Nullable) {
            return 1 + (item ? item.AsStringRef().Size() : 0);
        }
        return item.AsStringRef().Size();
    }

    ui64 GetDefaultValueWeight() const final {
        if constexpr (Nullable) {
            return 1;
        }
        return 0;
    }

    void SaveItem(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const final {
        Y_DEBUG_ABORT_UNLESS(data.buffers.size() == 3);
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        const TOffset* offsets = data.GetValues<TOffset>(1);
        const char* strData = data.GetValues<char>(2, 0);

        std::string_view str(strData + offsets[index], offsets[index + 1] - offsets[index]);
        out.PushString(str);
    }

    void SaveScalarItem(const arrow::Scalar& scalar, TOutputBuffer& out) const final {
        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        auto buffer = arrow::internal::checked_cast<const arrow::BaseBinaryScalar&>(scalar).value;
        std::string_view str(reinterpret_cast<const char*>(buffer->data()), buffer->size());
        out.PushString(str);
    }
};

template<bool Nullable, typename TDerived>
class TTupleBlockReaderBase : public IBlockReader {
public:
    TBlockItem GetItem(const arrow::ArrayData& data, size_t index) final {
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return {};
            }
        }
        return static_cast<TDerived*>(this)->GetChildrenItems(data, index);
    }

    TBlockItem GetScalarItem(const arrow::Scalar& scalar) final {
        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return {};
            }
        }

        const auto& structScalar = arrow::internal::checked_cast<const arrow::StructScalar&>(scalar);
        return static_cast<TDerived*>(this)->GetChildrenScalarItems(structScalar);
    }

    ui64 GetDataWeight(const arrow::ArrayData& data) const final {
        ui64 size = 0;
        if constexpr (Nullable) {
            size += data.length;
        }

        size += static_cast<const TDerived*>(this)->GetChildrenDataWeight(data);
        return size;
    }

    ui64 GetDataWeight(TBlockItem item) const final {
        return static_cast<const TDerived*>(this)->GetDataWeightImpl(item);
    }

    ui64 GetDefaultValueWeight() const final {
        ui64 size = 0;
        if constexpr (Nullable) {
            size = 1;
        }
        size += static_cast<const TDerived*>(this)->GetChildrenDefaultDataWeight();
        return size;
    }

    void SaveItem(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const final {
        if constexpr (Nullable) {
            if (IsNull(data, index)) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        static_cast<const TDerived*>(this)->SaveChildrenItems(data, index, out);
    }

    void SaveScalarItem(const arrow::Scalar& scalar, TOutputBuffer& out) const final {
        if constexpr (Nullable) {
            if (!scalar.is_valid) {
                return out.PushChar(0);
            }
            out.PushChar(1);
        }

        const auto& structScalar = arrow::internal::checked_cast<const arrow::StructScalar&>(scalar);

        static_cast<const TDerived*>(this)->SaveChildrenScalarItems(structScalar, out);
    }
};

template<bool Nullable>
class TTupleBlockReader final : public TTupleBlockReaderBase<Nullable, TTupleBlockReader<Nullable>> {
public:
    TTupleBlockReader(TVector<std::unique_ptr<IBlockReader>>&& children)
        : Children(std::move(children))
        , Items(Children.size())
    {}

    TBlockItem GetChildrenItems(const arrow::ArrayData& data, size_t index) {
        for (ui32 i = 0; i < Children.size(); ++i) {
            Items[i] = Children[i]->GetItem(*data.child_data[i], index);
        }

        return TBlockItem(Items.data());
    }

    TBlockItem GetChildrenScalarItems(const arrow::StructScalar& structScalar) {
        for (ui32 i = 0; i < Children.size(); ++i) {
            Items[i] = Children[i]->GetScalarItem(*structScalar.value[i]);
        }

        return TBlockItem(Items.data());
    }
    
    size_t GetDataWeightImpl(const TBlockItem& item) const {
        const TBlockItem* items = nullptr;
        ui64 size = 0;
        if constexpr (Nullable) {
            if (!item) {
                return this->GetDefaultValueWeight();
            }
            size = 1;
            items = item.GetOptionalValue().GetElements();
        } else {
            items = item.GetElements();
        }

        for (ui32 i = 0; i < Children.size(); ++i) {
            size += Children[i]->GetDataWeight(items[i]);
        }

        return size;
    }

    size_t GetChildrenDataWeight(const arrow::ArrayData& data) const {
        size_t size = 0;
        for (ui32 i = 0; i < Children.size(); ++i) {
            size += Children[i]->GetDataWeight(*data.child_data[i]);
        }

        return size;
    }

    size_t GetChildrenDefaultDataWeight() const {
        size_t size = 0;
        for (ui32 i = 0; i < Children.size(); ++i) {
            size += Children[i]->GetDefaultValueWeight();
        }
        return size;
    }

    void SaveChildrenItems(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const {
        for (ui32 i = 0; i < Children.size(); ++i) {
            Children[i]->SaveItem(*data.child_data[i], index, out);
        }
    }
    
    void SaveChildrenScalarItems(const arrow::StructScalar& structScalar, TOutputBuffer& out) const {
        for (ui32 i = 0; i < Children.size(); ++i) {
            Children[i]->SaveScalarItem(*structScalar.value[i], out);
        }
    }

private:
    const TVector<std::unique_ptr<IBlockReader>> Children;
    TVector<TBlockItem> Items;
};

template<typename TTzDate, bool Nullable>
class TTzDateBlockReader final : public TTupleBlockReaderBase<Nullable, TTzDateBlockReader<TTzDate, Nullable>> {
public:
    TBlockItem GetChildrenItems(const arrow::ArrayData& data, size_t index) {
        Y_DEBUG_ABORT_UNLESS(data.child_data.size() == 2);

        TBlockItem item {DateReader_.GetItem(*data.child_data[0], index)};
        item.SetTimezoneId(TimezoneReader_.GetItem(*data.child_data[1], index).Get<ui16>());
        return item;
    }

    TBlockItem GetChildrenScalarItems(const arrow::StructScalar& structScalar) {
        Y_DEBUG_ABORT_UNLESS(structScalar.value.size() == 2);

        TBlockItem item {DateReader_.GetScalarItem(*structScalar.value[0])};
        item.SetTimezoneId(TimezoneReader_.GetScalarItem(*structScalar.value[1]).Get<ui16>());
        return item;
    }

    size_t GetChildrenDataWeight(const arrow::ArrayData& data) const {
        Y_DEBUG_ABORT_UNLESS(data.child_data.size() == 2);

        size_t size = 0;
        size += DateReader_.GetDataWeight(*data.child_data[0]);
        size += TimezoneReader_.GetDataWeight(*data.child_data[1]);
        return size;
    }

    size_t GetDataWeightImpl(const TBlockItem& item) const {
        Y_UNUSED(item);
        return GetChildrenDefaultDataWeight();
    }
    
    size_t GetChildrenDefaultDataWeight() const {
        ui64 size = 0;
        if constexpr (Nullable) {
            size = 1;
        }

        size += DateReader_.GetDefaultValueWeight();
        size += TimezoneReader_.GetDefaultValueWeight();
        return size;
    }

    void SaveChildrenItems(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const {
        DateReader_.SaveItem(*data.child_data[0], index, out);
        TimezoneReader_.SaveItem(*data.child_data[1], index, out);
    }
    
    void SaveChildrenScalarItems(const arrow::StructScalar& structScalar, TOutputBuffer& out) const {
        DateReader_.SaveScalarItem(*structScalar.value[0], out);
        TimezoneReader_.SaveScalarItem(*structScalar.value[1], out);
    }

private:
    TFixedSizeBlockReader<typename TDataType<TTzDate>::TLayout, /* Nullable */false> DateReader_;
    TFixedSizeBlockReader<ui16, /* Nullable */false> TimezoneReader_;
};

class TExternalOptionalBlockReader final : public IBlockReader {
public:
    TExternalOptionalBlockReader(std::unique_ptr<IBlockReader>&& inner)
        : Inner(std::move(inner))
    {}

    TBlockItem GetItem(const arrow::ArrayData& data, size_t index) final {
        if (IsNull(data, index)) {
            return {};
        }

        return Inner->GetItem(*data.child_data.front(), index).MakeOptional();
    }

    TBlockItem GetScalarItem(const arrow::Scalar& scalar) final {
        if (!scalar.is_valid) {
            return {};
        }

        const auto& structScalar = arrow::internal::checked_cast<const arrow::StructScalar&>(scalar);
        return Inner->GetScalarItem(*structScalar.value.front()).MakeOptional();
    }

    ui64 GetDataWeight(const arrow::ArrayData& data) const final {
        return data.length + Inner->GetDataWeight(*data.child_data.front());
    }

    ui64 GetDataWeight(TBlockItem item) const final {
        if (!item) {
            return GetDefaultValueWeight();
        }
        return 1 + Inner->GetDataWeight(item.GetOptionalValue());
    }

    ui64 GetDefaultValueWeight() const final {
        return 1 + Inner->GetDefaultValueWeight();
    }

    void SaveItem(const arrow::ArrayData& data, size_t index, TOutputBuffer& out) const final {
        if (IsNull(data, index)) {
            return out.PushChar(0);
        }
        out.PushChar(1);

        Inner->SaveItem(*data.child_data.front(), index, out);
    }

    void SaveScalarItem(const arrow::Scalar& scalar, TOutputBuffer& out) const final {
        if (!scalar.is_valid) {
            return out.PushChar(0);
        }
        out.PushChar(1);

        const auto& structScalar = arrow::internal::checked_cast<const arrow::StructScalar&>(scalar);
        Inner->SaveScalarItem(*structScalar.value.front(), out);
    }

private:
    const std::unique_ptr<IBlockReader> Inner;
};

struct TReaderTraits {
    using TResult = IBlockReader;
    template <bool Nullable>
    using TTuple = TTupleBlockReader<Nullable>;
    template <typename T, bool Nullable>
    using TFixedSize = TFixedSizeBlockReader<T, Nullable>;
    template <typename TStringType, bool Nullable, NKikimr::NUdf::EDataSlot TOriginal>
    using TStrings = TStringBlockReader<TStringType, Nullable, TOriginal>;
    using TExtOptional = TExternalOptionalBlockReader;
    template<bool Nullable>
    using TResource = TResourceBlockReader<Nullable>;
    template<typename TTzDate, bool Nullable>
    using TTzDateReader = TTzDateBlockReader<TTzDate, Nullable>;

    static std::unique_ptr<TResult> MakePg(const TPgTypeDescription& desc, const IPgBuilder* pgBuilder) {
        Y_UNUSED(pgBuilder);
        if (desc.PassByValue) {
            return std::make_unique<TFixedSize<ui64, true>>();
        } else {
            return std::make_unique<TStrings<arrow::BinaryType, true, NKikimr::NUdf::EDataSlot::String>>();
        }
    }

    static std::unique_ptr<TResult> MakeResource(bool isOptional) {
        if (isOptional) {
            return std::make_unique<TResource<true>>();
        } else {
            return std::make_unique<TResource<false>>();
        }
    }

    template<typename TTzDate>
    static std::unique_ptr<TResult> MakeTzDate(bool isOptional) {
        if (isOptional) {
            return std::make_unique<TTzDateReader<TTzDate, true>>();
        } else {
            return std::make_unique<TTzDateReader<TTzDate, false>>();
        }
    }
};

template <typename TTraits>
std::unique_ptr<typename TTraits::TResult> MakeTupleBlockReaderImpl(bool isOptional, TVector<std::unique_ptr<typename TTraits::TResult>>&& children) {
    if (isOptional) {
        return std::make_unique<typename TTraits::template TTuple<true>>(std::move(children));
    } else {
        return std::make_unique<typename TTraits::template TTuple<false>>(std::move(children));
    }
}

template <typename TTraits, typename T>
std::unique_ptr<typename TTraits::TResult> MakeFixedSizeBlockReaderImpl(bool isOptional) {
    if (isOptional) {
        return std::make_unique<typename TTraits::template TFixedSize<T, true>>();
    } else {
        return std::make_unique<typename TTraits::template TFixedSize<T, false>>();
    }
}


template <typename TTraits, typename T, NKikimr::NUdf::EDataSlot TOriginal>
std::unique_ptr<typename TTraits::TResult> MakeStringBlockReaderImpl(bool isOptional) {
    if (isOptional) {
        return std::make_unique<typename TTraits::template TStrings<T, true, TOriginal>>();
    } else {
        return std::make_unique<typename TTraits::template TStrings<T, false, TOriginal>>();
    }
}

template<typename TTraits>
concept CanInstantiateBlockReaderForDecimal = requires {
    typename TTraits::template TFixedSize<NYql::NDecimal::TInt128, true>;
};

template <typename TTraits>
std::unique_ptr<typename TTraits::TResult> MakeBlockReaderImpl(const ITypeInfoHelper& typeInfoHelper, const TType* type, const IPgBuilder* pgBuilder) {
    const TType* unpacked = type;
    TOptionalTypeInspector typeOpt(typeInfoHelper, type);
    bool isOptional = false;
    if (typeOpt) {
        unpacked = typeOpt.GetItemType();
        isOptional = true;
    }

    TOptionalTypeInspector unpackedOpt(typeInfoHelper, unpacked);
    TPgTypeInspector unpackedPg(typeInfoHelper, unpacked);
    if (unpackedOpt || typeOpt && unpackedPg) {
        // at least 2 levels of optionals
        ui32 nestLevel = 0;
        auto currentType = type;
        auto previousType = type;
        for (;;) {
            ++nestLevel;
            previousType = currentType;
            TOptionalTypeInspector currentOpt(typeInfoHelper, currentType);
            currentType = currentOpt.GetItemType();
            TOptionalTypeInspector nexOpt(typeInfoHelper, currentType);
            if (!nexOpt) {
                break;
            }
        }

        if (TPgTypeInspector(typeInfoHelper, currentType)) {
            previousType = currentType;
            ++nestLevel;
        }

        auto reader = MakeBlockReaderImpl<TTraits>(typeInfoHelper, previousType, pgBuilder);
        for (ui32 i = 1; i < nestLevel; ++i) {
            reader = std::make_unique<typename TTraits::TExtOptional>(std::move(reader));
        }

        return reader;
    }
    else {
        type = unpacked;
    }

    TStructTypeInspector typeStruct(typeInfoHelper, type);
    if (typeStruct) {
        TVector<std::unique_ptr<typename TTraits::TResult>> members;
        for (ui32 i = 0; i < typeStruct.GetMembersCount(); i++) {
            members.emplace_back(MakeBlockReaderImpl<TTraits>(typeInfoHelper, typeStruct.GetMemberType(i), pgBuilder));
        }
        // XXX: Use Tuple block reader for Struct.
        return MakeTupleBlockReaderImpl<TTraits>(isOptional, std::move(members));
    }

    TTupleTypeInspector typeTuple(typeInfoHelper, type);
    if (typeTuple) {
        TVector<std::unique_ptr<typename TTraits::TResult>> children;
        for (ui32 i = 0; i < typeTuple.GetElementsCount(); ++i) {
            children.emplace_back(MakeBlockReaderImpl<TTraits>(typeInfoHelper, typeTuple.GetElementType(i), pgBuilder));
        }

        return MakeTupleBlockReaderImpl<TTraits>(isOptional, std::move(children));
    }

    TDataTypeInspector typeData(typeInfoHelper, type);
    if (typeData) {
        auto typeId = typeData.GetTypeId();
        switch (GetDataSlot(typeId)) {
        case NUdf::EDataSlot::Int8:
            return MakeFixedSizeBlockReaderImpl<TTraits, i8>(isOptional);
        case NUdf::EDataSlot::Bool:
        case NUdf::EDataSlot::Uint8:
            return MakeFixedSizeBlockReaderImpl<TTraits, ui8>(isOptional);
        case NUdf::EDataSlot::Int16:
            return MakeFixedSizeBlockReaderImpl<TTraits, i16>(isOptional);
        case NUdf::EDataSlot::Uint16:
        case NUdf::EDataSlot::Date:
            return MakeFixedSizeBlockReaderImpl<TTraits, ui16>(isOptional);
        case NUdf::EDataSlot::Int32:
        case NUdf::EDataSlot::Date32:
            return MakeFixedSizeBlockReaderImpl<TTraits, i32>(isOptional);
        case NUdf::EDataSlot::Uint32:
        case NUdf::EDataSlot::Datetime:
            return MakeFixedSizeBlockReaderImpl<TTraits, ui32>(isOptional);
        case NUdf::EDataSlot::Int64:
        case NUdf::EDataSlot::Interval:
        case NUdf::EDataSlot::Interval64:
        case NUdf::EDataSlot::Datetime64:
        case NUdf::EDataSlot::Timestamp64:
            return MakeFixedSizeBlockReaderImpl<TTraits, i64>(isOptional);
        case NUdf::EDataSlot::Uint64:
        case NUdf::EDataSlot::Timestamp:
            return MakeFixedSizeBlockReaderImpl<TTraits, ui64>(isOptional);
        case NUdf::EDataSlot::Float:
            return MakeFixedSizeBlockReaderImpl<TTraits, float>(isOptional);
        case NUdf::EDataSlot::Double:
            return MakeFixedSizeBlockReaderImpl<TTraits, double>(isOptional);
        case NUdf::EDataSlot::String:
            return MakeStringBlockReaderImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::String>(isOptional);
        case NUdf::EDataSlot::Yson:
            return MakeStringBlockReaderImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::Yson>(isOptional);
        case NUdf::EDataSlot::JsonDocument:
            return MakeStringBlockReaderImpl<TTraits, arrow::BinaryType, NUdf::EDataSlot::JsonDocument>(isOptional);
        case NUdf::EDataSlot::Utf8:
            return MakeStringBlockReaderImpl<TTraits, arrow::StringType, NUdf::EDataSlot::Utf8>(isOptional);
        case NUdf::EDataSlot::Json:
            return MakeStringBlockReaderImpl<TTraits, arrow::StringType, NUdf::EDataSlot::Json>(isOptional);
        case NUdf::EDataSlot::TzDate:
            return TTraits::template MakeTzDate<TTzDate>(isOptional);
        case NUdf::EDataSlot::TzDatetime:
            return TTraits::template MakeTzDate<TTzDatetime>(isOptional);
        case NUdf::EDataSlot::TzTimestamp:
            return TTraits::template MakeTzDate<TTzTimestamp>(isOptional);
        case NUdf::EDataSlot::TzDate32:
            return TTraits::template MakeTzDate<TTzDate32>(isOptional);
        case NUdf::EDataSlot::TzDatetime64:
            return TTraits::template MakeTzDate<TTzDatetime64>(isOptional);
        case NUdf::EDataSlot::TzTimestamp64:
            return TTraits::template MakeTzDate<TTzTimestamp64>(isOptional);
        case NUdf::EDataSlot::Decimal: {
            if constexpr (CanInstantiateBlockReaderForDecimal<TTraits>) {
                return MakeFixedSizeBlockReaderImpl<TTraits, NYql::NDecimal::TInt128>(isOptional);
            } else {
                Y_ENSURE(false, "Unsupported data slot");
            }
        }
        case NUdf::EDataSlot::Uuid:
        case NUdf::EDataSlot::DyNumber:
            Y_ENSURE(false, "Unsupported data slot");
        }
    }

    TResourceTypeInspector resource(typeInfoHelper, type);
    if (resource) {
        return TTraits::MakeResource(isOptional);
    }

    TPgTypeInspector typePg(typeInfoHelper, type);
    if (typePg) {
        auto desc = typeInfoHelper.FindPgTypeDescription(typePg.GetTypeId());
        return TTraits::MakePg(*desc, pgBuilder);
    }

    Y_ENSURE(false, "Unsupported type");
}

inline std::unique_ptr<IBlockReader> MakeBlockReader(const ITypeInfoHelper& typeInfoHelper, const TType* type) {
    return MakeBlockReaderImpl<TReaderTraits>(typeInfoHelper, type, nullptr);
}

inline void UpdateBlockItemSerializeProps(const ITypeInfoHelper& typeInfoHelper, const TType* type, TBlockItemSerializeProps& props) {
    if (!props.MaxSize.Defined()) {
        return;
    }

    for (;;) {
        TOptionalTypeInspector typeOpt(typeInfoHelper, type);
        if (!typeOpt) {
            break;
        }
        props.MaxSize = *props.MaxSize + 1;
        props.IsFixed = false;
        type = typeOpt.GetItemType();
    }

    TStructTypeInspector typeStruct(typeInfoHelper, type);
    if (typeStruct) {
        for (ui32 i = 0; i < typeStruct.GetMembersCount(); ++i) {
            UpdateBlockItemSerializeProps(typeInfoHelper, typeStruct.GetMemberType(i), props);
        }
        return;
    }

    TTupleTypeInspector typeTuple(typeInfoHelper, type);
    if (typeTuple) {
        for (ui32 i = 0; i < typeTuple.GetElementsCount(); ++i) {
            UpdateBlockItemSerializeProps(typeInfoHelper, typeTuple.GetElementType(i), props);
        }
        return;
    }

    TDataTypeInspector typeData(typeInfoHelper, type);
    if (typeData) {
        auto typeId = typeData.GetTypeId();
        auto slot = GetDataSlot(typeId);
        auto& dataTypeInfo = GetDataTypeInfo(slot);
        if (dataTypeInfo.Features & DecimalType) {
            *props.MaxSize += 16;
        } else if (dataTypeInfo.Features & StringType) {
            props.MaxSize = {};
            props.IsFixed = false;
        } else if (dataTypeInfo.Features & TzDateType) {
            *props.MaxSize += dataTypeInfo.FixedSize + sizeof(TTimezoneId);
        }
        else {
            *props.MaxSize += dataTypeInfo.FixedSize;
        }
        return;
    }

    TPgTypeInspector typePg(typeInfoHelper, type);
    if (typePg) {
        auto desc = typeInfoHelper.FindPgTypeDescription(typePg.GetTypeId());
        if (desc->PassByValue) {
            *props.MaxSize += 1 + 8;
        } else {
            props.MaxSize = {};
            props.IsFixed = false;
        }

        return;
    }

    Y_ENSURE(false, "Unsupported type");
}

}
}