Repository navigation
Expand file tree
/
Copy pathField.cpp
More file actions
919 lines (827 loc) · 34 KB
/
Copy pathField.cpp
File metadata and controls
919 lines (827 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#include <Common/Exception.h>
#include <Common/FieldVisitorDump.h>
#include <Common/FieldVisitorToString.h>
#include <Common/FieldVisitorWriteBinary.h>
#include <Core/AccurateComparison.h>
#include <Core/DecimalComparison.h>
#include <Core/Field.h>
#include <Core/CompareHelper.h>
#include <IO/ReadBuffer.h>
#include <IO/WriteBuffer.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadBufferFromString.h>
#include <IO/readDecimalText.h>
using namespace std::literals;
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_TYPE_OF_FIELD;
extern const int BAD_GET;
extern const int CANNOT_RESTORE_FROM_FIELD_DUMP;
extern const int DECIMAL_OVERFLOW;
extern const int INCORRECT_DATA;
extern const int NOT_IMPLEMENTED;
extern const int LOGICAL_ERROR;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
bool AggregateFunctionStateData::operator < (const AggregateFunctionStateData &) const
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Operator < is not implemented for AggregateFunctionStateData.");
}
bool AggregateFunctionStateData::operator <= (const AggregateFunctionStateData &) const
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Operator <= is not implemented for AggregateFunctionStateData.");
}
bool AggregateFunctionStateData::operator > (const AggregateFunctionStateData &) const
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Operator > is not implemented for AggregateFunctionStateData.");
}
bool AggregateFunctionStateData::operator >= (const AggregateFunctionStateData &) const
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Operator >= is not implemented for AggregateFunctionStateData.");
}
bool AggregateFunctionStateData::operator == (const AggregateFunctionStateData & rhs) const
{
if (name != rhs.name)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Comparing aggregate functions with different types: {} and {}",
name, rhs.name);
return data == rhs.data;
}
template <is_decimal T>
T DecimalField<T>::getScaleMultiplier() const
{
return DecimalUtils::scaleMultiplier<T>(scale);
}
template <is_decimal T>
const DecimalField<T> & DecimalField<T>::operator += (const DecimalField<T> & r)
{
if (scale != r.getScale())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Add different decimal fields");
dec += r.getValue();
return *this;
}
template <is_decimal T>
const DecimalField<T> & DecimalField<T>::operator -= (const DecimalField<T> & r)
{
if (scale != r.getScale())
throw Exception(ErrorCodes::LOGICAL_ERROR, "Sub different decimal fields");
dec -= r.getValue();
return *this;
}
bool Field::operator< (const Field & rhs) const
{
if (which < rhs.which)
return true;
if (which > rhs.which)
return false;
switch (which)
{
case Types::Null: return get<Null>() < rhs.get<Null>();
case Types::Bool: [[fallthrough]];
case Types::UInt64: return get<UInt64>() < rhs.get<UInt64>();
case Types::UInt128: return get<UInt128>() < rhs.get<UInt128>();
case Types::UInt256: return get<UInt256>() < rhs.get<UInt256>();
case Types::Int64: return get<Int64>() < rhs.get<Int64>();
case Types::Int128: return get<Int128>() < rhs.get<Int128>();
case Types::Int256: return get<Int256>() < rhs.get<Int256>();
case Types::UUID: return get<UUID>() < rhs.get<UUID>();
case Types::IPv4: return get<IPv4>() < rhs.get<IPv4>();
case Types::IPv6: return get<IPv6>() < rhs.get<IPv6>();
case Types::Float64:
static constexpr int nan_direction_hint = 1; /// Put NaN at the end
return FloatCompareHelper<Float64>::less(get<Float64>(), rhs.get<Float64>(), nan_direction_hint);
case Types::String: return get<String>() < rhs.get<String>();
case Types::Array: return get<Array>() < rhs.get<Array>();
case Types::Tuple: return get<Tuple>() < rhs.get<Tuple>();
case Types::Map: return get<Map>() < rhs.get<Map>();
case Types::Object: return get<Object>() < rhs.get<Object>();
case Types::Decimal32: return get<DecimalField<Decimal32>>() < rhs.get<DecimalField<Decimal32>>();
case Types::Decimal64: return get<DecimalField<Decimal64>>() < rhs.get<DecimalField<Decimal64>>();
case Types::Decimal128: return get<DecimalField<Decimal128>>() < rhs.get<DecimalField<Decimal128>>();
case Types::Decimal256: return get<DecimalField<Decimal256>>() < rhs.get<DecimalField<Decimal256>>();
case Types::AggregateFunctionState: return get<AggregateFunctionStateData>() < rhs.get<AggregateFunctionStateData>();
case Types::CustomType: return get<CustomType>() < rhs.get<CustomType>();
}
throw Exception(ErrorCodes::BAD_TYPE_OF_FIELD, "Bad type of Field");
}
bool Field::operator<= (const Field & rhs) const
{
if (which < rhs.which)
return true;
if (which > rhs.which)
return false;
switch (which)
{
case Types::Null: return get<Null>() <= rhs.get<Null>();
case Types::Bool: [[fallthrough]];
case Types::UInt64: return get<UInt64>() <= rhs.get<UInt64>();
case Types::UInt128: return get<UInt128>() <= rhs.get<UInt128>();
case Types::UInt256: return get<UInt256>() <= rhs.get<UInt256>();
case Types::Int64: return get<Int64>() <= rhs.get<Int64>();
case Types::Int128: return get<Int128>() <= rhs.get<Int128>();
case Types::Int256: return get<Int256>() <= rhs.get<Int256>();
case Types::UUID: return get<UUID>().toUnderType() <= rhs.get<UUID>().toUnderType();
case Types::IPv4: return get<IPv4>() <= rhs.get<IPv4>();
case Types::IPv6: return get<IPv6>() <= rhs.get<IPv6>();
case Types::Float64:
{
static constexpr int nan_direction_hint = 1; /// Put NaN at the end
Float64 f1 = get<Float64>();
Float64 f2 = get<Float64>();
return FloatCompareHelper<Float64>::less(f1, f2, nan_direction_hint)
|| FloatCompareHelper<Float64>::equals(f1, f2, nan_direction_hint);
}
case Types::String: return get<String>() <= rhs.get<String>();
case Types::Array: return get<Array>() <= rhs.get<Array>();
case Types::Tuple: return get<Tuple>() <= rhs.get<Tuple>();
case Types::Map: return get<Map>() <= rhs.get<Map>();
case Types::Object: return get<Object>() <= rhs.get<Object>();
case Types::Decimal32: return get<DecimalField<Decimal32>>() <= rhs.get<DecimalField<Decimal32>>();
case Types::Decimal64: return get<DecimalField<Decimal64>>() <= rhs.get<DecimalField<Decimal64>>();
case Types::Decimal128: return get<DecimalField<Decimal128>>() <= rhs.get<DecimalField<Decimal128>>();
case Types::Decimal256: return get<DecimalField<Decimal256>>() <= rhs.get<DecimalField<Decimal256>>();
case Types::AggregateFunctionState: return get<AggregateFunctionStateData>() <= rhs.get<AggregateFunctionStateData>();
case Types::CustomType: return get<CustomType>() <= rhs.get<CustomType>();
}
throw Exception(ErrorCodes::BAD_TYPE_OF_FIELD, "Bad type of Field");
}
bool Field::operator== (const Field & rhs) const
{
if (which != rhs.which)
return false;
switch (which)
{
case Types::Null: return get<Null>() == rhs.get<Null>();
case Types::Bool: [[fallthrough]];
case Types::UInt64: return get<UInt64>() == rhs.get<UInt64>();
case Types::Int64: return get<Int64>() == rhs.get<Int64>();
case Types::Float64:
static constexpr int nan_direction_hint = 1; /// Put NaN at the end
return FloatCompareHelper<Float64>::equals(get<Float64>(), rhs.get<Float64>(), nan_direction_hint);
case Types::UUID: return get<UUID>() == rhs.get<UUID>();
case Types::IPv4: return get<IPv4>() == rhs.get<IPv4>();
case Types::IPv6: return get<IPv6>() == rhs.get<IPv6>();
case Types::String: return get<String>() == rhs.get<String>();
case Types::Array: return get<Array>() == rhs.get<Array>();
case Types::Tuple: return get<Tuple>() == rhs.get<Tuple>();
case Types::Map: return get<Map>() == rhs.get<Map>();
case Types::Object: return get<Object>() == rhs.get<Object>();
case Types::UInt128: return get<UInt128>() == rhs.get<UInt128>();
case Types::UInt256: return get<UInt256>() == rhs.get<UInt256>();
case Types::Int128: return get<Int128>() == rhs.get<Int128>();
case Types::Int256: return get<Int256>() == rhs.get<Int256>();
case Types::Decimal32: return get<DecimalField<Decimal32>>() == rhs.get<DecimalField<Decimal32>>();
case Types::Decimal64: return get<DecimalField<Decimal64>>() == rhs.get<DecimalField<Decimal64>>();
case Types::Decimal128: return get<DecimalField<Decimal128>>() == rhs.get<DecimalField<Decimal128>>();
case Types::Decimal256: return get<DecimalField<Decimal256>>() == rhs.get<DecimalField<Decimal256>>();
case Types::AggregateFunctionState: return get<AggregateFunctionStateData>() == rhs.get<AggregateFunctionStateData>();
case Types::CustomType: return get<CustomType>() == rhs.get<CustomType>();
}
throw Exception(ErrorCodes::BAD_TYPE_OF_FIELD, "Bad type of Field");
}
static Field getBinaryValue(UInt8 type, ReadBuffer & buf)
{
switch (static_cast<Field::Types::Which>(type))
{
case Field::Types::Null:
{
return Field();
}
case Field::Types::UInt64:
{
UInt64 value = 0;
readVarUInt(value, buf);
return value;
}
case Field::Types::UInt128:
{
UInt128 value;
readBinary(value, buf);
return value;
}
case Field::Types::UInt256:
{
UInt256 value;
readBinary(value, buf);
return value;
}
case Field::Types::UUID:
{
UUID value;
readBinary(value, buf);
return value;
}
case Field::Types::IPv4:
{
IPv4 value;
readBinary(value, buf);
return value;
}
case Field::Types::IPv6:
{
IPv6 value;
readBinary(value.toUnderType(), buf);
return value;
}
case Field::Types::Int64:
{
Int64 value = 0;
readVarInt(value, buf);
return value;
}
case Field::Types::Int128:
{
Int128 value;
readBinary(value, buf);
return value;
}
case Field::Types::Int256:
{
Int256 value;
readBinary(value, buf);
return value;
}
case Field::Types::Float64:
{
Float64 value = 0;
readFloatBinary(value, buf);
return value;
}
case Field::Types::String:
{
std::string value;
readStringBinary(value, buf);
return value;
}
case Field::Types::Array:
{
Array value;
readBinaryArray(value, buf);
return value;
}
case Field::Types::Tuple:
{
Tuple value;
readBinary(value, buf);
return value;
}
case Field::Types::Map:
{
Map value;
readBinary(value, buf);
return value;
}
case Field::Types::Object:
{
Object value;
readBinary(value, buf);
return value;
}
case Field::Types::AggregateFunctionState:
{
AggregateFunctionStateData value;
readStringBinary(value.name, buf);
readStringBinary(value.data, buf);
return value;
}
case Field::Types::Bool:
{
UInt8 value = 0;
readBinary(value, buf);
return bool(value);
}
case Field::Types::Decimal32:
{
Decimal<Int32> value{};
readBinary(value, buf);
UInt32 scale = 0 ;
readBinary(scale, buf);
return DecimalField<Decimal32>(value, scale);
}
case Field::Types::Decimal64:
{
Decimal<Int64> value{};
readBinary(value, buf);
UInt32 scale = 0;
readBinary(scale, buf);
return DecimalField<Decimal64>(value, scale);
}
case Field::Types::Decimal128:
{
Decimal<Int128> value{};
readBinary(value, buf);
UInt32 scale = 0;
readBinary(scale, buf);
return DecimalField<Decimal128>(value, scale);
}
case Field::Types::Decimal256:
{
Decimal<Int256> value{};
readBinary(value, buf);
UInt32 scale = 0;
readBinary(scale, buf);
return DecimalField<Decimal256>(value, scale);
}
case Field::Types::CustomType:
return Field();
}
throw Exception(ErrorCodes::INCORRECT_DATA, "Unknown field type {}", std::to_string(type));
}
void readBinaryArray(Array & x, ReadBuffer & buf)
{
size_t size = 0;
readBinary(size, buf);
for (size_t index = 0; index < size; ++index)
x.push_back(readFieldBinary(buf));
}
void writeBinaryArray(const Array & x, WriteBuffer & buf)
{
size_t size = x.size();
writeBinary(size, buf);
for (const auto & elem : x)
writeFieldBinary(elem, buf);
}
void writeText(const Array & x, WriteBuffer & buf)
{
String res = applyVisitor(FieldVisitorToString(), Field(x));
buf.write(res.data(), res.size());
}
void readBinary(Tuple & x, ReadBuffer & buf)
{
size_t size = 0;
readBinary(size, buf);
for (size_t index = 0; index < size; ++index)
x.push_back(readFieldBinary(buf));
}
void writeBinary(const Tuple & x, WriteBuffer & buf)
{
const size_t size = x.size();
writeBinary(size, buf);
for (const auto & elem : x)
writeFieldBinary(elem, buf);
}
void writeText(const Tuple & x, WriteBuffer & buf)
{
writeFieldText(Field(x), buf);
}
void readBinary(Map & x, ReadBuffer & buf)
{
size_t size = 0;
readBinary(size, buf);
for (size_t index = 0; index < size; ++index)
x.push_back(readFieldBinary(buf));
}
void writeBinary(const Map & x, WriteBuffer & buf)
{
const size_t size = x.size();
writeBinary(size, buf);
for (const auto & elem : x)
writeFieldBinary(elem, buf);
}
void writeText(const Map & x, WriteBuffer & buf)
{
writeFieldText(Field(x), buf);
}
void readBinary(Object & x, ReadBuffer & buf)
{
size_t size = 0;
readBinary(size, buf);
for (size_t index = 0; index < size; ++index)
{
UInt8 type = 0;
String key;
readBinary(type, buf);
readBinary(key, buf);
x[key] = getBinaryValue(type, buf);
}
}
void writeBinary(const Object & x, WriteBuffer & buf)
{
const size_t size = x.size();
writeBinary(size, buf);
for (const auto & [key, value] : x)
{
const UInt8 type = value.getType();
writeBinary(type, buf);
writeBinary(key, buf);
Field::dispatch([&buf] (const auto & val) { FieldVisitorWriteBinary()(val, buf); }, value);
}
}
void writeText(const Object & x, WriteBuffer & buf)
{
writeFieldText(Field(x), buf);
}
void writeBinary(const CustomType & x, WriteBuffer & buf)
{
writeBinary(std::string_view(x.getTypeName()), buf);
writeBinary(x.toString(), buf);
}
void writeText(const CustomType & x, WriteBuffer & buf)
{
writeFieldText(Field(x), buf);
}
template <typename T>
void readQuoted(DecimalField<T> & x, ReadBuffer & buf)
{
assertChar('\'', buf);
T value{};
UInt32 scale = 0;
int32_t exponent = 0;
uint32_t max_digits = static_cast<uint32_t>(-1);
readDigits<true>(buf, value, max_digits, exponent, true);
if (exponent > 0)
{
scale = 0;
if (common::mulOverflow(value.value, DecimalUtils::scaleMultiplier<T>(exponent), value.value))
throw Exception(ErrorCodes::DECIMAL_OVERFLOW, "Decimal math overflow");
}
else
scale = -exponent;
assertChar('\'', buf);
x = DecimalField<T>{value, scale};
}
[[noreturn]] void readText(Array &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Array."); }
[[noreturn]] void readQuoted(Array &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Array."); }
[[noreturn]] void writeQuoted(const Array &, WriteBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot write Array quoted."); }
[[noreturn]] void readText(Tuple &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Tuple."); }
[[noreturn]] void readQuoted(Tuple &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Tuple."); }
[[noreturn]] void writeQuoted(const Tuple &, WriteBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot write Tuple quoted."); }
[[noreturn]] void readText(Map &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Map."); }
[[noreturn]] void readQuoted(Map &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Map."); }
[[noreturn]] void writeQuoted(const Map &, WriteBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot write Map quoted."); }
[[noreturn]] void readText(Object &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Object."); }
[[noreturn]] void readQuoted(Object &, ReadBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot read Object."); }
[[noreturn]] void writeQuoted(const Object &, WriteBuffer &) { throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Cannot write Object quoted."); }
[[noreturn]] void writeText(const AggregateFunctionStateData &, WriteBuffer &)
{
// This probably doesn't make any sense, but we have to have it for
// completeness, so that we can use toString(field_value) in field visitors.
throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot convert a Field of type AggregateFunctionStateData to human-readable text");
}
template void readQuoted<Decimal32>(DecimalField<Decimal32> & x, ReadBuffer & buf);
template void readQuoted<Decimal64>(DecimalField<Decimal64> & x, ReadBuffer & buf);
template void readQuoted<Decimal128>(DecimalField<Decimal128> & x, ReadBuffer & buf);
template void readQuoted<Decimal256>(DecimalField<Decimal256> & x, ReadBuffer & buf);
void writeFieldText(const Field & x, WriteBuffer & buf)
{
String res = Field::dispatch(FieldVisitorToString(), x);
buf.write(res.data(), res.size());
}
void writeFieldBinary(const Field & x, WriteBuffer & buf)
{
const UInt8 type = x.getType();
writeBinary(type, buf);
Field::dispatch([&buf] (const auto & value) { FieldVisitorWriteBinary()(value, buf); }, x);
}
Field readFieldBinary(ReadBuffer & buf)
{
UInt8 type = 0;
readBinary(type, buf);
return getBinaryValue(type, buf);
}
String Field::dump() const
{
return applyVisitor(FieldVisitorDump(), *this);
}
Field Field::restoreFromDump(std::string_view dump_)
{
auto show_error = [&dump_]
{
throw Exception(ErrorCodes::CANNOT_RESTORE_FROM_FIELD_DUMP, "Couldn't restore Field from dump: {}", String{dump_});
};
std::string_view dump = dump_;
trim(dump);
if (dump == "NULL")
return {};
std::string_view prefix = std::string_view{"Int64_"};
if (dump.starts_with(prefix))
{
Int64 value = parseFromString<Int64>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"UInt64_"};
if (dump.starts_with(prefix))
{
UInt64 value = parseFromString<UInt64>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"Int128_"};
if (dump.starts_with(prefix))
{
Int128 value = parseFromString<Int128>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"UInt128_"};
if (dump.starts_with(prefix))
{
UInt128 value = parseFromString<UInt128>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"Int256_"};
if (dump.starts_with(prefix))
{
Int256 value = parseFromString<Int256>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"UInt256_"};
if (dump.starts_with(prefix))
{
UInt256 value = parseFromString<UInt256>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"Float64_"};
if (dump.starts_with(prefix))
{
Float64 value = parseFromString<Float64>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"Decimal32_"};
if (dump_.starts_with(prefix))
{
DecimalField<Decimal32> decimal;
ReadBufferFromString buf{dump.substr(prefix.length())};
readQuoted(decimal, buf);
return decimal;
}
prefix = std::string_view{"Decimal64_"};
if (dump_.starts_with(prefix))
{
DecimalField<Decimal64> decimal;
ReadBufferFromString buf{dump.substr(prefix.length())};
readQuoted(decimal, buf);
return decimal;
}
prefix = std::string_view{"Decimal128_"};
if (dump_.starts_with(prefix))
{
DecimalField<Decimal128> decimal;
ReadBufferFromString buf{dump.substr(prefix.length())};
readQuoted(decimal, buf);
return decimal;
}
prefix = std::string_view{"Decimal256_"};
if (dump_.starts_with(prefix))
{
DecimalField<Decimal256> decimal;
ReadBufferFromString buf{dump.substr(prefix.length())};
readQuoted(decimal, buf);
return decimal;
}
if (dump.starts_with("\'"))
{
String str;
ReadBufferFromString buf{dump};
readQuoted(str, buf);
return str;
}
prefix = std::string_view{"Bool_"};
if (dump.starts_with(prefix))
{
bool value = parseFromString<bool>(dump.substr(prefix.length()));
return value;
}
prefix = std::string_view{"Array_["};
if (dump.starts_with(prefix))
{
std::string_view tail = dump.substr(prefix.length());
trimLeft(tail);
Array array;
while (tail != "]")
{
size_t separator = tail.find_first_of(",]");
if (separator == std::string_view::npos)
show_error();
bool comma = (tail[separator] == ',');
std::string_view element = tail.substr(0, separator);
tail.remove_prefix(separator);
if (comma)
tail.remove_prefix(1);
trimLeft(tail);
if (!comma && tail != "]")
show_error();
array.push_back(Field::restoreFromDump(element));
}
return array;
}
prefix = std::string_view{"Tuple_("};
if (dump.starts_with(prefix))
{
std::string_view tail = dump.substr(prefix.length());
trimLeft(tail);
Tuple tuple;
while (tail != ")")
{
size_t separator = tail.find_first_of(",)");
if (separator == std::string_view::npos)
show_error();
bool comma = (tail[separator] == ',');
std::string_view element = tail.substr(0, separator);
tail.remove_prefix(separator);
if (comma)
tail.remove_prefix(1);
trimLeft(tail);
if (!comma && tail != ")")
show_error();
tuple.push_back(Field::restoreFromDump(element));
}
return tuple;
}
prefix = std::string_view{"Map_("};
if (dump.starts_with(prefix))
{
std::string_view tail = dump.substr(prefix.length());
trimLeft(tail);
Map map;
while (tail != ")")
{
size_t separator = tail.find_first_of(",)");
if (separator == std::string_view::npos)
show_error();
bool comma = (tail[separator] == ',');
std::string_view element = tail.substr(0, separator);
tail.remove_prefix(separator);
if (comma)
tail.remove_prefix(1);
trimLeft(tail);
if (!comma && tail != ")")
show_error();
map.push_back(Field::restoreFromDump(element));
}
return map;
}
prefix = std::string_view{"AggregateFunctionState_("};
if (dump.starts_with(prefix))
{
std::string_view after_prefix = dump.substr(prefix.length());
size_t comma = after_prefix.find(',');
size_t end = after_prefix.find(')', comma + 1);
if ((comma == std::string_view::npos) || (end != after_prefix.length() - 1))
show_error();
std::string_view name_view = after_prefix.substr(0, comma);
std::string_view data_view = after_prefix.substr(comma + 1, end - comma - 1);
trim(name_view);
trim(data_view);
ReadBufferFromString name_buf{name_view};
ReadBufferFromString data_buf{data_view};
AggregateFunctionStateData res;
readQuotedString(res.name, name_buf);
readQuotedString(res.data, data_buf);
return res;
}
show_error();
UNREACHABLE();
}
template <typename T>
bool decimalEqual(T x, T y, UInt32 x_scale, UInt32 y_scale)
{
bool check_overflow = true;
using Comparator = DecimalComparison<T, T, EqualsOp>;
return Comparator::compare(x, y, x_scale, y_scale, check_overflow);
}
template <typename T>
bool decimalLess(T x, T y, UInt32 x_scale, UInt32 y_scale)
{
bool check_overflow = true;
using Comparator = DecimalComparison<T, T, LessOp>;
return Comparator::compare(x, y, x_scale, y_scale, check_overflow);
}
template <typename T>
bool decimalLessOrEqual(T x, T y, UInt32 x_scale, UInt32 y_scale)
{
bool check_overflow = true;
using Comparator = DecimalComparison<T, T, LessOrEqualsOp>;
return Comparator::compare(x, y, x_scale, y_scale, check_overflow);
}
template bool decimalEqual<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalEqual<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalEqual<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalEqual<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalEqual<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalEqual<Time64>(Time64 x, Time64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLess<Time64>(Time64 x, Time64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<Decimal32>(Decimal32 x, Decimal32 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<Decimal64>(Decimal64 x, Decimal64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<Decimal128>(Decimal128 x, Decimal128 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<Decimal256>(Decimal256 x, Decimal256 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<DateTime64>(DateTime64 x, DateTime64 y, UInt32 x_scale, UInt32 y_scale);
template bool decimalLessOrEqual<Time64>(Time64 x, Time64 y, UInt32 x_scale, UInt32 y_scale);
static void writeText(const Null & x, WriteBuffer & buf)
{
if (x.isNegativeInfinity())
writeText("-Inf", buf);
if (x.isPositiveInfinity())
writeText("+Inf", buf);
else
writeText("NULL", buf);
}
String fieldToString(const Field & x)
{
return Field::dispatch(
[] (const auto & value)
{
return toString(value);
},
x);
}
std::string_view fieldTypeToString(Field::Types::Which type)
{
switch (type)
{
case Field::Types::Which::Null: return "Null"sv;
case Field::Types::Which::Array: return "Array"sv;
case Field::Types::Which::Tuple: return "Tuple"sv;
case Field::Types::Which::Map: return "Map"sv;
case Field::Types::Which::Object: return "Object"sv;
case Field::Types::Which::AggregateFunctionState: return "AggregateFunctionState"sv;
case Field::Types::Which::Bool: return "Bool"sv;
case Field::Types::Which::String: return "String"sv;
case Field::Types::Which::Decimal32: return "Decimal32"sv;
case Field::Types::Which::Decimal64: return "Decimal64"sv;
case Field::Types::Which::Decimal128: return "Decimal128"sv;
case Field::Types::Which::Decimal256: return "Decimal256"sv;
case Field::Types::Which::Float64: return "Float64"sv;
case Field::Types::Which::Int64: return "Int64"sv;
case Field::Types::Which::Int128: return "Int128"sv;
case Field::Types::Which::Int256: return "Int256"sv;
case Field::Types::Which::UInt64: return "UInt64"sv;
case Field::Types::Which::UInt128: return "UInt128"sv;
case Field::Types::Which::UInt256: return "UInt256"sv;
case Field::Types::Which::UUID: return "UUID"sv;
case Field::Types::Which::IPv4: return "IPv4"sv;
case Field::Types::Which::IPv6: return "IPv6"sv;
case Field::Types::Which::CustomType: return "CustomType"sv;
}
}
/// Keep in mind, that "magic_enum" is very expensive for compiler, that's why we don't use it.
std::string_view Field::getTypeName() const
{
return fieldTypeToString(which);
}
template class DecimalField<Decimal32>;
template class DecimalField<Decimal64>;
template class DecimalField<Decimal128>;
template class DecimalField<Decimal256>;
template class DecimalField<DateTime64>;
template class DecimalField<Time64>;
template <typename T>
NearestFieldType<std::decay_t<T>> & Field::safeGet() &
{
const Types::Which target = TypeToEnum<NearestFieldType<std::decay_t<T>>>::value;
/// bool is stored as uint64, will be returned as UInt64 when requested as bool or UInt64, as Int64 when requested as Int64
/// also allow UInt64 <-> Int64 conversion
if (target != which &&
!(which == Field::Types::Bool && (target == Field::Types::UInt64 || target == Field::Types::Int64)) &&
!(isInt64OrUInt64FieldType(which) && isInt64OrUInt64FieldType(target)))
throw Exception(ErrorCodes::BAD_GET, "Bad get: has {}, requested {}", getTypeName(), fieldTypeToString(target));
return get<T>();
}
template NearestFieldType<std::decay_t<bool>> & Field::safeGet<bool>() &;
template NearestFieldType<std::decay_t<UInt8>> & Field::safeGet<UInt8>() &;
template NearestFieldType<std::decay_t<UInt16>> & Field::safeGet<UInt16>() &;
template NearestFieldType<std::decay_t<UInt32>> & Field::safeGet<UInt32>() &;
template NearestFieldType<std::decay_t<UInt64>> & Field::safeGet<UInt64>() &;
template NearestFieldType<std::decay_t<UInt128>> & Field::safeGet<UInt128>() &;
template NearestFieldType<std::decay_t<UInt256>> & Field::safeGet<UInt256>() &;
template NearestFieldType<std::decay_t<Int8>> & Field::safeGet<Int8>() &;
template NearestFieldType<std::decay_t<Int16>> & Field::safeGet<Int16>() &;
template NearestFieldType<std::decay_t<Int32>> & Field::safeGet<Int32>() &;
template NearestFieldType<std::decay_t<Int64>> & Field::safeGet<Int64>() &;
template NearestFieldType<std::decay_t<Int128>> & Field::safeGet<Int128>() &;
template NearestFieldType<std::decay_t<Int256>> & Field::safeGet<Int256>() &;
template NearestFieldType<std::decay_t<BFloat16>> & Field::safeGet<BFloat16>() &;
template NearestFieldType<std::decay_t<Float32>> & Field::safeGet<Float32>() &;
template NearestFieldType<std::decay_t<Float64>> & Field::safeGet<Float64>() &;
template NearestFieldType<std::decay_t<String>> & Field::safeGet<String>() &;
template NearestFieldType<std::decay_t<UUID>> & Field::safeGet<UUID>() &;
template NearestFieldType<std::decay_t<IPv4>> & Field::safeGet<IPv4>() &;
template NearestFieldType<std::decay_t<IPv6>> & Field::safeGet<IPv6>() &;
template NearestFieldType<std::decay_t<Decimal32>> & Field::safeGet<Decimal32>() &;
template NearestFieldType<std::decay_t<Decimal64>> & Field::safeGet<Decimal64>() &;
template NearestFieldType<std::decay_t<Decimal128>> & Field::safeGet<Decimal128>() &;
template NearestFieldType<std::decay_t<Decimal256>> & Field::safeGet<Decimal256>() &;
template NearestFieldType<std::decay_t<DateTime64>> & Field::safeGet<DateTime64>() &;
template NearestFieldType<std::decay_t<Time64>> & Field::safeGet<Time64>() &;
template NearestFieldType<std::decay_t<DecimalField<Decimal32>>> & Field::safeGet<DecimalField<Decimal32>>() &;
template NearestFieldType<std::decay_t<DecimalField<Decimal64>>> & Field::safeGet<DecimalField<Decimal64>>() &;
template NearestFieldType<std::decay_t<DecimalField<Decimal128>>> & Field::safeGet<DecimalField<Decimal128>>() &;
template NearestFieldType<std::decay_t<DecimalField<Decimal256>>> & Field::safeGet<DecimalField<Decimal256>>() &;
template NearestFieldType<std::decay_t<DecimalField<DateTime64>>> & Field::safeGet<DecimalField<DateTime64>>() &;
template NearestFieldType<std::decay_t<DecimalField<Time64>>> & Field::safeGet<DecimalField<Time64>>() &;
template NearestFieldType<std::decay_t<AggregateFunctionStateData>> & Field::safeGet<AggregateFunctionStateData>() &;
template NearestFieldType<std::decay_t<Array>> & Field::safeGet<Array>() &;
template NearestFieldType<std::decay_t<Map>> & Field::safeGet<Map>() &;
template NearestFieldType<std::decay_t<Object>> & Field::safeGet<Object>() &;
template NearestFieldType<std::decay_t<Tuple>> & Field::safeGet<Tuple>() &;
template NearestFieldType<std::decay_t<CustomType>> & Field::safeGet<CustomType>() &;
/// In Darwin unsigned long does not match any of the UInt* types
#ifdef OS_DARWIN
template NearestFieldType<std::decay_t<unsigned long>> & Field::safeGet<unsigned long>() &;
#endif
}