Skip to content
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

Support SETTINGS pairs for ClickHouse dialect #1327

Merged
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
2 changes: 1 addition & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub use self::query::{
MatchRecognizePattern, MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr,
NonBlock, Offset, OffsetRows, OrderByExpr, PivotValueSource, Query, RenameSelectItem,
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, SymbolDefinition, Table,
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
TableAlias, TableFactor, TableVersion, TableWithJoins, Top, TopQuantity, ValueTableMode,
Values, WildcardAdditionalOptions, With,
};
Expand Down
21 changes: 21 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ pub struct Query {
/// `FOR JSON { AUTO | PATH } [ , INCLUDE_NULL_VALUES ]`
/// (MSSQL-specific)
pub for_clause: Option<ForClause>,
/// ClickHouse syntax: `SELECT * FROM t SETTINGS key1 = value1, key2 = value2`
///
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query)
pub settings: Option<Vec<Setting>>,
}

impl fmt::Display for Query {
Expand All @@ -70,6 +74,9 @@ impl fmt::Display for Query {
if !self.limit_by.is_empty() {
write!(f, " BY {}", display_separated(&self.limit_by, ", "))?;
}
if let Some(ref settings) = self.settings {
write!(f, " SETTINGS {}", display_comma_separated(settings))?;
}
if let Some(ref fetch) = self.fetch {
write!(f, " {fetch}")?;
}
Expand Down Expand Up @@ -828,6 +835,20 @@ impl fmt::Display for ConnectBy {
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct Setting {
pub key: Ident,
pub value: Value,
}

impl fmt::Display for Setting {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} = {}", self.key, self.value)
}
}

/// An expression optionally followed by an alias.
///
/// Example:
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ define_keywords!(
SESSION_USER,
SET,
SETS,
SETTINGS,
SHARE,
SHOW,
SIMILAR,
Expand Down Expand Up @@ -850,6 +851,8 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
Keyword::FOR,
// for MYSQL PARTITION SELECTION
Keyword::PARTITION,
// for ClickHouse SELECT * FROM t SETTINGS ...
Keyword::SETTINGS,
// for Snowflake START WITH .. CONNECT BY
Keyword::START,
Keyword::CONNECT,
Expand Down
18 changes: 18 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7871,6 +7871,7 @@ impl<'a> Parser<'a> {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})
} else if self.parse_keyword(Keyword::UPDATE) {
Ok(Query {
Expand All @@ -7883,6 +7884,7 @@ impl<'a> Parser<'a> {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})
} else {
let body = self.parse_boxed_query_body(0)?;
Expand Down Expand Up @@ -7928,6 +7930,20 @@ impl<'a> Parser<'a> {
vec![]
};

let settings = if dialect_of!(self is ClickHouseDialect|GenericDialect)
&& self.parse_keyword(Keyword::SETTINGS)
{
let key_values = self.parse_comma_separated(|p| {
let key = p.parse_identifier(false)?;
p.expect_token(&Token::Eq)?;
let value = p.parse_value()?;
Ok(Setting { key, value })
})?;
Some(key_values)
} else {
None
};

let fetch = if self.parse_keyword(Keyword::FETCH) {
Some(self.parse_fetch()?)
} else {
Expand Down Expand Up @@ -7955,6 +7971,7 @@ impl<'a> Parser<'a> {
fetch,
locks,
for_clause,
settings,
})
}
}
Expand Down Expand Up @@ -9091,6 +9108,7 @@ impl<'a> Parser<'a> {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
alias,
})
Expand Down
38 changes: 37 additions & 1 deletion tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use test_utils::*;
use sqlparser::ast::Expr::{BinaryOp, Identifier, MapAccess};
use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::TableFactor::Table;
use sqlparser::ast::Value::Number;
use sqlparser::ast::*;

use sqlparser::dialect::ClickHouseDialect;
use sqlparser::dialect::GenericDialect;

Expand Down Expand Up @@ -549,6 +549,42 @@ fn parse_limit_by() {
);
}

#[test]
fn parse_settings_in_query() {
match clickhouse_and_generic()
.verified_stmt(r#"SELECT * FROM t SETTINGS max_threads = 1, max_block_size = 10000"#)
{
Statement::Query(query) => {
assert_eq!(
query.settings,
Some(vec![
Setting {
key: Ident::new("max_threads"),
value: Number("1".parse().unwrap(), false)
},
Setting {
key: Ident::new("max_block_size"),
value: Number("10000".parse().unwrap(), false)
},
])
);
}
_ => unreachable!(),
}

let invalid_cases = vec![
"SELECT * FROM t SETTINGS a",
"SELECT * FROM t SETTINGS a=",
"SELECT * FROM t SETTINGS a=1, b",
"SELECT * FROM t SETTINGS a=1, b=",
"SELECT * FROM t SETTINGS a=1, b=c",
];
for sql in invalid_cases {
clickhouse_and_generic()
.parse_sql_statements(sql)
.expect_err("Expected: SETTINGS key = value, found: ");
}
}
#[test]
fn parse_select_star_except() {
clickhouse().verified_stmt("SELECT * EXCEPT (prev_status) FROM anomalies");
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ fn parse_update_set_from() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
alias: Some(TableAlias {
name: Ident::new("t2"),
Expand Down Expand Up @@ -3427,6 +3428,7 @@ fn parse_create_table_as_table() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
});

match verified_stmt(sql1) {
Expand All @@ -3452,6 +3454,7 @@ fn parse_create_table_as_table() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
});

match verified_stmt(sql2) {
Expand Down Expand Up @@ -4996,6 +4999,7 @@ fn parse_interval_and_or_xor() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))];

assert_eq!(actual_ast, expected_ast);
Expand Down Expand Up @@ -7649,6 +7653,7 @@ fn parse_merge() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
alias: Some(TableAlias {
name: Ident {
Expand Down Expand Up @@ -9156,6 +9161,7 @@ fn parse_unload() {
locks: vec![],
for_clause: None,
order_by: vec![],
settings: None,
}),
to: Ident {
value: "s3://...".to_string(),
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn parse_create_procedure() {
locks: vec![],
for_clause: None,
order_by: vec![],
settings: None,
body: Box::new(SetExpr::Select(Box::new(Select {
distinct: None,
top: None,
Expand Down Expand Up @@ -546,6 +547,7 @@ fn parse_substring_in_select() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
query
);
Expand Down
15 changes: 15 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ fn parse_escaped_quote_identifiers_with_escape() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))
);
}
Expand Down Expand Up @@ -972,6 +973,7 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))
);
}
Expand Down Expand Up @@ -1016,6 +1018,7 @@ fn parse_escaped_backticks_with_escape() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))
);
}
Expand Down Expand Up @@ -1060,6 +1063,7 @@ fn parse_escaped_backticks_with_no_escape() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))
);
}
Expand Down Expand Up @@ -1264,6 +1268,7 @@ fn parse_simple_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1306,6 +1311,7 @@ fn parse_ignore_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1348,6 +1354,7 @@ fn parse_priority_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1387,6 +1394,7 @@ fn parse_priority_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1434,6 +1442,7 @@ fn parse_insert_as() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1493,6 +1502,7 @@ fn parse_insert_as() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1536,6 +1546,7 @@ fn parse_replace_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1573,6 +1584,7 @@ fn parse_empty_row_insert() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -1633,6 +1645,7 @@ fn parse_insert_with_on_duplicate_update() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
})),
source
);
Expand Down Expand Up @@ -2273,6 +2286,7 @@ fn parse_substring_in_select() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
query
);
Expand Down Expand Up @@ -2578,6 +2592,7 @@ fn parse_hex_string_introducer() {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}))
)
}
Expand Down
Loading
Loading