Bug #120980 DATE column vs INTEGER column comparison returns different results with column reference vs literal value

Submitted: 23 Jul 2:09 Modified: 28 Sep 1:45
Reporter: we 李 Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:8.0.46 OS:Any
Assigned to: CPU Architecture:Any

[23 Jul 2:09] we 李
Description:
When a DATE column is compared with an INTEGER column (containing NULL) using NOT BETWEEN, MySQL produces different results depending on whether the left operand is a column reference or a literal value, even though they are logically equivalent.

Query 1 (column reference) returns 1 row, while Query 2 (literal value) returns 0 rows, despite O_ORDERDATE = '1996-12-01' making them logically identical.

How to repeat:
CREATE TABLE t (
    date_col DATE,
    int_col INTEGER
);

INSERT INTO t VALUES ('1996-12-01', NULL);

-- Query 1: using column reference (returns 1)
SELECT COUNT(*) FROM t 
WHERE date_col = '1996-12-01' 
  AND date_col NOT BETWEEN '1992-03-04' AND int_col;

-- Query 2: using literal value (returns 0)
SELECT COUNT(*) FROM t 
WHERE date_col = '1996-12-01' 
  AND '1996-12-01' NOT BETWEEN '1992-03-04' AND int_col;

Suggested fix:
MySQL should apply consistent type inference for column references and literal values in BETWEEN comparisons. The type conversion path should be the same regardless of whether the operand is a column or a literal.
[30 Jul 1:23] we 李
Hello, has there been any update on this bug? It can cause incorrect query results in production. Thank you
[3 Aug 8:22] Chaithra Marsur Gopala Reddy
Hi we 李,

Thank you for the test case. Verified as described.
[28 Sep 1:45] we 李
This report references #113366 and reproduces the issue via
differential testing. The following provides a more detailed
root-cause analysis:

1. The problem is not limited to MEDIUMINT — it affects all numeric
   types:

   date_col NOT BETWEEN '2018-05-20' AND int_col   → 1
   date_col NOT BETWEEN '2018-05-20' AND dec_col   → 1
   date_col NOT BETWEEN '2018-05-20' AND str_col   → NULL
   date_col NOT BETWEEN '2018-05-20' AND NULL      → NULL

   A VARCHAR boundary correctly returns NULL, whereas INT/DECIMAL
   return 1. This indicates the problem lies in the implicit
   conversion path for numeric types, not in BETWEEN itself.

2. Comparing against the expanded form confirms that the root cause
   is a loss of NULL propagation:

   date_col BETWEEN '2018-05-20' AND int_col          → 0
   date_col >= '2018-05-20' AND date_col <= int_col   → NULL

   The two expressions are logically equivalent but produce different
   results. This shows that Item_func_between, when performing type
   aggregation (DATE + STRING + INT → DOUBLE), fails to set null_value
   correctly, causing an expression that should evaluate to NULL to
   instead return a definite 0/1.

3. The failure occurs specifically in the numeric conversion path.
   When the boundary operand is a numeric column (INT/DECIMAL), the
   type aggregation resolves to DOUBLE, and the conversion of the DATE
   and STRING operands to DOUBLE does not preserve their NULL state.
   When the boundary is a VARCHAR column, the aggregation resolves to
   a string comparison instead, which correctly propagates NULL. This
   asymmetry between the numeric and string conversion paths is the
   direct cause of the inconsistent results.

4. The expanded form (>= AND <=) uses Item_func_ge / Item_func_le,
   which check null_value on each operand and therefore return NULL
   correctly. Item_func_between does not perform an equivalent NULL
   check before comparison, so the NULL state is lost during type
   aggregation. This explains why BETWEEN and its expanded form
   diverge whenever a boundary operand is NULL.

   A consistent fix would require Item_func_between to check the
   null_value of its boundary operands (args[1], args[2]) before
   calling compare(), and to propagate NULL accordingly — or to
   ensure that the implicit conversion of a NULL DATE/STRING operand
   to DOUBLE sets null_value instead of yielding a definite value.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.081 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.