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
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@ pub enum Expr {
/// Time zone expression to apply.
time_zone: Box<Expr>,
},
/// Convert a timestamp to the session time zone, e.g. `ts AT LOCAL` in [PostgreSQL].
///
/// [PostgreSQL]: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT
AtLocal(Box<Expr>),
/// Extract a field from a timestamp e.g. `EXTRACT(MONTH FROM foo)`
/// Or `EXTRACT(MONTH, foo)`
///
Expand Down Expand Up @@ -2291,6 +2295,7 @@ impl fmt::Display for Expr {
} => {
write!(f, "{timestamp} AT TIME ZONE {time_zone}")
}
Expr::AtLocal(timestamp) => write!(f, "{timestamp} AT LOCAL"),
Expr::Interval(interval) => {
write!(f, "{interval}")
}
Expand Down
10 changes: 10 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@ impl Spanned for Expr {
timestamp,
time_zone,
} => timestamp.span().union(&time_zone.span()),
Expr::AtLocal(timestamp) => timestamp.span(),
Expr::Extract {
field: _,
syntax: _,
Expand Down Expand Up @@ -3693,4 +3694,13 @@ SETTINGS update_parallel_mode = 'auto'"#;
assert_eq!(test.get_source(operation.span()), expected, "{sql}");
}
}

#[test]
fn test_at_local_span() {
let dialect = &crate::dialect::PostgreSqlDialect {};
let mut test = SpanTest::new(dialect, "t.ts AT LOCAL");
let expr = test.0.parse_expr().unwrap();
assert!(matches!(expr, Expr::AtLocal(_)));
assert_eq!(test.get_source(expr.span()), "t.ts");
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,8 @@ impl Dialect for GenericDialect {
fn supports_add_column_if_not_exists(&self) -> bool {
true
}

fn supports_at_local(&self) -> bool {
true
}
}
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,11 @@ pub trait Dialect: Debug + Any {
{
Ok(p!(AtTz))
}
(Token::Word(w), _)
if w.keyword == Keyword::LOCAL && self.supports_at_local() =>
{
Ok(p!(AtTz))
}
_ => Ok(self.prec_unknown()),
}
}
Expand Down Expand Up @@ -1942,6 +1947,11 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports the `x AT LOCAL` expression.
fn supports_at_local(&self) -> bool {
false
}

/// Returns true if the dialect supports binary `IS` and `IS NOT` operators.
///
/// Their right operand is any expression, so `IS UNKNOWN`, `IS JSON` and
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@ impl Dialect for PostgreSqlDialect {
true
}

/// See <https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT>
fn supports_at_local(&self) -> bool {
true
}

/// [Postgres] supports optional field and precision options for `INTERVAL` data type.
///
/// [Postgres]: https://www.postgresql.org/docs/17/datatype-datetime.html
Expand Down
14 changes: 9 additions & 5 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4243,11 +4243,15 @@ impl<'a> Parser<'a> {
}
}
Keyword::AT => {
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
Ok(Expr::AtTimeZone {
timestamp: Box::new(expr),
time_zone: Box::new(self.parse_subexpr(precedence)?),
})
if self.dialect.supports_at_local() && self.parse_keyword(Keyword::LOCAL) {
Ok(Expr::AtLocal(Box::new(expr)))
} else {
self.expect_keywords(&[Keyword::TIME, Keyword::ZONE])?;
Ok(Expr::AtTimeZone {
timestamp: Box::new(expr),
time_zone: Box::new(self.parse_subexpr(precedence)?),
})
}
}
Keyword::GLOBAL if self.dialect.supports_global_in() => {
let negated = self.parse_keyword(Keyword::NOT);
Expand Down
46 changes: 46 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14597,3 +14597,49 @@ fn parse_alter_column_set_storage() {
);
}
}

#[test]
fn parse_at_local() {
let ts = || Box::new(Expr::Identifier(Ident::new("ts")));

assert_eq!(
pg_and_generic().verified_expr("ts AT LOCAL"),
Expr::AtLocal(ts())
);
pg_and_generic().verified_stmt("SELECT CAST((CAST(NULL AS TIMESTAMPTZ) AT LOCAL) AS TEXT)");

// AT LOCAL binds tighter than `+` and associates left with AT TIME ZONE.
assert_eq!(
pg_and_generic().verified_expr("x + ts AT LOCAL"),
Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("x"))),
op: BinaryOperator::Plus,
right: Box::new(Expr::AtLocal(ts())),
}
);
assert_eq!(
pg_and_generic().verified_expr("ts AT TIME ZONE 'UTC' AT LOCAL"),
Expr::AtLocal(Box::new(Expr::AtTimeZone {
timestamp: ts(),
time_zone: Box::new(Expr::Value(
Value::SingleQuotedString("UTC".to_string()).with_empty_span()
)),
}))
);
assert_eq!(
pg_and_generic().verified_expr("ts AT LOCAL AT TIME ZONE 'UTC'"),
Expr::AtTimeZone {
timestamp: Box::new(Expr::AtLocal(ts())),
time_zone: Box::new(Expr::Value(
Value::SingleQuotedString("UTC".to_string()).with_empty_span()
)),
}
);

assert_eq!(
all_dialects_where(|d| !d.supports_at_local()).parse_sql_statements("SELECT (ts AT LOCAL)"),
Err(ParserError::ParserError(
"Expected: ), found: AT".to_string()
))
);
}
Loading