Skip to content

Commit 6d7f5cf

Browse files
Snowflake: parse (EXECUTE IMMEDIATE) as RESULTSET/CURSOR payload
1 parent 9ed130d commit 6d7f5cf

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/ast/query.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ pub enum SetExpr {
180180
/// `SHOW` statement used as the query payload of a Snowflake `RESULTSET` or
181181
/// `CURSOR` declaration (`RESULTSET DEFAULT (SHOW ...)`, `CURSOR FOR (SHOW ...)`).
182182
Show(Statement),
183+
/// `EXECUTE IMMEDIATE` statement used as the query payload of a Snowflake
184+
/// `RESULTSET` declaration (`RESULTSET := (EXECUTE IMMEDIATE '<query>')`). The
185+
/// payload SQL is only known at runtime, so the contained statement carries
186+
/// the dynamic-SQL expression rather than a static query body.
187+
Execute(Statement),
183188
}
184189

185190
impl SetExpr {
@@ -209,6 +214,7 @@ impl fmt::Display for SetExpr {
209214
SetExpr::Merge(v) => v.fmt(f),
210215
SetExpr::Table(t) => t.fmt(f),
211216
SetExpr::Show(v) => v.fmt(f),
217+
SetExpr::Execute(v) => v.fmt(f),
212218
SetExpr::SetOperation {
213219
left,
214220
right,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ impl Spanned for SetExpr {
236236
SetExpr::Delete(statement) => statement.span(),
237237
SetExpr::Merge(statement) => statement.span(),
238238
SetExpr::Show(statement) => statement.span(),
239+
SetExpr::Execute(statement) => statement.span(),
239240
}
240241
}
241242
}

src/parser/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8361,6 +8361,30 @@ impl<'a> Parser<'a> {
83618361
/// [`SetExpr::Show`]. The pre-existing `(SELECT ...)` subquery path is left
83628362
/// untouched.
83638363
fn parse_snowflake_declaration_payload_expr(&mut self) -> Result<Expr, ParserError> {
8364+
let is_paren_execute = self.dialect.supports_execute_immediate()
8365+
&& self.peek_nth_token_ref(0).token == Token::LParen
8366+
&& matches!(
8367+
&self.peek_nth_token_ref(1).token,
8368+
Token::Word(w) if w.keyword == Keyword::EXECUTE
8369+
);
8370+
if is_paren_execute {
8371+
self.expect_token(&Token::LParen)?;
8372+
self.expect_keyword_is(Keyword::EXECUTE)?;
8373+
let execute = self.parse_execute()?;
8374+
self.expect_token(&Token::RParen)?;
8375+
return Ok(Expr::Subquery(Box::new(Query {
8376+
with: None,
8377+
body: Box::new(SetExpr::Execute(execute)),
8378+
order_by: None,
8379+
limit_clause: None,
8380+
fetch: None,
8381+
locks: vec![],
8382+
for_clause: None,
8383+
settings: None,
8384+
format_clause: None,
8385+
pipe_operators: vec![],
8386+
})));
8387+
}
83648388
let is_paren_show = self.dialect.supports_show_in_resultset_cursor()
83658389
&& self.peek_nth_token_ref(0).token == Token::LParen
83668390
&& matches!(

0 commit comments

Comments
 (0)