LucaCappelletti94 commented on code in PR #2972:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2972#discussion_r4578820885


##########
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) {

Review Comment:
   Add a new method to the `Dialect` trait instead of extending the use of the 
`dialect_is` macro.



##########
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);
+}

Review Comment:
   You should assert the trees that depend on precedence, because `a ^ b` alone 
passes while `a * b ^ c` parses as `(a * b) ^ c`.
   
   ```suggestion
   #[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}");
       }
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to