Skip to content

Commit 6008c92

Browse files
committed
Merge branch 'main' into chore/cleanup-feature-gates
2 parents 1e3eae6 + a597279 commit 6008c92

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

crates/pglt_statement_splitter/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ values ('insert', new.id, now());",
233233
]);
234234
}
235235

236+
#[test]
237+
fn with_ordinality() {
238+
Tester::from("insert into table (col) select 1 from other t cross join lateral jsonb_array_elements(t.buttons) with ordinality as a(b, nr) where t.buttons is not null;").expect_statements(vec!["insert into table (col) select 1 from other t cross join lateral jsonb_array_elements(t.buttons) with ordinality as a(b, nr) where t.buttons is not null;"]);
239+
}
240+
236241
#[test]
237242
fn unknown() {
238243
Tester::from("random stuff\n\nmore randomness\n\nselect 3").expect_statements(vec![

crates/pglt_statement_splitter/src/parser.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ impl Parser {
8585

8686
/// Start statement
8787
pub fn start_stmt(&mut self) {
88-
assert!(self.current_stmt_start.is_none());
88+
assert!(
89+
self.current_stmt_start.is_none(),
90+
"cannot start statement within statement at {:?}",
91+
self.tokens.get(self.current_stmt_start.unwrap())
92+
);
8993
self.current_stmt_start = Some(self.next_pos);
9094
}
9195

@@ -142,6 +146,21 @@ impl Parser {
142146
}
143147
}
144148

149+
/// Look ahead to the next relevant token
150+
fn look_ahead(&self) -> Option<&Token> {
151+
// we need to look ahead to the next relevant token
152+
let mut look_ahead_pos = self.next_pos + 1;
153+
loop {
154+
let token = self.tokens.get(look_ahead_pos)?;
155+
156+
if !is_irrelevant_token(token) {
157+
return Some(token);
158+
}
159+
160+
look_ahead_pos += 1;
161+
}
162+
}
163+
145164
fn look_back(&self) -> Option<&Token> {
146165
// we need to look back to the last relevant token
147166
let mut look_back_pos = self.next_pos - 1;

crates/pglt_statement_splitter/src/parser/common.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ pub(crate) fn unknown(p: &mut Parser, exclude: &[SyntaxKind]) {
169169
}
170170
p.advance();
171171
}
172+
Some(SyntaxKind::With) => {
173+
let next = p.look_ahead().map(|t| t.kind);
174+
if [
175+
// WITH ORDINALITY should not start a new statement
176+
SyntaxKind::Ordinality,
177+
]
178+
.iter()
179+
.all(|x| Some(x) != next.as_ref())
180+
{
181+
break;
182+
}
183+
p.advance();
184+
}
172185
Some(_) => {
173186
break;
174187
}

0 commit comments

Comments
 (0)