Skip to content

Commit d55fd6b

Browse files
committed
fix: with ordinality
1 parent 310b643 commit d55fd6b

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-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: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ 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 without statement {:?} at {:?}",
91+
self.current_stmt_start,
92+
self.tokens.get(self.current_stmt_start.unwrap()).unwrap()
93+
);
8994
self.current_stmt_start = Some(self.next_pos);
9095
}
9196

@@ -142,6 +147,21 @@ impl Parser {
142147
}
143148
}
144149

150+
/// Look ahead to the next relevant token *after the next token*
151+
fn look_ahead(&self) -> Option<&Token> {
152+
// we need to look ahead to the next relevant token
153+
let mut look_ahead_pos = self.next_pos + 1;
154+
loop {
155+
let token = self.tokens.get(look_ahead_pos)?;
156+
157+
if !is_irrelevant_token(token) {
158+
return Some(token);
159+
}
160+
161+
look_ahead_pos += 1;
162+
}
163+
}
164+
145165
fn look_back(&self) -> Option<&Token> {
146166
// we need to look back to the last relevant token
147167
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)