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 {
1013
1013
subquery : Box < Query > ,
1014
1014
alias : Option < TableAlias > ,
1015
1015
} ,
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
+ } ,
1016
1022
/// `TABLE(<expr>)[ AS <alias> ]`
1017
1023
TableFunction {
1018
1024
expr : Expr ,
@@ -1641,6 +1647,13 @@ impl fmt::Display for TableFactor {
1641
1647
}
1642
1648
Ok ( ( ) )
1643
1649
}
1650
+ TableFactor :: PassThroughQuery { query, alias } => {
1651
+ write ! ( f, "({query})" ) ?;
1652
+ if let Some ( alias) = alias {
1653
+ write ! ( f, " AS {alias}" ) ?;
1654
+ }
1655
+ Ok ( ( ) )
1656
+ }
1644
1657
TableFactor :: Function {
1645
1658
lateral,
1646
1659
name,
Original file line number Diff line number Diff line change @@ -1733,6 +1733,8 @@ impl Spanned for TableFactor {
1733
1733
} => subquery
1734
1734
. span ( )
1735
1735
. 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 ( ) ,
1736
1738
TableFactor :: TableFunction { expr, alias } => expr
1737
1739
. span ( )
1738
1740
. 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> {
10805
10805
| TableFactor::Pivot { alias, .. }
10806
10806
| TableFactor::Unpivot { alias, .. }
10807
10807
| TableFactor::MatchRecognize { alias, .. }
10808
- | TableFactor::NestedJoin { alias, .. } => {
10808
+ | TableFactor::NestedJoin { alias, .. }
10809
+ | TableFactor::PassThroughQuery { alias, .. } => {
10809
10810
// but not `FROM (mytable AS alias1) AS alias2`.
10810
10811
if let Some(inner_alias) = alias {
10811
10812
return Err(ParserError::ParserError(format!(
Original file line number Diff line number Diff line change @@ -12728,6 +12728,29 @@ fn parse_select_without_projection() {
12728
12728
dialects. verified_stmt ( "SELECT FROM users" ) ;
12729
12729
}
12730
12730
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
+
12731
12754
#[ test]
12732
12755
fn parse_update_from_before_select ( ) {
12733
12756
all_dialects ( )
You can’t perform that action at this time.
0 commit comments