Commit b9df528c authored by StefanoPetrilli's avatar StefanoPetrilli Committed by Dave Gosselin

MDEV-34278: Implements DISTINCT for ST_Collect

Implements the DISTINCT modifier for ST_Collect

Author: StefanoPetrilli <stefanop_1999@hotmail.it>
parent fd5578c6
...@@ -18,6 +18,13 @@ ST_EQUALS( (SELECT ST_COLLECT( location ) AS t FROM ...@@ -18,6 +18,13 @@ ST_EQUALS( (SELECT ST_COLLECT( location ) AS t FROM
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2 table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2
0,3 0) ',4326)) 0,3 0) ',4326))
1 1
SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
0) ',4326));
ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
0) ',4326))
1
INSERT INTO table_simple_aggregation (location) VALUES INSERT INTO table_simple_aggregation (location) VALUES
( ST_GEOMFROMTEXT('POINT(0 -0)' ,4326)), ( ST_GEOMFROMTEXT('POINT(0 -0)' ,4326)),
( NULL); ( NULL);
......
...@@ -35,9 +35,9 @@ table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2 ...@@ -35,9 +35,9 @@ table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2
0,3 0) ',4326)); 0,3 0) ',4326));
# --echo # Functional requirement F-8 Shall support DISTINCT in aggregates # --echo # Functional requirement F-8 Shall support DISTINCT in aggregates
# --echo # result shall be 1 # --echo # result shall be 1
# SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
# table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3 table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
# 0) ',4326)); 0) ',4326));
# --echo # Functional requirement F-5: ST_COLLECT shall support group by, which # --echo # Functional requirement F-5: ST_COLLECT shall support group by, which
# --echo # is given by aggregation machinery # --echo # is given by aggregation machinery
# --echo # result shall be # --echo # result shall be
......
...@@ -4649,6 +4649,9 @@ bool Item_func_collect::add() { ...@@ -4649,6 +4649,9 @@ bool Item_func_collect::add() {
if (tmp_arg[0]->null_value) if (tmp_arg[0]->null_value)
return 0; return 0;
if(is_distinct && list_contains_element(wkb))
return 0;
current_geometry_srid= uint4korr(wkb->ptr()); current_geometry_srid= uint4korr(wkb->ptr());
if (geometries.is_empty()) if (geometries.is_empty())
srid= current_geometry_srid; srid= current_geometry_srid;
...@@ -4664,7 +4667,6 @@ bool Item_func_collect::add() { ...@@ -4664,7 +4667,6 @@ bool Item_func_collect::add() {
void Item_func_collect::remove() { void Item_func_collect::remove() {
String value;
String *wkb= args[0]->val_str(&value); String *wkb= args[0]->val_str(&value);
has_cached_result= false; has_cached_result= false;
...@@ -4689,17 +4691,34 @@ void Item_func_collect::remove() { ...@@ -4689,17 +4691,34 @@ void Item_func_collect::remove() {
} }
Item_func_collect::Item_func_collect(THD *thd, Item *item_par) : bool Item_func_collect::list_contains_element(String *wkb) {
List_iterator<String> geometries_iterator(geometries);
String* temp_geometry;
while ((temp_geometry= geometries_iterator++))
{
String temp(temp_geometry->ptr(), temp_geometry->length(), &my_charset_bin);
if (wkb->eq(&temp, &my_charset_bin))
return true;
}
return false;
}
Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item *item_par) :
Item_sum_int(thd, item_par), Item_sum_int(thd, item_par),
mem_root(thd->mem_root), mem_root(thd->mem_root),
is_distinct(is_distinct),
group_collect_max_len(thd->variables.group_concat_max_len) group_collect_max_len(thd->variables.group_concat_max_len)
{ {
} }
Item_func_collect::Item_func_collect(THD *thd, Item_func_collect *item) : Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item) :
Item_sum_int(thd, item), Item_sum_int(thd, item),
mem_root(thd->mem_root), mem_root(thd->mem_root),
is_distinct(is_distinct),
group_collect_max_len(thd->variables.group_concat_max_len) group_collect_max_len(thd->variables.group_concat_max_len)
{ {
} }
...@@ -4776,5 +4795,5 @@ String *Item_func_collect::val_str(String *str) ...@@ -4776,5 +4795,5 @@ String *Item_func_collect::val_str(String *str)
Item *Item_func_collect::copy_or_same(THD *thd) { Item *Item_func_collect::copy_or_same(THD *thd) {
return new (thd->mem_root) Item_func_collect(thd, this); return new (thd->mem_root) Item_func_collect(thd, is_distinct, this);
} }
...@@ -2129,6 +2129,7 @@ class Item_func_collect :public Item_sum_int ...@@ -2129,6 +2129,7 @@ class Item_func_collect :public Item_sum_int
bool has_cached_result; bool has_cached_result;
String cached_result; String cached_result;
MEM_ROOT *mem_root; MEM_ROOT *mem_root;
bool is_distinct;
List<String> geometries; List<String> geometries;
String value; String value;
const uint group_collect_max_len; const uint group_collect_max_len;
...@@ -2137,10 +2138,11 @@ class Item_func_collect :public Item_sum_int ...@@ -2137,10 +2138,11 @@ class Item_func_collect :public Item_sum_int
bool add() override; bool add() override;
void cleanup() override; void cleanup() override;
void remove() override; void remove() override;
bool list_contains_element(String* wkb);
public: public:
Item_func_collect(THD *thd, Item *item_par); Item_func_collect(THD *thd, bool is_distinct, Item *item_par);
Item_func_collect(THD *thd, Item_func_collect *item); Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item);
enum Sumfunctype sum_func () const override enum Sumfunctype sum_func () const override
{ {
......
...@@ -11033,9 +11033,9 @@ sum_expr: ...@@ -11033,9 +11033,9 @@ sum_expr:
if (unlikely($$ == NULL)) if (unlikely($$ == NULL))
MYSQL_YYABORT; MYSQL_YYABORT;
} }
| ST_COLLECT_SYM '(' in_sum_expr ')' | ST_COLLECT_SYM '(' opt_distinct in_sum_expr ')'
{ {
$$= new (thd->mem_root) Item_func_collect(thd, $3); $$= new (thd->mem_root) Item_func_collect(thd, $3, $4);
if (unlikely($$ == NULL)) if (unlikely($$ == NULL))
MYSQL_YYABORT; MYSQL_YYABORT;
} }
......
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