Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathexpression.cpp
More file actions
3151 lines (2927 loc) · 82.1 KB
/
Copy pathexpression.cpp
File metadata and controls
3151 lines (2927 loc) · 82.1 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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2017 TOSHIBA Digital Solutions Corporation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
@file
@brief Implementation of Expr
*/
#include "util/type.h"
#include "util/allocator.h"
#include "util/container.h"
#include "util/time.h"
#include <algorithm>
#include <cctype>
#include <iostream>
#include <map>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include "expression.h"
#include "qp_def.h"
#include "gis_generator.h"
#include "function_map.h"
#include "lexer.h"
#include "query.h"
#include "meta_store.h"
/*!
* @brief Return the type of the expression
* @return The expression is a simple value
*/
bool Expr::isNumeric() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
return value_->isNumerical();
}
/*!
* @brief Return the type of the expression
* @return The expression is a simple value of the given type
*/
bool Expr::isNumericOfType(ColumnType t) const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
return value_->getType() == t;
}
/*!
* @brief Return the type of the expression
* @return The expression is a simple value of the given type
*/
bool Expr::isNumericInteger() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
return value_->isInteger();
}
/*!
* @brief Return the type of the expression
* @return The expression is a simple value of the given type
*/
bool Expr::isNumericFloat() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
return value_->isFloat();
}
/*!
* @brief Return the type of the expression
* @return The expression is a simple value
*/
bool Expr::isBoolean() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
ColumnType t = value_->getType();
return (t == COLUMN_TYPE_BOOL);
}
/*!
* @brief Return the type of the expression
* @return The expression is a string value
*/
bool Expr::isString() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
ColumnType t = value_->getType();
return (t == COLUMN_TYPE_STRING);
}
/*!
* @brief Return the type of the expression
* @return The expression is a geometry value
*/
bool Expr::isGeometry() const {
if (type_ != VALUE) return false;
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
ColumnType t = value_->getType();
return (t == COLUMN_TYPE_GEOMETRY);
}
/*!
* @brief Return the type of the expression
* @return The expression is an array
*/
bool Expr::isArrayValue() const {
return (type_ == VALUE && value_ != NULL && value_->isArray());
}
/*!
* @brief Return the type of the expression
* @return The expression is an array
*/
bool Expr::isExprArray() const {
return (type_ == EXPRARRAY);
}
/*!
* @brief Return the type of the expression
* @return The expression is a value
*/
bool Expr::isValue() const {
return (type_ == VALUE);
}
/*!
* @brief Return the type of the expression
* @return The expression is null
*/
bool Expr::isNullValue() const {
return (type_ == NULLVALUE);
}
/*!
* @brief Return the type of the expression
* @return The expression can have null
*/
bool Expr::isNullable() const {
if (isColumn() && columnInfo_->isNotNull()) {
return false;
} else {
return true;
}
}
/*!
* @brief Return the type of the expression
* @return The expression is an expr label
*/
bool Expr::isExprLabel() const {
return (type_ == EXPRLABEL);
}
/*!
* @brief Return the type of the expression
* @return The expression is an aggregation function
*/
bool Expr::isAggregation() const {
return (type_ == AGGREGATION);
}
/*!
* @brief Return the type of the expression
* @return The expression is a selection function
*/
bool Expr::isSelection() const {
return (type_ == SELECTION);
}
/*!
* @brief Return the type of the expression
* @return The expression is a column id
*/
bool Expr::isColumn() const {
return (type_ == COLUMN);
}
/*!
* @brief Return the type of the expression
* @return The expression is a timestamp
*/
bool Expr::isTimestamp() const {
if (type_ != VALUE) return false;
return (resolveValueType() == COLUMN_TYPE_TIMESTAMP);
}
bool Expr::isMicroTimestamp() const {
if (type_ != VALUE) return false;
return (resolveValueType() == COLUMN_TYPE_MICRO_TIMESTAMP);
}
bool Expr::isNanoTimestamp() const {
if (type_ != VALUE) return false;
return (resolveValueType() == COLUMN_TYPE_NANO_TIMESTAMP);
}
bool Expr::isTimestampFamily() const {
if (type_ != VALUE) return false;
return ValueProcessor::isTimestampFamily(resolveValueType());
}
/*!
* @brief Cast an numeric value to another type.
* @return casted value.
*/
template <typename T>
T Expr::castNumericValue() {
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
if (type_ != VALUE) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is not a value");
}
switch (value_->getType()) {
case COLUMN_TYPE_BYTE:
return static_cast<T>(
*reinterpret_cast<const int8_t *>(value_->data()));
case COLUMN_TYPE_SHORT:
return static_cast<T>(
*reinterpret_cast<const int16_t *>(value_->data()));
case COLUMN_TYPE_INT:
return static_cast<T>(
*reinterpret_cast<const int32_t *>(value_->data()));
case COLUMN_TYPE_LONG:
return static_cast<T>(
*reinterpret_cast<const int64_t *>(value_->data()));
case COLUMN_TYPE_FLOAT:
return static_cast<T>(*reinterpret_cast<const float *>(value_->data()));
case COLUMN_TYPE_DOUBLE:
return static_cast<T>(
*reinterpret_cast<const double *>(value_->data()));
default:
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_CANNOT_CAST, "Cannot cast value to numeric");
}
}
/*!
* @brief castNumericValue's specialized version for boolean
* @return Value in expression that casted to bool
*/
bool Expr::castNumericValue() {
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
if (isNullValue()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is null");
}
if (type_ != VALUE) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is not a value");
}
switch (value_->getType()) {
case COLUMN_TYPE_BOOL:
return *reinterpret_cast<const bool *>(value_->data());
default:
GS_THROW_USER_ERROR(
GS_ERROR_TQ_CONSTRAINT_CANNOT_CAST, "Cannot cast value to boolean");
}
}
/*!
* @brief Return the value of the expression
* @return The expression as Integer
*/
int Expr::getValueAsInt() {
return castNumericValue<int>();
}
/*!
* @brief Return the value of the expression
* @return The expression as Integer
*/
int64_t Expr::getValueAsInt64() {
return castNumericValue<int64_t>();
}
/*!
* @brief Return a expression type
* @return The type of expression
*/
Expr::Type Expr::getType() {
return type_;
}
ColumnType Expr::resolveValueType() const {
assert(type_ == VALUE);
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
return value_->getType();
}
/*!
* @brief Return the value of the expression
* @return The expression value as a Double
*/
double Expr::getValueAsDouble() {
return castNumericValue<double>();
}
/*!
* @brief Return the value of the expression
* @return The expression value as a Boolean
*/
bool Expr::getValueAsBool() {
return castNumericValue();
}
/*!
* @brief Stringify numeric value
* @return Stringified value
*/
template <typename T, typename R>
const char *Expr::stringifyValue(TransactionContext &txn) {
util::NormalOStringStream os;
os << R(*reinterpret_cast<const T *>(value_->data()));
return os2char(txn, os);
}
#ifdef _WIN32
/*!
* @brief Stringify numeric value
* @return Stringified value
*/
template <>
const char *Expr::stringifyValue<double, double>(TransactionContext &txn) {
double x = double(*reinterpret_cast<const double *>(value_->data()));
util::NormalOStringStream os;
if (_isnan(x)) {
os << "nan";
}
else if (x == std::numeric_limits<double>::infinity()) {
os << "inf";
}
else if (x == -std::numeric_limits<double>::infinity()) {
os << "-inf";
}
else {
os << x;
}
return os2char(txn, os);
}
#endif
template<typename T>
const char8_t* Expr::stringifyTimestamp(TransactionContext &txn) {
util::DateTime::ZonedOption option = util::DateTime::ZonedOption::create(
TRIM_MILLISECONDS, txn.getTimeZone());
option.asLocalTimeZone_ = USE_LOCAL_TIMEZONE;
const size_t maxSize = util::DateTime::MAX_FORMAT_SIZE;
char8_t *str =static_cast<char8_t*>(
txn.getDefaultAllocator().allocate(maxSize + 1));
const util::DateTime::Formatter formatter =
ValueProcessor::getTimestampFormatter(
*reinterpret_cast<const T*>(value_->data()), option);
const size_t size = formatter(str, maxSize);
str[size] = '\0';
return str;
}
/*!
* @brief Return the value of the expression
* @return The expression value as a string
*/
const char *Expr::getValueAsString(TransactionContext &txn) {
if (isNullValue()) {
return "NULL";
} else
if (type_ == VALUE) {
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
ColumnType t = value_->getType();
switch (t) {
case COLUMN_TYPE_STRING: {
const char *str = reinterpret_cast<const char *>(value_->data());
size_t size = value_->size();
if (str == NULL || size == 0) return "";
char *allocatedStr = reinterpret_cast<char *>(
txn.getDefaultAllocator().allocate(size + 1));
memcpy(allocatedStr, str, size);
allocatedStr[size] = '\0';
return allocatedStr;
}
case COLUMN_TYPE_BOOL:
if (*reinterpret_cast<const bool *>(value_->data())) {
return "TRUE";
}
else {
return "FALSE";
}
case COLUMN_TYPE_BYTE:
return stringifyValue<int8_t, int16_t>(txn);
case COLUMN_TYPE_SHORT:
return stringifyValue<int16_t, int16_t>(txn);
case COLUMN_TYPE_INT:
return stringifyValue<int32_t, int32_t>(txn);
case COLUMN_TYPE_LONG:
return stringifyValue<int64_t, int64_t>(txn);
case COLUMN_TYPE_FLOAT:
return stringifyValue<float, float>(txn);
case COLUMN_TYPE_DOUBLE:
return stringifyValue<double, double>(txn);
case COLUMN_TYPE_TIMESTAMP:
return stringifyTimestamp<Timestamp>(txn);
case COLUMN_TYPE_MICRO_TIMESTAMP:
return stringifyTimestamp<MicroTimestamp>(txn);
case COLUMN_TYPE_NANO_TIMESTAMP:
return stringifyTimestamp<NanoTimestamp>(txn);
case COLUMN_TYPE_GEOMETRY: {
Geometry *geom = geomCache_;
return geom->getString(txn);
}
default:
break;
}
}
else if (type_ == EXPRARRAY) {
util::NormalOStringStream os;
ExprList::const_iterator it;
if (arglist_ == NULL || arglist_->empty()) {
os << "EXPRARRAY(EMPTY)";
return os2char(txn, os);
}
it = arglist_->begin();
os << "EXPRARRAY(";
do {
if ((*it)->isString()) {
os << '\'' << (*it)->getValueAsString(txn) << '\'';
}
else {
os << (*it)->getValueAsString(txn);
}
++it;
if (it != arglist_->end()) {
os << ", ";
}
else {
break;
}
} while (true);
os << ')';
return os2char(txn, os);
}
else {
if (label_ != NULL) {
return label_;
}
else {
return "NULL";
}
}
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: cannot create string");
}
/*!
* @brief Return the value of the expression
* @return The expression as a Geometry
*/
Geometry *Expr::getGeometry() {
if (geomCache_ != NULL) {
return geomCache_;
}
else if (isNullValue()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is null");
}
else {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: getGeometry() is called in invalid "
"context.");
}
}
/*!
* @brief Return the value of the expression
* @return The expression as Expression list
*/
const ExprList &Expr::getArgList() {
if (type_ == EXPR && arglist_ != NULL) {
return *arglist_;
}
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: getArgList() is called in invalid context.");
}
/*!
* @brief Return the expression of an array element
* @param txn The transaction context
* @param txn Object manager
* @param idx element index
* @return The expression of the element
*/
Expr *Expr::getArrayElement(
TransactionContext &txn, ObjectManagerV4 &objectManager, AllocateStrategy &strategy, size_t idx) {
if (type_ == VALUE) {
assert(value_ != NULL);
if (idx >= value_->getArrayLength(txn, objectManager, strategy)) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CONSTRAINT_ARRAY_OUT_OF_RANGE,
"Specified index is out of range.");
}
const uint8_t *data;
uint32_t size;
value_->getArrayElement(
txn, objectManager, strategy, static_cast<uint32_t>(idx), data, size);
switch (value_->getType()) {
case COLUMN_TYPE_STRING_ARRAY:
return Expr::newStringValue(
reinterpret_cast<const char *>(data), size, txn);
case COLUMN_TYPE_BOOL_ARRAY:
return Expr::newBooleanValue(
*reinterpret_cast<const bool *>(data), txn);
case COLUMN_TYPE_BYTE_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const int8_t *>(data), txn);
case COLUMN_TYPE_SHORT_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const int16_t *>(data), txn);
case COLUMN_TYPE_INT_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const int32_t *>(data), txn);
case COLUMN_TYPE_LONG_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const int64_t *>(data), txn);
case COLUMN_TYPE_FLOAT_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const float *>(data), txn);
case COLUMN_TYPE_DOUBLE_ARRAY:
return Expr::newNumericValue(
*reinterpret_cast<const double *>(data), txn);
case COLUMN_TYPE_TIMESTAMP_ARRAY:
return Expr::newTimestampValue(
*reinterpret_cast<const Timestamp*>(data),txn);
default:
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: getArrayElement() is called with "
"invalid array type.");
break;
}
}
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: getArrayElement() is called in invalid "
"context.");
}
/*!
* @brief Return the length of array
* @param txn The transaction context
* @param txn Object manager
* @return The length of array
*/
size_t Expr::getArrayLength(
TransactionContext &txn, ObjectManagerV4 &objectManager, AllocateStrategy &strategy) const {
if (type_ == VALUE) {
assert(value_ != NULL);
return value_->getArrayLength(txn, objectManager, strategy);
}
else if (type_ == EXPRARRAY && arglist_ != NULL) {
return arglist_->size();
}
else {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: getArrayLength() is called in invalid "
"context.");
}
}
/*!
* @brief Return the timestamp in a expression structure
* @return The timestamp value
*/
Timestamp Expr::getTimeStamp() {
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
if (isNullValue()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is null");
}
try {
return ValueProcessor::getTimestamp(value_->getType(), value_->data());
}
catch (UserException &e) {
GS_RETHROW_USER_ERROR_CODED(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE, e,
"Invalid datatypes: argument is not a timestamp value");
}
}
NanoTimestamp Expr::getNanoTimestamp() {
if (value_ == NULL) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: value == NULL");
}
if (isNullValue()) {
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: expression is null");
}
try {
return ValueProcessor::getNanoTimestamp(
value_->getType(), value_->data());
}
catch (UserException &e) {
GS_RETHROW_USER_ERROR_CODED(
GS_ERROR_TQ_CONSTRAINT_INVALID_ARGUMENT_TYPE, e,
"Invalid datatypes: argument is not a timestamp value");
}
}
/*!
* @brief Make a deep copy of an expression
* @param txn The transaction context
* @param txn Object manager
* @return Duplicated expression
*/
Expr *Expr::dup(TransactionContext &txn, ObjectManagerV4 &objectManager, AllocateStrategy &strategy) {
Expr *e = NULL;
if (type_ == NULLVALUE) {
e = Expr::newNullValue(txn);
} else
if (type_ == VALUE) {
assert(value_ != NULL);
if (!value_->isArray()) {
switch (value_->getType()) {
case COLUMN_TYPE_STRING:
e = Expr::newStringValue(
reinterpret_cast<const char *>(value_->data()),
value_->size(), txn);
break;
case COLUMN_TYPE_BOOL:
e = Expr::newBooleanValue(
*reinterpret_cast<const bool *>(value_->data()), txn);
break;
case COLUMN_TYPE_BYTE:
e = Expr::newNumericValue(
*reinterpret_cast<const int8_t *>(value_->data()), txn);
break;
case COLUMN_TYPE_SHORT:
e = Expr::newNumericValue(
*reinterpret_cast<const int16_t *>(value_->data()), txn);
break;
case COLUMN_TYPE_INT:
e = Expr::newNumericValue(
*reinterpret_cast<const int32_t *>(value_->data()), txn);
break;
case COLUMN_TYPE_LONG:
e = Expr::newNumericValue(
*reinterpret_cast<const int64_t *>(value_->data()), txn);
break;
case COLUMN_TYPE_FLOAT:
e = Expr::newNumericValue(
*reinterpret_cast<const float *>(value_->data()), txn);
break;
case COLUMN_TYPE_DOUBLE:
e = Expr::newNumericValue(
*reinterpret_cast<const double *>(value_->data()), txn);
break;
case COLUMN_TYPE_TIMESTAMP:
e = Expr::newTimestampValue(
*reinterpret_cast<const Timestamp *>(value_->data()), txn);
break;
case COLUMN_TYPE_MICRO_TIMESTAMP:
e = Expr::newTimestampValue(
*reinterpret_cast<const MicroTimestamp *>(value_->data()), txn);
break;
case COLUMN_TYPE_NANO_TIMESTAMP:
e = Expr::newTimestampValue(
*reinterpret_cast<const NanoTimestamp *>(value_->data()), txn);
break;
case COLUMN_TYPE_GEOMETRY: {
Geometry *geom = getGeometry();
e = Expr::newGeometryValue(geom, txn);
} break;
default:
GS_THROW_USER_ERROR(GS_ERROR_TQ_CRITICAL_LOGIC_ERROR,
"Internal logic error: cannot determine column type.");
}
}
else {
Value *av;
av = QP_NEW Value();
av->copy(txn, objectManager, strategy, *value_);
e = Expr::newArrayValue(av, txn);
}
}
else {
e = QP_NEW Expr(txn);
e->type_ = type_;
e->op_ = op_;
if (label_) {
size_t len = strlen(label_) + 1;
e->label_ =
static_cast<char *>(txn.getDefaultAllocator().allocate(len));
memcpy(e->label_, label_, len);
}
assert(buf_ == NULL);
assert(value_ == NULL);
e->buf_ = NULL;
e->value_ = NULL;
e->columnInfo_ = columnInfo_;
e->columnId_ = columnId_;
e->columnType_ = columnType_;
e->functor_ = functor_;
e->geomCache_ = geomCache_;
if (arglist_ != NULL) {
e->arglist_ = QP_NEW ExprList(txn.getDefaultAllocator());
for (ExprList::const_iterator it = arglist_->begin();
it != arglist_->end(); it++) {
e->arglist_->insert(
e->arglist_->end(), (*it)->dup(txn, objectManager, strategy));
}
}
}
return e;
}
/*!
* @brief Common constructor
*/
void Expr::Init() {
type_ = VALUE;
label_ = NULL;
arglist_ = NULL;
value_ = NULL;
buf_ = NULL;
columnInfo_ = NULL;
columnId_ = 0;
functor_ = NULL;
columnType_ = COLUMN_TYPE_WITH_BEGIN;
geomCache_ = NULL;
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(bool v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(int8_t v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(int16_t v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(int32_t v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
* @param isTimestamp The int64 value is a timestamp
*/
Expr::Expr(int64_t v, TransactionContext &txn, bool isTimestamp) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
if (isTimestamp) {
value_->setTimestamp(v);
}
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(float v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param v Value
* @param txn The transaction context
*/
Expr::Expr(double v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
Expr::Expr(const MicroTimestamp &v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
Expr::Expr(const NanoTimestamp &v, TransactionContext &txn) {
Init();
type_ = VALUE;
value_ = QP_NEW Value(v);
}
/*!
* @brief ctor
*
* @param s String value
* @param txn The transaction context
* @param isLabel The string is a expression label
* @param needUnescape add escape character
*/
Expr::Expr(
const char *s, TransactionContext &txn, bool isLabel, bool needUnescape) {
Init();
if (isLabel) {
size_t len = strlen(s) + 1;
type_ = EXPRLABEL;
label_ = static_cast<char *>(txn.getDefaultAllocator().allocate(len));
memcpy(label_, s, len);
}
else {
util::String str(s, QP_ALLOCATOR);
if (needUnescape) {
unescape(str, '\'');
}
type_ = VALUE;
buf_ = QP_NEW util::XArray<uint8_t>(txn.getDefaultAllocator());
if (str.empty()) {
value_ =
QP_NEW Value(txn.getDefaultAllocator(), const_cast<char *>(""));
}
else {
value_ = QP_NEW Value(
txn.getDefaultAllocator(), const_cast<char *>(str.c_str()));
}
}
}
/*!
* @brief ctor
*
* @param s String value
* @param len String length
* @param txn The transaction context
* @param isLabel The string is a expression label
* @param needUnescape add escape character
*/
Expr::Expr(const char *s, size_t len, TransactionContext &txn, bool isLabel,
bool needUnescape) {
Init();
if (isLabel) {
type_ = EXPRLABEL;
label_ =
static_cast<char *>(txn.getDefaultAllocator().allocate(len + 1));
memcpy(label_, s, len);
label_[len] = '\0';
}
else {
util::String str(s, len, QP_ALLOCATOR);
if (needUnescape) {
unescape(str, '\'');
}
type_ = VALUE;
buf_ = QP_NEW util::XArray<uint8_t>(txn.getDefaultAllocator());
if (str.empty()) {
value_ =
QP_NEW Value(txn.getDefaultAllocator(), const_cast<char *>(""));
}
else {
value_ = QP_NEW Value(
txn.getDefaultAllocator(), const_cast<char *>(str.c_str()));
}
}
}
/*!
* @brief ctor of a column expression
*
* @param name Column name
* @param txn The transaction context
* @param columnId Column ID
* @param cInfo Column info
*/
Expr::Expr(const char *name, TransactionContext &txn, uint32_t columnId,
ColumnInfo *cInfo) {
Init();
type_ = COLUMN;
size_t len = strlen(name) + 1;
label_ = static_cast<char *>(txn.getDefaultAllocator().allocate(len));
memcpy(label_, name, len);
columnInfo_ = cInfo;
columnId_ = columnId;
if (cInfo != NULL) {
columnType_ = cInfo->getColumnType();
}
}
/*!
* @brief Ctor of binary operation
*
* @param op Operation
* @param arg1 argument1
* @param arg2 argument2
* @param txn The transaction context
*/
Expr::Expr(Operation op, Expr *arg1, Expr *arg2, TransactionContext &txn) {
Init();
type_ = EXPR;
op_ = op;
arglist_ = QP_NEW ExprList(txn.getDefaultAllocator());
arglist_->push_back(arg1);
You can’t perform that action at this time.
