Support SETTINGS pairs for ClickHouse dialect (#1327)

Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
This commit is contained in:
hulk 2024-07-07 19:17:43 +08:00 committed by GitHub
parent 44d7a20f64
commit 700bd03d6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 111 additions and 5 deletions

View file

@ -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,
};

View file

@ -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 {
@ -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}")?;
}
@ -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:

View file

@ -650,6 +650,7 @@ define_keywords!(
SESSION_USER,
SET,
SETS,
SETTINGS,
SHARE,
SHOW,
SIMILAR,
@ -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,

View file

@ -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 {
@ -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)?;
@ -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 {
@ -7955,6 +7971,7 @@ impl<'a> Parser<'a> {
fetch,
locks,
for_clause,
settings,
})
}
}
@ -9091,6 +9108,7 @@ impl<'a> Parser<'a> {
fetch: None,
locks: vec![],
for_clause: None,
settings: None,
}),
alias,
})