Skip to content

Commit

Permalink
fix: Add new trait methos for insert format, use maybe_parse
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Jan 10, 2025
1 parent 2943608 commit 3845c7f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/dialect/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ impl Dialect for ClickHouseDialect {
true
}

fn supports_insert_format(&self) -> bool {
true
}

// ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
// with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
//
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,11 @@ pub trait Dialect: Debug + Any {
fn supports_insert_table_function(&self) -> bool {
false
}

/// Does the dialect support insert formats, e.g. `INSERT INTO ... FORMAT <format>`
fn supports_insert_format(&self) -> bool {
false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
11 changes: 4 additions & 7 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12066,8 +12066,7 @@ impl<'a> Parser<'a> {
(columns, partitioned, after_columns, source, assignments)
};

let (format_clause, settings) = if dialect_of!(self is ClickHouseDialect | GenericDialect)
{
let (format_clause, settings) = if self.dialect.supports_insert_format() {
// Settings always comes before `FORMAT` for ClickHouse:
// <https://clickhouse.com/docs/en/sql-reference/statements/insert-into>
let settings = self.parse_settings()?;
Expand All @@ -12080,7 +12079,7 @@ impl<'a> Parser<'a> {

(format, settings)
} else {
(None, None)
Default::default()
};

let insert_alias = if dialect_of!(self is MySqlDialect | GenericDialect)
Expand Down Expand Up @@ -12173,15 +12172,13 @@ impl<'a> Parser<'a> {
}
}

// Parses format clause used for [ClickHouse]. Formats are different when using `SELECT` and
// `INSERT` and also when using the CLI for pipes. For `INSERT` it can take an optional values
// list which we try to parse here.
// Parses input format clause used for [ClickHouse].
//
// <https://clickhouse.com/docs/en/interfaces/formats>
pub fn parse_input_format_clause(&mut self) -> Result<InputFormatClause, ParserError> {
let ident = self.parse_identifier()?;
let values = self
.try_parse(|p| p.parse_comma_separated(|p| p.parse_expr()))
.maybe_parse(|p| p.parse_comma_separated(|p| p.parse_expr()))?
.unwrap_or_default();

Ok(InputFormatClause { ident, values })
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ fn test_insert_query_with_format_clause() {
];

for sql in &cases {
clickhouse_and_generic().verified_stmt(sql);
clickhouse().verified_stmt(sql);
}
}

Expand Down

0 comments on commit 3845c7f

Please sign in to comment.