Commit 768620ee authored by Alexander Barkov's avatar Alexander Barkov

MDEV-8189 field<>const and const<>field are not symmetric

parent 1b2f9126
......@@ -639,6 +639,16 @@ SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t2.pk <> t1.d1 AND t2.pk = 4;
# MDEV-8189 field<>const and const<>field are not symmetric
# Do the same EXPLAIN and SELECT
# with "t1.d1 <> t2.pk" instead of "t2.pk <> t1.d1"
EXPLAIN
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
DROP TABLE t1, t2;
--echo #
......
......@@ -600,6 +600,16 @@ SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t2.pk <> t1.d1 AND t2.pk = 4;
d1 pk i1
1 4 1
EXPLAIN
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL k1 9 NULL 3 Using index
1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 Using where
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
d1 pk i1
1 4 1
DROP TABLE t1, t2;
#
# BUG#778434 Wrong result with in_to_exists=on in maria-5.3-mwl89
......
......@@ -4321,6 +4321,23 @@ h
n
v
p
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index idx1 idx2 7 NULL 20 Using index; Using temporary; Using filesort
1 SIMPLE t3 eq_ref PRIMARY,idx2 PRIMARY 4 test.t2.i 1 Using where
1 SIMPLE t1 ref idx2 idx2 3 test.t3.v 5 Using where
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
b
h
n
v
p
SET SESSION join_cache_level=6;
EXPLAIN
SELECT t2.v FROM t1, t2, t3
......@@ -4339,6 +4356,23 @@ h
n
v
p
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index idx1 idx2 7 NULL 20 Using index; Using temporary; Using filesort
1 SIMPLE t3 eq_ref PRIMARY,idx2 PRIMARY 4 test.t2.i 1 Using where; Using join buffer (flat, BKA join); Key-ordered Rowid-ordered scan
1 SIMPLE t1 ref idx2 idx2 3 test.t3.v 5 Using where; Using join buffer (incremental, BKA join); Key-ordered Rowid-ordered scan
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
b
h
n
v
p
SET SESSION join_cache_level=4;
EXPLAIN
SELECT t2.v FROM t1, t2, t3
......@@ -4357,6 +4391,23 @@ h
n
v
p
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index idx1 idx2 7 NULL 20 Using index; Using temporary; Using filesort
1 SIMPLE t3 hash_ALL PRIMARY,idx2 #hash#PRIMARY 4 test.t2.i 20 Using where; Using join buffer (flat, BNLH join)
1 SIMPLE t1 hash_ALL idx2 #hash#idx2 3 test.t3.v 44 Using where; Using join buffer (incremental, BNLH join)
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
v
b
h
n
v
p
DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT;
#
......
......@@ -604,6 +604,16 @@ SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t2.pk <> t1.d1 AND t2.pk = 4;
d1 pk i1
1 4 1
EXPLAIN
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL k1 9 NULL 3 Using index
1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 Using where
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
d1 pk i1
1 4 1
DROP TABLE t1, t2;
#
# BUG#778434 Wrong result with in_to_exists=on in maria-5.3-mwl89
......
......@@ -2241,3 +2241,27 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
Warnings:
Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b`,`test`.`t2`.`c` AS `c` from `test`.`t2` where 0
drop table t1,t2;
#
# Start of 10.1 tests
#
#
# MDEV-8189 field<>const and const<>field are not symmetric
#
CREATE TABLE t1 (a INT, b INT, KEY(a));
INSERT INTO t1 (a) VALUES (10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(70);
EXPLAIN SELECT * FROM t1 WHERE a<>10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 3 Using index condition
EXPLAIN SELECT * FROM t1 WHERE 10<>a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 3 Using index condition
SELECT * FROM t1 WHERE a<>10;
a b
70 NULL
SELECT * FROM t1 WHERE 10<>a;
a b
70 NULL
DROP TABLE t1;
#
# End of 10.1 tests
#
......@@ -1092,6 +1092,27 @@ AND ( alias5.b >= alias3.b OR alias5.c != alias3.c )
);
COUNT(*)
3724
EXPLAIN
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias3 ALL PRIMARY NULL NULL NULL 19 Using where
1 PRIMARY alias4 ref PRIMARY,c c 4 test.alias3.d 1 Using index
1 PRIMARY alias5 eq_ref PRIMARY PRIMARY 4 test.alias4.b 1 Using where; FirstMatch(alias3)
1 PRIMARY alias2 ALL NULL NULL NULL NULL 14 Using join buffer (flat, BNL join)
1 PRIMARY alias1 ALL NULL NULL NULL NULL 14 Using join buffer (flat, BNL join)
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
COUNT(*)
3724
set optimizer_prune_level=@tmp_951283;
DROP TABLE t1,t2;
#
......
......@@ -1107,6 +1107,27 @@ AND ( alias5.b >= alias3.b OR alias5.c != alias3.c )
);
COUNT(*)
3724
EXPLAIN
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias3 ALL PRIMARY NULL NULL NULL 19 Using where
1 PRIMARY alias4 ref PRIMARY,c c 4 test.alias3.d 1 Using index
1 PRIMARY alias5 eq_ref PRIMARY PRIMARY 4 test.alias4.b 1 Using where; FirstMatch(alias3)
1 PRIMARY alias2 ALL NULL NULL NULL NULL 14 Using join buffer (flat, BNL join)
1 PRIMARY alias1 ALL NULL NULL NULL NULL 14 Using join buffer (incremental, BNL join)
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
COUNT(*)
3724
set optimizer_prune_level=@tmp_951283;
DROP TABLE t1,t2;
#
......
......@@ -1094,6 +1094,27 @@ AND ( alias5.b >= alias3.b OR alias5.c != alias3.c )
);
COUNT(*)
3724
EXPLAIN
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY alias3 ALL PRIMARY NULL NULL NULL 19 Using where
1 PRIMARY alias4 ref PRIMARY,c c 4 test.alias3.d 1 Using index
1 PRIMARY alias5 eq_ref PRIMARY PRIMARY 4 test.alias4.b 1 Using where; FirstMatch(alias3)
1 PRIMARY alias2 ALL NULL NULL NULL NULL 14 Using join buffer (flat, BNL join)
1 PRIMARY alias1 ALL NULL NULL NULL NULL 14 Using join buffer (flat, BNL join)
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
COUNT(*)
3724
set optimizer_prune_level=@tmp_951283;
DROP TABLE t1,t2;
#
......
......@@ -606,6 +606,16 @@ SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t2.pk <> t1.d1 AND t2.pk = 4;
d1 pk i1
1 4 1
EXPLAIN
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL k1 9 NULL 3 Using index
1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 Using where
SELECT t1.d1, t2.pk, t2.i1 FROM t1 STRAIGHT_JOIN t2 ON t2.i1
WHERE t1.d1 <> t2.pk AND t2.pk = 4;
d1 pk i1
1 4 1
DROP TABLE t1, t2;
#
# BUG#778434 Wrong result with in_to_exists=on in maria-5.3-mwl89
......
......@@ -2505,6 +2505,18 @@ SELECT t2.v FROM t1, t2, t3
WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
# MDEV-8189 field<>const and const<>field are not symmetric
# Do the same EXPLAIN and SELECT
# for "t2.v <> t3.v" instead of "t3.v <> t2.v"
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SET SESSION join_cache_level=6;
EXPLAIN
SELECT t2.v FROM t1, t2, t3
......@@ -2514,6 +2526,17 @@ SELECT t2.v FROM t1, t2, t3
WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
# MDEV-8189 field<>const and const<>field are not symmetric
# Do the same EXPLAIN and SELECT
# for "t2.v <> t3.v" instead of "t3.v <> t2.v"
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SET SESSION join_cache_level=4;
EXPLAIN
SELECT t2.v FROM t1, t2, t3
......@@ -2523,6 +2546,17 @@ SELECT t2.v FROM t1, t2, t3
WHERE t3.v <> t2.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
# MDEV-8189 field<>const and const<>field are not symmetric
# Do the same EXPLAIN and SELECT
# for "t2.v <> t3.v" instead of "t3.v <> t2.v"
EXPLAIN
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
SELECT t2.v FROM t1, t2, t3
WHERE t2.v <> t3.v AND t3.pk = t2.i AND t1.v = t3.v AND t1.pk*2<100
GROUP BY t2.v ORDER BY t1.pk,t2.v;
DROP TABLE t1,t2,t3;
SET SESSION join_cache_level=DEFAULT;
......
......@@ -1777,3 +1777,24 @@ explain extended select * from t2 where (b > 25 and b < 15) or c < 44;
explain extended select * from t2 where (b > 25 and b < 15) or (a>55 and a<44);
drop table t1,t2;
--echo #
--echo # Start of 10.1 tests
--echo #
--echo #
--echo # MDEV-8189 field<>const and const<>field are not symmetric
--echo #
CREATE TABLE t1 (a INT, b INT, KEY(a));
INSERT INTO t1 (a) VALUES (10),(10),(10),(10),(10),(10),(10),(10),(10),(10),(70);
EXPLAIN SELECT * FROM t1 WHERE a<>10;
EXPLAIN SELECT * FROM t1 WHERE 10<>a;
SELECT * FROM t1 WHERE a<>10;
SELECT * FROM t1 WHERE 10<>a;
DROP TABLE t1;
--echo #
--echo # End of 10.1 tests
--echo #
......@@ -1222,6 +1222,25 @@ WHERE alias3.d IN (
AND ( alias5.b >= alias3.b OR alias5.c != alias3.c )
);
# MDEV-8189 field<>const and const<>field are not symmetric
# Do the same EXPLAIN SELECT and SELECT
# with "alias3.c != alias5.c" instead of "alias5.c != alias3.c"
EXPLAIN
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
SELECT COUNT(*) FROM t1 AS alias1, t1 AS alias2, t2 AS alias3
WHERE alias3.d IN (
SELECT alias4.c FROM t2 AS alias4, t2 AS alias5
WHERE alias5.b = alias4.b
AND ( alias5.b >= alias3.b OR alias3.c != alias5.c )
);
set optimizer_prune_level=@tmp_951283;
DROP TABLE t1,t2;
......
......@@ -591,6 +591,7 @@ public:
Item_func_ne(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
longlong val_int();
enum Functype functype() const { return NE_FUNC; }
enum Functype rev_functype() const { return NE_FUNC; }
cond_result eq_cmp_result() const { return COND_FALSE; }
const char *func_name() const { return "<>"; }
Item *negated_item();
......
......@@ -4744,12 +4744,25 @@ Item_func_ne::add_key_fields(JOIN *join, KEY_FIELD **key_fields,
uint *and_level, table_map usable_tables,
SARGABLE_PARAM **sargables)
{
if (is_local_field(args[0]) && !(used_tables() & OUTER_REF_TABLE_BIT))
if (!(used_tables() & OUTER_REF_TABLE_BIT))
{
Item **values= is_local_field(args[1]) ? args : args + 1;
add_key_equal_fields(join, key_fields, *and_level, this,
(Item_field*) (args[0]->real_item()), false,
values, 1, usable_tables, sargables);
/*
QQ: perhaps test for !is_local_field(args[1]) is not really needed here.
Other comparison functions, e.g. Item_func_le, Item_func_gt, etc,
do not have this test. See Item_bool_func2::add_key_field_optimize_op().
Check with the optimizer team.
*/
if (is_local_field(args[0]) && !is_local_field(args[1]))
add_key_equal_fields(join, key_fields, *and_level, this,
(Item_field*) (args[0]->real_item()), false,
&args[1], 1, usable_tables, sargables);
/*
QQ: perhaps test for !is_local_field(args[0]) is not really needed here.
*/
if (is_local_field(args[1]) && !is_local_field(args[0]))
add_key_equal_fields(join, key_fields, *and_level, this,
(Item_field*) (args[1]->real_item()), false,
&args[0], 1, usable_tables, sargables);
}
}
......
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