File tree Expand file tree Collapse file tree 4 files changed +40
-1
lines changed
Expand file tree Collapse file tree 4 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -1013,6 +1013,12 @@ pub enum TableFactor {
10131013 subquery : Box < Query > ,
10141014 alias : Option < TableAlias > ,
10151015 } ,
1016+ /// A pass-through query string that is not parsed.
1017+ /// This is useful while building/rewriting queries with a known valid SQL string and to avoid parsing it.
1018+ PassThroughQuery {
1019+ query : String ,
1020+ alias : Option < TableAlias > ,
1021+ } ,
10161022 /// `TABLE(<expr>)[ AS <alias> ]`
10171023 TableFunction {
10181024 expr : Expr ,
@@ -1641,6 +1647,13 @@ impl fmt::Display for TableFactor {
16411647 }
16421648 Ok ( ( ) )
16431649 }
1650+ TableFactor :: PassThroughQuery { query, alias } => {
1651+ write ! ( f, "({query})" ) ?;
1652+ if let Some ( alias) = alias {
1653+ write ! ( f, " AS {alias}" ) ?;
1654+ }
1655+ Ok ( ( ) )
1656+ }
16441657 TableFactor :: Function {
16451658 lateral,
16461659 name,
Original file line number Diff line number Diff line change @@ -1733,6 +1733,8 @@ impl Spanned for TableFactor {
17331733 } => subquery
17341734 . span ( )
17351735 . union_opt ( & alias. as_ref ( ) . map ( |alias| alias. span ( ) ) ) ,
1736+ // This is usually created at runtime, so we don't have a span for it
1737+ TableFactor :: PassThroughQuery { query : _, alias : _ } => Span :: empty ( ) ,
17361738 TableFactor :: TableFunction { expr, alias } => expr
17371739 . span ( )
17381740 . union_opt ( & alias. as_ref ( ) . map ( |alias| alias. span ( ) ) ) ,
Original file line number Diff line number Diff line change @@ -10805,7 +10805,8 @@ impl<'a> Parser<'a> {
1080510805 | TableFactor::Pivot { alias, .. }
1080610806 | TableFactor::Unpivot { alias, .. }
1080710807 | TableFactor::MatchRecognize { alias, .. }
10808- | TableFactor::NestedJoin { alias, .. } => {
10808+ | TableFactor::NestedJoin { alias, .. }
10809+ | TableFactor::PassThroughQuery { alias, .. } => {
1080910810 // but not `FROM (mytable AS alias1) AS alias2`.
1081010811 if let Some(inner_alias) = alias {
1081110812 return Err(ParserError::ParserError(format!(
Original file line number Diff line number Diff line change @@ -12728,6 +12728,29 @@ fn parse_select_without_projection() {
1272812728 dialects. verified_stmt ( "SELECT FROM users" ) ;
1272912729}
1273012730
12731+ #[ test]
12732+ fn ast_with_pass_through_query ( ) {
12733+ let sql = "SELECT * FROM t1 AS t2" ;
12734+ let mut ast = all_dialects ( ) . verified_stmt ( sql) ;
12735+ let Statement :: Query ( ref mut query) = ast else {
12736+ panic ! ( "Expected Query" ) ;
12737+ } ;
12738+ let SetExpr :: Select ( ref mut select) = * query. body else {
12739+ panic ! ( "Expected SetExpr::Select" ) ;
12740+ } ;
12741+ let from = select. from . get_mut ( 0 ) . unwrap ( ) ;
12742+ from. relation = TableFactor :: PassThroughQuery {
12743+ query : "SELECT * FROM tx" . to_string ( ) ,
12744+ alias : Some ( TableAlias {
12745+ name : Ident :: new ( "ty" ) ,
12746+ columns : vec ! [ ] ,
12747+ } ) ,
12748+ } ;
12749+
12750+ // After modifying the AST, the SQL representation should be different
12751+ assert_eq ! ( ast. to_string( ) , "SELECT * FROM (SELECT * FROM tx) AS ty" ) ;
12752+ }
12753+
1273112754#[ test]
1273212755fn parse_update_from_before_select ( ) {
1273312756 all_dialects ( )
You can’t perform that action at this time.
0 commit comments