You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/20 20:30:18 UTC

(datafusion-sqlparser-rs) 01/01: SQLite: Parse `&` and `|` at the same precedence (#2683)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-2683-3d27cf74fce920cdf54848f7f84de7fced9c5f39
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 7bc304cd5be223cf9c4592dfacce024191a0d4a0
Author: Luca Cappelletti <ca...@gmail.com>
AuthorDate: Tue Oct 20 20:29:54 2026 +0000

    SQLite: Parse `&` and `|` at the same precedence (#2683)
---
 src/dialect/sqlite.rs     | 12 +++++++++-
 tests/sqlparser_sqlite.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs
index ec01f4a65..41b0e8be1 100644
--- a/src/dialect/sqlite.rs
+++ b/src/dialect/sqlite.rs
@@ -20,9 +20,10 @@ use alloc::boxed::Box;
 
 use crate::ast::BinaryOperator;
 use crate::ast::{Expr, Statement};
-use crate::dialect::Dialect;
+use crate::dialect::{Dialect, Precedence};
 use crate::keywords::Keyword;
 use crate::parser::{Parser, ParserError};
+use crate::tokenizer::Token;
 
 /// A [`Dialect`] for [SQLite](https://www.sqlite.org)
 ///
@@ -97,6 +98,15 @@ impl Dialect for SQLiteDialect {
         None
     }
 
+    /// `&` and `|` share one left-associative precedence level.
+    /// See <https://www.sqlite.org/lang_expr.html#operators_and_parse_affecting_attributes>
+    fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
+        match parser.peek_token_ref().token {
+            Token::Ampersand | Token::Pipe => Some(Ok(self.prec_value(Precedence::Ampersand))),
+            _ => None,
+        }
+    }
+
     fn supports_in_empty_list(&self) -> bool {
         true
     }
diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs
index b2006b2b5..9ac16f904 100644
--- a/tests/sqlparser_sqlite.rs
+++ b/tests/sqlparser_sqlite.rs
@@ -1186,3 +1186,63 @@ fn test_dollar_sign_in_identifier() {
         .parse_sql_statements("SELECT a$ FROM t")
         .unwrap_err();
 }
+
+#[test]
+fn parse_bitwise_and_or_share_precedence() {
+    fn num(n: &str) -> Expr {
+        Expr::value(number(n))
+    }
+    fn bin(left: Expr, op: BinaryOperator, right: Expr) -> Expr {
+        Expr::BinaryOp {
+            left: Box::new(left),
+            op,
+            right: Box::new(right),
+        }
+    }
+    use BinaryOperator::{BitwiseAnd, BitwiseOr, Eq, Multiply, Plus};
+
+    for (sql, expected) in [
+        (
+            "6 | 1 & 3",
+            bin(bin(num("6"), BitwiseOr, num("1")), BitwiseAnd, num("3")),
+        ),
+        (
+            "6 & 3 | 8",
+            bin(bin(num("6"), BitwiseAnd, num("3")), BitwiseOr, num("8")),
+        ),
+        (
+            "1 & 2 & 3",
+            bin(bin(num("1"), BitwiseAnd, num("2")), BitwiseAnd, num("3")),
+        ),
+        (
+            "1 | 2 | 3",
+            bin(bin(num("1"), BitwiseOr, num("2")), BitwiseOr, num("3")),
+        ),
+        (
+            "1 | 2 + 3 & 4 * 5",
+            bin(
+                bin(num("1"), BitwiseOr, bin(num("2"), Plus, num("3"))),
+                BitwiseAnd,
+                bin(num("4"), Multiply, num("5")),
+            ),
+        ),
+        (
+            "6 | (1 & 3)",
+            bin(
+                num("6"),
+                BitwiseOr,
+                Expr::Nested(Box::new(bin(num("1"), BitwiseAnd, num("3")))),
+            ),
+        ),
+        (
+            "1 | 2 = 3 & 4",
+            bin(
+                bin(num("1"), BitwiseOr, num("2")),
+                Eq,
+                bin(num("3"), BitwiseAnd, num("4")),
+            ),
+        ),
+    ] {
+        assert_eq!(sqlite().verified_expr(sql), expected, "{sql}");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org