This repository was archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathParseBasicExpressions.hpp
More file actions
595 lines (505 loc) · 16.9 KB
/
ParseBasicExpressions.hpp
File metadata and controls
595 lines (505 loc) · 16.9 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
/**
* Copyright 2011-2015 Quickstep Technologies LLC.
* Copyright 2015 Pivotal Software, Inc.
* Copyright 2016, Quickstep Research Group, Computer Sciences Department,
* University of Wisconsin—Madison.
*
* 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.
**/
#ifndef QUICKSTEP_PARSER_PARSE_BASIC_EXPRESSIONS_HPP_
#define QUICKSTEP_PARSER_PARSE_BASIC_EXPRESSIONS_HPP_
#include <cstddef>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "parser/ParseExpression.hpp"
#include "parser/ParseLiteralValue.hpp"
#include "parser/ParseString.hpp"
#include "parser/ParseTreeNode.hpp"
#include "utility/Macros.hpp"
#include "utility/PtrList.hpp"
namespace quickstep {
class BinaryOperation;
class UnaryOperation;
/** \addtogroup Parser
* @{
*/
/**
* @brief The parsed representation of a literal value.
**/
class ParseScalarLiteral : public ParseExpression {
public:
/**
* @brief Constructor.
*
* @param literal_value The parsed literal value to represent, which becomes
* owned by this ParseScalarLiteral.
**/
explicit ParseScalarLiteral(ParseLiteralValue *literal_value)
: ParseExpression(literal_value->line_number(),
literal_value->column_number()),
literal_value_(literal_value) {
}
/**
* @brief Destructor.
*/
~ParseScalarLiteral() override {
}
/**
* @brief Gets the parsed literal value.
*
* @return The parsed literal value.
*/
const ParseLiteralValue* literal_value() const {
return literal_value_.get();
}
ExpressionType getExpressionType() const override {
return kScalarLiteral;
}
std::string getName() const override {
return "Literal";
}
std::string generateName() const override {
return literal_value_->generateName();
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
std::unique_ptr<ParseLiteralValue> literal_value_;
DISALLOW_COPY_AND_ASSIGN(ParseScalarLiteral);
};
/**
* @brief The parsed representation of an attribute reference.
**/
class ParseAttribute : public ParseExpression {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param attr_name The name of the attribute.
* @param rel_name An optional relation name to qualify the attribute name
* (by default, all relations in the FROM list will be searched for
* the attribute).
**/
ParseAttribute(const int line_number,
const int column_number,
ParseString *attr_name,
ParseString *rel_name = nullptr)
: ParseExpression(line_number, column_number),
attr_name_(attr_name),
rel_name_(rel_name) {
}
/**
* @brief Destructor.
*/
~ParseAttribute() override {}
ExpressionType getExpressionType() const override {
return kAttribute;
}
std::string getName() const override {
return "AttributeReference";
}
/**
* @return Attribute name.
*/
const ParseString* attr_name() const {
return attr_name_.get();
}
/**
* @return Relation name.
*/
const ParseString* rel_name() const {
return rel_name_.get();
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
std::unique_ptr<ParseString> attr_name_, rel_name_;
DISALLOW_COPY_AND_ASSIGN(ParseAttribute);
};
/**
* @brief The parsed representation of an unary operation applied to an expression.
**/
class ParseUnaryExpression : public ParseExpression {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the first token of this node in the SQL statement.
* @param column_number Column number of the first token of this node in the SQL statement.
* @param op The UnaryOperation from the quickstep type system to apply.
* @param operand The parsed scalar representation of the unary operation's
* argument, which becomes owned by this ParseScalarUnaryExpression.
**/
ParseUnaryExpression(const int line_number,
const int column_number,
const UnaryOperation &op,
ParseExpression *operand)
: ParseExpression(line_number, column_number),
op_(op),
operand_(operand) {
}
/**
* @brief Destructor.
*/
~ParseUnaryExpression() override {
}
ExpressionType getExpressionType() const override {
return kUnaryExpression;
}
std::string getName() const override;
/**
* @return The unary operation.
*/
const UnaryOperation& op() const {
return op_;
}
/**
* @return The operand expression.
*/
const ParseExpression* operand() const {
return operand_.get();
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
const UnaryOperation &op_;
std::unique_ptr<ParseExpression> operand_;
DISALLOW_COPY_AND_ASSIGN(ParseUnaryExpression);
};
/**
* @brief The parsed representation of a binary operation applied to two
* expressions.
**/
class ParseBinaryExpression : public ParseExpression {
public:
/**
* @brief Constructor.
*
* @param line_number Line number of the binary operator token in the SQL statement.
* @param column_number Column number of the binary operator token in the SQL statement.
* @param op The BinaryOperation from the quickstep type system to apply.
* @param left_operand The parsed scalar representation of the binary
* operation's left argument, which becomes owned by this
* ParseScalarBinaryExpression.
* @param right_operand The parsed scalar representation of the binary
* operation's right argument, which becomes owned by this
* ParseScalarBinaryExpression.
**/
ParseBinaryExpression(const int line_number,
const int column_number,
const BinaryOperation &op,
ParseExpression *left_operand,
ParseExpression *right_operand)
: ParseExpression(line_number, column_number),
op_(op),
left_operand_(left_operand),
right_operand_(right_operand) {
}
/**
* @brief Destructor.
*/
~ParseBinaryExpression() override {
}
ExpressionType getExpressionType() const override {
return kBinaryExpression;
}
std::string getName() const override;
/**
* @return The binary operation.
*/
const BinaryOperation& op() const {
return op_;
}
/**
* @return The left operand expression.
*/
const ParseExpression* left_operand() const {
return left_operand_.get();
}
/**
* @return The right operand expression.
*/
const ParseExpression* right_operand() const {
return right_operand_.get();
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
const BinaryOperation &op_;
std::unique_ptr<ParseExpression> left_operand_;
std::unique_ptr<ParseExpression> right_operand_;
DISALLOW_COPY_AND_ASSIGN(ParseBinaryExpression);
};
/**
* @brief The parsed representation of '*' as a function argument.
*/
class ParseStar : public ParseTreeNode {
public:
ParseStar(const int line_number, const int column_number)
: ParseTreeNode(line_number, column_number) {}
std::string getName() const override {
return "Star";
}
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override {}
private:
DISALLOW_COPY_AND_ASSIGN(ParseStar);
};
/**
* @brief Parsed function call in the form of a name with a list of arguments in parentheses.
*/
class ParseFunctionCall : public ParseExpression {
public:
/**
* @brief Constructor.
* @note Takes ownership of all pointers.
*
* @param line_number The line number of the first token of the function call.
* @param column_number The column number of the first token of the first call.
* @param is_distinct Whether this function call contains the DISTINCT keyword.
* @param name The function name.
* @param arguments The function argument list.
*/
ParseFunctionCall(const int line_number,
const int column_number,
const bool is_distinct,
ParseString *name,
PtrList<ParseExpression> *arguments)
: ParseExpression(line_number, column_number),
is_distinct_(is_distinct),
name_(name),
arguments_(arguments) {
}
/**
* @brief Constructor for a function call that has "*" as the argument.
* @note Takes ownership of all pointers.
*
* @param line_number The line number of the first token of the function call.
* @param column_number The column number of the first token of the function call.
* @param name The function name.
* @param star The parsed star.
*/
ParseFunctionCall(const int line_number, const int column_number, ParseString *name, ParseStar *star)
: ParseExpression(line_number, column_number),
is_distinct_(false),
name_(name),
star_(star) {
}
/**
* @brief Destructor.
*/
~ParseFunctionCall() override {
}
ExpressionType getExpressionType() const override {
return kFunctionCall;
}
std::string getName() const override {
return "FunctionCall";
}
/**
* @return Whether this function call contains the DISTINCT keyword.
*/
bool is_distinct() const {
return is_distinct_;
}
/**
* @return The function name.
*/
const ParseString* name() const {
return name_.get();
}
/**
* @return The argument list.
*/
const PtrList<ParseExpression>* arguments() const {
return arguments_.get();
}
/**
* @return The parsed star.
*/
const ParseStar* star() const {
return star_.get();
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
const bool is_distinct_;
std::unique_ptr<ParseString> name_;
// Either <arguments_> or <star_> is NULL.
std::unique_ptr<PtrList<ParseExpression>> arguments_;
std::unique_ptr<ParseStar> star_;
DISALLOW_COPY_AND_ASSIGN(ParseFunctionCall);
};
/**
* @brief Parsed representation of EXTRACT(unit FROM date).
*/
class ParseExtractFunction : public ParseExpression {
public:
/**
* @brief Constructor.
*
* @param line_number The line number of the token "extract" in the statement.
* @param column_number The column number of the token "extract in the statement.
* @param extract_field The field to extract.
* @param source_expression The expression to extract a field from.
*/
ParseExtractFunction(const int line_number,
const int column_number,
ParseString *extract_field,
ParseExpression *date_expression)
: ParseExpression(line_number, column_number),
extract_field_(extract_field),
date_expression_(date_expression) {
}
ExpressionType getExpressionType() const override {
return kExtract;
}
std::string getName() const override {
return "Extract";
}
/**
* @return The field to extract.
*/
const ParseString* extract_field() const {
return extract_field_.get();
}
/**
* @return The expression to extract a field from.
*/
const ParseExpression* date_expression() const {
return date_expression_.get();
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
std::unique_ptr<ParseString> extract_field_;
std::unique_ptr<ParseExpression> date_expression_;
DISALLOW_COPY_AND_ASSIGN(ParseExtractFunction);
};
/**
* @brief Parsed representation of the substring function.
*/
class ParseSubstringFunction : public ParseExpression {
public:
static constexpr std::size_t kDefaultLength = std::numeric_limits<std::size_t>::max();
/**
* @brief Constructor.
*
* @param line_number The line number of the first token of the function call.
* @param column_number The column number of the first token of the function call.
* @param operand The operand of the substring.
* @param start_position The 1-based starting position of the substring.
* @param length Optional substring length.
*/
ParseSubstringFunction(const int line_number,
const int column_number,
ParseExpression *operand,
const std::size_t start_position,
const std::size_t length = kDefaultLength)
: ParseExpression(line_number, column_number),
operand_(operand),
start_position_(start_position),
length_(length) {}
ExpressionType getExpressionType() const override {
return kSubstring;
}
std::string getName() const override {
return "Substring";
}
/**
* @return The operand of the substring.
*/
const ParseExpression* operand() const {
return operand_.get();
}
/**
* @return The 1-based starting position of the substring.
*/
std::size_t start_position() const {
return start_position_;
}
/**
* @return Then substring length.
*/
std::size_t length() const {
return length_;
}
std::string generateName() const override;
protected:
void getFieldStringItems(
std::vector<std::string> *inline_field_names,
std::vector<std::string> *inline_field_values,
std::vector<std::string> *non_container_child_field_names,
std::vector<const ParseTreeNode*> *non_container_child_fields,
std::vector<std::string> *container_child_field_names,
std::vector<std::vector<const ParseTreeNode*>> *container_child_fields) const override;
private:
std::unique_ptr<ParseExpression> operand_;
const std::size_t start_position_;
const std::size_t length_;
DISALLOW_COPY_AND_ASSIGN(ParseSubstringFunction);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_PARSER_PARSE_BASIC_EXPRESSIONS_HPP_