Skip to content
Open
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
11 changes: 6 additions & 5 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9141,17 +9141,18 @@ pub enum SqlOption {
///
/// <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` for MSSQL or a PostgreSQL storage parameter
/// named without a value.
/// Option name without a value, e.g. `HEAP` for MSSQL or a PostgreSQL storage
/// parameter. The name may be namespaced, e.g. `toast.autovacuum_enabled`.
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#TableOptions>
Ident(Ident),
Ident(ObjectName),
/// Any option that consists of a key value pair where the value is an expression. e.g.
///
/// WITH(DISTRIBUTION = ROUND_ROBIN)
/// WITH (toast.autovacuum_enabled = false)
KeyValue {
/// The option key identifier.
key: Ident,
/// The option key.
key: ObjectName,
/// The expression value for the option.
value: Expr,
},
Expand Down
22 changes: 20 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,8 @@ impl Spanned for SqlOption {
fn span(&self) -> Span {
match self {
SqlOption::Clustered(table_options_clustered) => table_options_clustered.span(),
SqlOption::Ident(ident) => ident.span,
SqlOption::KeyValue { key, value } => key.span.union(&value.span()),
SqlOption::Ident(ident) => ident.span(),
SqlOption::KeyValue { key, value } => key.span().union(&value.span()),
SqlOption::Partition {
column_name,
range_direction: _,
Expand Down Expand Up @@ -3427,4 +3427,22 @@ SETTINGS update_parallel_mode = 'auto'"#;
assert_eq!(test.get_source(expr.span()), expected, "{sql}");
}
}

#[test]
fn test_namespaced_storage_parameter_span() {
let dialect = &crate::dialect::PostgreSqlDialect {};
let sql = "CREATE TABLE t (a INT) WITH (toast.autovacuum_enabled = false)";
let mut test = SpanTest::new(dialect, sql);
let options = match test.0.parse_statement().unwrap() {
Statement::CreateTable(crate::ast::CreateTable {
table_options: crate::ast::CreateTableOptions::With(options),
..
}) => options,
stmt => panic!("expected CREATE TABLE WITH, got {stmt:?}"),
};
assert_eq!(
test.get_source(options[0].span()),
"toast.autovacuum_enabled = false"
);
}
}
13 changes: 9 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9722,7 +9722,9 @@ impl<'a> Parser<'a> {
// Single parameter option
// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
if self.parse_keywords(&[Keyword::START, Keyword::TRANSACTION]) {
return Ok(Some(SqlOption::Ident(Ident::new("START TRANSACTION"))));
return Ok(Some(SqlOption::Ident(
Ident::new("START TRANSACTION").into(),
)));
}

// Custom option
Expand Down Expand Up @@ -9908,7 +9910,10 @@ impl<'a> Parser<'a> {
None => Expr::Identifier(self.parse_identifier()?),
};

Ok(Some(SqlOption::KeyValue { key, value }))
Ok(Some(SqlOption::KeyValue {
key: key.into(),
value,
}))
}

/// Parse plain options.
Expand Down Expand Up @@ -11083,7 +11088,7 @@ impl<'a> Parser<'a> {

match &self.peek_token_ref().token {
Token::Word(w) if w.keyword == Keyword::HEAP && is_mssql => {
Ok(SqlOption::Ident(self.parse_identifier()?))
Ok(SqlOption::Ident(ObjectName::from(self.parse_identifier()?)))
}
Token::Word(w) if w.keyword == Keyword::PARTITION && is_mssql => {
self.parse_option_partition()
Expand All @@ -11092,7 +11097,7 @@ impl<'a> Parser<'a> {
self.parse_option_clustered()
}
_ => {
let name = self.parse_identifier()?;
let name = ObjectName::from(self.parse_period_separated(|p| p.parse_identifier())?);
if self.dialect.supports_sql_option_without_value()
&& matches!(self.peek_token_ref().token, Token::Comma | Token::RParen)
{
Expand Down
26 changes: 13 additions & 13 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ fn parse_create_view_with_options() {
data_type: None,
options: Some(ColumnOptions::CommaSeparated(vec![ColumnOption::Options(
vec![SqlOption::KeyValue {
key: Ident::new("description"),
key: Ident::new("description").into(),
value: Expr::Value(
Value::DoubleQuotedString("field age".to_string()).with_span(
Span::new(Location::new(1, 42), Location::new(1, 52))
Expand All @@ -384,7 +384,7 @@ fn parse_create_view_with_options() {
};
assert_eq!(
&SqlOption::KeyValue {
key: Ident::new("description"),
key: Ident::new("description").into(),
value: Expr::Value(
Value::DoubleQuotedString("a view that expires in 2 days".to_string())
.with_empty_span()
Expand Down Expand Up @@ -511,7 +511,7 @@ fn parse_create_table_with_options() {
ColumnOptionDef {
name: None,
option: ColumnOption::Options(vec![SqlOption::KeyValue {
key: Ident::new("description"),
key: Ident::new("description").into(),
value: Expr::Value(
Value::DoubleQuotedString("field x".to_string()).with_span(
Span::new(Location::new(1, 42), Location::new(1, 52))
Expand All @@ -527,7 +527,7 @@ fn parse_create_table_with_options() {
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Options(vec![SqlOption::KeyValue {
key: Ident::new("description"),
key: Ident::new("description").into(),
value: Expr::Value(
Value::DoubleQuotedString("field y".to_string()).with_span(
Span::new(Location::new(1, 42), Location::new(1, 52))
Expand All @@ -548,7 +548,7 @@ fn parse_create_table_with_options() {
])),
CreateTableOptions::Options(vec![
SqlOption::KeyValue {
key: Ident::new("partition_expiration_days"),
key: Ident::new("partition_expiration_days").into(),
value: Expr::Value(
number("1").with_span(Span::new(
Location::new(1, 42),
Expand All @@ -557,7 +557,7 @@ fn parse_create_table_with_options() {
),
},
SqlOption::KeyValue {
key: Ident::new("description"),
key: Ident::new("description").into(),
value: Expr::Value(
Value::DoubleQuotedString("table option description".to_string())
.with_span(Span::new(
Expand Down Expand Up @@ -2342,7 +2342,7 @@ fn test_bigquery_create_function() {
number("42").with_empty_span()
))),
options: Some(vec![SqlOption::KeyValue {
key: Ident::new("x"),
key: Ident::new("x").into(),
value: Expr::Value(Value::SingleQuotedString("y".into()).with_empty_span()),
}]),
behavior: None,
Expand Down Expand Up @@ -2710,20 +2710,20 @@ fn test_export_data() {
Statement::ExportData(ExportData {
options: vec![
SqlOption::KeyValue {
key: Ident::new("uri"),
key: Ident::new("uri").into(),
value: Expr::Value(
Value::SingleQuotedString("gs://bucket/folder/*".to_owned())
.with_empty_span()
),
},
SqlOption::KeyValue {
key: Ident::new("format"),
key: Ident::new("format").into(),
value: Expr::Value(
Value::SingleQuotedString("PARQUET".to_owned()).with_empty_span()
),
},
SqlOption::KeyValue {
key: Ident::new("overwrite"),
key: Ident::new("overwrite").into(),
value: Expr::Value(Value::Boolean(true).with_empty_span()),
},
],
Expand Down Expand Up @@ -2812,20 +2812,20 @@ fn test_export_data() {
Statement::ExportData(ExportData {
options: vec![
SqlOption::KeyValue {
key: Ident::new("uri"),
key: Ident::new("uri").into(),
value: Expr::Value(
Value::SingleQuotedString("gs://bucket/folder/*".to_owned())
.with_empty_span()
),
},
SqlOption::KeyValue {
key: Ident::new("format"),
key: Ident::new("format").into(),
value: Expr::Value(
Value::SingleQuotedString("PARQUET".to_owned()).with_empty_span()
),
},
SqlOption::KeyValue {
key: Ident::new("overwrite"),
key: Ident::new("overwrite").into(),
value: Expr::Value(Value::Boolean(true).with_empty_span()),
},
],
Expand Down
46 changes: 25 additions & 21 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5257,13 +5257,13 @@ fn parse_create_table_with_options() {
assert_eq!(
vec![
SqlOption::KeyValue {
key: "foo".into(),
key: Ident::new("foo").into(),
value: Expr::Value(
(Value::SingleQuotedString("bar".into())).with_empty_span()
),
},
SqlOption::KeyValue {
key: "a".into(),
key: Ident::new("a").into(),
value: Expr::value(number("123")),
},
],
Expand Down Expand Up @@ -5514,7 +5514,8 @@ fn parse_alter_table() {
value: "classification".to_string(),
quote_style: Some('\''),
span: Span::empty(),
},
}
.into(),
value: Expr::Value(
(Value::SingleQuotedString("parquet".to_string())).with_empty_span()
),
Expand All @@ -5535,15 +5536,17 @@ fn parse_alter_table() {
value: "autovacuum_vacuum_scale_factor".to_string(),
quote_style: None,
span: Span::empty(),
},
}
.into(),
value: Expr::Value(test_utils::number("0.01").with_empty_span()),
},
SqlOption::KeyValue {
key: Ident {
value: "autovacuum_vacuum_threshold".to_string(),
quote_style: None,
span: Span::empty(),
},
}
.into(),
value: Expr::Value(test_utils::number("500").with_empty_span()),
}
],
Expand Down Expand Up @@ -5688,13 +5691,13 @@ fn parse_alter_view_with_options() {
assert_eq!(
vec![
SqlOption::KeyValue {
key: "foo".into(),
key: Ident::new("foo").into(),
value: Expr::Value(
(Value::SingleQuotedString("bar".into())).with_empty_span()
),
},
SqlOption::KeyValue {
key: "a".into(),
key: Ident::new("a").into(),
value: Expr::value(number("123")),
},
],
Expand Down Expand Up @@ -9241,13 +9244,13 @@ fn parse_create_view_with_options() {
assert_eq!(
CreateTableOptions::With(vec![
SqlOption::KeyValue {
key: "foo".into(),
key: Ident::new("foo").into(),
value: Expr::Value(
(Value::SingleQuotedString("bar".into())).with_empty_span()
),
},
SqlOption::KeyValue {
key: "a".into(),
key: Ident::new("a").into(),
value: Expr::value(number("123")),
},
]),
Expand Down Expand Up @@ -12043,11 +12046,11 @@ fn parse_cache_table() {
has_as: false,
options: vec![
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K1"),
key: Ident::with_quote('\'', "K1").into(),
value: Expr::Value((Value::SingleQuotedString("V1".into())).with_empty_span()),
},
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K2"),
key: Ident::with_quote('\'', "K2").into(),
value: Expr::value(number("0.88")),
},
],
Expand All @@ -12068,11 +12071,11 @@ fn parse_cache_table() {
has_as: false,
options: vec![
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K1"),
key: Ident::with_quote('\'', "K1").into(),
value: Expr::Value((Value::SingleQuotedString("V1".into())).with_empty_span()),
},
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K2"),
key: Ident::with_quote('\'', "K2").into(),
value: Expr::value(number("0.88")),
},
],
Expand All @@ -12093,11 +12096,11 @@ fn parse_cache_table() {
has_as: true,
options: vec![
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K1"),
key: Ident::with_quote('\'', "K1").into(),
value: Expr::Value((Value::SingleQuotedString("V1".into())).with_empty_span()),
},
SqlOption::KeyValue {
key: Ident::with_quote('\'', "K2"),
key: Ident::with_quote('\'', "K2").into(),
value: Expr::value(number("0.88")),
},
],
Expand Down Expand Up @@ -13656,7 +13659,8 @@ fn parse_unload() {
value: "format".to_string(),
quote_style: None,
span: Span::empty(),
},
}
.into(),
value: Expr::Value(
(Value::SingleQuotedString("AVRO".to_string())).with_empty_span()
)
Expand Down Expand Up @@ -15907,13 +15911,13 @@ fn test_create_connector() {
with_dcproperties,
Some(vec![
SqlOption::KeyValue {
key: Ident::with_quote('\'', "user"),
key: Ident::with_quote('\'', "user").into(),
value: Expr::Value(
(Value::SingleQuotedString("root".to_string())).with_empty_span()
)
},
SqlOption::KeyValue {
key: Ident::with_quote('\'', "password"),
key: Ident::with_quote('\'', "password").into(),
value: Expr::Value(
(Value::SingleQuotedString("password".to_string())).with_empty_span()
)
Expand Down Expand Up @@ -15978,13 +15982,13 @@ fn test_alter_connector() {
properties,
Some(vec![
SqlOption::KeyValue {
key: Ident::with_quote('\'', "user"),
key: Ident::with_quote('\'', "user").into(),
value: Expr::Value(
(Value::SingleQuotedString("root".to_string())).with_empty_span()
)
},
SqlOption::KeyValue {
key: Ident::with_quote('\'', "password"),
key: Ident::with_quote('\'', "password").into(),
value: Expr::Value(
(Value::SingleQuotedString("password".to_string())).with_empty_span()
)
Expand Down Expand Up @@ -22142,7 +22146,7 @@ fn parse_cache_table_options_without_flag() {
table_name: ObjectName::from(vec![Ident::new("t")]),
has_as,
options: vec![SqlOption::KeyValue {
key: Ident::with_quote('\'', "k"),
key: Ident::with_quote('\'', "k").into(),
value: Expr::Value(Value::SingleQuotedString("v".into()).with_empty_span()),
}],
query,
Expand Down
Loading
Loading