forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast_rules.cpp
More file actions
660 lines (619 loc) · 18.2 KB
/
cast_rules.cpp
File metadata and controls
660 lines (619 loc) · 18.2 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
#include "duckdb/function/cast_rules.hpp"
#include "duckdb/common/helper.hpp"
#include "duckdb/common/numeric_utils.hpp"
#include "duckdb/common/case_insensitive_map.hpp"
namespace duckdb {
//! The target type determines the preferred implicit casts
static int64_t TargetTypeCost(const LogicalType &type) {
switch (type.id()) {
case LogicalTypeId::BIGINT:
return 101;
case LogicalTypeId::INTEGER:
return 102;
case LogicalTypeId::HUGEINT:
return 103;
case LogicalTypeId::DOUBLE:
return 104;
case LogicalTypeId::DECIMAL:
return 105;
case LogicalTypeId::TIMESTAMP_NS:
return 119;
case LogicalTypeId::TIMESTAMP:
return 120;
case LogicalTypeId::TIMESTAMP_MS:
return 121;
case LogicalTypeId::TIMESTAMP_SEC:
return 122;
case LogicalTypeId::TIMESTAMP_TZ:
return 123;
case LogicalTypeId::VARCHAR:
return 149;
case LogicalTypeId::STRUCT:
case LogicalTypeId::MAP:
case LogicalTypeId::LIST:
case LogicalTypeId::UNION:
case LogicalTypeId::ARRAY:
return 160;
case LogicalTypeId::ANY:
return int64_t(AnyType::GetCastScore(type));
default:
return 110;
}
}
static int64_t ImplicitCastTinyint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::SMALLINT:
case LogicalTypeId::INTEGER:
case LogicalTypeId::BIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastSmallint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::INTEGER:
case LogicalTypeId::BIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastInteger(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::BIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastBigint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastUTinyint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::USMALLINT:
case LogicalTypeId::UINTEGER:
case LogicalTypeId::UBIGINT:
case LogicalTypeId::SMALLINT:
case LogicalTypeId::INTEGER:
case LogicalTypeId::BIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::UHUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastUSmallint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::UINTEGER:
case LogicalTypeId::UBIGINT:
case LogicalTypeId::INTEGER:
case LogicalTypeId::BIGINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::UHUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastUInteger(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::UBIGINT:
case LogicalTypeId::BIGINT:
case LogicalTypeId::UHUGEINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastUBigint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::UHUGEINT:
case LogicalTypeId::HUGEINT:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastFloat(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::DOUBLE:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::BIGINT:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastDouble(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::BIGINT:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastDecimal(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::BIGINT:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastHugeint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastUhugeint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::FLOAT:
case LogicalTypeId::DOUBLE:
case LogicalTypeId::DECIMAL:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastDate(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_TZ:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_NS:
case LogicalTypeId::TIMESTAMP_SEC:
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::DOUBLE:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastEnum(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastTimestampSec(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_MS:
case LogicalTypeId::TIMESTAMP_NS:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastTimestampMS(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP:
case LogicalTypeId::TIMESTAMP_NS:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastTimestampNS(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP:
// we allow casting ALL timestamps, including nanosecond ones, to TimestampNS
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastTimestamp(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP_NS:
return TargetTypeCost(to);
case LogicalTypeId::TIMESTAMP_TZ:
return TargetTypeCost(to);
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::DOUBLE:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastTimestamptz(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::TIMESTAMP_NS:
return TargetTypeCost(to);
case LogicalTypeId::TIMESTAMP:
return TargetTypeCost(to);
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::DOUBLE:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastVarint(const LogicalType &to) {
switch (to.id()) {
case LogicalTypeId::DOUBLE:
return TargetTypeCost(to);
default:
return -1;
}
}
static int64_t ImplicitCastTime(const LogicalType &to) {
switch (to.id())
{
case LogicalTypeId::VARCHAR:
return TargetTypeCost(to);
case LogicalTypeId::DOUBLE:
return 200;
default:
return -1;
}
}
static int64_t ImplicitCastVarchar(const LogicalType &to) {
switch (to.id())
{
case LogicalTypeId::DOUBLE:
return 200;
default:
return -1;
}
}
bool LogicalTypeIsValid(const LogicalType &type) {
switch (type.id()) {
case LogicalTypeId::STRUCT:
case LogicalTypeId::UNION:
case LogicalTypeId::LIST:
case LogicalTypeId::MAP:
case LogicalTypeId::ARRAY:
case LogicalTypeId::DECIMAL:
// these types are only valid with auxiliary info
if (!type.AuxInfo()) {
return false;
}
break;
default:
break;
}
switch (type.id()) {
case LogicalTypeId::ANY:
case LogicalTypeId::INVALID:
case LogicalTypeId::UNKNOWN:
return false;
case LogicalTypeId::STRUCT: {
auto child_count = StructType::GetChildCount(type);
for (idx_t i = 0; i < child_count; i++) {
if (!LogicalTypeIsValid(StructType::GetChildType(type, i))) {
return false;
}
}
return true;
}
case LogicalTypeId::UNION: {
auto member_count = UnionType::GetMemberCount(type);
for (idx_t i = 0; i < member_count; i++) {
if (!LogicalTypeIsValid(UnionType::GetMemberType(type, i))) {
return false;
}
}
return true;
}
case LogicalTypeId::LIST:
case LogicalTypeId::MAP:
return LogicalTypeIsValid(ListType::GetChildType(type));
case LogicalTypeId::ARRAY:
return LogicalTypeIsValid(ArrayType::GetChildType(type));
default:
return true;
}
}
int64_t CastRules::ImplicitCast(const LogicalType &from, const LogicalType &to) {
if (from.id() == LogicalTypeId::SQLNULL || to.id() == LogicalTypeId::ANY) {
// NULL expression can be cast to anything
return TargetTypeCost(to);
}
if (from.id() == LogicalTypeId::UNKNOWN) {
// parameter expression can be cast to anything for no cost
return 0;
}
if (from.id() == LogicalTypeId::STRING_LITERAL) {
// string literals can be cast to any type for low cost as long as the type is valid
// i.e. we cannot cast to LIST(ANY) as we don't know what "ANY" should be
// we cannot cast to DECIMAL without precision/width specified
if (!LogicalTypeIsValid(to)) {
return -1;
}
if (to.id() == LogicalTypeId::VARCHAR && to.GetAlias().empty()) {
return 1;
}
if (to.id() == LogicalTypeId::DOUBLE) {
return 2;
}
return 20;
}
if (from.id() == LogicalTypeId::INTEGER_LITERAL) {
// the integer literal has an underlying type - this type always matches
if (IntegerLiteral::GetType(from).id() == to.id()) {
return 0;
}
// integer literals can be cast to any other integer type for a low cost, but only if the literal fits
if (IntegerLiteral::FitsInType(from, to)) {
// to avoid ties we prefer BIGINT, INT, ...
auto target_cost = TargetTypeCost(to);
if (target_cost < 100) {
throw InternalException("Integer literal implicit cast - TargetTypeCost should be >= 100");
}
return target_cost - 90;
}
// in any other case we use the casting rules of the preferred type of the literal
return CastRules::ImplicitCast(IntegerLiteral::GetType(from), to);
}
if (from.GetAlias() != to.GetAlias()) {
// if aliases are different, an implicit cast is not possible
return -1;
}
if (from.id() == LogicalTypeId::LIST && to.id() == LogicalTypeId::LIST) {
// Lists can be cast if their child types can be cast
auto child_cost = ImplicitCast(ListType::GetChildType(from), ListType::GetChildType(to));
if (child_cost >= 1) {
// subtract one from the cost because we prefer LIST[X] -> LIST[VARCHAR] over LIST[X] -> VARCHAR
child_cost--;
}
return child_cost;
}
if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::ARRAY) {
// Arrays can be cast if their child types can be cast and the source and target has the same size
// or the target type has a unknown (any) size.
auto from_size = ArrayType::GetSize(from);
auto to_size = ArrayType::GetSize(to);
auto to_is_any_size = ArrayType::IsAnySize(to);
if (from_size == to_size || to_is_any_size) {
auto child_cost = ImplicitCast(ArrayType::GetChildType(from), ArrayType::GetChildType(to));
if (child_cost >= 100) {
// subtract one from the cost because we prefer ARRAY[X] -> ARRAY[VARCHAR] over ARRAY[X] -> VARCHAR
child_cost--;
}
return child_cost;
}
return -1; // Not possible if the sizes are different
}
if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::LIST) {
// Arrays can be cast to lists for the cost of casting the child type
auto child_cost = ImplicitCast(ArrayType::GetChildType(from), ListType::GetChildType(to));
if (child_cost < 0) {
return -1;
}
// add 1 because we prefer ARRAY->ARRAY casts over ARRAY->LIST casts
return child_cost + 1;
}
if (from.id() == LogicalTypeId::LIST && (to.id() == LogicalTypeId::ARRAY && !ArrayType::IsAnySize(to))) {
// Lists can be cast to arrays for the cost of casting the child type, if the target size is known
// there is no way for us to resolve the size at bind-time without inspecting the list values.
// TODO: if we can access the expression we could resolve the size if the list is constant.
return ImplicitCast(ListType::GetChildType(from), ArrayType::GetChildType(to));
}
if (from.id() == LogicalTypeId::UNION && to.id() == LogicalTypeId::UNION) {
// Check that the target union type is fully resolved.
if (to.AuxInfo() == nullptr) {
// If not, try anyway and let the actual cast logic handle it.
// This is to allow passing unions into functions that take a generic union type (without specifying member
// types) as an argument.
return 0;
}
// Unions can be cast if the source tags are a subset of the target tags
// in which case the most expensive cost is used
int64_t cost = -1;
for (idx_t from_member_idx = 0; from_member_idx < UnionType::GetMemberCount(from); from_member_idx++) {
auto &from_member_name = UnionType::GetMemberName(from, from_member_idx);
bool found = false;
for (idx_t to_member_idx = 0; to_member_idx < UnionType::GetMemberCount(to); to_member_idx++) {
auto &to_member_name = UnionType::GetMemberName(to, to_member_idx);
if (StringUtil::CIEquals(from_member_name, to_member_name)) {
auto &from_member_type = UnionType::GetMemberType(from, from_member_idx);
auto &to_member_type = UnionType::GetMemberType(to, to_member_idx);
auto child_cost = ImplicitCast(from_member_type, to_member_type);
cost = MaxValue(cost, child_cost);
found = true;
break;
}
}
if (!found) {
return -1;
}
}
return cost;
}
if (from.id() == LogicalTypeId::STRUCT && to.id() == LogicalTypeId::STRUCT) {
if (to.AuxInfo() == nullptr) {
// If this struct is not fully resolved, we'll leave it to the actual cast logic to handle it.
return 0;
}
auto &source_children = StructType::GetChildTypes(from);
auto &target_children = StructType::GetChildTypes(to);
if (source_children.size() != target_children.size()) {
// different number of children: not possible
return -1;
}
auto target_is_unnamed = StructType::IsUnnamed(to);
auto source_is_unnamed = StructType::IsUnnamed(from);
auto named_struct_cast = !source_is_unnamed && !target_is_unnamed;
int64_t cost = -1;
if (named_struct_cast) {
// Collect the target members in a map for easy lookup
case_insensitive_map_t<idx_t> target_members;
for (idx_t target_idx = 0; target_idx < target_children.size(); target_idx++) {
auto &target_name = target_children[target_idx].first;
if (target_members.find(target_name) != target_members.end()) {
// duplicate name in target struct
return -1;
}
target_members[target_name] = target_idx;
}
// Match the source members to the target members by name
for (idx_t source_idx = 0; source_idx < source_children.size(); source_idx++) {
auto &source_child = source_children[source_idx];
auto entry = target_members.find(source_child.first);
if (entry == target_members.end()) {
// element in source struct was not found in target struct
return -1;
}
auto target_idx = entry->second;
target_members.erase(entry);
auto child_cost = ImplicitCast(source_child.second, target_children[target_idx].second);
if (child_cost == -1) {
return -1;
}
cost = MaxValue(cost, child_cost);
}
} else {
// Match the source members to the target members by position
for (idx_t i = 0; i < source_children.size(); i++) {
auto &source_child = source_children[i];
auto &target_child = target_children[i];
auto child_cost = ImplicitCast(source_child.second, target_child.second);
if (child_cost == -1) {
return -1;
}
cost = MaxValue(cost, child_cost);
}
}
return cost;
}
if (from.id() == to.id()) {
// arguments match: do nothing
return 0;
}
// Special case: Anything can be cast to a union if the source type is a member of the union
if (to.id() == LogicalTypeId::UNION) {
// check that the union type is fully resolved.
if (to.AuxInfo() == nullptr) {
return -1;
}
// check if the union contains something castable from the source type
// in which case the least expensive (most specific) cast should be used
bool found = false;
auto cost = NumericLimits<int64_t>::Maximum();
for (idx_t i = 0; i < UnionType::GetMemberCount(to); i++) {
auto target_member = UnionType::GetMemberType(to, i);
auto target_cost = ImplicitCast(from, target_member);
if (target_cost != -1) {
found = true;
cost = MinValue(cost, target_cost);
}
}
return found ? cost : -1;
}
switch (from.id()) {
case LogicalTypeId::TINYINT:
return ImplicitCastTinyint(to);
case LogicalTypeId::SMALLINT:
return ImplicitCastSmallint(to);
case LogicalTypeId::INTEGER:
return ImplicitCastInteger(to);
case LogicalTypeId::BIGINT:
return ImplicitCastBigint(to);
case LogicalTypeId::UTINYINT:
return ImplicitCastUTinyint(to);
case LogicalTypeId::USMALLINT:
return ImplicitCastUSmallint(to);
case LogicalTypeId::UINTEGER:
return ImplicitCastUInteger(to);
case LogicalTypeId::UBIGINT:
return ImplicitCastUBigint(to);
case LogicalTypeId::HUGEINT:
return ImplicitCastHugeint(to);
case LogicalTypeId::UHUGEINT:
return ImplicitCastUhugeint(to);
case LogicalTypeId::FLOAT:
return ImplicitCastFloat(to);
case LogicalTypeId::DOUBLE:
return ImplicitCastDouble(to);
case LogicalTypeId::DATE:
return ImplicitCastDate(to);
case LogicalTypeId::DECIMAL:
return ImplicitCastDecimal(to);
case LogicalTypeId::ENUM:
return ImplicitCastEnum(to);
case LogicalTypeId::TIMESTAMP_SEC:
return ImplicitCastTimestampSec(to);
case LogicalTypeId::TIMESTAMP_MS:
return ImplicitCastTimestampMS(to);
case LogicalTypeId::TIMESTAMP_NS:
return ImplicitCastTimestampNS(to);
case LogicalTypeId::TIMESTAMP:
return ImplicitCastTimestamp(to);
case LogicalTypeId::VARINT:
return ImplicitCastVarint(to);
case LogicalTypeId::TIME:
return ImplicitCastTime(to);
case LogicalTypeId::VARCHAR:
return ImplicitCastVarchar(to);
// unequal comparison between timestamp_tz and varchar
case LogicalTypeId::TIMESTAMP_TZ:
return ImplicitCastTimestamptz(to);
default:
return -1;
}
}
} // namespace duckdb