Skip to content
Open
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/clickhouse_datetime_zone.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE t (a DateTime('UTC'), b DateTime(3, 'Europe/Rome')) ENGINE = MergeTree ORDER BY a
21 changes: 17 additions & 4 deletions src/ast/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,11 @@ pub enum DataType {
///
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
Time(Option<u64>, TimezoneInfo),
/// Datetime with optional time precision, see [MySQL][1].
/// Datetime with optional precision and time zone, see [MySQL][1] and [ClickHouse][2].
///
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
Datetime(Option<u64>),
/// [2]: https://clickhouse.com/docs/sql-reference/data-types/datetime
Datetime(Option<u64>, Option<String>),
/// Datetime with time precision and optional timezone, see [ClickHouse][1].
///
/// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
Expand Down Expand Up @@ -743,8 +744,20 @@ impl fmt::Display for DataType {
DataType::Time(precision, timezone_info) => {
format_datetime_precision_and_tz(f, "TIME", precision, timezone_info)
}
DataType::Datetime(precision) => {
format_type_with_optional_length(f, "DATETIME", precision, false)
DataType::Datetime(precision, time_zone) => {
write!(f, "DATETIME")?;
match (precision, time_zone) {
(None, None) => Ok(()),
(Some(precision), None) => write!(f, "({precision})"),
(None, Some(time_zone)) => {
write!(f, "('{}')", escape_single_quote_string(time_zone))
}
(Some(precision), Some(time_zone)) => write!(
f,
"({precision}, '{}')",
escape_single_quote_string(time_zone)
),
}
}
DataType::Timestamp(precision, timezone_info) => {
format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
Expand Down
29 changes: 28 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13908,7 +13908,10 @@ impl<'a> Parser<'a> {
Keyword::UUID => Ok(DataType::Uuid),
Keyword::DATE => Ok(DataType::Date),
Keyword::DATE32 => Ok(DataType::Date32),
Keyword::DATETIME => Ok(DataType::Datetime(self.parse_optional_precision()?)),
Keyword::DATETIME => {
let (precision, time_zone) = self.parse_optional_datetime_arguments()?;
Ok(DataType::Datetime(precision, time_zone))
}
Keyword::DATETIME64 => {
self.prev_token();
let (precision, time_zone) = self.parse_datetime_64()?;
Expand Down Expand Up @@ -15222,6 +15225,30 @@ impl<'a> Parser<'a> {
Ok((precision, time_zone))
}

/// Parse the optional `(precision)`, `(time_zone)` or `(precision, time_zone)`
/// of a `DATETIME` type, see [ClickHouse].
///
/// [ClickHouse]: https://clickhouse.com/docs/sql-reference/data-types/datetime
fn parse_optional_datetime_arguments(
&mut self,
) -> Result<(Option<u64>, Option<String>), ParserError> {
if !self.consume_token(&Token::LParen) {
return Ok((None, None));
}
let precision = if matches!(self.peek_token_ref().token, Token::Number(..)) {
Some(self.parse_literal_uint()?)
} else {
None
};
let time_zone = if precision.is_none() || self.consume_token(&Token::Comma) {
Some(self.parse_literal_string()?)
} else {
None
};
self.expect_token(&Token::RParen)?;
Ok((precision, time_zone))
}

/// Parse an optional character length specification `(n | MAX [CHARACTERS|OCTETS])`.
pub fn parse_optional_character_length(
&mut self,
Expand Down
8 changes: 4 additions & 4 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ fn parse_typed_struct_syntax_bigquery() {
assert_eq!(
&Expr::Struct {
values: vec![Expr::TypedString(TypedString {
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
value: ValueWithSpan {
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into()),
span: Span::empty(),
Expand All @@ -926,7 +926,7 @@ fn parse_typed_struct_syntax_bigquery() {
})],
fields: vec![StructField {
field_name: None,
field_type: DataType::Datetime(None),
field_type: DataType::Datetime(None, None),
options: None,
}]
},
Expand Down Expand Up @@ -1259,7 +1259,7 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
assert_eq!(
&Expr::Struct {
values: vec![Expr::TypedString(TypedString {
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
value: ValueWithSpan {
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into()),
span: Span::empty(),
Expand All @@ -1268,7 +1268,7 @@ fn parse_typed_struct_syntax_bigquery_and_generic() {
})],
fields: vec![StructField {
field_name: None,
field_type: DataType::Datetime(None),
field_type: DataType::Datetime(None, None),
options: None,
}]
},
Expand Down
59 changes: 56 additions & 3 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ fn parse_create_table_with_variant_default_expressions() {
vec![
ColumnDef {
name: Ident::new("a"),
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Materialized(Expr::Function(Function {
Expand All @@ -863,7 +863,7 @@ fn parse_create_table_with_variant_default_expressions() {
},
ColumnDef {
name: Ident::new("b"),
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Ephemeral(Some(Expr::Function(Function {
Expand All @@ -884,7 +884,7 @@ fn parse_create_table_with_variant_default_expressions() {
},
ColumnDef {
name: Ident::new("c"),
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Ephemeral(None)
Expand Down Expand Up @@ -2485,3 +2485,56 @@ fn parse_unterminated_dollar_tag_spans() {
]
);
}

#[test]
fn parse_datetime_precision_and_time_zone() {
for (data_type, expected) in [
("DATETIME", DataType::Datetime(None, None)),
("DATETIME(3)", DataType::Datetime(Some(3), None)),
(
"DATETIME('UTC')",
DataType::Datetime(None, Some("UTC".into())),
),
(
"DATETIME(3, 'Europe/Rome')",
DataType::Datetime(Some(3), Some("Europe/Rome".into())),
),
] {
let sql = format!("CREATE TABLE t (a {data_type}) ENGINE = MergeTree ORDER BY a");
match clickhouse_and_generic().verified_stmt(&sql) {
Statement::CreateTable(CreateTable { columns, .. }) => {
assert_eq!(columns, vec![column_def("a".into(), expected)]);
}
_ => unreachable!(),
}
}

clickhouse_and_generic().one_statement_parses_to(
"CREATE TABLE t (a Nullable(DateTime('Asia/Tokyo'))) ENGINE = MergeTree ORDER BY a",
"CREATE TABLE t (a Nullable(DATETIME('Asia/Tokyo'))) ENGINE = MergeTree ORDER BY a",
);

for (sql, error) in [
(
"SELECT CAST(a AS DATETIME())",
"Expected: literal string, found: )",
),
(
"SELECT CAST(a AS DATETIME(3, 4))",
"Expected: literal string, found: 4",
),
(
"SELECT CAST(a AS DATETIME('UTC', 3))",
"Expected: ), found: ,",
),
] {
assert_eq!(
clickhouse_and_generic()
.parse_sql_statements(sql)
.unwrap_err()
.to_string(),
format!("sql parser error: {error}"),
"{sql}"
);
}
}
2 changes: 1 addition & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7155,7 +7155,7 @@ fn parse_literal_datetime() {
let select = verified_only_select(sql);
assert_eq!(
&Expr::TypedString(TypedString {
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
value: ValueWithSpan {
value: Value::SingleQuotedString("1999-01-01 01:23:34.45".into()),
span: Span::empty(),
Expand Down
15 changes: 14 additions & 1 deletion tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3546,7 +3546,7 @@ fn parse_table_column_option_on_update() {
assert_eq!(
vec![ColumnDef {
name: Ident::with_quote('`', "modification_time"),
data_type: DataType::Datetime(None),
data_type: DataType::Datetime(None, None),
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::OnUpdate(call("CURRENT_TIMESTAMP", [])),
Expand Down Expand Up @@ -5247,3 +5247,16 @@ fn parse_key_constraint_index_type_before_columns() {
);
}
}

#[test]
fn parse_datetime_precision() {
assert_eq!(
mysql_and_generic().verified_expr("CAST(a AS DATETIME(6))"),
Expr::Cast {
kind: CastKind::Cast,
expr: Box::new(Expr::Identifier(Ident::new("a"))),
data_type: DataType::Datetime(Some(6), None),
format: None,
}
);
}
Loading