Skip to content

feat: Add support for MSSQL table options #1414

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 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
113 changes: 110 additions & 3 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,15 @@ pub enum CreateTableOptions {
/// e.g. `WITH (description = "123")`
///
/// <https://www.postgresql.org/docs/current/sql-createtable.html>
///
/// T-sql supports more specific options that's not only key-value pairs.
///
/// WITH (
/// DISTRIBUTION = ROUND_ROBIN,
/// CLUSTERED INDEX (column_a DESC, column_b)
/// )
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#syntax>
With(Vec<SqlOption>),
/// Options specified using the `OPTIONS` keyword.
/// e.g. `OPTIONS(description = "123")`
Expand Down Expand Up @@ -5589,14 +5598,112 @@ pub struct HiveFormat {
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct SqlOption {
pub struct ClusteredIndex {
pub name: Ident,
pub value: Expr,
pub asc: Option<bool>,
}

impl fmt::Display for ClusteredIndex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)?;
match self.asc {
Some(true) => write!(f, " ASC"),
Some(false) => write!(f, " DESC"),
_ => Ok(()),
}
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum TableOptionsClustered {
ColumnstoreIndex,
ColumnstoreIndexOrder(Vec<Ident>),
Index(Vec<ClusteredIndex>),
}

impl fmt::Display for TableOptionsClustered {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TableOptionsClustered::ColumnstoreIndex => {
write!(f, "CLUSTERED COLUMNSTORE INDEX")
}
TableOptionsClustered::ColumnstoreIndexOrder(values) => {
write!(
f,
"CLUSTERED COLUMNSTORE INDEX ORDER ({})",
display_comma_separated(values)
)
}
TableOptionsClustered::Index(values) => {
write!(f, "CLUSTERED INDEX ({})", display_comma_separated(values))
}
}
}
}

/// Specifies which partition the boundary values on table partitioning belongs to.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum PartitionRangeDirection {
Left,
Right,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum SqlOption {
/// Clustered represents the clustered version of table storage for T-sql.
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#TableOptions>
Clustered(TableOptionsClustered),
/// Single identifier options, e.g. `HEAP`.
Ident(Ident),
/// Any option that consists of a key value pair where the value is an expression.
KeyValue { name: Ident, value: Expr },
/// One or more table partitions and represents which partition the boundary values belong to.
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#TablePartitionOptions>
Partition {
column_name: Ident,
range_direction: Option<PartitionRangeDirection>,
for_values: Vec<Expr>,
},
}

impl fmt::Display for SqlOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} = {}", self.name, self.value)
match self {
SqlOption::Clustered(c) => write!(f, "{}", c),
SqlOption::Ident(ident) => {
write!(f, "{}", ident)
}
SqlOption::KeyValue { name, value } => {
write!(f, "{} = {}", name, value)
}
SqlOption::Partition {
column_name,
range_direction,
for_values,
} => {
let direction = match range_direction {
Some(PartitionRangeDirection::Left) => " LEFT",
Some(PartitionRangeDirection::Right) => " RIGHT",
None => "",
};

write!(
f,
"PARTITION ({} RANGE{} FOR VALUES ({}))",
column_name,
direction,
display_comma_separated(for_values)
)
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ define_keywords!(
COLLECTION,
COLUMN,
COLUMNS,
COLUMNSTORE,
COMMENT,
COMMIT,
COMMITTED,
Expand Down Expand Up @@ -354,6 +355,7 @@ define_keywords!(
HASH,
HAVING,
HEADER,
HEAP,
HIGH_PRIORITY,
HISTORY,
HIVEVAR,
Expand Down
108 changes: 104 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4458,7 +4458,7 @@ impl<'a> Parser<'a> {
let name = self.parse_object_name(allow_unquoted_hyphen)?;
let columns = self.parse_view_columns()?;
let mut options = CreateTableOptions::None;
let with_options = self.parse_options(Keyword::WITH)?;
let with_options = self.parse_table_options(Keyword::WITH)?;
if !with_options.is_empty() {
options = CreateTableOptions::With(with_options);
}
Expand Down Expand Up @@ -5621,7 +5621,8 @@ impl<'a> Parser<'a> {
let clustered_by = self.parse_optional_clustered_by()?;
let hive_formats = self.parse_hive_formats()?;
// PostgreSQL supports `WITH ( options )`, before `AS`
let with_options = self.parse_options(Keyword::WITH)?;
// T-sql supports `WITH` options for clustering and distribution
let with_options = self.parse_table_options(Keyword::WITH)?;
let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;

let engine = if self.parse_keyword(Keyword::ENGINE) {
Expand Down Expand Up @@ -6399,6 +6400,17 @@ impl<'a> Parser<'a> {
Ok(None)
}

pub fn parse_table_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
if self.parse_keyword(keyword) {
self.expect_token(&Token::LParen)?;
let options = self.parse_comma_separated(Parser::parse_sql_option)?;
self.expect_token(&Token::RParen)?;
Ok(options)
} else {
Ok(vec![])
}
}

pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
if self.parse_keyword(keyword) {
self.expect_token(&Token::LParen)?;
Expand Down Expand Up @@ -6484,11 +6496,99 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
pub fn parse_key_value(&mut self) -> Result<(Ident, Expr), ParserError> {
let name = self.parse_identifier(false)?;
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;
Ok(SqlOption { name, value })

Ok((name, value))
}

pub fn parse_sql_option(&mut self) -> Result<SqlOption, ParserError> {
let next_token = self.peek_token();
let is_mssql = dialect_of!(self is MsSqlDialect|GenericDialect);

let Token::Word(w) = next_token.token else {
let (name, value) = self.parse_key_value()?;
return Ok(SqlOption::KeyValue { name, value });
};

match w.keyword {
Keyword::HEAP if is_mssql => Ok(SqlOption::Ident(self.parse_identifier(false)?)),
Keyword::PARTITION if is_mssql => self.parse_table_option_partition(),
Keyword::CLUSTERED if is_mssql => self.parse_table_option_clustered(),
_ => {
let (name, value) = self.parse_key_value()?;
Ok(SqlOption::KeyValue { name, value })
}
}
}

pub fn parse_table_option_clustered(&mut self) -> Result<SqlOption, ParserError> {
self.expect_keyword(Keyword::CLUSTERED)?;

if self.parse_keywords(&[Keyword::COLUMNSTORE, Keyword::INDEX]) {
if self.parse_keyword(Keyword::ORDER) {
Ok(SqlOption::Clustered(
TableOptionsClustered::ColumnstoreIndexOrder(
self.parse_parenthesized_column_list(IsOptional::Mandatory, false)?,
),
))
} else {
Ok(SqlOption::Clustered(
TableOptionsClustered::ColumnstoreIndex,
))
}
} else {
self.expect_keyword(Keyword::INDEX)?;
self.expect_token(&Token::LParen)?;

let columns = self.parse_comma_separated(|p| {
let name = p.parse_identifier(false)?;
let asc = if p.parse_keyword(Keyword::ASC) {
Some(true)
} else if p.parse_keyword(Keyword::DESC) {
Some(false)
} else {
None
};

Ok(ClusteredIndex { name, asc })
})?;

self.expect_token(&Token::RParen)?;

Ok(SqlOption::Clustered(TableOptionsClustered::Index(columns)))
}
}

pub fn parse_table_option_partition(&mut self) -> Result<SqlOption, ParserError> {
self.expect_keyword(Keyword::PARTITION)?;
self.expect_token(&Token::LParen)?;
let column_name = self.parse_identifier(false)?;

self.expect_keyword(Keyword::RANGE)?;
let range_direction = if self.parse_keyword(Keyword::LEFT) {
Some(PartitionRangeDirection::Left)
} else if self.parse_keyword(Keyword::RIGHT) {
Some(PartitionRangeDirection::Right)
} else {
None
};

self.expect_keywords(&[Keyword::FOR, Keyword::VALUES])?;
self.expect_token(&Token::LParen)?;

let for_values = self.parse_comma_separated(Parser::parse_expr)?;

self.expect_token(&Token::RParen)?;
self.expect_token(&Token::RParen)?;

Ok(SqlOption::Partition {
column_name,
range_direction,
for_values,
})
}

pub fn parse_partition(&mut self) -> Result<Partition, ParserError> {
Expand Down
14 changes: 7 additions & 7 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn parse_create_view_with_options() {
ViewColumnDef {
name: Ident::new("age"),
data_type: None,
options: Some(vec![SqlOption {
options: Some(vec![SqlOption::KeyValue {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString("field age".to_string())),
}])
Expand All @@ -288,7 +288,7 @@ fn parse_create_view_with_options() {
unreachable!()
};
assert_eq!(
&SqlOption {
&SqlOption::KeyValue {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString(
"a view that expires in 2 days".to_string()
Expand Down Expand Up @@ -415,7 +415,7 @@ fn parse_create_table_with_options() {
},
ColumnOptionDef {
name: None,
option: ColumnOption::Options(vec![SqlOption {
option: ColumnOption::Options(vec![SqlOption::KeyValue {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString(
"field x".to_string()
Expand All @@ -430,7 +430,7 @@ fn parse_create_table_with_options() {
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Options(vec![SqlOption {
option: ColumnOption::Options(vec![SqlOption::KeyValue {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString(
"field y".to_string()
Expand All @@ -449,11 +449,11 @@ fn parse_create_table_with_options() {
Ident::new("age"),
])),
Some(vec![
SqlOption {
SqlOption::KeyValue {
name: Ident::new("partition_expiration_days"),
value: Expr::Value(number("1")),
},
SqlOption {
SqlOption::KeyValue {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString(
"table option description".to_string()
Expand Down Expand Up @@ -2010,7 +2010,7 @@ fn test_bigquery_create_function() {
function_body: Some(CreateFunctionBody::AsAfterOptions(Expr::Value(number(
"42"
)))),
options: Some(vec![SqlOption {
options: Some(vec![SqlOption::KeyValue {
name: Ident::new("x"),
value: Expr::Value(Value::SingleQuotedString("y".into())),
}]),
Expand Down
Loading
Loading