Commit 5c69879f authored by Igor Babaev's avatar Igor Babaev

Fixed bug mdev-11593.

When a condition containing NULLIF is pushed into a materialized
view/derived table the clone of the Item_func_nullif item must
be processed in a special way to guarantee that the first argument
points to the same item as the third argument.
parent 2c734e7b
......@@ -8140,3 +8140,41 @@ EXPLAIN
}
}
DROP TABLE t1,t2;
#
# MDEV-11593: pushdown of condition with NULLIF
#
CREATE TABLE t1 (i INT);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (2), (1);
SELECT * FROM v1 WHERE NULLIF(1, i);
i
2
EXPLAIN FORMAT=JSON
SELECT * FROM v1 WHERE NULLIF(1, i);
EXPLAIN
{
"query_block": {
"select_id": 1,
"table": {
"table_name": "<derived2>",
"access_type": "ALL",
"rows": 2,
"filtered": 100,
"attached_condition": "nullif(1,v1.i)",
"materialized": {
"query_block": {
"select_id": 2,
"table": {
"table_name": "t1",
"access_type": "ALL",
"rows": 2,
"filtered": 100,
"attached_condition": "nullif(1,t1.i)"
}
}
}
}
}
}
DROP VIEW v1;
DROP TABLE t1;
......@@ -1273,3 +1273,19 @@ SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
WHERE i IN ( SELECT MIN(j) FROM t2 );
DROP TABLE t1,t2;
--echo #
--echo # MDEV-11593: pushdown of condition with NULLIF
--echo #
CREATE TABLE t1 (i INT);
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES (2), (1);
SELECT * FROM v1 WHERE NULLIF(1, i);
EXPLAIN FORMAT=JSON
SELECT * FROM v1 WHERE NULLIF(1, i);
DROP VIEW v1;
DROP TABLE t1;
......@@ -1070,6 +1070,11 @@ class Item_func_nullif :public Item_func_hybrid_field_type
*/
Item_cache *m_cache;
int compare();
void reset_first_arg_if_needed()
{
if (arg_count == 3 && args[0] != args[2])
args[0]= args[2];
}
public:
/*
Here we pass three arguments to the parent constructor, as NULLIF
......@@ -1120,6 +1125,12 @@ class Item_func_nullif :public Item_func_hybrid_field_type
}
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_nullif>(thd, mem_root, this); }
Item *derived_field_transformer_for_having(THD *thd, uchar *arg)
{ reset_first_arg_if_needed(); return this; }
Item *derived_field_transformer_for_where(THD *thd, uchar *arg)
{ reset_first_arg_if_needed(); return this; }
Item *derived_grouping_field_transformer_for_where(THD *thd, uchar *arg)
{ reset_first_arg_if_needed(); return this; }
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment