Skip to content
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
40 changes: 22 additions & 18 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ impl fmt::Display for TableFactor {
write!(f, " {sample}")?;
}
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
if !index_hints.is_empty() {
write!(f, " {}", display_separated(index_hints, " "))?;
Expand Down Expand Up @@ -1932,7 +1932,7 @@ impl fmt::Display for TableFactor {
NewLine.fmt(f)?;
f.write_str(")")?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -1948,14 +1948,14 @@ impl fmt::Display for TableFactor {
write!(f, "{name}")?;
write!(f, "({})", display_comma_separated(args))?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
TableFactor::TableFunction { expr, alias } => {
write!(f, "TABLE({expr})")?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -1973,13 +1973,13 @@ impl fmt::Display for TableFactor {
}

if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
if *with_offset {
write!(f, " WITH OFFSET")?;
}
if let Some(alias) = with_offset_alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -1995,7 +1995,7 @@ impl fmt::Display for TableFactor {
columns = display_comma_separated(columns)
)?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -2014,7 +2014,7 @@ impl fmt::Display for TableFactor {
write!(f, " WITH ({})", display_comma_separated(columns))?;
}
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -2024,7 +2024,7 @@ impl fmt::Display for TableFactor {
} => {
write!(f, "({table_with_joins})")?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -2051,8 +2051,8 @@ impl fmt::Display for TableFactor {
write!(f, " DEFAULT ON NULL ({expr})")?;
}
write!(f, ")")?;
if alias.is_some() {
write!(f, " AS {}", alias.as_ref().unwrap())?;
if let Some(alias) = alias {
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -2075,8 +2075,8 @@ impl fmt::Display for TableFactor {
name,
display_comma_separated(columns)
)?;
if alias.is_some() {
write!(f, " AS {}", alias.as_ref().unwrap())?;
if let Some(alias) = alias {
write!(f, " {alias}")?;
}
Ok(())
}
Expand Down Expand Up @@ -2109,8 +2109,8 @@ impl fmt::Display for TableFactor {
}
write!(f, "PATTERN ({pattern}) ")?;
write!(f, "DEFINE {})", display_comma_separated(symbols))?;
if alias.is_some() {
write!(f, " AS {}", alias.as_ref().unwrap())?;
if let Some(alias) = alias {
write!(f, " {alias}")?;
}
Ok(())
}
Expand All @@ -2135,7 +2135,7 @@ impl fmt::Display for TableFactor {
columns = display_comma_separated(columns)
)?;
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}
Ok(())
}
Expand Down Expand Up @@ -2168,7 +2168,7 @@ impl fmt::Display for TableFactor {
write!(f, ")")?;

if let Some(alias) = alias {
write!(f, " AS {alias}")?;
write!(f, " {alias}")?;
}

Ok(())
Expand All @@ -2181,13 +2181,17 @@ impl fmt::Display for TableFactor {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct TableAlias {
/// Tells whether the alias was introduced with an explicit, preceding "AS"
/// keyword, e.g. `AS name`. Typically, the keyword is preceding the name
/// (e.g. `.. FROM table AS t ..`).
pub explicit: bool,
pub name: Ident,
pub columns: Vec<TableAliasColumnDef>,
}

impl fmt::Display for TableAlias {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)?;
write!(f, "{}{}", if self.explicit { "AS " } else { "" }, self.name)?;
if !self.columns.is_empty() {
write!(f, " ({})", display_comma_separated(&self.columns))?;
}
Expand Down
26 changes: 19 additions & 7 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
// specific language governing permissions and limitations
// under the License.

use crate::ast::{
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, AlterTable,
ColumnOptions, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreateView,
ExportData, Owner, TypedString,
use crate::{
ast::{
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, AlterTable,
ColumnOptions, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreateView,
ExportData, Owner, TypedString,
},
tokenizer::TokenWithSpan,
};
use core::iter;

Expand Down Expand Up @@ -96,6 +99,12 @@ pub trait Spanned {
fn span(&self) -> Span;
}

impl Spanned for TokenWithSpan {
fn span(&self) -> Span {
self.span
}
}

impl Spanned for Query {
fn span(&self) -> Span {
let Query {
Expand Down Expand Up @@ -2079,9 +2088,12 @@ impl Spanned for FunctionArgExpr {

impl Spanned for TableAlias {
fn span(&self) -> Span {
let TableAlias { name, columns } = self;

union_spans(iter::once(name.span).chain(columns.iter().map(|i| i.span())))
let TableAlias {
explicit: _,
name,
columns,
} = self;
union_spans(core::iter::once(name.span).chain(columns.iter().map(Spanned::span)))
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11140,10 +11140,15 @@ impl<'a> Parser<'a> {
fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
parser.dialect.is_table_factor_alias(explicit, kw, parser)
}
let explicit = self.peek_keyword(Keyword::AS);
match self.parse_optional_alias_inner(None, validator)? {
Some(name) => {
let columns = self.parse_table_alias_column_defs()?;
Ok(Some(TableAlias { name, columns }))
Ok(Some(TableAlias {
explicit,
name,
columns,
}))
}
None => Ok(None),
}
Expand Down Expand Up @@ -12775,6 +12780,7 @@ impl<'a> Parser<'a> {
let closing_paren_token = self.expect_token(&Token::RParen)?;

let alias = TableAlias {
explicit: false,
name,
columns: vec![],
};
Expand All @@ -12801,7 +12807,11 @@ impl<'a> Parser<'a> {
let query = self.parse_query()?;
let closing_paren_token = self.expect_token(&Token::RParen)?;

let alias = TableAlias { name, columns };
let alias = TableAlias {
explicit: false,
name,
columns,
};
Cte {
alias,
query,
Expand Down
14 changes: 8 additions & 6 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ pub fn single_quoted_string(s: impl Into<String>) -> Value {
Value::SingleQuotedString(s.into())
}

pub fn table_alias(name: impl Into<String>) -> Option<TableAlias> {
pub fn table_alias(explicit: bool, name: impl Into<String>) -> Option<TableAlias> {
Some(TableAlias {
explicit,
name: Ident::new(name),
columns: vec![],
})
Expand Down Expand Up @@ -405,13 +406,14 @@ pub fn table_from_name(name: ObjectName) -> TableFactor {
}
}

pub fn table_with_alias(name: impl Into<String>, alias: impl Into<String>) -> TableFactor {
pub fn table_with_alias(
name: impl Into<String>,
with_as_keyword: bool,
alias: impl Into<String>,
) -> TableFactor {
TableFactor::Table {
name: ObjectName::from(vec![Ident::new(name)]),
alias: Some(TableAlias {
name: Ident::new(alias),
columns: vec![],
}),
alias: table_alias(with_as_keyword, alias),
args: None,
with_hints: vec![],
version: None,
Expand Down
14 changes: 4 additions & 10 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ fn parse_table_identifiers() {
fn parse_hyphenated_table_identifiers() {
bigquery().one_statement_parses_to(
"select * from foo-bar f join baz-qux b on f.id = b.id",
"SELECT * FROM foo-bar AS f JOIN baz-qux AS b ON f.id = b.id",
"SELECT * FROM foo-bar f JOIN baz-qux b ON f.id = b.id",
);

assert_eq!(
Expand Down Expand Up @@ -1766,7 +1766,7 @@ fn parse_join_constraint_unnest_alias() {
.joins,
vec![Join {
relation: TableFactor::UNNEST {
alias: table_alias("f"),
alias: table_alias(true, "f"),
array_exprs: vec![Expr::CompoundIdentifier(vec![
Ident::new("t1"),
Ident::new("a")
Expand Down Expand Up @@ -1841,10 +1841,7 @@ fn parse_merge() {
assert_eq!(
TableFactor::Table {
name: ObjectName::from(vec![Ident::new("inventory")]),
alias: Some(TableAlias {
name: Ident::new("T"),
columns: vec![],
}),
alias: table_alias(true, "T"),
args: Default::default(),
with_hints: Default::default(),
version: Default::default(),
Expand All @@ -1859,10 +1856,7 @@ fn parse_merge() {
assert_eq!(
TableFactor::Table {
name: ObjectName::from(vec![Ident::new("newArrivals")]),
alias: Some(TableAlias {
name: Ident::new("S"),
columns: vec![],
}),
alias: table_alias(true, "S"),
args: Default::default(),
with_hints: Default::default(),
version: Default::default(),
Expand Down
Loading