Skip to content

[chore] add passthrough query as a tablefactor #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,12 @@ pub enum TableFactor {
subquery: Box<Query>,
alias: Option<TableAlias>,
},
/// A pass-through query string that is not parsed.
/// This is useful while building/rewriting queries with a known valid SQL string and to avoid parsing it.
PassThroughQuery {
query: String,
alias: Option<TableAlias>,
},
/// `TABLE(<expr>)[ AS <alias> ]`
TableFunction {
expr: Expr,
Expand Down Expand Up @@ -1767,6 +1773,13 @@ impl fmt::Display for TableFactor {
}
Ok(())
}
TableFactor::PassThroughQuery { query, alias } => {
write!(f, "({query})")?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
}
Ok(())
}
TableFactor::Function {
lateral,
name,
Expand Down
2 changes: 2 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,8 @@ impl Spanned for TableFactor {
} => subquery
.span()
.union_opt(&alias.as_ref().map(|alias| alias.span())),
// This is usually created at runtime, so we don't have a span for it
TableFactor::PassThroughQuery { query: _, alias: _ } => Span::empty(),
TableFactor::TableFunction { expr, alias } => expr
.span()
.union_opt(&alias.as_ref().map(|alias| alias.span())),
Expand Down
3 changes: 2 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11971,7 +11971,8 @@ impl<'a> Parser<'a> {
| TableFactor::Pivot { alias, .. }
| TableFactor::Unpivot { alias, .. }
| TableFactor::MatchRecognize { alias, .. }
| TableFactor::NestedJoin { alias, .. } => {
| TableFactor::NestedJoin { alias, .. }
| TableFactor::PassThroughQuery { alias, .. } => {
// but not `FROM (mytable AS alias1) AS alias2`.
if let Some(inner_alias) = alias {
return Err(ParserError::ParserError(format!(
Expand Down
23 changes: 23 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14124,6 +14124,29 @@ fn parse_select_without_projection() {
dialects.verified_stmt("SELECT FROM users");
}

#[test]
fn ast_with_pass_through_query() {
let sql = "SELECT * FROM t1 AS t2";
let mut ast = all_dialects().verified_stmt(sql);
let Statement::Query(ref mut query) = ast else {
panic!("Expected Query");
};
let SetExpr::Select(ref mut select) = *query.body else {
panic!("Expected SetExpr::Select");
};
let from = select.from.get_mut(0).unwrap();
from.relation = TableFactor::PassThroughQuery {
query: "SELECT * FROM tx".to_string(),
alias: Some(TableAlias {
name: Ident::new("ty"),
columns: vec![],
}),
};

// After modifying the AST, the SQL representation should be different
assert_eq!(ast.to_string(), "SELECT * FROM (SELECT * FROM tx) AS ty");
}

#[test]
fn parse_update_from_before_select() {
verified_stmt("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");
Expand Down