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/08/30 11:38:05 UTC
(datafusion-sqlparser-rs) branch gh-readonly-queue/main/pr-2436-75a1040597fd6233f24a6b41641b360e30fb01b9 created (now c0054b77)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-2436-75a1040597fd6233f24a6b41641b360e30fb01b9
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
at c0054b77 Honour operator precedence in `IS [NOT] DISTINCT FROM` (#2436)
This branch includes the following new commits:
new c0054b77 Honour operator precedence in `IS [NOT] DISTINCT FROM` (#2436)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion-sqlparser-rs) 01/01: Honour operator precedence in `IS [NOT] DISTINCT FROM` (#2436)
Posted by gi...@apache.org.
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-2436-75a1040597fd6233f24a6b41641b360e30fb01b9
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
commit c0054b7785322fe9e4b0ce485749ad730e7ef278
Author: Zvonimir <zv...@datadoghq.com>
AuthorDate: Sun Aug 30 11:37:41 2026 +0000
Honour operator precedence in `IS [NOT] DISTINCT FROM` (#2436)
Co-authored-by: Claude Opus 5 <no...@anthropic.com>
---
src/dialect/mod.rs | 7 +-
src/parser/mod.rs | 7 +-
tests/sqlparser_common.rs | 191 ++++++++++++++++++++++++++++++++++++++++++++++
tests/sqlparser_mysql.rs | 33 ++++++++
4 files changed, 235 insertions(+), 3 deletions(-)
diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs
index f99cbe2e..ff83a4da 100644
--- a/src/dialect/mod.rs
+++ b/src/dialect/mod.rs
@@ -990,11 +990,16 @@ pub trait Dialect: Debug + Any {
Precedence::Caret => 22,
Precedence::Pipe => 21,
Precedence::Colon => 21,
+ // "any other operator" -- `->`, `@>`, custom operators. PostgreSQL
+ // places this row above `BETWEEN` / `LIKE` and below `+` / `-`
+ // (`%left Op OPERATOR RIGHT_ARROW '|'` in gram.y), so it must bind
+ // more tightly than `IS`, whose right operand would otherwise stop
+ // short of it.
+ Precedence::PgOther => 21,
Precedence::Between => 20,
Precedence::Eq => 20,
Precedence::Like => 19,
Precedence::Is => 17,
- Precedence::PgOther => 16,
Precedence::UnaryNot => 15,
Precedence::And => 10,
Precedence::Or => 5,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 953453a2..6af0fb77 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -4094,11 +4094,14 @@ impl<'a> Parser<'a> {
} else if self.parse_keywords(&[Keyword::NOT, Keyword::UNKNOWN]) {
Ok(Expr::IsNotUnknown(Box::new(expr)))
} else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
- let expr2 = self.parse_expr()?;
+ // The right operand binds no more loosely than `IS`
+ // itself, so that e.g. `a IS DISTINCT FROM b AND c`
+ // parses as `(a IS DISTINCT FROM b) AND c`.
+ let expr2 = self.parse_subexpr(precedence)?;
Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
{
- let expr2 = self.parse_expr()?;
+ let expr2 = self.parse_subexpr(precedence)?;
Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
} else if self.parse_keyword(Keyword::JSON) {
self.parse_is_json_predicate(expr, false)
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index c8a2453a..5792cf00 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -1999,6 +1999,197 @@ fn parse_is_not_distinct_from() {
);
}
+#[test]
+fn parse_is_distinct_from_precedence() {
+ use self::Expr::*;
+
+ // The right operand of `IS [NOT] DISTINCT FROM` binds tighter than `AND`/`OR`,
+ // so the boolean operator must end up at the root of the tree.
+ assert_eq!(
+ BinaryOp {
+ left: Box::new(IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Expr::value(number("1"))),
+ )),
+ op: BinaryOperator::And,
+ right: Box::new(BinaryOp {
+ left: Box::new(Identifier(Ident::new("b"))),
+ op: BinaryOperator::Eq,
+ right: Box::new(Expr::value(number("2"))),
+ }),
+ },
+ verified_expr("a IS DISTINCT FROM 1 AND b = 2")
+ );
+
+ assert_eq!(
+ BinaryOp {
+ left: Box::new(IsNotDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Expr::value(number("1"))),
+ )),
+ op: BinaryOperator::Or,
+ right: Box::new(BinaryOp {
+ left: Box::new(Identifier(Ident::new("b"))),
+ op: BinaryOperator::Eq,
+ right: Box::new(Expr::value(number("2"))),
+ }),
+ },
+ verified_expr("a IS NOT DISTINCT FROM 1 OR b = 2")
+ );
+
+ // `AND` binds tighter than `OR` within the surrounding expression.
+ assert_eq!(
+ BinaryOp {
+ left: Box::new(BinaryOp {
+ left: Box::new(IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Expr::value(number("1"))),
+ )),
+ op: BinaryOperator::And,
+ right: Box::new(Identifier(Ident::new("b"))),
+ }),
+ op: BinaryOperator::Or,
+ right: Box::new(Identifier(Ident::new("c"))),
+ },
+ verified_expr("a IS DISTINCT FROM 1 AND b OR c")
+ );
+ assert_eq!(
+ BinaryOp {
+ left: Box::new(IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Expr::value(number("1"))),
+ )),
+ op: BinaryOperator::Or,
+ right: Box::new(BinaryOp {
+ left: Box::new(Identifier(Ident::new("b"))),
+ op: BinaryOperator::And,
+ right: Box::new(Identifier(Ident::new("c"))),
+ }),
+ },
+ verified_expr("a IS DISTINCT FROM 1 OR b AND c")
+ );
+
+ // Explicit parentheses still push the boolean expression into the right operand.
+ assert_eq!(
+ IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Nested(Box::new(BinaryOp {
+ left: Box::new(Expr::value(number("1"))),
+ op: BinaryOperator::And,
+ right: Box::new(Identifier(Ident::new("b"))),
+ }))),
+ ),
+ verified_expr("a IS DISTINCT FROM (1 AND b)")
+ );
+
+ // sqlparser resolves the IS family left-associatively, consistent with how
+ // `a IS NULL IS NULL` already parses. Deliberately more permissive than
+ // PostgreSQL, which declares IS as %nonassoc and rejects the chain.
+ assert_eq!(
+ IsNull(Box::new(IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Identifier(Ident::new("b"))),
+ ))),
+ verified_expr("a IS DISTINCT FROM b IS NULL")
+ );
+
+ // Operators that bind tighter than `IS` are still part of the right operand.
+ assert_eq!(
+ IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(BinaryOp {
+ left: Box::new(Identifier(Ident::new("b"))),
+ op: BinaryOperator::Plus,
+ right: Box::new(Expr::value(number("1"))),
+ }),
+ ),
+ verified_expr("a IS DISTINCT FROM b + 1")
+ );
+
+ assert_eq!(
+ IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(BinaryOp {
+ left: Box::new(Identifier(Ident::new("b"))),
+ op: BinaryOperator::Eq,
+ right: Box::new(Identifier(Ident::new("c"))),
+ }),
+ ),
+ verified_expr("a IS DISTINCT FROM b = c")
+ );
+
+ // `NOT` binds more loosely than `IS`, so it applies to the whole comparison.
+ assert_eq!(
+ UnaryOp {
+ op: UnaryOperator::Not,
+ expr: Box::new(IsDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Identifier(Ident::new("b"))),
+ )),
+ },
+ verified_expr("NOT a IS DISTINCT FROM b")
+ );
+
+ assert_eq!(
+ BinaryOp {
+ left: Box::new(IsNotDistinctFrom(
+ Box::new(Identifier(Ident::new("a"))),
+ Box::new(Identifier(Ident::new("b"))),
+ )),
+ op: BinaryOperator::And,
+ right: Box::new(IsNotDistinctFrom(
+ Box::new(Identifier(Ident::new("c"))),
+ Box::new(Identifier(Ident::new("d"))),
+ )),
+ },
+ verified_expr("a IS NOT DISTINCT FROM b AND c IS NOT DISTINCT FROM d")
+ );
+}
+
+#[test]
+fn parse_pg_other_operator_precedence() {
+ let arrow_k = |left: Expr| Expr::BinaryOp {
+ left: Box::new(left),
+ op: BinaryOperator::Arrow,
+ right: Box::new(Expr::Value(
+ Value::SingleQuotedString("k".into()).with_empty_span(),
+ )),
+ };
+ let t_a = || Expr::CompoundIdentifier(vec![Ident::new("t"), Ident::new("a")]);
+
+ // `->` binds tighter than comparison operators, so the arrow expression is
+ // the left operand rather than the comparison being the arrow's key.
+ let expected_eq = |left: Expr| Expr::BinaryOp {
+ left: Box::new(arrow_k(left)),
+ op: BinaryOperator::Eq,
+ right: Box::new(Identifier(Ident::new("b"))),
+ };
+
+ // Dialects with lambda functions read a bare `a ->` as the start of a lambda.
+ assert_eq!(
+ expected_eq(Identifier(Ident::new("a"))),
+ all_dialects_where(|d| !d.supports_lambda_functions()).verified_expr("a -> 'k' = b")
+ );
+ assert_eq!(
+ expected_eq(t_a()),
+ all_dialects().verified_expr("t.a -> 'k' = b")
+ );
+
+ // `LIKE` sits below `=` in the precedence table, so cover that boundary too.
+ assert_eq!(
+ Expr::Like {
+ negated: false,
+ any: false,
+ expr: Box::new(arrow_k(t_a())),
+ pattern: Box::new(Expr::Value(
+ Value::SingleQuotedString("x".into()).with_empty_span(),
+ )),
+ escape_char: None,
+ },
+ all_dialects().verified_expr("t.a -> 'k' LIKE 'x'")
+ );
+}
+
#[test]
fn parse_not_precedence() {
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs
index 0df8393e..a8d7f667 100644
--- a/tests/sqlparser_mysql.rs
+++ b/tests/sqlparser_mysql.rs
@@ -4947,3 +4947,36 @@ fn parse_adjacent_string_literal_concatenation() {
fn parse_group_by_with_rollup() {
mysql().verified_stmt("SELECT * FROM tbl GROUP BY col1, col2 WITH ROLLUP");
}
+
+#[test]
+fn parse_is_distinct_from_json_arrow_precedence() {
+ // MySQL's `->` binds tighter than `IS [NOT] DISTINCT FROM`, so the JSON
+ // extraction must stay inside the right operand.
+ assert_eq!(
+ Expr::IsDistinctFrom(
+ Box::new(Expr::Identifier(Ident::new("a"))),
+ Box::new(Expr::BinaryOp {
+ left: Box::new(Expr::Identifier(Ident::new("b"))),
+ op: BinaryOperator::Arrow,
+ right: Box::new(Expr::Value(
+ Value::SingleQuotedString("k".into()).with_empty_span()
+ )),
+ }),
+ ),
+ mysql_and_generic().verified_expr("a IS DISTINCT FROM b -> 'k'")
+ );
+
+ assert_eq!(
+ Expr::IsNotDistinctFrom(
+ Box::new(Expr::Identifier(Ident::new("a"))),
+ Box::new(Expr::BinaryOp {
+ left: Box::new(Expr::Identifier(Ident::new("b"))),
+ op: BinaryOperator::LongArrow,
+ right: Box::new(Expr::Value(
+ Value::SingleQuotedString("k".into()).with_empty_span()
+ )),
+ }),
+ ),
+ mysql_and_generic().verified_expr("a IS NOT DISTINCT FROM b ->> 'k'")
+ );
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org