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
5 changes: 3 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9111,9 +9111,10 @@ pub enum SqlOption {
/// 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
20 changes: 19 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ impl Spanned for SqlOption {
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::KeyValue { key, value } => key.span().union(&value.span()),
SqlOption::Partition {
column_name,
range_direction: _,
Expand Down Expand Up @@ -3423,4 +3423,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"
);
}
}
7 changes: 5 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9928,7 +9928,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 @@ -11112,7 +11115,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())?);
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;

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 @@ -2339,7 +2339,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 @@ -2707,20 +2707,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 @@ -2809,20 +2809,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 @@ -5253,13 +5253,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 @@ -5510,7 +5510,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 @@ -5531,15 +5532,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 @@ -5684,13 +5687,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 @@ -9231,13 +9234,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 @@ -12032,11 +12035,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 @@ -12057,11 +12060,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 @@ -12082,11 +12085,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 @@ -13640,7 +13643,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 @@ -15891,13 +15895,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 @@ -15962,13 +15966,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 @@ -22116,7 +22120,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
4 changes: 2 additions & 2 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ fn parse_create_table_with_valid_options() {
value: "DISTRIBUTION".to_string(),
quote_style: None,
span: Span::empty(),
},
}.into(),
value: Expr::Identifier(Ident {
value: "ROUND_ROBIN".to_string(),
quote_style: None,
Expand Down Expand Up @@ -1818,7 +1818,7 @@ fn parse_create_table_with_valid_options() {
value: "DISTRIBUTION".to_string(),
quote_style: None,
span: Span::empty(),
},
}.into(),
value: Expr::Function(
Function {
name: ObjectName::from(
Expand Down
Loading
Loading