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 @@ -1135,6 +1135,12 @@ pub enum TableFactor {
1135
1135
subquery : Box < Query > ,
1136
1136
alias : Option < TableAlias > ,
1137
1137
} ,
1138
+ /// A pass-through query string that is not parsed.
1139
+ /// This is useful while building/rewriting queries with a known valid SQL string and to avoid parsing it.
1140
+ PassThroughQuery {
1141
+ query : String ,
1142
+ alias : Option < TableAlias > ,
1143
+ } ,
1138
1144
/// `TABLE(<expr>)[ AS <alias> ]`
1139
1145
TableFunction {
1140
1146
expr : Expr ,
@@ -1767,6 +1773,13 @@ impl fmt::Display for TableFactor {
1767
1773
}
1768
1774
Ok ( ( ) )
1769
1775
}
1776
+ TableFactor :: PassThroughQuery { query, alias } => {
1777
+ write ! ( f, "({query})" ) ?;
1778
+ if let Some ( alias) = alias {
1779
+ write ! ( f, " AS {alias}" ) ?;
1780
+ }
1781
+ Ok ( ( ) )
1782
+ }
1770
1783
TableFactor :: Function {
1771
1784
lateral,
1772
1785
name,
Original file line number Diff line number Diff line change @@ -1876,6 +1876,8 @@ impl Spanned for TableFactor {
1876
1876
} => subquery
1877
1877
. span ( )
1878
1878
. union_opt ( & alias. as_ref ( ) . map ( |alias| alias. span ( ) ) ) ,
1879
+ // This is usually created at runtime, so we don't have a span for it
1880
+ TableFactor :: PassThroughQuery { query : _, alias : _ } => Span :: empty ( ) ,
1879
1881
TableFactor :: TableFunction { expr, alias } => expr
1880
1882
. span ( )
1881
1883
. union_opt ( & alias. as_ref ( ) . map ( |alias| alias. span ( ) ) ) ,
Original file line number Diff line number Diff line change @@ -11971,7 +11971,8 @@ impl<'a> Parser<'a> {
11971
11971
| TableFactor::Pivot { alias, .. }
11972
11972
| TableFactor::Unpivot { alias, .. }
11973
11973
| TableFactor::MatchRecognize { alias, .. }
11974
- | TableFactor::NestedJoin { alias, .. } => {
11974
+ | TableFactor::NestedJoin { alias, .. }
11975
+ | TableFactor::PassThroughQuery { alias, .. } => {
11975
11976
// but not `FROM (mytable AS alias1) AS alias2`.
11976
11977
if let Some(inner_alias) = alias {
11977
11978
return Err(ParserError::ParserError(format!(
Original file line number Diff line number Diff line change @@ -14124,6 +14124,29 @@ fn parse_select_without_projection() {
14124
14124
dialects. verified_stmt ( "SELECT FROM users" ) ;
14125
14125
}
14126
14126
14127
+ #[ test]
14128
+ fn ast_with_pass_through_query ( ) {
14129
+ let sql = "SELECT * FROM t1 AS t2" ;
14130
+ let mut ast = all_dialects ( ) . verified_stmt ( sql) ;
14131
+ let Statement :: Query ( ref mut query) = ast else {
14132
+ panic ! ( "Expected Query" ) ;
14133
+ } ;
14134
+ let SetExpr :: Select ( ref mut select) = * query. body else {
14135
+ panic ! ( "Expected SetExpr::Select" ) ;
14136
+ } ;
14137
+ let from = select. from . get_mut ( 0 ) . unwrap ( ) ;
14138
+ from. relation = TableFactor :: PassThroughQuery {
14139
+ query : "SELECT * FROM tx" . to_string ( ) ,
14140
+ alias : Some ( TableAlias {
14141
+ name : Ident :: new ( "ty" ) ,
14142
+ columns : vec ! [ ] ,
14143
+ } ) ,
14144
+ } ;
14145
+
14146
+ // After modifying the AST, the SQL representation should be different
14147
+ assert_eq ! ( ast. to_string( ) , "SELECT * FROM (SELECT * FROM tx) AS ty" ) ;
14148
+ }
14149
+
14127
14150
#[ test]
14128
14151
fn parse_update_from_before_select ( ) {
14129
14152
verified_stmt ( "UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id" ) ;
You can’t perform that action at this time.
0 commit comments