forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionRange.cpp
More file actions
643 lines (604 loc) · 29.5 KB
/
ExpressionRange.cpp
File metadata and controls
643 lines (604 loc) · 29.5 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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ExpressionRange.h"
#include "ExtractFromTime.h"
#include "DateTruncate.h"
#include "GroupByAndAggregate.h"
#include "Execute.h"
#include "QueryPhysicalInputsCollector.h"
#include <cfenv>
#define DEF_OPERATOR(fname, op) \
ExpressionRange fname(const ExpressionRange& other) const { \
if (type_ == ExpressionRangeType::Invalid || other.type_ == ExpressionRangeType::Invalid) { \
return ExpressionRange::makeInvalidRange(); \
} \
CHECK(type_ == other.type_); \
switch (type_) { \
case ExpressionRangeType::Integer: \
return binOp<int64_t>(other, \
[](const int64_t x, const int64_t y) { return int64_t(checked_int64_t(x) op y); }); \
case ExpressionRangeType::Float: \
return binOp<float>(other, [](const float x, const float y) { \
std::feclearexcept(FE_OVERFLOW); \
std::feclearexcept(FE_UNDERFLOW); \
auto result = x op y; \
if (std::fetestexcept(FE_OVERFLOW) || std::fetestexcept(FE_UNDERFLOW)) { \
throw std::runtime_error("overflow / underflow"); \
} \
return result; \
}); \
case ExpressionRangeType::Double: \
return binOp<double>(other, [](const double x, const double y) { \
std::feclearexcept(FE_OVERFLOW); \
std::feclearexcept(FE_UNDERFLOW); \
auto result = x op y; \
if (std::fetestexcept(FE_OVERFLOW) || std::fetestexcept(FE_UNDERFLOW)) { \
throw std::runtime_error("overflow / underflow"); \
} \
return result; \
}); \
default: \
CHECK(false); \
} \
CHECK(false); \
return ExpressionRange::makeInvalidRange(); \
}
DEF_OPERATOR(ExpressionRange::operator+, +)
DEF_OPERATOR(ExpressionRange::operator-, -)
DEF_OPERATOR(ExpressionRange::operator*, *)
ExpressionRange ExpressionRange::operator/(const ExpressionRange& other) const {
if (type_ != ExpressionRangeType::Integer || other.type_ != ExpressionRangeType::Integer) {
return ExpressionRange::makeInvalidRange();
}
if (other.int_min_ * other.int_max_ <= 0) {
// if the other interval contains 0, the rule is more complicated;
// punt for now, we can revisit by splitting the other interval and
// taking the convex hull of the resulting two intervals
return ExpressionRange::makeInvalidRange();
}
return binOp<int64_t>(other, [](const int64_t x, const int64_t y) { return int64_t(checked_int64_t(x) / y); });
}
ExpressionRange ExpressionRange::operator||(const ExpressionRange& other) const {
if (type_ != other.type_) {
return ExpressionRange::makeInvalidRange();
}
ExpressionRange result;
switch (type_) {
case ExpressionRangeType::Invalid:
return ExpressionRange::makeInvalidRange();
case ExpressionRangeType::Integer: {
result.type_ = ExpressionRangeType::Integer;
result.has_nulls_ = has_nulls_ || other.has_nulls_;
result.int_min_ = std::min(int_min_, other.int_min_);
result.int_max_ = std::max(int_max_, other.int_max_);
result.bucket_ = std::min(bucket_, other.bucket_);
break;
}
case ExpressionRangeType::Float:
case ExpressionRangeType::Double: {
result.type_ = type_;
result.has_nulls_ = has_nulls_ || other.has_nulls_;
result.fp_min_ = std::min(fp_min_, other.fp_min_);
result.fp_max_ = std::max(fp_max_, other.fp_max_);
break;
}
default:
CHECK(false);
}
return result;
}
bool ExpressionRange::operator==(const ExpressionRange& other) const {
if (type_ != other.type_) {
return false;
}
switch (type_) {
case ExpressionRangeType::Invalid:
return true;
case ExpressionRangeType::Integer: {
return has_nulls_ == other.has_nulls_ && int_min_ == other.int_min_ && int_max_ == other.int_max_;
}
case ExpressionRangeType::Float:
case ExpressionRangeType::Double: {
return has_nulls_ == other.has_nulls_ && fp_min_ == other.fp_min_ && fp_max_ == other.fp_max_;
}
default:
CHECK(false);
}
return false;
}
ExpressionRange getExpressionRange(const Analyzer::BinOper* expr,
const std::vector<InputTableInfo>& query_infos,
const Executor*);
ExpressionRange getExpressionRange(const Analyzer::Constant* expr);
ExpressionRange getExpressionRange(const Analyzer::ColumnVar* col_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor);
ExpressionRange getExpressionRange(const Analyzer::LikeExpr* like_expr);
ExpressionRange getExpressionRange(const Analyzer::CaseExpr* case_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor*);
ExpressionRange getExpressionRange(const Analyzer::UOper* u_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor*);
ExpressionRange getExpressionRange(const Analyzer::ExtractExpr* extract_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor*);
ExpressionRange getExpressionRange(const Analyzer::DatetruncExpr* datetrunc_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor);
ExpressionRange getExpressionRange(const Analyzer::Expr* expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
auto bin_oper_expr = dynamic_cast<const Analyzer::BinOper*>(expr);
if (bin_oper_expr) {
return getExpressionRange(bin_oper_expr, query_infos, executor);
}
auto constant_expr = dynamic_cast<const Analyzer::Constant*>(expr);
if (constant_expr) {
return getExpressionRange(constant_expr);
}
auto column_var_expr = dynamic_cast<const Analyzer::ColumnVar*>(expr);
if (column_var_expr) {
return getExpressionRange(column_var_expr, query_infos, executor);
}
auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
if (like_expr) {
return getExpressionRange(like_expr);
}
auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
if (case_expr) {
return getExpressionRange(case_expr, query_infos, executor);
}
auto u_expr = dynamic_cast<const Analyzer::UOper*>(expr);
if (u_expr) {
return getExpressionRange(u_expr, query_infos, executor);
}
auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
if (extract_expr) {
return getExpressionRange(extract_expr, query_infos, executor);
}
auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
if (datetrunc_expr) {
return getExpressionRange(datetrunc_expr, query_infos, executor);
}
return ExpressionRange::makeInvalidRange();
}
namespace {
int64_t scale_down_interval_endpoint(const int64_t endpoint, const SQLTypeInfo& ti) {
return endpoint / static_cast<int64_t>(exp_to_scale(ti.get_scale()));
}
int64_t scale_up_interval_endpoint(const int64_t endpoint, const SQLTypeInfo& ti) {
return endpoint * static_cast<int64_t>(exp_to_scale(ti.get_scale()));
}
} // namespace
ExpressionRange getExpressionRange(const Analyzer::BinOper* expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
const auto& lhs = getExpressionRange(expr->get_left_operand(), query_infos, executor);
const auto& rhs = getExpressionRange(expr->get_right_operand(), query_infos, executor);
switch (expr->get_optype()) {
case kPLUS:
return lhs + rhs;
case kMINUS:
return lhs - rhs;
case kMULTIPLY: {
const auto& lhs_type = expr->get_left_operand()->get_type_info();
auto er = lhs * rhs;
if (lhs_type.is_decimal() && er.getType() != ExpressionRangeType::Invalid) {
CHECK(er.getType() == ExpressionRangeType::Integer);
CHECK_EQ(int64_t(0), er.getBucket());
return ExpressionRange::makeIntRange(scale_down_interval_endpoint(er.getIntMin(), lhs_type),
scale_down_interval_endpoint(er.getIntMax(), lhs_type),
0,
er.hasNulls());
}
return er;
}
case kDIVIDE: {
const auto& lhs_type = expr->get_left_operand()->get_type_info();
if (lhs_type.is_decimal() && lhs.getType() != ExpressionRangeType::Invalid) {
CHECK(lhs.getType() == ExpressionRangeType::Integer);
const auto adjusted_lhs = ExpressionRange::makeIntRange(scale_up_interval_endpoint(lhs.getIntMin(), lhs_type),
scale_up_interval_endpoint(lhs.getIntMax(), lhs_type),
0,
lhs.hasNulls());
return adjusted_lhs / rhs;
}
return lhs / rhs;
}
default:
break;
}
return ExpressionRange::makeInvalidRange();
}
ExpressionRange getExpressionRange(const Analyzer::Constant* constant_expr) {
if (constant_expr->get_is_null()) {
return ExpressionRange::makeInvalidRange();
}
const auto constant_type = constant_expr->get_type_info().get_type();
const auto datum = constant_expr->get_constval();
switch (constant_type) {
case kSMALLINT: {
const int64_t v = datum.smallintval;
return ExpressionRange::makeIntRange(v, v, 0, false);
}
case kINT: {
const int64_t v = datum.intval;
return ExpressionRange::makeIntRange(v, v, 0, false);
}
case kBIGINT:
case kNUMERIC:
case kDECIMAL: {
const int64_t v = datum.bigintval;
return ExpressionRange::makeIntRange(v, v, 0, false);
}
case kTIME:
case kTIMESTAMP:
case kDATE: {
const int64_t v = datum.timeval;
return ExpressionRange::makeIntRange(v, v, 0, false);
}
case kFLOAT: {
return ExpressionRange::makeFloatRange(datum.floatval, datum.floatval, false);
}
case kDOUBLE: {
return ExpressionRange::makeDoubleRange(datum.doubleval, datum.doubleval, false);
}
default:
break;
}
return ExpressionRange::makeInvalidRange();
}
#define FIND_STAT_FRAG(stat_name) \
const auto stat_name##_frag = std::stat_name##_element( \
fragments.begin(), \
fragments.end(), \
[&has_nulls, col_id, col_ti](const Fragmenter_Namespace::FragmentInfo& lhs, \
const Fragmenter_Namespace::FragmentInfo& rhs) { \
auto lhs_meta_it = lhs.getChunkMetadataMap().find(col_id); \
if (lhs_meta_it == lhs.getChunkMetadataMap().end()) { \
return false; \
} \
auto rhs_meta_it = rhs.getChunkMetadataMap().find(col_id); \
CHECK(rhs_meta_it != rhs.getChunkMetadataMap().end()); \
if (lhs_meta_it->second.chunkStats.has_nulls || rhs_meta_it->second.chunkStats.has_nulls) { \
has_nulls = true; \
} \
if (col_ti.is_fp()) { \
return extract_##stat_name##_stat_double(lhs_meta_it->second.chunkStats, col_ti) < \
extract_##stat_name##_stat_double(rhs_meta_it->second.chunkStats, col_ti); \
} \
return extract_##stat_name##_stat(lhs_meta_it->second.chunkStats, col_ti) < \
extract_##stat_name##_stat(rhs_meta_it->second.chunkStats, col_ti); \
}); \
if (stat_name##_frag == fragments.end()) { \
return ExpressionRange::makeInvalidRange(); \
}
namespace {
double extract_min_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
return col_ti.get_type() == kDOUBLE ? stats.min.doubleval : stats.min.floatval;
}
double extract_max_stat_double(const ChunkStats& stats, const SQLTypeInfo& col_ti) {
return col_ti.get_type() == kDOUBLE ? stats.max.doubleval : stats.max.floatval;
}
int64_t get_conservative_datetrunc_bucket(const DatetruncField datetrunc_field) {
const int64_t day_seconds{24 * 3600};
const int64_t year_days{365};
switch (datetrunc_field) {
case dtYEAR:
return year_days * day_seconds;
case dtQUARTER:
return 90 * day_seconds; // 90 is least number of days in any quater
case dtMONTH:
return 28 * day_seconds;
case dtDAY:
return day_seconds;
case dtHOUR:
return 3600;
case dtMINUTE:
return 60;
case dtMILLENNIUM:
return 1000 * year_days * day_seconds;
case dtCENTURY:
return 100 * year_days * day_seconds;
case dtDECADE:
return 10 * year_days * day_seconds;
case dtWEEK:
return 7 * day_seconds;
case dtQUARTERDAY:
return 4 * 60 * 50;
default:
return 0;
}
}
} // namespace
ExpressionRange getLeafColumnRange(const Analyzer::ColumnVar* col_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor,
const bool is_outer_join_proj) {
bool has_nulls = is_outer_join_proj;
int col_id = col_expr->get_column_id();
const auto& col_phys_ti =
col_expr->get_type_info().is_array() ? col_expr->get_type_info().get_elem_type() : col_expr->get_type_info();
const auto col_ti = get_logical_type_info(col_phys_ti);
switch (col_ti.get_type()) {
case kTEXT:
case kCHAR:
case kVARCHAR:
CHECK_EQ(kENCODING_DICT, col_ti.get_compression());
case kBOOLEAN:
case kSMALLINT:
case kINT:
case kBIGINT:
case kDECIMAL:
case kNUMERIC:
case kDATE:
case kTIMESTAMP:
case kTIME:
case kFLOAT:
case kDOUBLE: {
ssize_t ti_idx = -1;
for (size_t i = 0; i < query_infos.size(); ++i) {
if (col_expr->get_table_id() == query_infos[i].table_id) {
ti_idx = i;
break;
}
}
CHECK_NE(ssize_t(-1), ti_idx);
const auto& query_info = query_infos[ti_idx].info;
const auto& fragments = query_info.fragments;
const auto cd = executor->getColumnDescriptor(col_expr);
if (cd && cd->isVirtualCol) {
CHECK(cd->columnName == "rowid");
CHECK_EQ(kBIGINT, col_ti.get_type());
const int64_t num_tuples = query_info.getNumTuples();
return ExpressionRange::makeIntRange(0, std::max(num_tuples - 1, int64_t(0)), 0, has_nulls);
}
if (query_info.getNumTuples() == 0) {
// The column doesn't contain any values, synthesize an empty range.
return col_ti.is_fp() ? ExpressionRange::makeFloatRange(0, -1, false)
: ExpressionRange::makeIntRange(0, -1, 0, false);
}
FIND_STAT_FRAG(min);
FIND_STAT_FRAG(max);
const auto min_it = min_frag->getChunkMetadataMap().find(col_id);
if (min_it == min_frag->getChunkMetadataMap().end()) {
return ExpressionRange::makeInvalidRange();
}
const auto max_it = max_frag->getChunkMetadataMap().find(col_id);
CHECK(max_it != max_frag->getChunkMetadataMap().end());
for (const auto& fragment : fragments) {
const auto it = fragment.getChunkMetadataMap().find(col_id);
if (it != fragment.getChunkMetadataMap().end()) {
if (it->second.chunkStats.has_nulls) {
has_nulls = true;
}
}
}
if (col_ti.is_fp()) {
const auto min_val = extract_min_stat_double(min_it->second.chunkStats, col_ti);
const auto max_val = extract_max_stat_double(max_it->second.chunkStats, col_ti);
return col_ti.get_type() == kFLOAT ? ExpressionRange::makeFloatRange(min_val, max_val, has_nulls)
: ExpressionRange::makeDoubleRange(min_val, max_val, has_nulls);
}
const auto min_val = extract_min_stat(min_it->second.chunkStats, col_ti);
const auto max_val = extract_max_stat(max_it->second.chunkStats, col_ti);
if (max_val < min_val) {
// The column doesn't contain any non-null values, synthesize an empty range.
CHECK_LT(max_val, 0);
CHECK_GT(min_val, 0);
CHECK_EQ(-(min_val + 1), max_val);
return ExpressionRange::makeIntRange(0, -1, 0, has_nulls);
}
const int64_t bucket = col_ti.get_type() == kDATE ? get_conservative_datetrunc_bucket(dtDAY) : 0;
return ExpressionRange::makeIntRange(min_val, max_val, bucket, has_nulls);
}
default:
break;
}
return ExpressionRange::makeInvalidRange();
}
ExpressionRange getExpressionRange(const Analyzer::ColumnVar* col_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
const int rte_idx = col_expr->get_rte_idx();
CHECK_GE(rte_idx, 0);
CHECK_LT(static_cast<size_t>(rte_idx), query_infos.size());
bool is_outer_join_proj = rte_idx > 0 && executor->isOuterJoin();
#ifdef HAVE_RAVM
if (col_expr->get_table_id() > 0) {
auto col_range = executor->getColRange(PhysicalInput{col_expr->get_column_id(), col_expr->get_table_id()});
if (is_outer_join_proj) {
col_range.setHasNulls();
}
return col_range;
}
#endif // HAVE_RAVM
return getLeafColumnRange(col_expr, query_infos, executor, is_outer_join_proj);
}
#undef FIND_STAT_FRAG
ExpressionRange getExpressionRange(const Analyzer::LikeExpr* like_expr) {
const auto& ti = like_expr->get_type_info();
CHECK(ti.is_boolean());
const auto& arg_ti = like_expr->get_arg()->get_type_info();
return ExpressionRange::makeIntRange(arg_ti.get_notnull() ? 0 : inline_int_null_val(ti), 1, 0, false);
}
ExpressionRange getExpressionRange(const Analyzer::CaseExpr* case_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
const auto& expr_pair_list = case_expr->get_expr_pair_list();
auto expr_range = ExpressionRange::makeInvalidRange();
for (const auto& expr_pair : expr_pair_list) {
CHECK_EQ(expr_pair.first->get_type_info().get_type(), kBOOLEAN);
const auto crt_range = getExpressionRange(expr_pair.second.get(), query_infos, executor);
if (crt_range.getType() == ExpressionRangeType::Invalid) {
return ExpressionRange::makeInvalidRange();
}
expr_range = (expr_range.getType() != ExpressionRangeType::Invalid) ? expr_range || crt_range : crt_range;
}
const auto else_expr = case_expr->get_else_expr();
CHECK(else_expr);
const auto else_null_expr = dynamic_cast<const Analyzer::Constant*>(else_expr);
if (else_null_expr && else_null_expr->get_is_null()) {
expr_range.setHasNulls();
return expr_range;
}
return expr_range || getExpressionRange(else_expr, query_infos, executor);
}
namespace {
ExpressionRange fpRangeFromDecimal(const ExpressionRange& arg_range,
const int64_t scale,
const SQLTypeInfo& target_ti) {
CHECK(target_ti.is_fp());
if (target_ti.get_type() == kFLOAT) {
return ExpressionRange::makeFloatRange(static_cast<float>(arg_range.getIntMin()) / scale,
static_cast<float>(arg_range.getIntMax()) / scale,
arg_range.hasNulls());
}
return ExpressionRange::makeDoubleRange(static_cast<double>(arg_range.getIntMin()) / scale,
static_cast<double>(arg_range.getIntMax()) / scale,
arg_range.hasNulls());
}
} // namespace
ExpressionRange getExpressionRange(const Analyzer::UOper* u_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
if (u_expr->get_optype() == kUNNEST) {
return getExpressionRange(u_expr->get_operand(), query_infos, executor);
}
if (u_expr->get_optype() != kCAST) {
return ExpressionRange::makeInvalidRange();
}
const auto& ti = u_expr->get_type_info();
if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
const auto sdp = executor->getStringDictionaryProxy(ti.get_comp_param(), executor->getRowSetMemoryOwner(), true);
CHECK(sdp);
const auto const_operand = dynamic_cast<const Analyzer::Constant*>(u_expr->get_operand());
CHECK(const_operand);
CHECK(const_operand->get_constval().stringval);
const int64_t v = sdp->getIdOfString(*const_operand->get_constval().stringval);
return ExpressionRange::makeIntRange(v, v, 0, false);
}
const auto arg_range = getExpressionRange(u_expr->get_operand(), query_infos, executor);
const auto& arg_ti = u_expr->get_operand()->get_type_info();
// Timestamp to date cast is generated like a date trunc on day in the executor.
if (arg_ti.get_type() == kTIMESTAMP && ti.get_type() == kDATE) {
const auto dt_expr = makeExpr<Analyzer::DatetruncExpr>(arg_ti, false, dtDAY, u_expr->get_own_operand());
return getExpressionRange(dt_expr.get(), query_infos, executor);
}
switch (arg_range.getType()) {
case ExpressionRangeType::Float:
case ExpressionRangeType::Double: {
if (ti.is_fp()) {
return ti.get_type() == kDOUBLE
? ExpressionRange::makeDoubleRange(arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls())
: ExpressionRange::makeFloatRange(arg_range.getFpMin(), arg_range.getFpMax(), arg_range.hasNulls());
}
if (ti.is_integer()) {
return ExpressionRange::makeIntRange(arg_range.getFpMin(), arg_range.getFpMax(), 0, arg_range.hasNulls());
}
break;
}
case ExpressionRangeType::Integer: {
if (ti.is_decimal()) {
CHECK_EQ(int64_t(0), arg_range.getBucket());
const int64_t scale = exp_to_scale(ti.get_scale() - arg_ti.get_scale());
return ExpressionRange::makeIntRange(
arg_range.getIntMin() * scale, arg_range.getIntMax() * scale, 0, arg_range.hasNulls());
}
if (arg_ti.is_decimal()) {
CHECK_EQ(int64_t(0), arg_range.getBucket());
const int64_t scale = exp_to_scale(arg_ti.get_scale());
if (ti.is_fp()) {
return fpRangeFromDecimal(arg_range, scale, ti);
}
return ExpressionRange::makeIntRange(
arg_range.getIntMin() / scale, arg_range.getIntMax() / scale, 0, arg_range.hasNulls());
}
if (ti.is_integer() || ti.is_time()) {
return arg_range;
}
if (ti.get_type() == kFLOAT) {
return ExpressionRange::makeFloatRange(arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
}
if (ti.get_type() == kDOUBLE) {
return ExpressionRange::makeDoubleRange(arg_range.getIntMin(), arg_range.getIntMax(), arg_range.hasNulls());
}
break;
}
case ExpressionRangeType::Invalid:
break;
default:
CHECK(false);
}
return ExpressionRange::makeInvalidRange();
}
ExpressionRange getExpressionRange(const Analyzer::ExtractExpr* extract_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
const int32_t extract_field{extract_expr->get_field()};
const auto arg_range = getExpressionRange(extract_expr->get_from_expr(), query_infos, executor);
const bool has_nulls = arg_range.getType() == ExpressionRangeType::Invalid || arg_range.hasNulls();
switch (extract_field) {
case kYEAR: {
if (arg_range.getType() == ExpressionRangeType::Invalid) {
return ExpressionRange::makeInvalidRange();
}
CHECK(arg_range.getType() == ExpressionRangeType::Integer);
const int64_t year_range_min = ExtractFromTime(kYEAR, arg_range.getIntMin());
const int64_t year_range_max = ExtractFromTime(kYEAR, arg_range.getIntMax());
return ExpressionRange::makeIntRange(year_range_min, year_range_max, 0, arg_range.hasNulls());
}
case kEPOCH:
return arg_range;
case kQUARTERDAY:
case kQUARTER:
return ExpressionRange::makeIntRange(1, 4, 0, has_nulls);
case kMONTH:
return ExpressionRange::makeIntRange(1, 12, 0, has_nulls);
case kDAY:
return ExpressionRange::makeIntRange(1, 31, 0, has_nulls);
case kHOUR:
return ExpressionRange::makeIntRange(0, 23, 0, has_nulls);
case kMINUTE:
return ExpressionRange::makeIntRange(0, 59, 0, has_nulls);
case kSECOND:
return ExpressionRange::makeIntRange(0, 60, 0, has_nulls);
case kDOW:
return ExpressionRange::makeIntRange(0, 6, 0, has_nulls);
case kISODOW:
return ExpressionRange::makeIntRange(1, 7, 0, has_nulls);
case kDOY:
return ExpressionRange::makeIntRange(1, 366, 0, has_nulls);
case kWEEK:
return ExpressionRange::makeIntRange(1, 53, 0, has_nulls);
default:
CHECK(false);
}
return ExpressionRange::makeInvalidRange();
}
ExpressionRange getExpressionRange(const Analyzer::DatetruncExpr* datetrunc_expr,
const std::vector<InputTableInfo>& query_infos,
const Executor* executor) {
const auto arg_range = getExpressionRange(datetrunc_expr->get_from_expr(), query_infos, executor);
if (arg_range.getType() == ExpressionRangeType::Invalid) {
return ExpressionRange::makeInvalidRange();
}
const int64_t min_ts = DateTruncate(datetrunc_expr->get_field(), arg_range.getIntMin());
const int64_t max_ts = DateTruncate(datetrunc_expr->get_field(), arg_range.getIntMax());
const int64_t bucket = get_conservative_datetrunc_bucket(datetrunc_expr->get_field());
return ExpressionRange::makeIntRange(min_ts, max_ts, bucket, arg_range.hasNulls());
}