From 245c2eca27f6926106f9999a9ac6a7925b8d084d Mon Sep 17 00:00:00 2001 From: Ayman Elkfrawy Date: Thu, 5 Feb 2026 11:15:15 -0800 Subject: [PATCH] respect alias explicit settings --- src/ast/query.rs | 2 +- tests/sqlparser_common.rs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ast/query.rs b/src/ast/query.rs index e720f23b5..f6146e629 100644 --- a/src/ast/query.rs +++ b/src/ast/query.rs @@ -1945,7 +1945,7 @@ impl fmt::Display for TableFactor { TableFactor::PassThroughQuery { query, alias } => { write!(f, "({query})")?; if let Some(alias) = alias { - write!(f, " AS {alias}")?; + write!(f, " {alias}")?; } Ok(()) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 4a72e6b8e..6104fe76e 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -15219,6 +15219,30 @@ fn ast_with_pass_through_query() { }), }; + // After modifying the AST, the SQL representation should be different + assert_eq!(ast.to_string(), "SELECT * FROM (SELECT * FROM tx) ty"); +} + +#[test] +fn ast_with_pass_through_query_with_explicit_alias() { + 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 { + explicit: true, + 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"); }