forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
659 lines (544 loc) · 14.8 KB
/
parser.go
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
package parser
// #include "query_types.h"
// #include <stdlib.h>
import "C"
import (
"common"
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"time"
"unsafe"
)
type From struct {
TableName string
}
type Operation int
type ValueType int
const (
ValueRegex ValueType = C.VALUE_REGEX
ValueInt = C.VALUE_INT
ValueFloat = C.VALUE_FLOAT
ValueString = C.VALUE_STRING
ValueIntoName = C.VALUE_INTO_NAME
ValueTableName = C.VALUE_TABLE_NAME
ValueSimpleName = C.VALUE_SIMPLE_NAME
ValueDuration = C.VALUE_DURATION
ValueWildcard = C.VALUE_WILDCARD
ValueFunctionCall = C.VALUE_FUNCTION_CALL
ValueExpression = C.VALUE_EXPRESSION
)
type Value struct {
Name string
Type ValueType
Elems []*Value
compiledRegex *regexp.Regexp
}
func (self *Value) IsFunctionCall() bool {
return self.Type == ValueFunctionCall
}
func (self *Value) GetCompiledRegex() (*regexp.Regexp, bool) {
return self.compiledRegex, self.Type == ValueRegex
}
type FromClauseType int
const (
FromClauseArray FromClauseType = C.FROM_ARRAY
FromClauseMerge FromClauseType = C.FROM_MERGE
FromClauseInnerJoin FromClauseType = C.FROM_INNER_JOIN
)
type TableName struct {
Name *Value
Alias string
}
func (self *TableName) GetAlias() string {
if self.Alias != "" {
return self.Alias
}
return self.Name.Name
}
type FromClause struct {
Type FromClauseType
Names []*TableName
}
type IntoClause struct {
Target *Value
}
type GroupByClause struct {
FillWithZero bool
FillValue *Value
Elems []*Value
}
func (self GroupByClause) GetGroupByTime() (*time.Duration, error) {
for _, groupBy := range self.Elems {
if groupBy.IsFunctionCall() {
// TODO: check the number of arguments and return an error
if len(groupBy.Elems) != 1 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "time function only accepts one argument")
}
// TODO: check the function name
// TODO: error checking
arg := groupBy.Elems[0].Name
durationInt, err := common.ParseTimeDuration(arg)
if err != nil {
return nil, common.NewQueryError(common.InvalidArgument, fmt.Sprintf("invalid argument %s to the time function", arg))
}
duration := time.Duration(durationInt)
return &duration, nil
}
}
return nil, nil
}
type WhereCondition struct {
isBooleanExpression bool
Left interface{}
Operation string
Right *WhereCondition
}
func (self *WhereCondition) GetBoolExpression() (*Value, bool) {
if self.isBooleanExpression {
return self.Left.(*Value), true
}
return nil, false
}
func (self *WhereCondition) GetLeftWhereCondition() (*WhereCondition, bool) {
if !self.isBooleanExpression {
return self.Left.(*WhereCondition), true
}
return nil, false
}
type BasicQuery struct {
queryString string
startTime time.Time
endTime time.Time
}
type SelectDeleteCommonQuery struct {
BasicQuery
FromClause *FromClause
Condition *WhereCondition
endTimeSet bool
}
type SelectQuery struct {
SelectDeleteCommonQuery
ColumnNames []*Value
groupByClause *GroupByClause
IntoClause *IntoClause
Limit int
Ascending bool
}
type ListType int
const (
Series ListType = iota
ContinuousQueries
)
type ListQuery struct {
Type ListType
}
type DropQuery struct {
Id int
}
type DropSeriesQuery struct {
tableName string
}
func (self *DropSeriesQuery) GetTableName() string {
return self.tableName
}
type DeleteQuery struct {
SelectDeleteCommonQuery
}
type Query struct {
QueryString string
SelectQuery *SelectQuery
DeleteQuery *DeleteQuery
ListQuery *ListQuery
DropSeriesQuery *DropSeriesQuery
DropQuery *DropQuery
}
func (self *Query) GetQueryString() string {
if self.SelectQuery != nil {
return self.SelectQuery.GetQueryString()
} else if self.ListQuery != nil {
return "list series"
} else if self.DeleteQuery != nil {
return self.DeleteQuery.GetQueryString()
}
return self.QueryString
}
func (self *Query) IsListQuery() bool {
return self.ListQuery != nil
}
func (self *Query) IsListSeriesQuery() bool {
return self.ListQuery != nil && self.ListQuery.Type == Series
}
func (self *Query) IsListContinuousQueriesQuery() bool {
return self.ListQuery != nil && self.ListQuery.Type == ContinuousQueries
}
func (self *BasicQuery) GetQueryString() string {
return self.queryString
}
func (self *SelectQuery) GetColumnNames() []*Value {
return self.ColumnNames
}
func (self *SelectQuery) IsSinglePointQuery() bool {
w := self.GetWhereCondition()
if w == nil {
return false
}
leftWhereCondition, ok := w.GetLeftWhereCondition()
if !ok {
return false
}
leftBoolExpression, ok := leftWhereCondition.GetBoolExpression()
if !ok {
return false
}
rightBoolExpression, ok := w.Right.GetBoolExpression()
if !ok {
return false
}
if leftBoolExpression.Name != "=" && rightBoolExpression.Name != "=" {
return false
}
if leftBoolExpression.Elems[0].Name != "time" || rightBoolExpression.Elems[0].Name != "sequence_number" {
return false
}
return true
}
func (self *SelectQuery) GetSinglePointQuerySequenceNumber() (int64, error) {
w := self.GetWhereCondition()
rightBoolExpression, _ := w.Right.GetBoolExpression()
sequence := rightBoolExpression.Elems[1].Name
sequence_number, err := strconv.ParseInt(sequence, 10, 64)
if err != nil {
return 0, fmt.Errorf("The column sequence_number can only be queried as an integer.")
}
return sequence_number, nil
}
func (self *SelectQuery) IsContinuousQuery() bool {
return self.GetIntoClause() != nil
}
func (self *SelectQuery) IsValidContinuousQuery() bool {
groupByClause := self.GetGroupByClause()
if len(groupByClause.Elems) == 0 {
return true
}
for _, elem := range groupByClause.Elems {
if elem.Name == "time" {
return true
}
}
return false
}
func (self *SelectQuery) GetIntoClause() *IntoClause {
return self.IntoClause
}
func (self *SelectDeleteCommonQuery) GetFromClause() *FromClause {
return self.FromClause
}
func setupSlice(hdr *reflect.SliceHeader, ptr unsafe.Pointer, size C.size_t) {
hdr.Cap = int(size)
hdr.Len = int(size)
hdr.Data = uintptr(ptr)
}
func GetGroupByClause(groupByClause *C.groupby_clause) (*GroupByClause, error) {
if groupByClause == nil {
return &GroupByClause{Elems: nil}, nil
}
values, err := GetValueArray(groupByClause.elems)
if err != nil {
return nil, err
}
fillWithZero := false
var fillValue *Value
if groupByClause.fill_function != nil {
fun, err := GetValue(groupByClause.fill_function)
if err != nil {
return nil, err
}
if fun.Name != "fill" {
return nil, fmt.Errorf("You can't use %s with group by", fun.Name)
}
if len(fun.Elems) != 1 {
return nil, fmt.Errorf("`fill` accepts one argument only")
}
fillValue = fun.Elems[0]
fillWithZero = true
}
return &GroupByClause{
Elems: values,
FillWithZero: fillWithZero,
FillValue: fillValue,
}, nil
}
func GetValueArray(array *C.value_array) ([]*Value, error) {
if array == nil {
return nil, nil
}
var values []*C.value
setupSlice((*reflect.SliceHeader)((unsafe.Pointer(&values))), unsafe.Pointer(array.elems), array.size)
valuesSlice := make([]*Value, 0, array.size)
for _, value := range values {
value, err := GetValue(value)
if err != nil {
return nil, err
}
valuesSlice = append(valuesSlice, value)
}
return valuesSlice, nil
}
func GetStringArray(array *C.array) []string {
if array == nil {
return nil
}
var values []*C.char
setupSlice((*reflect.SliceHeader)((unsafe.Pointer(&values))), unsafe.Pointer(array.elems), array.size)
stringSlice := make([]string, 0, array.size)
for _, value := range values {
stringSlice = append(stringSlice, C.GoString(value))
}
return stringSlice
}
func GetValue(value *C.value) (*Value, error) {
v := &Value{}
v.Name = C.GoString(value.name)
var err error
v.Elems, err = GetValueArray(value.args)
if err != nil {
return nil, err
}
v.Type = ValueType(value.value_type)
isCaseInsensitive := value.is_case_insensitive != 0
if v.Type == ValueRegex {
if isCaseInsensitive {
v.compiledRegex, err = regexp.Compile("(?i)" + v.Name)
} else {
v.compiledRegex, err = regexp.Compile(v.Name)
}
}
return v, err
}
func GetTableName(name *C.table_name) (*TableName, error) {
value, err := GetValue(name.name)
if err != nil {
return nil, err
}
table := &TableName{Name: value}
if name.alias != nil {
table.Alias = C.GoString(name.alias)
}
return table, nil
}
func GetTableNameArray(array *C.table_name_array) ([]*TableName, error) {
var names []*C.table_name
setupSlice((*reflect.SliceHeader)((unsafe.Pointer(&names))), unsafe.Pointer(array.elems), array.size)
tableNamesSlice := make([]*TableName, 0, array.size)
for _, name := range names {
tableName, err := GetTableName(name)
if err != nil {
return nil, err
}
tableNamesSlice = append(tableNamesSlice, tableName)
}
return tableNamesSlice, nil
}
func GetFromClause(fromClause *C.from_clause) (*FromClause, error) {
arr, err := GetTableNameArray(fromClause.names)
if err != nil {
return nil, err
}
return &FromClause{FromClauseType(fromClause.from_clause_type), arr}, nil
}
func GetIntoClause(intoClause *C.into_clause) (*IntoClause, error) {
if intoClause == nil {
return nil, nil
}
target, err := GetValue(intoClause.target)
if err != nil {
return nil, err
}
return &IntoClause{target}, nil
}
func GetWhereCondition(condition *C.condition) (*WhereCondition, error) {
if condition.is_bool_expression != 0 {
expr, err := GetValue((*C.value)(condition.left))
if err != nil {
return nil, err
}
return &WhereCondition{
isBooleanExpression: true,
Left: expr,
Operation: "",
Right: nil,
}, nil
}
c := &WhereCondition{}
var err error
c.Left, err = GetWhereCondition((*C.condition)(condition.left))
if err != nil {
return nil, err
}
c.Operation = C.GoString(condition.op)
c.Right, err = GetWhereCondition((*C.condition)(unsafe.Pointer(condition.right)))
return c, err
}
func (self *SelectDeleteCommonQuery) GetWhereCondition() *WhereCondition {
return self.Condition
}
func (self *SelectQuery) GetGroupByClause() *GroupByClause {
return self.groupByClause
}
// This is just for backward compatability so we don't have
// to change all the code.
func ParseSelectQuery(query string) (*SelectQuery, error) {
queries, err := ParseQuery(query)
if err != nil {
return nil, err
}
if len(queries) == 0 {
return nil, fmt.Errorf("No queries found")
}
selectQuery := queries[0].SelectQuery
if selectQuery == nil {
return nil, fmt.Errorf("Query isn't a select query: '%s'", queries[0].GetQueryString())
}
return selectQuery, nil
}
func ParseQuery(query string) ([]*Query, error) {
queryString := C.CString(query)
defer C.free(unsafe.Pointer(queryString))
q := C.parse_query(queryString)
defer C.close_query(&q)
if q.error != nil {
str := C.GoString(q.error.err)
err := fmt.Errorf("Error at %d:%d. %s", q.error.line, q.error.column, str)
return nil, err
}
if q.list_series_query != 0 {
return []*Query{&Query{QueryString: query, ListQuery: &ListQuery{Type: Series}}}, nil
}
if q.list_continuous_queries_query != 0 {
return []*Query{&Query{QueryString: query, ListQuery: &ListQuery{Type: ContinuousQueries}}}, nil
}
if q.select_query != nil {
selectQuery, err := parseSelectQuery(query, q.select_query)
if err != nil {
return nil, err
}
return []*Query{&Query{QueryString: query, SelectQuery: selectQuery}}, nil
} else if q.delete_query != nil {
deleteQuery, err := parseDeleteQuery(query, q.delete_query)
if err != nil {
return nil, err
}
return []*Query{&Query{QueryString: query, DeleteQuery: deleteQuery}}, nil
} else if q.drop_series_query != nil {
dropSeriesQuery, err := parseDropSeriesQuery(query, q.drop_series_query)
if err != nil {
return nil, err
}
return []*Query{&Query{QueryString: query, DropSeriesQuery: dropSeriesQuery}}, nil
} else if q.drop_query != nil {
return []*Query{&Query{QueryString: query, DropQuery: &DropQuery{Id: int(q.drop_query.id)}}}, nil
}
return nil, fmt.Errorf("Unknown query type encountered")
}
func parseDropSeriesQuery(queryStirng string, dropSeriesQuery *C.drop_series_query) (*DropSeriesQuery, error) {
name, err := GetValue(dropSeriesQuery.name)
if err != nil {
return nil, err
}
return &DropSeriesQuery{
tableName: name.Name,
}, nil
}
func parseSelectDeleteCommonQuery(queryString string, fromClause *C.from_clause, whereCondition *C.condition) (SelectDeleteCommonQuery, error) {
goQuery := SelectDeleteCommonQuery{
BasicQuery: BasicQuery{
queryString: queryString,
startTime: time.Unix(0, 0),
endTime: time.Now(),
},
}
var err error
// get the from clause
goQuery.FromClause, err = GetFromClause(fromClause)
if err != nil {
return goQuery, err
}
// get the where condition
if whereCondition != nil {
goQuery.Condition, err = GetWhereCondition(whereCondition)
if err != nil {
return goQuery, err
}
}
var startTime, endTime time.Time
goQuery.Condition, endTime, err = getTime(goQuery.GetWhereCondition(), false)
if err != nil {
return goQuery, err
}
goQuery.endTimeSet = endTime.Unix() > 0
if goQuery.endTimeSet {
goQuery.endTime = endTime
}
goQuery.Condition, startTime, err = getTime(goQuery.GetWhereCondition(), true)
if err != nil {
return goQuery, err
}
if startTime.Unix() > 0 {
goQuery.startTime = startTime
} else if goQuery.endTime.Unix() > 0 {
goQuery.startTime = time.Unix(math.MinInt64, 0)
}
return goQuery, nil
}
func parseSelectQuery(queryString string, q *C.select_query) (*SelectQuery, error) {
limit := q.limit
if limit == -1 {
// no limit by default
limit = 0
}
basicQuery, err := parseSelectDeleteCommonQuery(queryString, q.from_clause, q.where_condition)
if err != nil {
return nil, err
}
goQuery := &SelectQuery{
SelectDeleteCommonQuery: basicQuery,
Limit: int(limit),
Ascending: q.ascending != 0,
}
// get the column names
goQuery.ColumnNames, err = GetValueArray(q.c)
if err != nil {
return nil, err
}
// get the group by clause
if q.group_by == nil {
goQuery.groupByClause = &GroupByClause{}
} else {
goQuery.groupByClause, err = GetGroupByClause(q.group_by)
if err != nil {
return nil, err
}
}
// get the into clause
goQuery.IntoClause, err = GetIntoClause(q.into_clause)
if err != nil {
return goQuery, err
}
return goQuery, nil
}
func parseDeleteQuery(queryString string, query *C.delete_query) (*DeleteQuery, error) {
basicQuery, err := parseSelectDeleteCommonQuery(queryString, query.from_clause, query.where_condition)
if err != nil {
return nil, err
}
goQuery := &DeleteQuery{
SelectDeleteCommonQuery: basicQuery,
}
if basicQuery.GetWhereCondition() != nil {
return nil, fmt.Errorf("Delete queries can't have where clause that don't reference time")
}
return goQuery, nil
}