Skip to content

Commit 09859f5

Browse files
author
Jan Wedvik
committed
Bug#34787357 Hypergraph: row estimates for field=non_field_term ignores indexes and histogram
Post-push fix: The initial commit triggers an assert for "reversed" conditions, e.g. expression=field_with_histogram (while field_with_histogram=expression does not). This commit adds new test cases and a fix for that issue. Change-Id: I48e6a51417135c2d868e839ac92458d68958ed7e
1 parent 7f0b580 commit 09859f5

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

mysql-test/r/hypergraph_bugs.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,11 @@ EXPLAIN
949949
-> Filter: (t1.c = floor(rand(0))) (rows=16) (actual rows=16 loops=1)
950950
-> Table scan on t1 (rows=256) (actual rows=256 loops=1)
951951

952+
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE FLOOR(RAND(0))=c;
953+
EXPLAIN
954+
-> Filter: (floor(rand(0)) = t1.c) (rows=16) (actual rows=16 loops=1)
955+
-> Table scan on t1 (rows=256) (actual rows=256 loops=1)
956+
952957
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE d=FLOOR(RAND(0));
953958
EXPLAIN
954959
-> Filter: (t1.d = floor(rand(0))) (rows=4) (actual rows=16 loops=1)
@@ -999,6 +1004,11 @@ EXPLAIN
9991004
-> Filter: (t1.c <> floor(rand(0))) (rows=240) (actual rows=240 loops=1)
10001005
-> Table scan on t1 (rows=256) (actual rows=256 loops=1)
10011006

1007+
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE FLOOR(RAND(0))<>c;
1008+
EXPLAIN
1009+
-> Filter: (floor(rand(0)) <> t1.c) (rows=240) (actual rows=240 loops=1)
1010+
-> Table scan on t1 (rows=256) (actual rows=256 loops=1)
1011+
10021012
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE b<>(SELECT MIN(b) FROM t1);
10031013
EXPLAIN
10041014
-> Filter: (t1.b <> (select #2)) (rows=240) (actual rows=240 loops=1)

mysql-test/t/hypergraph_bugs.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE b=FLOOR(RAND(0));
781781
--replace_regex $elide_costs_and_time
782782
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE c=FLOOR(RAND(0));
783783

784+
# Make estimate from histogram, even if value is unknown. Note that the field
785+
# is on the right hand side of '='.
786+
--replace_regex $elide_costs_and_time
787+
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE FLOOR(RAND(0))=c;
788+
784789
# Make estimate from second field of index. Since 'b' and 'd' are independent,
785790
# the estimate will be too low.
786791
--replace_regex $elide_costs_and_time
@@ -824,6 +829,11 @@ EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE b<>FLOOR(RAND(0));
824829
--replace_regex $elide_costs_and_time
825830
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE c<>FLOOR(RAND(0));
826831

832+
# Make estimate from histogram, even if value is unknown. Note that the field
833+
# is on the right hand side of '<>'.
834+
--replace_regex $elide_costs_and_time
835+
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE FLOOR(RAND(0))<>c;
836+
827837
# Make estimate from index, even if value is unknown.
828838
--replace_regex $elide_costs_and_time
829839
EXPLAIN ANALYZE SELECT 1 FROM t1 WHERE b<>(SELECT MIN(b) FROM t1);

sql/histograms/histogram.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,12 +1964,13 @@ bool Histogram::get_raw_selectivity(Item **items, size_t item_count,
19641964
assert(item_count == 2);
19651965
/*
19661966
Verify that one side of the predicate is a column/field, and that the
1967-
other side is a constant value.
1967+
other side is a constant value (except for EQUALS_TO and NOT_EQUALS_TO).
19681968
1969-
Make sure that we have the constant item as the right side argument of
1969+
Make sure that we have the field item as the left side argument of
19701970
the predicate internally.
19711971
*/
1972-
if (items[0]->const_item() && items[1]->type() == Item::FIELD_ITEM) {
1972+
if (items[0]->type() != Item::FIELD_ITEM &&
1973+
items[1]->type() == Item::FIELD_ITEM) {
19731974
// Flip the operators as well as the operator itself.
19741975
switch (op) {
19751976
case enum_operator::GREATER_THAN:
@@ -1991,10 +1992,9 @@ bool Histogram::get_raw_selectivity(Item **items, size_t item_count,
19911992
items_flipped[0] = items[1];
19921993
items_flipped[1] = items[0];
19931994
return get_selectivity(items_flipped, item_count, op, selectivity);
1994-
} else if ((items[0]->type() != Item::FIELD_ITEM ||
1995-
!items[1]->const_item()) &&
1996-
op != enum_operator::EQUALS_TO &&
1997-
op != enum_operator::NOT_EQUALS_TO) {
1995+
} else if (items[0]->type() != Item::FIELD_ITEM ||
1996+
(!items[1]->const_item() && op != enum_operator::EQUALS_TO &&
1997+
op != enum_operator::NOT_EQUALS_TO)) {
19981998
return true;
19991999
}
20002000
break;

0 commit comments

Comments
 (0)