Skip to content

Add support for INCLUDE/EXCLUDE NULLS for UNPIVOT #1849

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 3 commits into from
May 15, 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
20 changes: 20 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9633,6 +9633,26 @@ impl fmt::Display for OpenStatement {
}
}

/// Specifies Include / Exclude NULL within UNPIVOT command.
/// For example
/// `UNPIVOT (column1 FOR new_column IN (col3, col4, col5, col6))`
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum NullInclusion {
IncludeNulls,
ExcludeNulls,
}

impl fmt::Display for NullInclusion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
NullInclusion::IncludeNulls => write!(f, "INCLUDE NULLS"),
NullInclusion::ExcludeNulls => write!(f, "EXCLUDE NULLS"),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
11 changes: 8 additions & 3 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,7 @@ pub enum TableFactor {
///
/// Syntax:
/// ```sql
/// table UNPIVOT(value FOR name IN (column1, [ column2, ... ])) [ alias ]
/// table UNPIVOT [ { INCLUDE | EXCLUDE } NULLS ] (value FOR name IN (column1, [ column2, ... ])) [ alias ]
/// ```
///
/// See <https://docs.snowflake.com/en/sql-reference/constructs/unpivot>.
Expand All @@ -1345,6 +1345,7 @@ pub enum TableFactor {
value: Ident,
name: Ident,
columns: Vec<Ident>,
null_inclusion: Option<NullInclusion>,
alias: Option<TableAlias>,
},
/// A `MATCH_RECOGNIZE` operation on a table.
Expand Down Expand Up @@ -2015,15 +2016,19 @@ impl fmt::Display for TableFactor {
}
TableFactor::Unpivot {
table,
null_inclusion,
value,
name,
columns,
alias,
} => {
write!(f, "{table} UNPIVOT")?;
if let Some(null_inclusion) = null_inclusion {
write!(f, " {null_inclusion} ")?;
}
write!(
f,
"{} UNPIVOT({} FOR {} IN ({}))",
table,
"({} FOR {} IN ({}))",
value,
name,
display_comma_separated(columns)
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,7 @@ impl Spanned for TableFactor {
TableFactor::Unpivot {
table,
value,
null_inclusion: _,
name,
columns,
alias,
Expand Down
10 changes: 10 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13368,6 +13368,15 @@ impl<'a> Parser<'a> {
&mut self,
table: TableFactor,
) -> Result<TableFactor, ParserError> {
let null_inclusion = if self.parse_keyword(Keyword::INCLUDE) {
self.expect_keyword_is(Keyword::NULLS)?;
Some(NullInclusion::IncludeNulls)
} else if self.parse_keyword(Keyword::EXCLUDE) {
self.expect_keyword_is(Keyword::NULLS)?;
Some(NullInclusion::ExcludeNulls)
} else {
None
};
self.expect_token(&Token::LParen)?;
let value = self.parse_identifier()?;
self.expect_keyword_is(Keyword::FOR)?;
Expand All @@ -13379,6 +13388,7 @@ impl<'a> Parser<'a> {
Ok(TableFactor::Unpivot {
table: Box::new(table),
value,
null_inclusion,
name,
columns,
alias,
Expand Down
109 changes: 70 additions & 39 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10746,49 +10746,47 @@ fn parse_unpivot_table() {
"SELECT * FROM sales AS s ",
"UNPIVOT(quantity FOR quarter IN (Q1, Q2, Q3, Q4)) AS u (product, quarter, quantity)"
);

pretty_assertions::assert_eq!(
verified_only_select(sql).from[0].relation,
Unpivot {
table: Box::new(TableFactor::Table {
name: ObjectName::from(vec![Ident::new("sales")]),
alias: Some(TableAlias {
name: Ident::new("s"),
columns: vec![]
}),
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
let base_unpivot = Unpivot {
table: Box::new(TableFactor::Table {
name: ObjectName::from(vec![Ident::new("sales")]),
alias: Some(TableAlias {
name: Ident::new("s"),
columns: vec![],
}),
value: Ident {
value: "quantity".to_string(),
quote_style: None,
span: Span::empty()
},
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
sample: None,
index_hints: vec![],
}),
null_inclusion: None,
value: Ident {
value: "quantity".to_string(),
quote_style: None,
span: Span::empty(),
},

name: Ident {
value: "quarter".to_string(),
quote_style: None,
span: Span::empty()
},
columns: ["Q1", "Q2", "Q3", "Q4"]
name: Ident {
value: "quarter".to_string(),
quote_style: None,
span: Span::empty(),
},
columns: ["Q1", "Q2", "Q3", "Q4"]
.into_iter()
.map(Ident::new)
.collect(),
alias: Some(TableAlias {
name: Ident::new("u"),
columns: ["product", "quarter", "quantity"]
.into_iter()
.map(Ident::new)
.map(TableAliasColumnDef::from_name)
.collect(),
alias: Some(TableAlias {
name: Ident::new("u"),
columns: ["product", "quarter", "quantity"]
.into_iter()
.map(TableAliasColumnDef::from_name)
.collect(),
}),
}
);
}),
};
pretty_assertions::assert_eq!(verified_only_select(sql).from[0].relation, base_unpivot);
assert_eq!(verified_stmt(sql).to_string(), sql);

let sql_without_aliases = concat!(
Expand All @@ -10808,6 +10806,38 @@ fn parse_unpivot_table() {
verified_stmt(sql_without_aliases).to_string(),
sql_without_aliases
);

let sql_unpivot_exclude_nulls = concat!(
"SELECT * FROM sales AS s ",
"UNPIVOT EXCLUDE NULLS (quantity FOR quarter IN (Q1, Q2, Q3, Q4)) AS u (product, quarter, quantity)"
);

if let Unpivot { null_inclusion, .. } =
&verified_only_select(sql_unpivot_exclude_nulls).from[0].relation
{
assert_eq!(*null_inclusion, Some(NullInclusion::ExcludeNulls));
}

assert_eq!(
verified_stmt(sql_unpivot_exclude_nulls).to_string(),
sql_unpivot_exclude_nulls
);

let sql_unpivot_include_nulls = concat!(
"SELECT * FROM sales AS s ",
"UNPIVOT INCLUDE NULLS (quantity FOR quarter IN (Q1, Q2, Q3, Q4)) AS u (product, quarter, quantity)"
);

if let Unpivot { null_inclusion, .. } =
&verified_only_select(sql_unpivot_include_nulls).from[0].relation
{
assert_eq!(*null_inclusion, Some(NullInclusion::IncludeNulls));
}

assert_eq!(
verified_stmt(sql_unpivot_include_nulls).to_string(),
sql_unpivot_include_nulls
);
}

#[test]
Expand Down Expand Up @@ -10904,6 +10934,7 @@ fn parse_pivot_unpivot_table() {
sample: None,
index_hints: vec![],
}),
null_inclusion: None,
value: Ident {
value: "population".to_string(),
quote_style: None,
Expand Down