Skip to content

Commit

Permalink
MDEV-8610 "WHERE CONTAINS(indexed_geometry_column,1)" causes full tab…
Browse files Browse the repository at this point in the history
…le scan
  • Loading branch information
Alexander Barkov committed Aug 13, 2015
1 parent 9d884fd commit 60985e5
Show file tree
Hide file tree
Showing 6 changed files with 729 additions and 664 deletions.
24 changes: 24 additions & 0 deletions mysql-test/r/gis-rtree.result
Original file line number Diff line number Diff line change
Expand Up @@ -1618,5 +1618,29 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 34 NULL 1 Using where
DROP TABLE t1;
#
# MDEV-8610 "WHERE CONTAINS(indexed_geometry_column,1)" causes full table scan
#
CREATE TABLE t1 (a GEOMETRY NOT NULL, SPATIAL KEY(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (Point(1,1)),(Point(2,2)),(Point(3,3));
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1.0);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1e0);
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,TIME'00:00:00');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,DATE'2001-01-01');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,TIMESTAMP'2001-01-01 00:00:00');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
DROP TABLE t1;
#
# End of 10.1 tests
#
14 changes: 14 additions & 0 deletions mysql-test/t/gis-rtree.test
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,20 @@ EXPLAIN SELECT * FROM t1 WHERE MBRINTERSECTS(Point(1,2),a);
EXPLAIN SELECT * FROM t1 WHERE ST_INTERSECTS(Point(1,2),a);
DROP TABLE t1;

--echo #
--echo # MDEV-8610 "WHERE CONTAINS(indexed_geometry_column,1)" causes full table scan
--echo #
CREATE TABLE t1 (a GEOMETRY NOT NULL, SPATIAL KEY(a)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (Point(1,1)),(Point(2,2)),(Point(3,3));
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1);
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1.0);
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,1e0);
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,TIME'00:00:00');
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,DATE'2001-01-01');
EXPLAIN SELECT * FROM t1 WHERE CONTAINS(a,TIMESTAMP'2001-01-01 00:00:00');
DROP TABLE t1;


--echo #
--echo # End of 10.1 tests
--echo #
73 changes: 73 additions & 0 deletions sql/item_geofunc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "set_var.h"
#ifdef HAVE_SPATIAL
#include <m_ctype.h>
#include "opt_range.h"


Field *Item_geometry_func::tmp_table_field(TABLE *t_arg)
Expand Down Expand Up @@ -929,6 +930,78 @@ String *Item_func_spatial_collection::val_str(String *str)
Functions for spatial relations
*/

static SEL_ARG sel_arg_impossible(SEL_ARG::IMPOSSIBLE);

SEL_ARG *
Item_func_spatial_rel::get_mm_leaf(RANGE_OPT_PARAM *param,
Field *field, KEY_PART *key_part,
Item_func::Functype type, Item *value)
{
DBUG_ENTER("Item_func_spatial_rel::get_mm_leaf");
if (key_part->image_type != Field::itMBR)
DBUG_RETURN(0);
if (value->cmp_type() != STRING_RESULT)
DBUG_RETURN(&sel_arg_impossible);

if (param->using_real_indexes &&
!field->optimize_range(param->real_keynr[key_part->key],
key_part->part))
DBUG_RETURN(0);

if (value->save_in_field_no_warnings(field, 1))
DBUG_RETURN(&sel_arg_impossible); // Bad GEOMETRY value

DBUG_ASSERT(!field->real_maybe_null()); // SPATIAL keys do not support NULL

uchar *str= (uchar*) alloc_root(param->mem_root, key_part->store_length + 1);
if (!str)
DBUG_RETURN(0); // out of memory
field->get_key_image(str, key_part->length, key_part->image_type);
SEL_ARG *tree;
if (!(tree= new (param->mem_root) SEL_ARG(field, str, str)))
DBUG_RETURN(0); // out of memory

switch (type) {
case SP_EQUALS_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_EQUAL;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_DISJOINT_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_DISJOINT;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_INTERSECTS_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_TOUCHES_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_CROSSES_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_WITHIN_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_WITHIN;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_CONTAINS_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_CONTAIN;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
case SP_OVERLAPS_FUNC:
tree->min_flag= GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
tree->max_flag= NO_MAX_RANGE;
break;
default:
DBUG_ASSERT(0);
break;
}
DBUG_RETURN(tree);
}


const char *Item_func_spatial_mbr_rel::func_name() const
{
switch (spatial_rel) {
Expand Down
3 changes: 3 additions & 0 deletions sql/item_geofunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ class Item_func_spatial_rel: public Item_bool_func2
protected:
enum Functype spatial_rel;
String tmp_value1, tmp_value2;
SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *param, Field *field,
KEY_PART *key_part,
Item_func::Functype type, Item *value);
public:
Item_func_spatial_rel(Item *a, Item *b, enum Functype sp_rel)
:Item_bool_func2(a, b), spatial_rel(sp_rel)
Expand Down
Loading

0 comments on commit 60985e5

Please sign in to comment.