From 6c9e0f820b7462d3ffdd861a3cb5345cc34ad4b7 Mon Sep 17 00:00:00 2001 From: nuno-faria Date: Sun, 15 Nov 2026 12:25:59 +0000 Subject: [PATCH] fix: Treat ^ as exponentation in DuckDB --- src/parser/mod.rs | 7 ++++--- tests/sqlparser_common.rs | 12 ++++++++---- tests/sqlparser_duckdb.rs | 10 ++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 76bd114071..9181481a72 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -3989,9 +3989,10 @@ impl<'a> Parser<'a> { Token::StringConcat => Some(BinaryOperator::StringConcat), Token::Pipe => Some(BinaryOperator::BitwiseOr), Token::Caret => { - // In PostgreSQL, ^ stands for the exponentiation operation, - // and # stands for XOR. See https://www.postgresql.org/docs/current/functions-math.html - if dialect_is!(dialect is PostgreSqlDialect) { + // In PostgreSQL and DuckDB, ^ stands for the exponentiation operation. + // See https://www.postgresql.org/docs/current/functions-math.html, + // https://duckdb.org/docs/2.0/sql/functions/numeric. + if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect) { Some(BinaryOperator::PGExp) } else { Some(BinaryOperator::BitwiseXor) diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index f2f7849154..f89d6e2aa4 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2857,13 +2857,13 @@ fn parse_string_agg() { ); } -/// selects all dialects but PostgreSQL -pub fn all_dialects_but_pg() -> TestedDialects { +/// selects all dialects but PostgreSQL and DuckDB +pub fn all_dialects_but_pg_and_duckdb() -> TestedDialects { TestedDialects::new( all_dialects() .dialects .into_iter() - .filter(|x| !x.is::()) + .filter(|x| !x.is::() && !x.is::()) .collect(), ) } @@ -2871,7 +2871,11 @@ pub fn all_dialects_but_pg() -> TestedDialects { #[test] fn parse_bitwise_ops() { let bitwise_ops = &[ - ("^", BinaryOperator::BitwiseXor, all_dialects_but_pg()), + ( + "^", + BinaryOperator::BitwiseXor, + all_dialects_but_pg_and_duckdb(), + ), ("|", BinaryOperator::BitwiseOr, all_dialects()), ("&", BinaryOperator::BitwiseAnd, all_dialects()), ]; diff --git a/tests/sqlparser_duckdb.rs b/tests/sqlparser_duckdb.rs index 216d514713..aa41aa76b2 100644 --- a/tests/sqlparser_duckdb.rs +++ b/tests/sqlparser_duckdb.rs @@ -976,3 +976,13 @@ fn display_create_macro_propagates_argument_list_errors() { } } } + +#[test] +fn test_exponentiation() { + let expected_expr = Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::PGExp, + right: Box::new(Expr::Identifier(Ident::new("b"))), + }; + assert_eq!(duckdb().verified_expr("a ^ b"), expected_expr); +}