WL#13454: ST_Collect - Spatial aggregations

Affects: Server-8.0   —   Status: Complete

ST_Collect, a spatial (GIS) aggregate function, with windowing support, transforming geometries into multigeometries. The inspiration for this worklog is taken from PostGIS. https://postgis.net/docs/ST_Collect.html

A use case for this is to make a single point cloud (multpoint) from a column of points.

Syntax: adding this to sum_expr where our aggregations live

sum_expr: 
...
        | ST_COLLECT_SYM '(' in_sum_expr ')' opt_windowing_clause
        | ST_COLLECT_SYM '(' DISTINCT in_sum_expr ')' opt_windowing_clause
...

Example gathering the positions where production happens from a table:

create table sales_with_location 
( running_number integer not null auto_increment, 
year integer, 
country varchar(256), 
product varchar(256), 
profit double, 
capital_position Geometry, 
primary key ( running_number));

insert into sales_with_location 
( year , country ,product , profit , capital_position ) values
( 2000 , "Finland" , "Computer"   ,   1500 ,st_geomfromtext('point(60 -24)',4326)),
( 2000 , "Finland" , "Phone"      ,    100 ,st_geomfromtext('point(60 -24)',4326)),
( 2001 , "Finland" , "Phone"      ,     10 ,st_geomfromtext('point(60 -24)',4326)),
( 2000 , "India"   , "Calculator" ,     75 ,st_geomfromtext('point(28 -77)',4326)),
( 2000 , "India"   , "Calculator" ,     75 ,st_geomfromtext('point(28 -77)',4326)),
( 2000 , "India"   , "Computer"   ,   1200 ,st_geomfromtext('point(28 -77)',4326)),
( 2000 , "USA"     , "Calculator" ,     75 ,st_geomfromtext('point(38  77)',4326)),
( 2000 , "USA"     , "Computer"   ,   1500 ,st_geomfromtext('point(38  77)',4326)),
( 2001 , "USA"     , "Calculator" ,     50 ,st_geomfromtext('point(38  77)',4326)),
( 2001 , "USA"     , "Computer"   ,   1500 ,st_geomfromtext('point(38  77)',4326)),
( 2001 , "USA"     , "Computer"   ,   1200 ,st_geomfromtext('point(38  77)',4326)),
( 2001 , "USA"     , "TV"         ,    150 ,st_geomfromtext('point(38  77)',4326)),
( 2001 , "USA"     , "TV"         ,    100 ,st_geomfromtext('point(38  77)',4326));
SELECT st_astext(st_collect(distinct capital_position) ) as t FROM sales_with_location;

+---------------------------------------+
| t                                     |
+---------------------------------------+
| MULTIPOINT((60 -24),(28 -77),(38 77)) |
+---------------------------------------+
1 row in set (0.00 sec)

If all the geometries are of the same type and this is points, linestrings or polygons the result will be a Multipoint, Multilinestring or a Multipolygon. Otherwise a Geometrycollection is returned.