Skip to content

Commit 3845c7f

Browse files
committed
fix: Add new trait methos for insert format, use maybe_parse
1 parent 2943608 commit 3845c7f

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/dialect/clickhouse.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ impl Dialect for ClickHouseDialect {
5555
true
5656
}
5757

58+
fn supports_insert_format(&self) -> bool {
59+
true
60+
}
61+
5862
// ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
5963
// with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
6064
//

src/dialect/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,11 @@ pub trait Dialect: Debug + Any {
797797
fn supports_insert_table_function(&self) -> bool {
798798
false
799799
}
800+
801+
/// Does the dialect support insert formats, e.g. `INSERT INTO ... FORMAT <format>`
802+
fn supports_insert_format(&self) -> bool {
803+
false
804+
}
800805
}
801806

802807
/// This represents the operators for which precedence must be defined

src/parser/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12066,8 +12066,7 @@ impl<'a> Parser<'a> {
1206612066
(columns, partitioned, after_columns, source, assignments)
1206712067
};
1206812068

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

1208112080
(format, settings)
1208212081
} else {
12083-
(None, None)
12082+
Default::default()
1208412083
};
1208512084

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

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

1218712184
Ok(InputFormatClause { ident, values })

tests/sqlparser_clickhouse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ fn test_insert_query_with_format_clause() {
14201420
];
14211421

14221422
for sql in &cases {
1423-
clickhouse_and_generic().verified_stmt(sql);
1423+
clickhouse().verified_stmt(sql);
14241424
}
14251425
}
14261426

0 commit comments

Comments
 (0)