summaryrefslogtreecommitdiffstats
path: root/ydb/core/formats/clickhouse_block.cpp
blob: b0b20eb1e2f55cdaa275b5e48cc96324eb4d2e43 (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
// The code in this file is based on original ClickHouse source code
// which is licensed under Apache license v2.0
// See: https://github.com/ClickHouse/ClickHouse/

#include "factory.h"

#include <ydb/core/scheme/scheme_tablecell.h>
#include <ydb/library/yql/public/decimal/yql_decimal.h>
#include <util/stream/str.h>
#include <util/generic/ptr.h>
#include <util/generic/vector.h>
#include <util/generic/string.h>
#include <util/generic/hash.h>

namespace NKikHouse {
namespace NSerialization {

class IColumn;
using TMutableColumnPtr = TIntrusivePtr<IColumn>;
using TColumnPtr = TIntrusiveConstPtr<IColumn>;

class IDataType;
using TDataTypePtr = TIntrusiveConstPtr<IDataType>;

// Generic data type interface
class IDataType : public TThrRefBase {
public:
    virtual ~IDataType() = default;
    virtual const TString& getName() const = 0;
    virtual TMutableColumnPtr createColumn() const = 0;

    // Converts 'const this' into TIntrusiveConstPtr
    TDataTypePtr getPtr() const {
        return TDataTypePtr(const_cast<IDataType*>(this));
    }
};

// Generic column interface
class IColumn : public TThrRefBase {
public:
    virtual ~IColumn() = default;
    virtual const TDataTypePtr& getType() const = 0;
    virtual void insertData(const char* buf, size_t sz) = 0;
    virtual void insertDefault() = 0;
    virtual size_t rows() const = 0;
    virtual size_t byteSize() const = 0;
    // Does binary serialization
    virtual void serialize(IOutputStream& out) const = 0;
};

struct TTypeAndName {
    TDataTypePtr Type;
    TString Name;
};

using TTypesAndNames = TVector<TTypeAndName>;

// A block of data with several named columns that have the same number of rows
class TBlock {
    TTypesAndNames TypesAndNames;
    TVector<TMutableColumnPtr> Data;
public:
    TBlock() = default;

    TBlock(TTypesAndNames&& typesAndNames)
        : TypesAndNames(std::move(typesAndNames))
    {
        for (const auto& tn : TypesAndNames) {
            Data.push_back(tn.Type->createColumn());
        }
    }

    TBlock cloneEmpty() const {
        return TBlock(TTypesAndNames(TypesAndNames));
    }

    size_t columns() const {
        return TypesAndNames.size();
    }

    size_t rows() const {
        return Data.empty() ? 0 : Data.front()->rows();
    }

    const TDataTypePtr& getType(size_t i) const {
        return TypesAndNames.at(i).Type;
    }

    const TString& getName(size_t i) const {
        return TypesAndNames.at(i).Name;
    }

    TMutableColumnPtr getMutableColumn(size_t i) {
        return Data.at(i);
    }

    TColumnPtr getColumn(size_t i) const {
        return Data.at(i);
    }

    // Check that all columns have the same number of rows
    void checkNumberOfRows() const {
        if (Data.empty())
            return;

        size_t expectedRowCount = Data[0]->rows();
        for (size_t i = 1; i < Data.size(); ++i) {
            if (Data[i]->rows() != expectedRowCount) {
                throw yexception() << "Column '" << getName(i) << "' in the block has different number of rows: "
                    << Data[i]->rows() << " expected: " << expectedRowCount;
            }
        }
    }

    // Writes some metadata before the block contents
    void serializeBlockHeader(IOutputStream& out) const;
};


inline void writeVarUInt(ui64 x, IOutputStream& out) {
    for (size_t i = 0; i < 9; ++i) {
        ui8 byte = x & 0x7F;
        if (x > 0x7F)
            byte |= 0x80;

        out.Write(byte);

        x >>= 7;
        if (!x)
            return;
    }
}

inline void writeStringBinary(const TStringBuf& s, IOutputStream& out) {
    writeVarUInt(s.size(), out);
    out.Write(s);
}

template <typename T>
inline void writePODBinary(const T & x, IOutputStream& out) {
    out.Write(reinterpret_cast<const char *>(&x), sizeof(x));
}


void TBlock::serializeBlockHeader(IOutputStream& out) const {
#define APPLY_FOR_BLOCK_INFO_FIELDS(M) \
    M(bool,     is_overflows,     false,     1) \
    M(i32,      bucket_num,     -1,     2)

#define DECLARE_FIELD(TYPE, NAME, DEFAULT, FIELD_NUM) \
    TYPE NAME = DEFAULT;

    APPLY_FOR_BLOCK_INFO_FIELDS(DECLARE_FIELD)
#undef DECLARE_FIELD

    /// Set of pairs `FIELD_NUM`, value in binary form. Then 0.
#define WRITE_FIELD(TYPE, NAME, DEFAULT, FIELD_NUM) \
    writeVarUInt(FIELD_NUM, out); \
    writePODBinary(NAME, out);

    APPLY_FOR_BLOCK_INFO_FIELDS(WRITE_FIELD)
#undef WRITE_FIELD

    writeVarUInt(0, out);
}


// Writes blocks into an output stream
class TBlockWriter {
    ui32 ClientRevision = 0;
    IOutputStream& Out;
public:
    TBlockWriter(IOutputStream& out, ui32 clientRevision)
        : ClientRevision(clientRevision)
        , Out(out)
    {}

    void Write(const TBlock& block) {
        // Additional information about the block.
        if (ClientRevision > 0)
            block.serializeBlockHeader(Out);

        block.checkNumberOfRows();

        // Dimensions
        size_t columns = block.columns();
        size_t rows = block.rows();

        writeVarUInt(columns, Out);
        writeVarUInt(rows, Out);

        for (size_t i = 0; i < columns; ++i) {
            TString columnName = block.getName(i);
            writeStringBinary(columnName, Out);

            TDataTypePtr type = block.getType(i);
            TString typeName = type->getName();
            writeStringBinary(typeName, Out);

            // Data
            if (rows) {
                // Zero items of data is always represented as zero number of bytes.
                TColumnPtr column = block.getColumn(i);
                writeData(column, Out);
            }
        }
    }

private:
    void writeData(const TColumnPtr & column, IOutputStream& out) {
        column->serialize(out);
    }
};


class TDataTypeBase : public IDataType {
    TString Name;
protected:
    explicit TDataTypeBase(const TString& name)
        : Name(name)
    {}
public:
    const TString& getName() const override {
        return Name;
    }
};


class TColumnBase : public IColumn {
    TDataTypePtr Type;
protected:
    explicit TColumnBase(TDataTypePtr dataType)
        : Type(dataType)
    {}
public:
    const TDataTypePtr& getType() const override {
        return Type;
    }
};


class TNullableColumn : public TColumnBase {
    TVector<char> Nulls;
    TMutableColumnPtr Values;
public:
    TNullableColumn(TDataTypePtr dataType);

    void insertData(const char* buf, size_t sz) override {
        Nulls.push_back(0);
        Values->insertData(buf, sz);
    }

    void insertDefault() override {
        Nulls.push_back(1);
        Values->insertDefault();
    }

    size_t rows() const override {
        return Nulls.size();
    }

    size_t byteSize() const override {
        return Nulls.size() + Values->byteSize();
    }

    void serialize(IOutputStream& out) const override {
        out.Write(Nulls.data(), Nulls.size());
        Values->serialize(out);
    }
};


class TFixedSizeColumn : public TColumnBase {
    TVector<char> Data;
    const size_t ElementSize;
public:
    TFixedSizeColumn(TDataTypePtr dataType, size_t elementSize)
        : TColumnBase(dataType)
        , ElementSize(elementSize)
    {}

    void insertData(const char* buf, size_t sz) override {
        if (sz != ElementSize) {
            throw yexception() << "Data size " << sz << " doesn't match element size " << ElementSize;
        }
        Data.insert(Data.end(), buf, buf + sz);
    }

    void insertDefault() override {
        Data.resize(Data.size() + ElementSize);
    }

    size_t rows() const override {
        return Data.size() / ElementSize;
    }

    size_t byteSize() const override {
        return Data.size();
    }

    void serialize(IOutputStream& out) const override {
        out.Write(Data.data(), Data.size());
    }
};


class TStringColumn : public TColumnBase {
    using TOffset = size_t;
    TVector<char> Data;         // The buffer to store all strings data
    TVector<TOffset> Offsets;   // Offsets in the buffer (i-th offset points to the end of i-th string)
public:
    TStringColumn(TDataTypePtr dataType)
        : TColumnBase(dataType)
    {}

    void insertData(const char* buf, size_t sz) override {
        Data.insert(Data.end(), buf, buf + sz);
        Data.push_back(0); // Always append '\0' at the end
        Offsets.push_back(Data.size());
    }

    void insertDefault() override {
        Data.push_back(0);
        Offsets.push_back(Data.size());
    }

    size_t rows() const override {
        return Offsets.size();
    }

    size_t byteSize() const override {
        return Data.size() + Offsets.size()*sizeof(TOffset);
    }

    void serialize(IOutputStream& out) const override {
        if (rows() == 0)
            return;

        size_t size = Offsets[0] - 1;
        writeVarUInt(size, out);
        out.Write(Data.data(), size);

        for (size_t i = 1; i < Offsets.size(); ++i) {
            size_t size = Offsets[i] - 1 - Offsets[i-1];
            writeVarUInt(size, out);
            out.Write(Data.data() + Offsets[i-1], size);
        }
    }
};


class TNullableDataType : public TDataTypeBase {
    const TDataTypePtr Nested;
public:
    explicit TNullableDataType(const TDataTypePtr& nested)
        : TDataTypeBase("Nullable(" + nested->getName() + ")")
        , Nested(nested)
    {}

    TMutableColumnPtr createColumn() const override {
        return new TNullableColumn(this->getPtr());
    }

    TDataTypePtr getNested() const {
        return Nested;
    }
};


template<class TElement>
class TFixedSizeDataType : public TDataTypeBase {
public:
    explicit TFixedSizeDataType(const TString& name)
        : TDataTypeBase(name)
    {}

    TMutableColumnPtr createColumn() const override {
        return new TFixedSizeColumn(this->getPtr(), sizeof(TElement));
    }
};


class TStringDataType : public TDataTypeBase {
public:
    TStringDataType()
        : TDataTypeBase("String")
    {}

    TMutableColumnPtr createColumn() const override {
        return new TStringColumn(this->getPtr());
    }
};


TNullableColumn::TNullableColumn(TDataTypePtr dataType)
    : TColumnBase(dataType)
    , Nulls()
    , Values()
{
    const TNullableDataType* nullableType = dynamic_cast<const TNullableDataType*>(dataType.Get());
    Values = nullableType->getNested()->createColumn();
}


using namespace NKikimr;

// Returns specific data types by their names
class TDataTypeRegistry : public TThrRefBase {
    THashMap<TString, TDataTypePtr> Types;

private:
    void Register(TDataTypePtr dataType) {
        Types[dataType->getName()] = dataType;
    }

public:
    TDataTypeRegistry() {
        Register(new TStringDataType());

        Register(new TFixedSizeDataType<i8>("Int8"));
        Register(new TFixedSizeDataType<i16>("Int16"));
        Register(new TFixedSizeDataType<i32>("Int32"));
        Register(new TFixedSizeDataType<i64>("Int64"));

        Register(new TFixedSizeDataType<ui8>("UInt8"));
        Register(new TFixedSizeDataType<ui16>("UInt16"));
        Register(new TFixedSizeDataType<ui32>("UInt32"));
        Register(new TFixedSizeDataType<ui64>("UInt64"));

        Register(new TFixedSizeDataType<float>("Float32"));
        Register(new TFixedSizeDataType<double>("Float64"));

        Register(new TFixedSizeDataType<ui16>("Date"));
        Register(new TFixedSizeDataType<ui32>("DateTime"));

        Register(new TFixedSizeDataType<NYql::NDecimal::TInt128>("Decimal(22,9)"));
    }

    TDataTypePtr Get(TStringBuf name) const {
        return Types.at(name);
    }

    TDataTypePtr GetByYdbType(NScheme::TTypeInfo type) const {

    #define CONVERT(ydbType, chType) \
        case NScheme::NTypeIds::ydbType: \
            return Get(#chType);

        switch (type.GetTypeId()) {
        CONVERT(Bool,   UInt8);

        CONVERT(Int8,   Int8);
        CONVERT(Int16,  Int16);
        CONVERT(Int32,  Int32);
        CONVERT(Int64,  Int64);

        CONVERT(Uint8,  UInt8);
        CONVERT(Uint16, UInt16);
        CONVERT(Uint32, UInt32);
        CONVERT(Uint64, UInt64);

        CONVERT(Float,  Float32);
        CONVERT(Double, Float64);

        CONVERT(String, String);
        CONVERT(Utf8,   String);
        CONVERT(Json,   String);
        CONVERT(Yson,   String);

        CONVERT(Date,       Date);
        CONVERT(Datetime,   DateTime);
        CONVERT(Timestamp,  UInt64);
        CONVERT(Interval,   Int64);

        CONVERT(Decimal, Decimal(22,9));

        // Some internal types
        CONVERT(PairUi64Ui64,   String);
        CONVERT(ActorId,        String);
        CONVERT(StepOrderId,    String);

        case NScheme::NTypeIds::Pg:
            // TODO: support pg types
            throw yexception() << "Unsupported pg type";

        default:
            throw yexception() << "Unsupported type: " << type.GetTypeId();
        }
    #undef CONVERT
    }
};

using TDataTypeRegistryPtr = TIntrusiveConstPtr<TDataTypeRegistry>;

void AddNull(const TMutableColumnPtr& column) {
    // Default value is NULL for Nullable column
    column->insertDefault();
}

constexpr NYql::NDecimal::TInt128 Decimal128Min = NYql::NDecimal::GetBounds<38, true, true>().first;
constexpr NYql::NDecimal::TInt128 Decimal128Max = NYql::NDecimal::GetBounds<38, true, true>().second;

void AddDecimal(const TMutableColumnPtr& column, const TCell& cell) {
    struct THalves {
        ui64 lo;
        ui64 hi;
    };

    if (cell.Size() != sizeof(THalves)) {
        AddNull(column);
        return;
    }

    const THalves halves = cell.AsValue<THalves>();
    const NYql::NDecimal::TInt128 val = NYql::NDecimal::FromHalfs(halves.lo, halves.hi);

    // Return MAX Decimal128 instead of +inf and MIN Decimal128 instead of -inf
    if (val == NYql::NDecimal::Inf()) {
        auto infVal = NYql::NDecimal::MakePair(Decimal128Max);
        column->insertData((const char*)&infVal, sizeof(infVal));
        return;
    }

    if (val == -NYql::NDecimal::Inf()) {
        auto minusInfVal = NYql::NDecimal::MakePair(Decimal128Min);
        column->insertData((const char*)&minusInfVal, sizeof(minusInfVal));
        return;
    }

    if (NYql::NDecimal::IsNormal(val)) {
        column->insertData(cell.Data(), cell.Size());
        return;
    } else {
        // Convert all non-numbers to NULLs
        AddNull(column);
        return;
    }
}

size_t AddValue(const TMutableColumnPtr& column, const TCell& cell, NScheme::TTypeInfo type) {
    size_t prevBytes = column->byteSize();
    if (cell.IsNull()) {
        AddNull(column);
    } else {
        auto typeId = type.GetTypeId();
        if (typeId == NScheme::NTypeIds::Pg) {
            // TODO: support pg types
            Y_ABORT_UNLESS(false, "pg types are not supported");
        } else if (typeId == NScheme::NTypeIds::Decimal) {
            AddDecimal(column, cell);
        } else {
            column->insertData(cell.Data(), cell.Size());
        }
    }
    return column->byteSize() - prevBytes;
}

TTypesAndNames MakeColumns(const TDataTypeRegistryPtr& dataTypeRegistry, const std::vector<std::pair<TString, NScheme::TTypeInfo>>& columns) {
    TTypesAndNames res;
    for (auto& c : columns) {
        TDataTypePtr dataType = dataTypeRegistry->GetByYdbType(c.second);
        dataType = new TNullableDataType(dataType);
        res.push_back({dataType, c.first});
    }
    return res;
}

} // namespace NSerialization

using namespace NSerialization;

// Saves rows in ClickHouse native format so that they can be sent to CH
// and processed there without further conversions
class TBlockBuilder : public NKikimr::IBlockBuilder {
public:
    explicit TBlockBuilder(TDataTypeRegistryPtr dataTypeRegistry);
    ~TBlockBuilder();
    bool Start(const std::vector<std::pair<TString, NScheme::TTypeInfo>>& columns, ui64 maxRowsInBlock, ui64 maxBytesInBlock, TString& err) override;
    void AddRow(const NKikimr::TDbTupleRef& key, const NKikimr::TDbTupleRef& value) override;
    TString Finish() override;
    size_t Bytes() const override;

private:
    std::unique_ptr<IBlockBuilder> Clone() const override;

private:
    const TDataTypeRegistryPtr DataTypeRegistry;

    class TImpl;
    TAutoPtr<TImpl> Impl;
};


class TBlockBuilder::TImpl {
    constexpr static ui32 DBMS_MIN_REVISION_WITH_CURRENT_AGGREGATION_VARIANT_SELECTION_METHOD = 54408;
public:
    TImpl(const TDataTypeRegistryPtr& dataTypeRegistry, const std::vector<std::pair<TString, NScheme::TTypeInfo>>& columns, ui64 maxRowsInBlock, ui64 maxBytesInBlock)
        : MaxRowsInBlock(maxRowsInBlock)
        , MaxBytesInBlock(maxBytesInBlock)
        , BlockTemplate(MakeColumns(dataTypeRegistry, columns))
        , Out(Buffer)
        , BlockWriter(Out, DBMS_MIN_REVISION_WITH_CURRENT_AGGREGATION_VARIANT_SELECTION_METHOD)
    {
        StartNewBlock();
    }

    void AddRow(const TDbTupleRef& key, const TDbTupleRef& value) {
        Y_UNUSED(key);

        if (CurrentBlockRows >= MaxRowsInBlock || CurrentBlockBytes >= MaxBytesInBlock) {
            FinishCurrentBlock();
        }

        ++CurrentBlockRows;
        for (size_t ci = 0; ci < value.ColumnCount; ++ci) {
            CurrentBlockBytes += AddValue(CurrentBlock.getMutableColumn(ci), value.Columns[ci], value.Types[ci]);
        }
    }

    TString Finish() {
        FinishCurrentBlock();
        Out.Finish();
        return Buffer;
    }

    size_t Bytes() const {
        return CurrentBlockBytes + Buffer.size();
    }

private:
    void FinishCurrentBlock() {
        if (CurrentBlockRows > 0) {
            BlockWriter.Write(CurrentBlock);

            StartNewBlock();
        }
    }

    void StartNewBlock() {
        CurrentBlockRows = 0;
        CurrentBlockBytes = 0;
        CurrentBlock = BlockTemplate.cloneEmpty();
    }

private:
    TDataTypeRegistryPtr DateTypeRegistry;
    const ui64 MaxRowsInBlock;
    const ui64 MaxBytesInBlock;
    TBlock BlockTemplate;
    size_t CurrentBlockRows;
    size_t CurrentBlockBytes;
    TBlock CurrentBlock;
    TString Buffer;
    TStringOutput Out;
    TBlockWriter BlockWriter;
};


TBlockBuilder::TBlockBuilder(TDataTypeRegistryPtr dataTypeRegistry)
    : DataTypeRegistry(dataTypeRegistry)
{}

TBlockBuilder::~TBlockBuilder() {
}

bool TBlockBuilder::Start(const std::vector<std::pair<TString,  NScheme::TTypeInfo>>& columns, ui64 maxRowsInBlock, ui64 maxBytesInBlock, TString& err) {
    try {
        Impl.Reset(new TImpl(DataTypeRegistry, columns, maxRowsInBlock, maxBytesInBlock));
    } catch (std::exception& e) {
        err = e.what();
        return false;
    }
    return true;
}

void TBlockBuilder::AddRow(const TDbTupleRef& key, const TDbTupleRef& value) {
    if (Impl)
        Impl->AddRow(key, value);
}

TString TBlockBuilder::Finish() {
    if (!Impl)
        return TString();

    return Impl->Finish();
}

size_t TBlockBuilder::Bytes() const {
    if (!Impl)
        return 0;

    return Impl->Bytes();
}

std::unique_ptr<IBlockBuilder> TBlockBuilder::Clone() const {
    return std::make_unique<TBlockBuilder>(DataTypeRegistry);
}

////////////////////////////////////////////////////////////////////////////////

void RegisterFormat(NKikimr::TFormatFactory& factory) {
    TDataTypeRegistryPtr dataTypeRegistry(new TDataTypeRegistry);
    factory.RegisterBlockBuilder(std::make_unique<TBlockBuilder>(dataTypeRegistry), "clickhouse_native");
}

} // namespace NKikHouse