forked from substrait-io/substrait-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathType.h
More file actions
661 lines (503 loc) · 16.8 KB
/
Type.h
File metadata and controls
661 lines (503 loc) · 16.8 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
/* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include <iostream>
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>
namespace io::substrait {
enum class TypeKind : int8_t {
kBool = 1,
kI8 = 2,
kI16 = 3,
kI32 = 4,
kI64 = 5,
kFp32 = 6,
kFp64 = 7,
kString = 8,
kBinary = 9,
kTimestamp = 10,
kDate = 11,
kTime = 12,
kIntervalYear = 13,
kIntervalDay = 14,
kTimestampTz = 15,
kUuid = 16,
kFixedChar = 17,
kVarchar = 18,
kFixedBinary = 19,
kDecimal = 20,
kStruct = 21,
kList = 22,
kMap = 23,
KIND_NOT_SET = 0,
};
template <TypeKind KIND>
struct TypeTraits {};
template <>
struct TypeTraits<TypeKind::kBool> {
static constexpr const char* signature = "bool";
static constexpr const char* typeString = "boolean";
};
template <>
struct TypeTraits<TypeKind::kI8> {
static constexpr const char* signature = "i8";
static constexpr const char* typeString = "i8";
};
template <>
struct TypeTraits<TypeKind::kI16> {
static constexpr const char* signature = "i16";
static constexpr const char* typeString = "i16";
};
template <>
struct TypeTraits<TypeKind::kI32> {
static constexpr const char* signature = "i32";
static constexpr const char* typeString = "i32";
};
template <>
struct TypeTraits<TypeKind::kI64> {
static constexpr const char* signature = "i64";
static constexpr const char* typeString = "i64";
};
template <>
struct TypeTraits<TypeKind::kFp32> {
static constexpr const char* signature = "fp32";
static constexpr const char* typeString = "fp32";
};
template <>
struct TypeTraits<TypeKind::kFp64> {
static constexpr const char* signature = "fp64";
static constexpr const char* typeString = "fp64";
};
template <>
struct TypeTraits<TypeKind::kString> {
static constexpr const char* signature = "str";
static constexpr const char* typeString = "string";
};
template <>
struct TypeTraits<TypeKind::kBinary> {
static constexpr const char* signature = "vbin";
static constexpr const char* typeString = "binary";
};
template <>
struct TypeTraits<TypeKind::kTimestamp> {
static constexpr const char* signature = "ts";
static constexpr const char* typeString = "timestamp";
};
template <>
struct TypeTraits<TypeKind::kTimestampTz> {
static constexpr const char* signature = "tstz";
static constexpr const char* typeString = "timestamp_tz";
};
template <>
struct TypeTraits<TypeKind::kDate> {
static constexpr const char* signature = "date";
static constexpr const char* typeString = "date";
};
template <>
struct TypeTraits<TypeKind::kTime> {
static constexpr const char* signature = "time";
static constexpr const char* typeString = "time";
};
template <>
struct TypeTraits<TypeKind::kIntervalYear> {
static constexpr const char* signature = "iyear";
static constexpr const char* typeString = "interval_year";
};
template <>
struct TypeTraits<TypeKind::kIntervalDay> {
static constexpr const char* signature = "iday";
static constexpr const char* typeString = "interval_day";
};
template <>
struct TypeTraits<TypeKind::kUuid> {
static constexpr const char* signature = "uuid";
static constexpr const char* typeString = "uuid";
};
template <>
struct TypeTraits<TypeKind::kFixedChar> {
static constexpr const char* signature = "fchar";
static constexpr const char* typeString = "fixedchar";
};
template <>
struct TypeTraits<TypeKind::kVarchar> {
static constexpr const char* signature = "vchar";
static constexpr const char* typeString = "varchar";
};
template <>
struct TypeTraits<TypeKind::kFixedBinary> {
static constexpr const char* signature = "fbin";
static constexpr const char* typeString = "fixedbinary";
};
template <>
struct TypeTraits<TypeKind::kDecimal> {
static constexpr const char* signature = "dec";
static constexpr const char* typeString = "decimal";
};
template <>
struct TypeTraits<TypeKind::kStruct> {
static constexpr const char* signature = "struct";
static constexpr const char* typeString = "struct";
};
template <>
struct TypeTraits<TypeKind::kList> {
static constexpr const char* signature = "list";
static constexpr const char* typeString = "list";
};
template <>
struct TypeTraits<TypeKind::kMap> {
static constexpr const char* signature = "map";
static constexpr const char* typeString = "map";
};
class ParameterizedType {
public:
explicit ParameterizedType(bool nullable = false) : nullable_(nullable) {}
[[nodiscard]] virtual std::string signature() const = 0;
[[nodiscard]] virtual TypeKind kind() const = 0;
/// Deserialize substrait raw type string into Substrait extension type.
/// @param rawType - substrait extension raw string type
static std::shared_ptr<const ParameterizedType> decode(
const std::string& rawType){
return decode(rawType, true);
}
static std::shared_ptr<const ParameterizedType> decode(
const std::string& rawType,bool isParameterized);
[[nodiscard]] const bool& nullable() const {
return nullable_;
}
[[nodiscard]] bool nullMatch(
const std::shared_ptr<const ParameterizedType>& type) const {
return nullable() || nullable() == type->nullable();
}
/// Test type is a Wildcard type or not.
[[nodiscard]] virtual bool isWildcard() const {
return false;
}
[[nodiscard]] virtual bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const = 0;
private:
const bool nullable_;
};
using ParameterizedTypePtr = std::shared_ptr<const ParameterizedType>;
class Type : public ParameterizedType {
public:
explicit Type(bool nullable = false) : ParameterizedType(nullable) {}
};
using TypePtr = std::shared_ptr<const Type>;
/// Types used in function argument declarations.
template <TypeKind Kind>
class TypeBase : public Type {
public:
explicit TypeBase(bool nullable = false) : Type(nullable) {}
[[nodiscard]] std::string signature() const override {
return TypeTraits<Kind>::signature;
}
[[nodiscard]] TypeKind kind() const override {
return Kind;
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override {
return kind() == type->kind() && nullMatch(type);
}
};
template <TypeKind Kind>
class ScalarType : public TypeBase<Kind> {
public:
explicit ScalarType(bool nullable) : TypeBase<Kind>(nullable) {}
};
class Decimal : public TypeBase<TypeKind::kDecimal> {
public:
Decimal(int precision, int scale, bool nullable = false)
: TypeBase<TypeKind::kDecimal>(nullable),
precision_(precision),
scale_(scale) {}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] const int& precision() const {
return precision_;
}
[[nodiscard]] const int& scale() const {
return scale_;
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const int precision_;
const int scale_;
};
class FixedBinary : public TypeBase<TypeKind::kFixedBinary> {
public:
explicit FixedBinary(int length, bool nullable = false)
: TypeBase<TypeKind::kFixedBinary>(nullable), length_(length) {}
[[nodiscard]] const int& length() const {
return length_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const int length_;
};
class FixedChar : public TypeBase<TypeKind::kFixedChar> {
public:
explicit FixedChar(int length, bool nullable = false)
: TypeBase<TypeKind::kFixedChar>(nullable), length_(length){};
[[nodiscard]] const int& length() const {
return length_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const int length_;
};
class Varchar : public TypeBase<TypeKind::kVarchar> {
public:
explicit Varchar(int length, bool nullable = false)
: TypeBase<TypeKind::kVarchar>(nullable), length_(length){};
[[nodiscard]] const int& length() const {
return length_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const int length_;
};
class List : public TypeBase<TypeKind::kList> {
public:
explicit List(TypePtr elementType, bool nullable = false)
: TypeBase<TypeKind::kList>(nullable),
elementType_(std::move(elementType)){};
[[nodiscard]] const TypePtr& elementType() const {
return elementType_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const TypePtr elementType_;
};
class Struct : public TypeBase<TypeKind::kStruct> {
public:
explicit Struct(std::vector<TypePtr> types, bool nullable = false)
: TypeBase<TypeKind::kStruct>(nullable), children_(std::move(types)) {}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] const std::vector<TypePtr>& children() const {
return children_;
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const std::vector<TypePtr> children_;
};
class Map : public TypeBase<TypeKind::kMap> {
public:
Map(TypePtr keyType, TypePtr valueType, bool nullable = false)
: TypeBase<TypeKind::kMap>(nullable),
keyType_(std::move(keyType)),
valueType_(std::move(valueType)) {}
[[nodiscard]] const TypePtr& keyType() const {
return keyType_;
}
[[nodiscard]] const TypePtr& valueType() const {
return valueType_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const TypePtr keyType_;
const TypePtr valueType_;
};
class ParameterizedTypeBase : public ParameterizedType {
public:
explicit ParameterizedTypeBase(bool nullable = false)
: ParameterizedType(nullable) {}
};
/// A string literal type can present the 'any1'.
class StringLiteral : public ParameterizedTypeBase {
public:
explicit StringLiteral(std::string value)
: ParameterizedTypeBase(false), value_(std::move(value)) {}
[[nodiscard]] std::string signature() const override {
return value_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::KIND_NOT_SET;
}
[[nodiscard]] const std::string& value() const {
return value_;
}
[[nodiscard]] bool isWildcard() const override {
return value_.find("any") == 0 || value_ == "T";
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const std::string value_;
};
using StringLiteralPtr = std::shared_ptr<const StringLiteral>;
class ParameterizedDecimal : public ParameterizedTypeBase {
public:
ParameterizedDecimal(
StringLiteralPtr precision,
StringLiteralPtr scale,
bool nullable = false)
: ParameterizedTypeBase(nullable),
precision_(std::move(precision)),
scale_(std::move(scale)) {}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] const StringLiteralPtr& precision() const {
return precision_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kDecimal;
}
[[nodiscard]] const StringLiteralPtr& scale() const {
return scale_;
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
StringLiteralPtr precision_;
StringLiteralPtr scale_;
};
class ParameterizedFixedBinary : public ParameterizedTypeBase {
public:
explicit ParameterizedFixedBinary(
StringLiteralPtr length,
bool nullable = false)
: ParameterizedTypeBase(nullable), length_(std::move(length)) {}
[[nodiscard]] const StringLiteralPtr& length() const {
return length_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kFixedBinary;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const StringLiteralPtr length_;
};
class ParameterizedFixedChar : public ParameterizedTypeBase {
public:
explicit ParameterizedFixedChar(
StringLiteralPtr length,
bool nullable = false)
: ParameterizedTypeBase(nullable), length_(std::move(length)) {}
[[nodiscard]] const StringLiteralPtr& length() const {
return length_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kFixedChar;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const StringLiteralPtr length_;
};
class ParameterizedVarchar : public ParameterizedTypeBase {
public:
explicit ParameterizedVarchar(StringLiteralPtr length, bool nullable = false)
: ParameterizedTypeBase(nullable), length_(std::move(length)) {}
[[nodiscard]] const StringLiteralPtr& length() const {
return length_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kVarchar;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const StringLiteralPtr length_;
};
class ParameterizedList : public ParameterizedTypeBase {
public:
explicit ParameterizedList(
ParameterizedTypePtr elementType,
bool nullable = false)
: ParameterizedTypeBase(nullable), elementType_(std::move(elementType)){};
[[nodiscard]] const ParameterizedTypePtr& elementType() const {
return elementType_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kList;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const ParameterizedTypePtr elementType_;
};
class ParameterizedStruct : public ParameterizedTypeBase {
public:
explicit ParameterizedStruct(
std::vector<ParameterizedTypePtr> types,
bool nullable = false)
: ParameterizedTypeBase(nullable), children_(std::move(types)) {}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] const std::vector<ParameterizedTypePtr>& children() const {
return children_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kStruct;
}
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const std::vector<ParameterizedTypePtr> children_;
};
class ParameterizedMap : public ParameterizedTypeBase {
public:
ParameterizedMap(
ParameterizedTypePtr keyType,
ParameterizedTypePtr valueType,
bool nullable = false)
: ParameterizedTypeBase(nullable),
keyType_(std::move(keyType)),
valueType_(std::move(valueType)) {}
[[nodiscard]] const ParameterizedTypePtr& keyType() const {
return keyType_;
}
[[nodiscard]] TypeKind kind() const override {
return TypeKind::kMap;
}
[[nodiscard]] const ParameterizedTypePtr& valueType() const {
return valueType_;
}
[[nodiscard]] std::string signature() const override;
[[nodiscard]] bool isMatch(
const std::shared_ptr<const ParameterizedType>& type) const override;
private:
const ParameterizedTypePtr keyType_;
const ParameterizedTypePtr valueType_;
};
std::shared_ptr<const ScalarType<TypeKind::kBool>> BOOL();
std::shared_ptr<const ScalarType<TypeKind::kI8>> TINYINT();
std::shared_ptr<const ScalarType<TypeKind::kI16>> SMALLINT();
std::shared_ptr<const ScalarType<TypeKind::kI32>> INTEGER();
std::shared_ptr<const ScalarType<TypeKind::kI64>> BIGINT();
std::shared_ptr<const ScalarType<TypeKind::kFp32>> FLOAT();
std::shared_ptr<const ScalarType<TypeKind::kFp64>> DOUBLE();
std::shared_ptr<const ScalarType<TypeKind::kString>> STRING();
std::shared_ptr<const ScalarType<TypeKind::kBinary>> BINARY();
std::shared_ptr<const ScalarType<TypeKind::kTimestamp>> TIMESTAMP();
std::shared_ptr<const ScalarType<TypeKind::kTimestampTz>> TIMESTAMP_TZ();
std::shared_ptr<const ScalarType<TypeKind::kDate>> DATE();
std::shared_ptr<const ScalarType<TypeKind::kTime>> TIME();
std::shared_ptr<const ScalarType<TypeKind::kIntervalYear>> INTERVAL_YEAR();
std::shared_ptr<const ScalarType<TypeKind::kIntervalDay>> INTERVAL_DAY();
std::shared_ptr<const ScalarType<TypeKind::kUuid>> UUID();
std::shared_ptr<const Decimal> DECIMAL(int precision, int scale);
std::shared_ptr<const Varchar> VARCHAR(int len);
std::shared_ptr<const FixedChar> FIXED_CHAR(int len);
std::shared_ptr<const FixedBinary> FIXED_BINARY(int len);
std::shared_ptr<const List> LIST(const TypePtr& elementType);
std::shared_ptr<const Map> MAP(
const TypePtr& keyType,
const TypePtr& valueType);
std::shared_ptr<const Struct> STRUCT(const std::vector<TypePtr>& children);
} // namespace io::substrait