Skip to content

Commit

Permalink
builtins: implement ST_MemCollect and ST_MemUnion
Browse files Browse the repository at this point in the history
These are implemented as aliases for ST_Collect and ST_Union. In PostGIS
these are memory-optimized versions, but for now it should be sufficient
to simply make them functional.

Release justification: low risk, high benefit changes to existing functionality

Release note (sql change): Implement the geometry aggregate builtins
`ST_MemCollect` and `ST_MemUnion`.
  • Loading branch information
erikgrinaker committed Aug 31, 2020
1 parent 42e73f7 commit c9ff65a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
4 changes: 4 additions & 0 deletions docs/generated/sql/aggregates.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@
</span></td></tr>
<tr><td><a name="st_makeline"></a><code>st_makeline(arg1: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Forms a LineString from Point, MultiPoint or LineStrings. Other shapes will be ignored.</p>
</span></td></tr>
<tr><td><a name="st_memcollect"></a><code>st_memcollect(arg1: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Collects geometries into a GeometryCollection or multi-type as appropriate.</p>
</span></td></tr>
<tr><td><a name="st_memunion"></a><code>st_memunion(arg1: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Applies a spatial union to the geometries provided.</p>
</span></td></tr>
<tr><td><a name="st_union"></a><code>st_union(arg1: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Applies a spatial union to the geometries provided.</p>
</span></td></tr>
<tr><td><a name="stddev"></a><code>stddev(arg1: <a href="decimal.html">decimal</a>) &rarr; <a href="decimal.html">decimal</a></code></td><td><span class="funcdesc"><p>Calculates the standard deviation of the selected values.</p>
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -4327,6 +4327,11 @@ SELECT ST_AsEWKT(ST_Union(g::geometry)) FROM ( VALUES (NULL), ('POINT(1 0)'), ('
----
LINESTRING (0 0, 5 0)

query T
SELECT ST_AsEWKT(ST_MemUnion(g::geometry)) FROM ( VALUES (NULL), ('POINT(1 0)'), ('LINESTRING(0 0, 5 0)')) tbl(g)
----
LINESTRING (0 0, 5 0)

query T
SELECT ST_AsEWKT(ST_Collect(g::geometry)) FROM ( VALUES (NULL) ) tbl(g)
----
Expand Down Expand Up @@ -4365,6 +4370,10 @@ SELECT ST_AsEWKT(ST_Collect(g::geometry)) FROM ( VALUES ('GEOMETRYCOLLECTION (PO
----
GEOMETRYCOLLECTION (GEOMETRYCOLLECTION (POINT (1 1)), GEOMETRYCOLLECTION (POINT (2 2)))

query T
SELECT ST_AsEWKT(ST_MemCollect(g::geometry)) FROM ( VALUES (NULL), ('POINT (1 1)'), ('POINT EMPTY'), ('POINT (2 2)')) tbl(g)
----
MULTIPOINT (1 1, EMPTY, 2 2)

query T
SELECT ST_AsEWKT(
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/optbuilder/groupby.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,11 @@ func (b *Builder) constructAggregate(name string, args []opt.ScalarExpr) opt.Sca
return b.factory.ConstructVarPop(args[0])
case "st_makeline":
return b.factory.ConstructSTMakeLine(args[0])
case "st_collect":
case "st_collect", "st_memcollect":
return b.factory.ConstructSTCollect(args[0])
case "st_extent":
return b.factory.ConstructSTExtent(args[0])
case "st_union":
case "st_union", "st_memunion":
return b.factory.ConstructSTUnion(args[0])
case "xor_agg":
return b.factory.ConstructXorAgg(args[0])
Expand Down
82 changes: 46 additions & 36 deletions pkg/sql/sem/builtins/aggregate_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,42 +400,10 @@ var aggregates = map[string]builtinDefinition{
tree.VolatilityImmutable,
),
),
"st_union": makeBuiltin(
tree.FunctionProperties{
Class: tree.AggregateClass,
NullableArgs: true,
AvailableOnPublicSchema: true,
},
makeAggOverload(
[]*types.T{types.Geometry},
types.Geometry,
func(
params []*types.T, evalCtx *tree.EvalContext, arguments tree.Datums,
) tree.AggregateFunc {
return &stUnionAgg{}
},
infoBuilder{
info: "Applies a spatial union to the geometries provided.",
}.String(),
tree.VolatilityImmutable,
),
),
"st_collect": makeBuiltin(
tree.FunctionProperties{
Class: tree.AggregateClass,
NullableArgs: true,
AvailableOnPublicSchema: true,
},
makeAggOverload(
[]*types.T{types.Geometry},
types.Geometry,
newSTCollectAgg,
infoBuilder{
info: "Collects geometries into a GeometryCollection or multi-type as appropriate.",
}.String(),
tree.VolatilityImmutable,
),
),
"st_union": makeSTUnionBuiltin(),
"st_memunion": makeSTUnionBuiltin(),
"st_collect": makeSTCollectBuiltin(),
"st_memcollect": makeSTCollectBuiltin(),

AnyNotNull: makePrivate(makeBuiltin(aggProps(),
makeAggOverloadWithReturnType(
Expand Down Expand Up @@ -627,6 +595,48 @@ func makeStdDevBuiltin() builtinDefinition {
)
}

func makeSTCollectBuiltin() builtinDefinition {
return makeBuiltin(
tree.FunctionProperties{
Class: tree.AggregateClass,
NullableArgs: true,
AvailableOnPublicSchema: true,
},
makeAggOverload(
[]*types.T{types.Geometry},
types.Geometry,
newSTCollectAgg,
infoBuilder{
info: "Collects geometries into a GeometryCollection or multi-type as appropriate.",
}.String(),
tree.VolatilityImmutable,
),
)
}

func makeSTUnionBuiltin() builtinDefinition {
return makeBuiltin(
tree.FunctionProperties{
Class: tree.AggregateClass,
NullableArgs: true,
AvailableOnPublicSchema: true,
},
makeAggOverload(
[]*types.T{types.Geometry},
types.Geometry,
func(
params []*types.T, evalCtx *tree.EvalContext, arguments tree.Datums,
) tree.AggregateFunc {
return &stUnionAgg{}
},
infoBuilder{
info: "Applies a spatial union to the geometries provided.",
}.String(),
tree.VolatilityImmutable,
),
)
}

type stMakeLineAgg struct {
flatCoords []float64
layout geom.Layout
Expand Down

0 comments on commit c9ff65a

Please sign in to comment.