Skip to content

Commit 63d8f41

Browse files
authored
Fix(parser)!: treat NEXT as a func keyword, parse NEXT VALUE FOR in tsql, oracle (#4467)
1 parent 07fa69d commit 63d8f41

File tree

6 files changed

+24
-4
lines changed

6 files changed

+24
-4
lines changed

sqlglot/dialects/oracle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class Parser(parser.Parser):
128128

129129
NO_PAREN_FUNCTION_PARSERS = {
130130
**parser.Parser.NO_PAREN_FUNCTION_PARSERS,
131+
"NEXT": lambda self: self._parse_next_value_for(),
131132
"SYSDATE": lambda self: self.expression(exp.CurrentTimestamp, sysdate=True),
132133
}
133134

sqlglot/dialects/tsql.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ class Parser(parser.Parser):
597597
),
598598
}
599599

600+
NO_PAREN_FUNCTION_PARSERS = {
601+
**parser.Parser.NO_PAREN_FUNCTION_PARSERS,
602+
"NEXT": lambda self: self._parse_next_value_for(),
603+
}
604+
600605
# The DCOLON (::) operator serves as a scope resolution (exp.ScopeResolution) operator in T-SQL
601606
COLUMN_OPERATORS = {
602607
**parser.Parser.COLUMN_OPERATORS,

sqlglot/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class Parser(metaclass=_Parser):
586586
TokenType.INSERT,
587587
TokenType.LIKE,
588588
TokenType.MERGE,
589+
TokenType.NEXT,
589590
TokenType.OFFSET,
590591
TokenType.PRIMARY_KEY,
591592
TokenType.RANGE,
@@ -1107,7 +1108,6 @@ class Parser(metaclass=_Parser):
11071108
exp.ConnectByRoot, this=self._parse_column()
11081109
),
11091110
"IF": lambda self: self._parse_if(),
1110-
"NEXT": lambda self: self._parse_next_value_for(),
11111111
}
11121112

11131113
INVALID_FUNC_NAME_TOKENS = {

tests/dialects/test_presto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,8 @@ def test_match_recognize(self):
12031203
DEFINE
12041204
B AS totalprice < PREV(totalprice),
12051205
C AS totalprice > PREV(totalprice) AND totalprice <= A.totalprice,
1206-
D AS totalprice > PREV(totalprice)
1206+
D AS totalprice > PREV(totalprice),
1207+
E AS MAX(foo) >= NEXT(bar)
12071208
)""",
12081209
pretty=True,
12091210
)

tests/dialects/test_tsql.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,3 +2075,18 @@ def test_parsename(self):
20752075
"tsql": UnsupportedError,
20762076
},
20772077
)
2078+
2079+
def test_next_value_for(self):
2080+
self.validate_identity(
2081+
"SELECT NEXT VALUE FOR db.schema.sequence_name OVER (ORDER BY foo), col"
2082+
)
2083+
self.validate_all(
2084+
"SELECT NEXT VALUE FOR db.schema.sequence_name",
2085+
read={
2086+
"oracle": "SELECT NEXT VALUE FOR db.schema.sequence_name",
2087+
"tsql": "SELECT NEXT VALUE FOR db.schema.sequence_name",
2088+
},
2089+
write={
2090+
"oracle": "SELECT NEXT VALUE FOR db.schema.sequence_name",
2091+
},
2092+
)

tests/fixtures/identity.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,6 @@ JSON_OBJECT('x': 1 RETURNING VARCHAR(100))
830830
JSON_OBJECT('x': 1 RETURNING VARBINARY FORMAT JSON ENCODING UTF8)
831831
PRIOR AS x
832832
SELECT if.x
833-
SELECT NEXT VALUE FOR db.schema.sequence_name
834-
SELECT NEXT VALUE FOR db.schema.sequence_name OVER (ORDER BY foo), col
835833
SELECT PERCENTILE_CONT(x, 0.5) OVER ()
836834
WITH my_cte AS (SELECT 'a' AS desc) SELECT desc AS description FROM my_cte
837835
WITH my_cte AS (SELECT 'a' AS asc) SELECT asc AS description FROM my_cte

0 commit comments

Comments
 (0)