Skip to content

MySQL: Bind && with the precedence of AND - #2576

Open
LucaCappelletti94 wants to merge 1 commit into
mainfrom
upstream/mysql-double-ampersand-precedence
Open

LucaCappelletti94 wants to merge 1 commit into
mainfrom
upstream/mysql-double-ampersand-precedence

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

Since #2144, MySQL's && parses as BinaryOperator::And, but its precedence still came from the shared group that ranks the PostgreSQL overlap operator above the comparison operators. So x < 1 && y > 2 parsed as ((x < (1 AND y)) > 2) and x = a && c as x = (a AND c). Both then printed with AND, which reads back as a different tree.

MySQL ranks && with AND, below every comparison (https://dev.mysql.com/doc/refman/8.4/en/operator-precedence.html). The precedence lookup now gives Token::Overlap the AND precedence whenever supports_double_ampersand_operator() holds, the same check parse_infix uses to build the AND node. PostgreSQL, Redshift and Generic keep && as the overlap operator at its current precedence. The test added in #2144 now also checks that the comparison cases give the same trees as their AND spellings.

@LucaCappelletti94 LucaCappelletti94 added the bug Something isn't working label Sep 25, 2026
@github-actions github-actions Bot added the MySQL label Sep 25, 2026
@codecov-commenter

codecov-commenter commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.15%. Comparing base (88ffee6) to head (ee97692).
⚠️ Report is 22 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2576      +/-   ##
==========================================
+ Coverage   81.02%   81.15%   +0.13%     
==========================================
  Files          42       42              
  Lines       33436    33744     +308     
  Branches    33436    33744     +308     
==========================================
+ Hits        27090    27384     +294     
- Misses       2789     2797       +8     
- Partials     3557     3563       +6     

☔ 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 marked this pull request as ready for review September 25, 2026 08:15
Comment thread src/dialect/mod.rs Outdated
| Token::ExclamationMarkDoubleTilde
| Token::ExclamationMarkDoubleTildeAsterisk
| Token::Spaceship => Ok(p!(Eq)),
Token::Overlap if self.supports_double_ampersand_operator() => Ok(p!(And)),

@s5dsn-eqee s5dsn-eqee Sep 29, 2026 •

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.

My bad. After looking into it more, I think overriding get_next_precedence in MySqlDialect would be cleaner, the same way Oracle does for StringConcat:

fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {

smt like this

 fn get_next_precedence(&self, parser: &Parser) -> Option<Result<u8, ParserError>> {
     match parser.peek_token_ref().token {
         Token::Overlap => Some(Ok(self.prec_value(Precedence::And))),
         _ => None,
     }
 }

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.

@LucaCappelletti94

Nit, not for this PR: supports_double_ampersand_operator() reads as "supports &&", while it actually means "&& is boolean AND" (Generic/PG/Redshift support && as overlap with the flag off). A name like supports_double_ampersand_as_and() could be a follow-up; it's a public trait method, so it'd be a breaking change.

@s5dsn-eqee s5dsn-eqee 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.

The framework already lets a dialect change precedence locally by overriding Dialect::get_next_precedence.

@LucaCappelletti94
LucaCappelletti94 force-pushed the upstream/mysql-double-ampersand-precedence branch from c100cac to ee97692 Compare October 2, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working MySQL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants