Skip to content

Commit 5116dd2

Browse files
committed
sql/parser: fix minor issue with recent LTREE additions
We recently added parser support for FIRST_CONTAINS and FIRST_CONTAINED_BY operators (`?@>` and `?<@` respectively). The AI review pointed out a minor bug in how we handle these operators in the scanner - we incorrectly advanced the position unconditionally before matching the following characters. Instead, we need to use `peekN` to look a few characters ahead to see whether we have a match, and this commit does that. Note that the impact of the bug is very minor - we simply would put the `^` character in the error in the wrong position. Note that two newly-added tests have slightly different behavior with regards to the `^` character which seems to be due to peculiarities of the tokenizing / parsing that I didn't look closely into. Additionally, this commit adds a newline character in an unrelated parser test file. Release note: None
1 parent 8b1e1a9 commit 5116dd2

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

pkg/sql/parser/testdata/changefeed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@ CREATE CHANGEFEED FOR database INTO 'foo'
177177
at or near "into": syntax error
178178
DETAIL: source SQL:
179179
CREATE CHANGEFEED FOR database INTO 'foo'
180-
^
180+
^

pkg/sql/parser/testdata/select_exprs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,10 +2171,26 @@ SELECT (((ARRAY[('A.B'), ('')])::LTREE[]) ?@> (('A')::LTREE)) -- fully parenthes
21712171
SELECT ARRAY['_', '_']::LTREE[] ?@> '_'::LTREE -- literals removed
21722172
SELECT ARRAY['A.B', '']::LTREE[] ?@> 'A'::LTREE -- identifiers removed
21732173

2174+
error
2175+
SELECT ARRAY['A.B', '']::LTREE[] ?@< 'A'::LTREE
2176+
----
2177+
at or near "<": syntax error
2178+
DETAIL: source SQL:
2179+
SELECT ARRAY['A.B', '']::LTREE[] ?@< 'A'::LTREE
2180+
^
2181+
21742182
parse
21752183
SELECT ARRAY['A.B', '']::LTREE[] ?<@ 'A'::LTREE
21762184
----
21772185
SELECT ARRAY['A.B', '']::LTREE[] ?<@ 'A'::LTREE
21782186
SELECT (((ARRAY[('A.B'), ('')])::LTREE[]) ?<@ (('A')::LTREE)) -- fully parenthesized
21792187
SELECT ARRAY['_', '_']::LTREE[] ?<@ '_'::LTREE -- literals removed
21802188
SELECT ARRAY['A.B', '']::LTREE[] ?<@ 'A'::LTREE -- identifiers removed
2189+
2190+
error
2191+
SELECT ARRAY['A.B', '']::LTREE[] ?<< 'A'::LTREE
2192+
----
2193+
at or near "<": syntax error
2194+
DETAIL: source SQL:
2195+
SELECT ARRAY['A.B', '']::LTREE[] ?<< 'A'::LTREE
2196+
^

pkg/sql/scanner/scan.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,18 +311,16 @@ func (s *SQLScanner) Scan(lval ScanSymType) {
311311
lval.SetID(lexbase.JSON_ALL_EXISTS)
312312
return
313313
case '@':
314-
s.pos++
315-
switch s.peek() {
314+
switch s.peekN(1) {
316315
case '>': // ?@>
317-
s.pos++
316+
s.pos += 2
318317
lval.SetID(lexbase.FIRST_CONTAINS)
319318
return
320319
}
321320
case '<':
322-
s.pos++
323-
switch s.peek() {
321+
switch s.peekN(1) {
324322
case '@': // ?<@
325-
s.pos++
323+
s.pos += 2
326324
lval.SetID(lexbase.FIRST_CONTAINED_BY)
327325
return
328326
}

0 commit comments

Comments
 (0)