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
7 changes: 4 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new method to the Dialect trait instead of extending the use of the dialect_is macro.

Some(BinaryOperator::PGExp)
} else {
Some(BinaryOperator::BitwiseXor)
Expand Down
12 changes: 8 additions & 4 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2857,21 +2857,25 @@ 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::<PostgreSqlDialect>())
.filter(|x| !x.is::<PostgreSqlDialect>() && !x.is::<DuckDbDialect>())
.collect(),
)
}

#[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()),
];
Expand Down
10 changes: 10 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Comment on lines +980 to +988

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should assert the trees that depend on precedence, because a ^ b alone passes while a * b ^ c parses as (a * b) ^ c.

Suggested change
#[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);
}
#[test]
fn test_exponentiation() {
use BinaryOperator::{Multiply, PGExp, Plus};
let ident = |name| Box::new(Expr::Identifier(Ident::new(name)));
let binary = |left, op, right| Box::new(Expr::BinaryOp { left, op, right });
let minus = |expr| Box::new(Expr::UnaryOp { op: UnaryOperator::Minus, expr });
for (sql, expected) in [
("a * b ^ c", binary(ident("a"), Multiply, binary(ident("b"), PGExp, ident("c")))),
("a + b ^ c", binary(ident("a"), Plus, binary(ident("b"), PGExp, ident("c")))),
("-a ^ b", binary(minus(ident("a")), PGExp, ident("b"))),
("a ^ b ^ c", binary(binary(ident("a"), PGExp, ident("b")), PGExp, ident("c"))),
] {
assert_eq!(duckdb().verified_expr(sql), *expected, "{sql}");
}
}

Loading