From acbec143a06d371642f877f047f5c36750c2aa9b Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Fri, 27 Nov 2026 22:23:36 +0100 Subject: [PATCH 1/2] PostgreSQL: Support AT LOCAL --- src/ast/mod.rs | 5 ++++ src/ast/spans.rs | 1 + src/dialect/generic.rs | 4 ++++ src/dialect/mod.rs | 10 ++++++++ src/dialect/postgresql.rs | 5 ++++ src/parser/mod.rs | 14 +++++++---- tests/sqlparser_postgres.rs | 46 +++++++++++++++++++++++++++++++++++++ 7 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index a1f42e56cb..0631c82583 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1182,6 +1182,10 @@ pub enum Expr { /// Time zone expression to apply. time_zone: Box, }, + /// 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), /// Extract a field from a timestamp e.g. `EXTRACT(MONTH FROM foo)` /// Or `EXTRACT(MONTH, foo)` /// @@ -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}") } diff --git a/src/ast/spans.rs b/src/ast/spans.rs index 39dc01e720..db9ecb4442 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -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: _, diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index fdf3529054..94ccf71f6b 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -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 + } } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 9c74cc62af..798f735c93 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -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()), } } @@ -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 diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index 361396b5fe..2cbd9987d6 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -550,6 +550,11 @@ impl Dialect for PostgreSqlDialect { true } + /// See + 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 diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 297dbad775..3f98cb6204 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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); diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 1cd2009e89..3b39a1a41b 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -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() + )) + ); +} From f4fd5e4d5063a1cd09a2a7da00e5194ccce417f5 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Fri, 27 Nov 2026 22:32:42 +0100 Subject: [PATCH 2/2] Cover the AT LOCAL span --- src/ast/spans.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ast/spans.rs b/src/ast/spans.rs index db9ecb4442..a5c308bcbf 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -3694,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"); + } }