Skip to content

Postgres: Support BETWEEN SYMMETRIC / ASYMMETRIC - #2522

Open
BenSatori wants to merge 3 commits into
apache:mainfrom
BenSatori:postgres-between-symmetric
Open

BenSatori wants to merge 3 commits into
apache:mainfrom
BenSatori:postgres-between-symmetric

Conversation

@BenSatori

Copy link
Copy Markdown
Contributor

Before this fix, PostgreSQL's BETWEEN SYMMETRIC / BETWEEN ASYMMETRIC modifier wasn't parsed:

SELECT * FROM customers WHERE a NOT BETWEEN SYMMETRIC 10 AND 1;

This adds a symmetric field to Expr::Between and parses the optional SYMMETRIC/ASYMMETRIC keyword after BETWEEN (per the SQL standard / PostgreSQL docs: https://www.postgresql.org/docs/current/functions-comparison.html). ASYMMETRIC is the default behavior and is not round-tripped in the displayed SQL.

AI assistance disclosure: this change was drafted with GitHub Copilot.

Adds parsing for the SQL standard BETWEEN SYMMETRIC / ASYMMETRIC modifiers, e.g. `a NOT BETWEEN SYMMETRIC 10 AND 1`.

See https://www.postgresql.org/docs/current/functions-comparison.html
@BenSatori BenSatori changed the title General: Support BETWEEN SYMMETRIC / ASYMMETRIC Postgres: Support BETWEEN SYMMETRIC / ASYMMETRIC Sep 17, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.96%. Comparing base (9ae00e7) to head (1e7ae91).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2522   +/-   ##
=======================================
  Coverage   80.96%   80.96%           
=======================================
  Files          42       42           
  Lines       33359    33364    +5     
  Branches    33359    33364    +5     
=======================================
+ Hits        27009    27014    +5     
  Misses       2790     2790           
  Partials     3560     3560           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 left a comment

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 add the Dialect trait method the parser suggestion calls:

     fn supports_in_empty_list(&self) -> bool {
         false
     }
+
+    /// Returns true if the dialect supports `BETWEEN [ ASYMMETRIC | SYMMETRIC ]`.
+    fn supports_between_symmetric(&self) -> bool {
+        false
+    }

Then, you should enable it for PostgreSQL and the generic dialect, where both keywords are reserved.

     fn supports_comment_optimizer_hint(&self) -> bool {
         true
     }
+
+    fn supports_between_symmetric(&self) -> bool {
+        true
+    }
 }

Comment thread src/parser/mod.rs
Comment on lines +4462 to +4467
let symmetric = if self.parse_keyword(Keyword::SYMMETRIC) {
true
} else {
let _ = self.parse_keyword(Keyword::ASYMMETRIC);
false
};

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 gate the modifier behind a dialect method. Consumed unconditionally, it breaks a BETWEEN symmetric AND 5 in every dialect where symmetric is an ordinary identifier.

Suggested change
let symmetric = if self.parse_keyword(Keyword::SYMMETRIC) {
true
} else {
let _ = self.parse_keyword(Keyword::ASYMMETRIC);
false
};
let symmetric = self.dialect.supports_between_symmetric()
&& self.parse_one_of_keywords(&[Keyword::SYMMETRIC, Keyword::ASYMMETRIC])
== Some(Keyword::SYMMETRIC);

Comment thread tests/sqlparser_common.rs
Comment on lines +20076 to +20091
// See https://www.postgresql.org/docs/current/functions-comparison.html
match verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}

// ASYMMETRIC is the default behavior and is not preserved as SQL text.
one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);

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.

Some additional red tests.

Suggested change
// See https://www.postgresql.org/docs/current/functions-comparison.html
match verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}
// ASYMMETRIC is the default behavior and is not preserved as SQL text.
one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);
// See https://www.postgresql.org/docs/current/functions-comparison.html
let supported = all_dialects_where(|d| d.supports_between_symmetric());
match supported.verified_expr("1 BETWEEN SYMMETRIC 2 AND 3") {
Expr::Between {
symmetric, negated, ..
} => {
assert!(symmetric);
assert!(!negated);
}
_ => unreachable!(),
}
// ASYMMETRIC is the default behavior and is not preserved as SQL text.
supported.one_statement_parses_to(
"SELECT * FROM t WHERE 1 NOT BETWEEN ASYMMETRIC 2 AND 3",
"SELECT * FROM t WHERE 1 NOT BETWEEN 2 AND 3",
);
let unsupported = all_dialects_where(|d| !d.supports_between_symmetric());
unsupported.verified_expr("a BETWEEN symmetric AND 5");
unsupported.verified_expr("a BETWEEN asymmetric AND 5");

@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Unfortunately, this pull request is stale because it has been open 60 days with no activity. Please remove the stale label or comment or this will be closed in 7 days.

@github-actions github-actions Bot added the Stale label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PostgreSQL Stale waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants