Skip to content

Parse SETTINGS clause for ClickHouse table-valued functions #1358

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 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub use self::query::{
OffsetRows, OrderBy, OrderByExpr, PivotValueSource, Query, RenameSelectItem,
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
TableAlias, TableFactor, TableVersion, TableWithJoins, Top, TopQuantity, ValueTableMode,
Values, WildcardAdditionalOptions, With, WithFill,
TableAlias, TableFactor, TableFunctionArgs, TableVersion, TableWithJoins, Top, TopQuantity,
ValueTableMode, Values, WildcardAdditionalOptions, With, WithFill,
};
pub use self::value::{
escape_double_quote_string, escape_quoted_string, DateTimeField, DollarQuotedString,
Expand Down
22 changes: 20 additions & 2 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,19 @@ impl fmt::Display for ExprWithAlias {
}
}

/// Arguments to a table-valued function
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct TableFunctionArgs {
pub args: Vec<FunctionArg>,
/// ClickHouse-specific SETTINGS clause.
/// For example,
/// `SELECT * FROM executable('generate_random.py', TabSeparated, 'id UInt32, random String', SETTINGS send_chunk_header = false, pool_size = 16)`
/// [`executable` table function](https://clickhouse.com/docs/en/engines/table-functions/executable)
pub settings: Option<Vec<Setting>>,
}

/// A table name or a parenthesized subquery with an optional alias
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -916,7 +929,7 @@ pub enum TableFactor {
/// This field's value is `Some(v)`, where `v` is a (possibly empty)
/// vector of arguments, in the case of a table-valued function call,
/// whereas it's `None` in the case of a regular table name.
args: Option<Vec<FunctionArg>>,
args: Option<TableFunctionArgs>,
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
with_hints: Vec<Expr>,
/// Optional version qualifier to facilitate table time-travel, as
Expand Down Expand Up @@ -1314,7 +1327,12 @@ impl fmt::Display for TableFactor {
write!(f, "PARTITION ({})", display_comma_separated(partitions))?;
}
if let Some(args) = args {
write!(f, "({})", display_comma_separated(args))?;
write!(f, "(")?;
write!(f, "{}", display_comma_separated(&args.args))?;
if let Some(ref settings) = args.settings {
write!(f, ", SETTINGS {}", display_comma_separated(settings))?;
}
write!(f, ")")?;
}
if *with_ordinality {
write!(f, " WITH ORDINALITY")?;
Expand Down
96 changes: 66 additions & 30 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3426,6 +3426,27 @@ impl<'a> Parser<'a> {
Ok(values)
}

fn parse_comma_separated_end(&mut self) -> Option<Token> {
if !self.consume_token(&Token::Comma) {
Some(Token::Comma)
} else if self.options.trailing_commas {
let token = self.peek_token().token;
match token {
Token::Word(ref kw)
if keywords::RESERVED_FOR_COLUMN_ALIAS.contains(&kw.keyword) =>
{
Some(token)
}
Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => {
Some(token)
}
_ => None,
}
} else {
None
}
}

/// Parse a comma-separated list of 1+ items accepted by `F`
pub fn parse_comma_separated<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
where
Expand All @@ -3434,22 +3455,8 @@ impl<'a> Parser<'a> {
let mut values = vec![];
loop {
values.push(f(self)?);
if !self.consume_token(&Token::Comma) {
if self.parse_comma_separated_end().is_some() {
break;
} else if self.options.trailing_commas {
match self.peek_token().token {
Token::Word(kw)
if keywords::RESERVED_FOR_COLUMN_ALIAS.contains(&kw.keyword) =>
{
break;
}
Token::RParen
| Token::SemiColon
| Token::EOF
| Token::RBracket
| Token::RBrace => break,
_ => continue,
}
}
}
Ok(values)
Expand Down Expand Up @@ -8099,19 +8106,7 @@ 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 settings = self.parse_settings()?;

let fetch = if self.parse_keyword(Keyword::FETCH) {
Some(self.parse_fetch()?)
Expand Down Expand Up @@ -8158,6 +8153,23 @@ impl<'a> Parser<'a> {
}
}

fn parse_settings(&mut self) -> Result<Option<Vec<Setting>>, ParserError> {
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
};
Ok(settings)
}

/// Parse a mssql `FOR [XML | JSON | BROWSE]` clause
pub fn parse_for_clause(&mut self) -> Result<Option<ForClause>, ParserError> {
if self.parse_keyword(Keyword::XML) {
Expand Down Expand Up @@ -9372,9 +9384,9 @@ impl<'a> Parser<'a> {
// Parse potential version qualifier
let version = self.parse_table_version()?;

// Postgres, MSSQL: table-valued functions:
// Postgres, MSSQL, ClickHouse: table-valued functions:
let args = if self.consume_token(&Token::LParen) {
Some(self.parse_optional_args()?)
Some(self.parse_table_function_args()?)
} else {
None
};
Expand Down Expand Up @@ -10317,6 +10329,30 @@ impl<'a> Parser<'a> {
}
}

fn parse_table_function_args(&mut self) -> Result<TableFunctionArgs, ParserError> {
{
let settings = self.parse_settings()?;
if self.consume_token(&Token::RParen) {
return Ok(TableFunctionArgs {
args: vec![],
settings,
});
}
}
let mut args = vec![];
let settings = loop {
if let Some(settings) = self.parse_settings()? {
break Some(settings);
}
args.push(self.parse_function_args()?);
if self.parse_comma_separated_end().is_some() {
break None;
}
};
self.expect_token(&Token::RParen)?;
Ok(TableFunctionArgs { args, settings })
}

/// Parses a potentially empty list of arguments to a window function
/// (including the closing parenthesis).
///
Expand Down
38 changes: 38 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,44 @@ fn parse_create_table_on_commit_and_as_query() {
}
}

#[test]
fn parse_select_table_function_settings() {
let sql = r#"SELECT * FROM table_function(arg, SETTINGS s0 = 3, s1 = 's')"#;
match clickhouse_and_generic().verified_stmt(sql) {
Statement::Query(q) => {
let from = &q.body.as_select().unwrap().from;
assert_eq!(from.len(), 1);
assert_eq!(from[0].joins, vec![]);
match &from[0].relation {
Table { args, .. } => {
let args = args.as_ref().unwrap();
assert_eq!(
args.args,
vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier("arg".into())
))]
);
assert_eq!(
args.settings,
Some(vec![
Setting {
key: "s0".into(),
value: Value::Number("3".parse().unwrap(), false)
},
Setting {
key: "s1".into(),
value: Value::SingleQuotedString("s".into())
}
])
)
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}

fn clickhouse() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {})],
Expand Down