Skip to content

Commit 68d3f14

Browse files
romanoffayman-sigma
authored andcommitted
Added TIMESTAMP_NTZ type support with precision to Snowflake dialect (apache#2080)
1 parent 9eaea7e commit 68d3f14

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/ast/data_type.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ pub enum DataType {
375375
/// Databricks timestamp without time zone. See [1].
376376
///
377377
/// [1]: https://docs.databricks.com/aws/en/sql/language-manual/data-types/timestamp-ntz-type
378-
TimestampNtz,
378+
TimestampNtz(Option<u64>),
379379
/// Interval type.
380380
Interval {
381381
/// [PostgreSQL] fields specification like `INTERVAL YEAR TO MONTH`.
@@ -676,7 +676,9 @@ impl fmt::Display for DataType {
676676
DataType::Timestamp(precision, timezone_info) => {
677677
format_datetime_precision_and_tz(f, "TIMESTAMP", precision, timezone_info)
678678
}
679-
DataType::TimestampNtz => write!(f, "TIMESTAMP_NTZ"),
679+
DataType::TimestampNtz(precision) => {
680+
format_type_with_optional_length(f, "TIMESTAMP_NTZ", precision, false)
681+
}
680682
DataType::Datetime64(precision, timezone) => {
681683
format_clickhouse_datetime_precision_and_timezone(
682684
f,

src/parser/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10573,7 +10573,9 @@ impl<'a> Parser<'a> {
1057310573
self.parse_optional_precision()?,
1057410574
TimezoneInfo::Tz,
1057510575
)),
10576-
Keyword::TIMESTAMP_NTZ => Ok(DataType::TimestampNtz),
10576+
Keyword::TIMESTAMP_NTZ => {
10577+
Ok(DataType::TimestampNtz(self.parse_optional_precision()?))
10578+
}
1057710579
Keyword::TIME => {
1057810580
let precision = self.parse_optional_precision()?;
1057910581
let tz = if self.parse_keyword(Keyword::WITH) {

tests/sqlparser_databricks.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ fn data_type_timestamp_ntz() {
351351
assert_eq!(
352352
databricks().verified_expr("TIMESTAMP_NTZ '2025-03-29T18:52:00'"),
353353
Expr::TypedString(TypedString {
354-
data_type: DataType::TimestampNtz,
354+
data_type: DataType::TimestampNtz(None),
355355
value: ValueWithSpan {
356356
value: Value::SingleQuotedString("2025-03-29T18:52:00".to_owned()),
357357
span: Span::empty(),
@@ -368,7 +368,7 @@ fn data_type_timestamp_ntz() {
368368
expr: Box::new(Expr::Nested(Box::new(Expr::Identifier(
369369
"created_at".into()
370370
)))),
371-
data_type: DataType::TimestampNtz,
371+
data_type: DataType::TimestampNtz(None),
372372
format: None
373373
}
374374
);
@@ -380,7 +380,7 @@ fn data_type_timestamp_ntz() {
380380
columns,
381381
vec![ColumnDef {
382382
name: "x".into(),
383-
data_type: DataType::TimestampNtz,
383+
data_type: DataType::TimestampNtz(None),
384384
options: vec![],
385385
}]
386386
);

tests/sqlparser_snowflake.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4657,6 +4657,21 @@ fn test_create_database() {
46574657
assert!(err.contains("Expected"), "Unexpected error: {err}");
46584658
}
46594659

4660+
#[test]
4661+
fn test_timestamp_ntz_with_precision() {
4662+
snowflake().verified_stmt("SELECT CAST('2024-01-01 01:00:00' AS TIMESTAMP_NTZ(1))");
4663+
snowflake().verified_stmt("SELECT CAST('2024-01-01 01:00:00' AS TIMESTAMP_NTZ(9))");
4664+
4665+
let select =
4666+
snowflake().verified_only_select("SELECT CAST('2024-01-01 01:00:00' AS TIMESTAMP_NTZ(9))");
4667+
match expr_from_projection(only(&select.projection)) {
4668+
Expr::Cast { data_type, .. } => {
4669+
assert_eq!(*data_type, DataType::TimestampNtz(Some(9)));
4670+
}
4671+
_ => unreachable!(),
4672+
}
4673+
}
4674+
46604675
#[test]
46614676
fn test_drop_constraints() {
46624677
snowflake().verified_stmt("ALTER TABLE tbl DROP PRIMARY KEY");

0 commit comments

Comments
 (0)