Skip to content
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

fix(sqlparser): Display changelog with quoted identifiers #19029

Merged
merged 2 commits into from
Oct 21, 2024
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
9 changes: 3 additions & 6 deletions src/sqlparser/src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,9 @@ impl fmt::Display for Cte {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.cte_inner {
CteInner::Query(query) => write!(f, "{} AS ({})", self.alias, query)?,
CteInner::ChangeLog(obj_name) => write!(
f,
"{} AS changelog from {}",
self.alias,
obj_name.real_value()
)?,
CteInner::ChangeLog(obj_name) => {
write!(f, "{} AS changelog from {}", self.alias, obj_name)?
}
}
Ok(())
}
Expand Down
64 changes: 0 additions & 64 deletions src/sqlparser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2836,70 +2836,6 @@ fn parse_ctes() {
}
}

#[test]
fn parse_changelog_ctes() {
let cte_sqls = vec!["foo", "bar"];
let with = &format!(
"WITH a AS changelog from {}, b AS changelog from {} SELECT foo + bar FROM a, b",
cte_sqls[0], cte_sqls[1]
);

fn assert_changelog_ctes(expected: &[&str], sel: &Query) {
for (i, exp) in expected.iter().enumerate() {
let Cte { alias, cte_inner } = &sel.with.as_ref().unwrap().cte_tables[i];
if let CteInner::ChangeLog(from) = cte_inner {
assert_eq!(*exp, from.to_string());
assert_eq!(
if i == 0 {
Ident::new_unchecked("a")
} else {
Ident::new_unchecked("b")
},
alias.name
);
assert!(alias.columns.is_empty());
} else {
panic!("expected CteInner::ChangeLog")
}
}
}

// Top-level CTE
assert_changelog_ctes(&cte_sqls, &verified_query(with));
// CTE in a subquery
let sql = &format!("SELECT ({})", with);
let select = verified_only_select(sql);
match expr_from_projection(only(&select.projection)) {
Expr::Subquery(ref subquery) => {
assert_changelog_ctes(&cte_sqls, subquery.as_ref());
}
_ => panic!("expected subquery"),
}
// CTE in a derived table
let sql = &format!("SELECT * FROM ({})", with);
let select = verified_only_select(sql);
match only(select.from).relation {
TableFactor::Derived { subquery, .. } => {
assert_changelog_ctes(&cte_sqls, subquery.as_ref())
}
_ => panic!("expected derived table"),
}
// CTE in a view
let sql = &format!("CREATE VIEW v AS {}", with);
match verified_stmt(sql) {
Statement::CreateView { query, .. } => assert_changelog_ctes(&cte_sqls, &query),
_ => panic!("expected CREATE VIEW"),
}
// CTE in a CTE...
let sql = &format!("WITH outer_cte AS ({}) SELECT * FROM outer_cte", with);
let select = verified_query(sql);
if let CteInner::Query(query) = &only(&select.with.unwrap().cte_tables).cte_inner {
assert_changelog_ctes(&cte_sqls, query);
} else {
panic!("expected CteInner::Query")
}
}

#[test]
fn parse_cte_renamed_columns() {
let sql = "WITH cte (col1, col2) AS (SELECT foo, bar FROM baz) SELECT * FROM cte";
Expand Down
6 changes: 6 additions & 0 deletions src/sqlparser/tests/testdata/select.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,9 @@
- input: select date t; -- A column "date" aliased to "t"
formatted_sql: SELECT date AS t
formatted_ast: 'Query(Query { with: None, body: Select(Select { distinct: All, projection: [ExprWithAlias { expr: Identifier(Ident { value: "date", quote_style: None }), alias: Ident { value: "t", quote_style: None } }], from: [], lateral_views: [], selection: None, group_by: [], having: None }), order_by: [], limit: None, offset: None, fetch: None })'
- input: |-
WITH a_log AS changelog from a,
"B_log" AS changelog from public."B"
SELECT a0 + b0 FROM a_log, "B_log"
formatted_sql: WITH a_log AS changelog from a, "B_log" AS changelog from public."B" SELECT a0 + b0 FROM a_log, "B_log"
formatted_ast: 'Query(Query { with: Some(With { recursive: false, cte_tables: [Cte { alias: TableAlias { name: Ident { value: "a_log", quote_style: None }, columns: [] }, cte_inner: ChangeLog(ObjectName([Ident { value: "a", quote_style: None }])) }, Cte { alias: TableAlias { name: Ident { value: "B_log", quote_style: Some(''"'') }, columns: [] }, cte_inner: ChangeLog(ObjectName([Ident { value: "public", quote_style: None }, Ident { value: "B", quote_style: Some(''"'') }])) }] }), body: Select(Select { distinct: All, projection: [UnnamedExpr(BinaryOp { left: Identifier(Ident { value: "a0", quote_style: None }), op: Plus, right: Identifier(Ident { value: "b0", quote_style: None }) })], from: [TableWithJoins { relation: Table { name: ObjectName([Ident { value: "a_log", quote_style: None }]), alias: None, as_of: None }, joins: [] }, TableWithJoins { relation: Table { name: ObjectName([Ident { value: "B_log", quote_style: Some(''"'') }]), alias: None, as_of: None }, joins: [] }], lateral_views: [], selection: None, group_by: [], having: None }), order_by: [], limit: None, offset: None, fetch: None })'
Loading