Skip to content

Commit 5c69879

Browse files
committed
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.
1 parent 2c734e7 commit 5c69879

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

mysql-test/r/derived_cond_pushdown.result

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8140,3 +8140,41 @@ EXPLAIN
81408140
}
81418141
}
81428142
DROP TABLE t1,t2;
8143+
#
8144+
# MDEV-11593: pushdown of condition with NULLIF
8145+
#
8146+
CREATE TABLE t1 (i INT);
8147+
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
8148+
INSERT INTO t1 VALUES (2), (1);
8149+
SELECT * FROM v1 WHERE NULLIF(1, i);
8150+
i
8151+
2
8152+
EXPLAIN FORMAT=JSON
8153+
SELECT * FROM v1 WHERE NULLIF(1, i);
8154+
EXPLAIN
8155+
{
8156+
"query_block": {
8157+
"select_id": 1,
8158+
"table": {
8159+
"table_name": "<derived2>",
8160+
"access_type": "ALL",
8161+
"rows": 2,
8162+
"filtered": 100,
8163+
"attached_condition": "nullif(1,v1.i)",
8164+
"materialized": {
8165+
"query_block": {
8166+
"select_id": 2,
8167+
"table": {
8168+
"table_name": "t1",
8169+
"access_type": "ALL",
8170+
"rows": 2,
8171+
"filtered": 100,
8172+
"attached_condition": "nullif(1,t1.i)"
8173+
}
8174+
}
8175+
}
8176+
}
8177+
}
8178+
}
8179+
DROP VIEW v1;
8180+
DROP TABLE t1;

mysql-test/t/derived_cond_pushdown.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,3 +1273,19 @@ SELECT * FROM ( SELECT DISTINCT * FROM t1 ) AS sq
12731273
WHERE i IN ( SELECT MIN(j) FROM t2 );
12741274

12751275
DROP TABLE t1,t2;
1276+
1277+
--echo #
1278+
--echo # MDEV-11593: pushdown of condition with NULLIF
1279+
--echo #
1280+
1281+
CREATE TABLE t1 (i INT);
1282+
CREATE OR REPLACE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1;
1283+
1284+
INSERT INTO t1 VALUES (2), (1);
1285+
1286+
SELECT * FROM v1 WHERE NULLIF(1, i);
1287+
EXPLAIN FORMAT=JSON
1288+
SELECT * FROM v1 WHERE NULLIF(1, i);
1289+
1290+
DROP VIEW v1;
1291+
DROP TABLE t1;

sql/item_cmpfunc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,11 @@ class Item_func_nullif :public Item_func_hybrid_field_type
10701070
*/
10711071
Item_cache *m_cache;
10721072
int compare();
1073+
void reset_first_arg_if_needed()
1074+
{
1075+
if (arg_count == 3 && args[0] != args[2])
1076+
args[0]= args[2];
1077+
}
10731078
public:
10741079
/*
10751080
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
11201125
}
11211126
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
11221127
{ return get_item_copy<Item_func_nullif>(thd, mem_root, this); }
1128+
Item *derived_field_transformer_for_having(THD *thd, uchar *arg)
1129+
{ reset_first_arg_if_needed(); return this; }
1130+
Item *derived_field_transformer_for_where(THD *thd, uchar *arg)
1131+
{ reset_first_arg_if_needed(); return this; }
1132+
Item *derived_grouping_field_transformer_for_where(THD *thd, uchar *arg)
1133+
{ reset_first_arg_if_needed(); return this; }
11231134
};
11241135

11251136

0 commit comments

Comments
 (0)