Bug #117017 Incorrect Operator Precedence of IN and Comparators in MySQL 8.4.3 and 9.1.0
Submitted: 18 Dec 2024 19:11 Modified: 19 Dec 2024 12:03
Reporter: Team QueryHouse Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: comparison, IN, operator precedence

[18 Dec 2024 19:11] Team QueryHouse
Description:
Hi,

According to the official documentation, comparison operators and the `IN` operator have the same precedence level and are left-associative. Here’s the relevant excerpt:

<https://dev.mysql.com/doc/refman/8.4/en/operator-precedence.html>

<https://dev.mysql.com/doc/refman/9.1/en/operator-precedence.html>

```sql
(highest)
...
= (comparison), <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN, MEMBER OF
...
(lowest)
```

However, we’ve observed that MySQL always evaluates the `IN` operator before comparison operators, which seems inconsistent with the documentation.

How to repeat:
**Minimized Query to Reproduce**:

```sql
SELECT 2 = 5 IN (0, 20, 100);
```

**Expected Result**:

```
mysql> SELECT (2 = 5) IN (0, 20, 100);
+-------------------------+
| (2 = 5) IN (0, 20, 100) |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)
```
This is the expected result where the comparison is evaluated first, per left-associativity. (Note the parentheses around `2 = 5`, which are required for correct behavior.)

**Actual result** (MySQL, tested on version 8.4.3 and 9.1.0):

```
mysql> SELECT 2 = 5 IN (0, 20, 100);
+-----------------------+
| 2 = 5 IN (0, 20, 100) |
+-----------------------+
|                     0 |
+-----------------------+
1 row in set (0.01 sec)

mysql> SELECT 2 = (5 IN (0, 20, 100));
+-------------------------+
| 2 = (5 IN (0, 20, 100)) |
+-------------------------+
|                       0 |
+-------------------------+
1 row in set (0.00 sec)
```

The actual result is the same as when `IN` is explicitly forced to be evaluated first with parentheses.

Suggested fix:
We believe this behavior is a bug, as it contradicts the documented precedence rules.

We suggest that MySQL be updated to to evaluate comparisons and `IN` according to left-associativity as specified, or that the documentation be updated to align with the current behavior.

Thank you for your time and attention to this matter. We look forward to your response.

Best regards,
[19 Dec 2024 12:03] MySQL Verification Team
Hi Team QueryHouse,

Thank you for your bug report.

This is now a verified bug in the Optimiser.

It affects version 8.0 and higher.