-
Notifications
You must be signed in to change notification settings - Fork 40
/
lex.go
769 lines (696 loc) · 16.1 KB
/
lex.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
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
// Copyright 2023 Sneller, 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.
//go:generate goyacc partiql.y
//go:generate goimports -w y.go
//go:generate go run _generate/main.go -i keywords.txt -o lookup_gen.go
//go:generate go fmt lookup_gen.go
package partiql
import (
"bytes"
"fmt"
"io"
"math/big"
"strconv"
"time"
"github.com/SnellerInc/sneller/date"
"github.com/SnellerInc/sneller/expr"
)
const eof = -1
func init() {
expr.IsKeyword = func(x string) bool {
term, _ := lookupKeyword([]byte(x))
return term != -1
}
}
// used in testing
var faketime *expr.Timestamp
type scanner struct {
from []byte
pos int
err error
result *expr.Query
// notkw is set when
// we are not in keyword context
notkw bool
// the last symbol returned by `Lex`
lastsym int
// value of UTCNOW(); populated lazily
// (we need every instance of UTCNOW()
// to produce the same time exactly,
// so we can't call time.Now() more than once)
now *expr.Timestamp
}
func (s *scanner) utcnow() *expr.Timestamp {
if faketime != nil {
return faketime
}
if s.now == nil {
s.now = &expr.Timestamp{Value: date.Now().Truncate(time.Microsecond)}
}
return s.now
}
func (s *scanner) Err() error {
return s.err
}
// position determines a human-readable line and column coordinates
func (s *scanner) position(p int) (line int, column int, ok bool) {
if p > len(s.from) {
return 0, 0, false
}
buf := s.from
line = 1
column = 1
for len(buf) > 0 {
end := bytes.IndexByte(buf, '\n')
if end == -1 {
end = len(buf)
buf = buf[:0]
} else {
buf = buf[end+1:]
}
if p <= end {
// position in the current line or at the '\n'
column = p + 1
ok = true
return
}
column = end + 1
p -= end + 1
if p > 0 {
line++
}
}
return
}
// chomp whitespace from input
func (s *scanner) chompws() {
for s.pos < len(s.from) {
if isspace(s.from[s.pos]) {
s.notkw = false
s.pos++
} else {
const (
singleline = 1
multiline = 2
)
comment := 0
switch c := s.from[s.pos]; c {
case '#':
comment = singleline
s.pos += 1
case '-':
if s.pos+1 < len(s.from) && s.from[s.pos+1] == '-' {
comment = singleline
s.pos += 2
}
case '/':
if s.pos+1 < len(s.from) && s.from[s.pos+1] == '*' {
comment = multiline // don't alter s.pos here
}
case '*':
if s.pos+1 < len(s.from) && s.from[s.pos+1] == '/' {
s.err = s.mkerror(len("*/"), `unexpected "/" or end of multi-line comment`)
return
}
}
switch comment {
case 0:
return
case singleline:
p := bytes.IndexByte(s.from[s.pos:], '\n')
if p >= 0 {
s.pos += p + 1
} else {
s.pos = len(s.from)
}
case multiline:
s.multlinecomment()
if s.err != nil {
return
}
}
}
}
}
func (s *scanner) multlinecomment() {
stack := []int{s.pos} // a stack of open comments positions
s.pos += 2
for len(stack) > 0 {
if s.pos >= len(s.from) {
if len(stack) > 0 {
s.pos = stack[len(stack)-1] // move cursor to the last comment start
s.err = s.mkerror(len("/*"), "unterminated comment")
}
return
}
rest := s.from[s.pos:]
p := bytes.IndexAny(rest, "/*")
if p == -1 {
s.pos = len(s.from)
continue
}
s.pos += p
switch rest[p] {
case '*': // try to match '*/'
if p+1 < len(rest) && rest[p+1] == '/' {
stack = stack[:len(stack)-1]
s.pos += 2
} else {
s.pos += 1
}
case '/': // try to match '/*'
if p+1 < len(rest) && rest[p+1] == '*' {
stack = append(stack, s.pos)
s.pos += 2
} else {
s.pos += 1
}
}
}
}
func (s *scanner) peek() byte {
if s.pos == len(s.from) {
s.err = io.EOF
return 0
}
return s.from[s.pos]
}
func (s *scanner) peekat(i int) byte {
if s.pos+i < len(s.from) {
return s.from[s.pos+i]
}
return 0
}
func isdigit(x byte) bool {
return x >= '0' && x <= '9'
}
func isalpha(x byte) bool {
return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')
}
func isident(x byte) bool {
return isalpha(x) || isdigit(x) || x == '_' || x == '@'
}
func isspace(x byte) bool {
return x == ' ' || x == '\n' || x == '\t' || x == '\r' || x == '\f' || x == '\v'
}
func (s *scanner) Lex(l *yySymType) int {
s.lastsym = s.lex(l)
return s.lastsym
}
func (s *scanner) lex(l *yySymType) int {
if s.err != nil || s.pos >= len(s.from) {
return eof
}
s.chompws()
if s.err != nil || s.pos >= len(s.from) {
return eof
}
b := s.peek()
if isdigit(b) {
return s.lexNumber(l)
}
if b == '-' && isdigit(s.peekat(1)) {
if s.lastsym == NUMBER {
// the case: NUMBER-{digits} --- return the '-' operator
// ^^
// we're here
s.notkw = false
s.pos++
return '-'
}
return s.lexNumber(l)
}
if b == '.' && isdigit(s.peekat(1)) {
return s.lexNumber(l)
}
switch b {
case '\'':
return s.lexString(l)
case '"':
return s.lexQuotedIdent(l)
case '`':
return s.lexIon(l)
}
// NOTE: isident() accepts isdigit(),
// but due to the check above, we always
// parse words starting with a digit as a number
if isident(b) {
return s.lexIdent(l)
}
switch b {
case '=':
s.pos++
return EQ
case '!':
if s.peekat(1) == '=' {
s.pos += 2
return NE
}
s.pos++
return NOT
case '<':
if s.peekat(1) == '<' {
s.pos += 2
return SHIFT_LEFT_LOGICAL
}
if s.peekat(1) == '=' {
s.pos += 2
return LE
}
if s.peekat(1) == '>' {
s.pos += 2
return NE
}
s.pos++
return LT
case '>':
if s.peekat(1) == '>' {
if s.peekat(2) == '>' {
s.pos += 3
return SHIFT_RIGHT_LOGICAL
}
s.pos += 2
return SHIFT_RIGHT_ARITHMETIC
}
if s.peekat(1) == '=' {
s.pos += 2
return GE
}
s.pos++
return GT
case '.':
// if we encounter a dot,
// the text *immediately* following this
// cannot be a keyword
s.pos++
s.notkw = true
return int(b)
case '|':
if s.peekat(1) == '|' {
s.pos += 2
return CONCAT
}
s.notkw = false
s.pos++
return int(b)
case '+':
if s.peekat(1) == '+' {
s.pos += 2
return APPEND
}
s.notkw = false
s.pos++
return int(b)
case ',', '*', '-', '/', '%', ':', '&', '^', '[', ']', '(', ')', '{', '}':
// literal operators
s.notkw = false
s.pos++
return int(b)
case '~':
switch s.peekat(1) {
case '*':
s.pos += 2
return REGEXP_MATCH_CI
case '~':
if s.peekat(2) == '*' {
s.pos += 3
return ILIKE
}
s.pos += 2
return LIKE
}
s.pos++
return '~'
default:
s.err = s.mkerror(1, "unexpected character %q", b)
return ERROR
}
}
// issep returns whether x is a word separator
func issep(x byte) bool {
if isspace(x) {
return true
}
switch x {
case '(', ')', ',', '=', '<', '>', '!', '~':
return true
}
return false
}
// lex an identifier and either return it
// as an identifier or a keyword (if it matches one)
//
// as a bit of a hack, we use some state in the lexer
// to determine if a keyword could be present in the
// current lexical context (otherwise you can't have
// columns named 'outer' or 'join' or 'select', etc.)
func (s *scanner) lexIdent(l *yySymType) int {
startpos := s.pos
s.pos++
for s.pos < len(s.from) && isident(s.from[s.pos]) {
s.pos++
}
wordend := s.pos == len(s.from) || issep(s.from[s.pos])
if !s.notkw && wordend {
// don't perform string allocation if we have a keyword
term, enum := lookupKeyword(s.from[startpos:s.pos])
if term == AGGREGATE {
l.integer = enum
return AGGREGATE
} else if term != -1 {
// SQL keyword following AS or BY, interpret the
// next word as a case-sensitive identifier
if term == AS {
s.chompws()
s.notkw = true
}
return term
}
}
s.notkw = s.notkw || !wordend
l.str = string(s.from[startpos:s.pos])
return ID
}
// lexNumber lexes a number-like thing
// (NOTE: this is too permissive; we do the actual
// checking for valid numbers at parse time)
func (s *scanner) lexNumber(l *yySymType) int {
startpos := s.pos
floatnum := s.from[s.pos] == '.'
s.pos++
var prev byte
ok := func(x byte) bool {
switch x {
// white-space chars
case ' ', '\n', '\t', '\r', '\f', '\v':
return false
// operators
case '(', ')', '[', ']', '{', '}', '*', '/', '%', '&', '!', '^', '~', '|', ',':
return false
case '-', '+':
// it's might be a sign inside the engineering notation
esign := prev == 'e' || prev == 'E'
floatnum = floatnum || esign
return esign
case '.':
floatnum = true
}
return true
}
for s.pos < len(s.from) && ok(s.from[s.pos]) {
prev = s.from[s.pos]
s.pos++
}
// FIXME: don't allocate a string here
str := string(s.from[startpos:s.pos])
if !floatnum {
i, err := strconv.ParseInt(str, 0, 64)
if err == nil {
l.expr = expr.Integer(i)
return NUMBER
}
}
f, err := strconv.ParseFloat(str, 64)
if err == nil {
l.expr = expr.Float(f)
return NUMBER
}
r, okk := new(big.Rat).SetString(str)
if !okk {
s.err = err
return ERROR
}
// limit the amount of space this big.Rat can occupy
// FIXME: determine this before parsing it!
if !r.Num().IsInt64() || !r.Denom().IsInt64() {
s.err = s.mkerror(len(str), "text string %q produces a number out-of-range", str)
return ERROR
}
l.expr = (*expr.Rational)(r)
return NUMBER
}
func isprint(x byte) bool {
return x >= 32 && x < 127
}
func (s *scanner) lexQuotedIdent(l *yySymType) int {
startpos := s.pos
s.pos++ // skip leading '"'
ok := false
needquote := false
for ; s.pos < len(s.from); s.pos++ {
if s.from[s.pos] == '\\' {
needquote = true
s.pos++
continue
}
if !isprint(s.from[s.pos]) {
needquote = true
}
if s.from[s.pos] == '"' {
ok = true
break
}
}
if !ok {
s.err = io.ErrUnexpectedEOF
return ERROR
}
s.pos++
if !needquote {
l.str = string(s.from[startpos+1 : s.pos-1])
return ID
}
out, err := strconv.Unquote(string(s.from[startpos:s.pos]))
if err != nil {
s.err = err
return ERROR
}
l.str = out
return ID
}
func (s *scanner) lexString(l *yySymType) int {
s.pos++ // ignore starting character
startpos := s.pos
ok := false
needquote := false
for ; s.pos < len(s.from); s.pos++ {
if s.from[s.pos] == '\'' {
ok = true
break
}
if s.from[s.pos] == '\\' {
needquote = true
s.pos++
continue
}
if !isprint(s.from[s.pos]) {
needquote = true
}
}
if !ok {
s.err = io.ErrUnexpectedEOF
return ERROR
}
s.pos++ // ignore ending character
// fast-path for strings that don't need quoting:
// just produce the bytes directly
if !needquote {
l.str = string(s.from[startpos : s.pos-1])
return STRING
}
// otherwise, do the slow thing
out, err := expr.Unescape(s.from[startpos : s.pos-1])
if err != nil {
s.err = err
return ERROR
}
l.str = out
return STRING
}
func (s *scanner) lexIon(l *yySymType) int {
// TODO: support lexing an arbitrary
// textual ion datum; right now we only
// lex timestamps!
body := s.from[s.pos+1:]
end := bytes.IndexByte(body, '`')
if end == -1 {
s.err = s.mkerror(len(body), "unterminated ion datum literal, missing '`'")
return ERROR
}
t, ok := date.Parse(body[:end])
if !ok {
s.err = s.mkerror(end+2, "couldn't parse ion literal %s", s.from[s.pos:s.pos+end+2])
return ERROR
}
t = t.Truncate(time.Microsecond)
s.pos = s.pos + end + 2
l.expr = &expr.Timestamp{Value: t}
return ION
}
func toint(e expr.Node) (int, error) {
if i, ok := e.(expr.Integer); ok {
return int(i), nil
}
if f, ok := e.(expr.Float); ok {
if float64(int(f)) != float64(f) {
return 0, fmt.Errorf("cannot use %g as an index", float64(f))
}
return int(f), nil
}
// FIXME
r := (*big.Rat)(e.(*expr.Rational))
if !r.IsInt() || !r.Num().IsInt64() {
return 0, fmt.Errorf("integer out-of-range for indexing")
}
return int(r.Num().Int64()), nil
}
func (s *scanner) mkerror(length int, msg string, args ...any) *LexerError {
err := &LexerError{}
err.Message = fmt.Sprintf(msg, args...)
err.Position = s.pos
err.Length = length
err.Line, err.Column, _ = s.position(err.Position)
return err
}
func (s *scanner) Error(msg string) {
if s.err != nil {
return
}
s.err = s.mkerror(0, msg)
}
// LexerError describes a lexing error
type LexerError struct {
Position int // offset in the input string
Line int // line
Column int // column
Length int // length of wrong substring (0 if unknown)
Message string // textual description of an error
}
func (e *LexerError) Error() string {
if e.Line > 0 && e.Column > 0 {
return fmt.Sprintf("at %d:%d: %s", e.Line, e.Column, e.Message)
}
return fmt.Sprintf("at position %d: %s", e.Position, e.Message)
}
var exprstar = expr.Star{}
func toAggregate(op expr.AggregateOp, distinct bool, args []expr.Node, filter expr.Node, over *expr.Window) (*expr.Aggregate, error) {
agg, err := toAggregateAux(op, distinct, args, filter, over)
if err != nil {
return nil, fmt.Errorf("%v: %s", op, err)
}
return agg, nil
}
func toAggregateAux(op expr.AggregateOp, distinct bool, args []expr.Node, filter expr.Node, over *expr.Window) (*expr.Aggregate, error) {
var body expr.Node
if len(args) > 0 {
body = args[0]
args = args[1:]
}
if distinct {
if op == expr.OpCount {
op = expr.OpCountDistinct
}
if !op.AcceptDistinct() {
return nil, fmt.Errorf("does not accept DISTINCT")
}
}
if expr.Equal(body, exprstar) {
if !op.AcceptStar() {
return nil, fmt.Errorf("does not accept '*'")
}
} else {
if !op.AcceptExpression() {
return nil, fmt.Errorf("accepts only *")
}
}
switch op {
case expr.OpApproxCountDistinct:
return createApproxCountDistinct(body, args, filter, over)
case expr.OpApproxPercentile:
return createApproxPercentile(body, args, filter, over)
default:
if len(args) > 0 {
return nil, fmt.Errorf("does not accept arguments")
}
return &expr.Aggregate{Op: op, Inner: body, Over: over, Filter: filter}, nil
}
}
func createApproxCountDistinct(body expr.Node, args []expr.Node, filter expr.Node, over *expr.Window) (*expr.Aggregate, error) {
if len(args) > 1 {
return nil, fmt.Errorf("accepts at most 1 argument")
}
precision := expr.ApproxCountDistinctDefaultPrecision
if len(args) == 1 {
precisionExpr, ok := args[0].(expr.Integer)
if !ok {
return nil, fmt.Errorf("precision has to be a constant integer")
}
precision = int(precisionExpr)
if precision < expr.ApproxCountDistinctMinPrecision || precision > expr.ApproxCountDistinctMaxPrecision {
return nil, fmt.Errorf("precision has to be in range [%d, %d]",
expr.ApproxCountDistinctMinPrecision, expr.ApproxCountDistinctMaxPrecision)
}
}
return &expr.Aggregate{
Op: expr.OpApproxCountDistinct,
Precision: uint8(precision),
Inner: body,
Over: over,
Filter: filter}, nil
}
func createApproxPercentile(body expr.Node, args []expr.Node, filter expr.Node, over *expr.Window) (*expr.Aggregate, error) {
if len(args) != 1 {
return nil, fmt.Errorf("accepts 1 argument")
}
p, ok := args[0].(expr.Float)
if !ok {
return nil, fmt.Errorf("percentile p=%v has to be floating point", args[0])
}
if p < 0.0 || p > 1.0 {
return nil, fmt.Errorf("percentile p=%v has to be in range [0.0, 1.0]", p)
}
return &expr.Aggregate{
Op: expr.OpApproxPercentile,
Misc: float32(p),
Inner: body,
Over: over,
Filter: filter}, nil
}
func createCase(optionalExpr expr.Node, limbs []expr.CaseLimb, elseExpr expr.Node) expr.Node {
if optionalExpr != nil {
// "simplified" CASE
for i := range limbs {
limbs[i].When = expr.Compare(expr.Equals, optionalExpr, limbs[i].When)
}
}
return &expr.Case{
Limbs: limbs,
Else: elseExpr,
}
}
func parseExplain(s string) (expr.ExplainFormat, error) {
switch s {
case "":
return expr.ExplainNone, nil
case "default":
return expr.ExplainDefault, nil
case "text":
return expr.ExplainText, nil
case "list":
return expr.ExplainList, nil
case "gv", "graphviz":
return expr.ExplainGraphviz, nil
}
return expr.ExplainNone, fmt.Errorf("%q is a wrong explain type", s)
}