Skip to content

Commit 2831eee

Browse files
MDEV-34278: Implements DISTINCT for ST_Collect
Implements the DISTINCT modifier for ST_Collect Author: StefanoPetrilli <stefanop_1999@hotmail.it>
1 parent b07cf47 commit 2831eee

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

mysql-test/main/spatial_utility_function_collect.result

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ ST_EQUALS( (SELECT ST_COLLECT( location ) AS t FROM
1818
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2
1919
0,3 0) ',4326))
2020
1
21+
SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
22+
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
23+
0) ',4326));
24+
ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
25+
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
26+
0) ',4326))
27+
1
2128
INSERT INTO table_simple_aggregation (location) VALUES
2229
( ST_GEOMFROMTEXT('POINT(0 -0)' ,4326)),
2330
( NULL);

mysql-test/main/spatial_utility_function_collect.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,0 0,1 0,2
3535
0,3 0) ',4326));
3636
# --echo # Functional requirement F-8 Shall support DISTINCT in aggregates
3737
# --echo # result shall be 1
38-
# SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
39-
# table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
40-
# 0) ',4326));
38+
SELECT ST_EQUALS( (SELECT ST_COLLECT( DISTINCT location ) AS t FROM
39+
table_simple_aggregation) , ST_GEOMFROMTEXT('MULTIPOINT(0 0,1 0,2 0,3
40+
0) ',4326));
4141
# --echo # Functional requirement F-5: ST_COLLECT shall support group by, which
4242
# --echo # is given by aggregation machinery
4343
# --echo # result shall be

sql/item_sum.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4621,6 +4621,9 @@ bool Item_func_collect::add() {
46214621
if (tmp_arg[0]->null_value)
46224622
return 0;
46234623

4624+
if(is_distinct && list_contains_element(wkb))
4625+
return 0;
4626+
46244627
current_geometry_srid= uint4korr(wkb->ptr());
46254628
if (geometries.is_empty())
46264629
srid= current_geometry_srid;
@@ -4636,7 +4639,6 @@ bool Item_func_collect::add() {
46364639

46374640

46384641
void Item_func_collect::remove() {
4639-
String value;
46404642
String *wkb= args[0]->val_str(&value);
46414643
has_cached_result= false;
46424644

@@ -4661,17 +4663,34 @@ void Item_func_collect::remove() {
46614663
}
46624664

46634665

4664-
Item_func_collect::Item_func_collect(THD *thd, Item *item_par) :
4666+
bool Item_func_collect::list_contains_element(String *wkb) {
4667+
List_iterator<String> geometries_iterator(geometries);
4668+
String* temp_geometry;
4669+
while ((temp_geometry= geometries_iterator++))
4670+
{
4671+
String temp(temp_geometry->ptr(), temp_geometry->length(), &my_charset_bin);
4672+
4673+
if (wkb->eq(&temp, &my_charset_bin))
4674+
return true;
4675+
}
4676+
4677+
return false;
4678+
}
4679+
4680+
4681+
Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item *item_par) :
46654682
Item_sum_int(thd, item_par),
46664683
mem_root(thd->mem_root),
4684+
is_distinct(is_distinct),
46674685
group_collect_max_len(thd->variables.group_concat_max_len)
46684686
{
46694687
}
46704688

46714689

4672-
Item_func_collect::Item_func_collect(THD *thd, Item_func_collect *item) :
4690+
Item_func_collect::Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item) :
46734691
Item_sum_int(thd, item),
46744692
mem_root(thd->mem_root),
4693+
is_distinct(is_distinct),
46754694
group_collect_max_len(thd->variables.group_concat_max_len)
46764695
{
46774696
}
@@ -4748,5 +4767,5 @@ String *Item_func_collect::val_str(String *str)
47484767

47494768

47504769
Item *Item_func_collect::copy_or_same(THD *thd) {
4751-
return new (thd->mem_root) Item_func_collect(thd, this);
4770+
return new (thd->mem_root) Item_func_collect(thd, is_distinct, this);
47524771
}

sql/item_sum.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,7 @@ class Item_func_collect :public Item_sum_int
21252125
bool has_cached_result;
21262126
String cached_result;
21272127
MEM_ROOT *mem_root;
2128+
bool is_distinct;
21282129
List<String> geometries;
21292130
String value;
21302131
const uint group_collect_max_len;
@@ -2133,10 +2134,11 @@ class Item_func_collect :public Item_sum_int
21332134
bool add() override;
21342135
void cleanup() override;
21352136
void remove() override;
2137+
bool list_contains_element(String* wkb);
21362138

21372139
public:
2138-
Item_func_collect(THD *thd, Item *item_par);
2139-
Item_func_collect(THD *thd, Item_func_collect *item);
2140+
Item_func_collect(THD *thd, bool is_distinct, Item *item_par);
2141+
Item_func_collect(THD *thd, bool is_distinct, Item_func_collect *item);
21402142

21412143
enum Sumfunctype sum_func () const override
21422144
{

sql/sql_yacc.yy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11311,9 +11311,9 @@ sum_expr:
1131111311
if (unlikely($$ == NULL))
1131211312
MYSQL_YYABORT;
1131311313
}
11314-
| ST_COLLECT_SYM '(' in_sum_expr ')'
11314+
| ST_COLLECT_SYM '(' opt_distinct in_sum_expr ')'
1131511315
{
11316-
$$= new (thd->mem_root) Item_func_collect(thd, $3);
11316+
$$= new (thd->mem_root) Item_func_collect(thd, $3, $4);
1131711317
if (unlikely($$ == NULL))
1131811318
MYSQL_YYABORT;
1131911319
}

0 commit comments

Comments
 (0)