[MDEV-11362] True condition elimination does not work for DECIMAL and temporal dynamic SQL parameters Created: 2016-11-27  Updated: 2018-06-08  Resolved: 2018-06-08

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0, 10.1, 10.2
Fix Version/s: 10.4.0

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-4912 Data type plugin API version 1 Closed
relates to MDEV-11361 Equal condition propagation does not ... Open
relates to MDEV-16426 Optimizer erroneously treats equal co... Closed

 Description   

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE ?+a<=>?+a' USING 1,1;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1 |
+-------+------+--------------------------------------------------------+

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DOUBLE);
INSERT INTO t1 VALUES (1),(2),(3);
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE ?+a<=>?+a' USING 1e0,1e0;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1 |
+-------+------+--------------------------------------------------------+

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DECIMAL(10,1));
INSERT INTO t1 VALUES (1),(2),(3);
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE ?+a<=>?+a' USING 1.0,1.0;
SHOW WARNINGS;

+-------+------+------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                    |
+-------+------+------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((1.0 + `test`.`t1`.`a`) <=> (1.0 + `test`.`t1`.`a`)) |
+-------+------+------------------------------------------------------------------------------------------------------------+

Notice, the true conditions where correctly eliminated for INT and DOUBLE, but not for DECIMAL.
This happens because DECIMAL_VALUE is not handled in Item_param::eq() or Item_param::value_eq() in 10.3

The problem is also repeatable with a parameter on the left side and a literal on the right side:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DECIMAL(10,1));
INSERT INTO t1 VALUES (1),(2),(3);
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE ?+a<=>1.0+a' USING 1.0;
SHOW WARNINGS;

+-------+------+------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                              |
+-------+------+------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1.0 + `test`.`t1`.`a` <=> 1.0 + `test`.`t1`.`a` |
+-------+------+------------------------------------------------------------------------------------------------------+

This happens for the same reason: DECIMAL_VALUE is not handler in Item_param::eq() / Item_param::value_eq().

Note, if I now put the listeral on the left side and the parameter in the right side, it works fine:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a DECIMAL(10,1));
INSERT INTO t1 VALUES (1),(2),(3);
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE 1.0+a<=>?+a' USING 1.0;
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1 |
+-------+------+--------------------------------------------------------+

Now execution goes through Item_decimal::eq() which correctly detects an Item_param with the same decimal value as equal.

Summary:

  • Propagation does not work for two parameters
  • Propagation works with a literal on the left side and a parameter on the right side
  • Propagation does not work with a parameter on the left side and a literal on the right side.

This happens because Item_param::eq() and Item_literal::eq() are not symmetric for DECIMAL.



 Comments   
Comment by Alexander Barkov [ 2018-06-07 ]

The same problem is repeatable for temporal data types:

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (a TIME);
INSERT INTO t1 VALUES (1),(2),(3);

-- This works fine
EXPLAIN EXTENDED SELECT * FROM t1 WHERE COALESCE(TIME'10:20:30',a)<=>COALESCE(TIME'10:20:30',a);
SHOW WARNINGS;

+-------+------+--------------------------------------------------------+
| Level | Code | Message                                                |
+-------+------+--------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where 1 |
+-------+------+--------------------------------------------------------+

-- This does not work
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE COALESCE(?,a)<=>COALESCE(?,a)' USING TIME'10:20:30',TIME'10:20:30';
SHOW WARNINGS;

+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                      |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where coalesce(<cache>(TIME'10:20:30'),`test`.`t1`.`a`) <=> coalesce(<cache>(TIME'10:20:30'),`test`.`t1`.`a`) |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+

-- This does not work
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE COALESCE(TIME''10:20:30'',a)<=>COALESCE(?,a)' USING TIME'10:20:30';
SHOW WARNINGS;

+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                             |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where coalesce(TIME'10:20:30',`test`.`t1`.`a`) <=> coalesce(<cache>(TIME'10:20:30'),`test`.`t1`.`a`) |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+

-- This does not work
EXECUTE IMMEDIATE 'EXPLAIN EXTENDED SELECT * FROM t1 WHERE COALESCE(?,a)<=>COALESCE(TIME''10:20:30'',a)' USING TIME'10:20:30';
SHOW WARNINGS;

+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                             |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `test`.`t1`.`a` AS `a` from `test`.`t1` where coalesce(<cache>(TIME'10:20:30'),`test`.`t1`.`a`) <=> coalesce(TIME'10:20:30',`test`.`t1`.`a`) |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------+

Summary:

  • With two literals propagation works fine
  • If there is a parameter, propagation does not work

This happens because Item_temporal_literal::basic_const_item() returns true, while Item_param::basic_const_item::basic_const_item() returns false when bound to a temporal value.

Generated at Thu Feb 08 07:49:22 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.