You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "zhangfengcdt (via GitHub)" <gi...@apache.org> on 2026/10/16 12:32:43 UTC
[I] ST_Union_Agg / ST_Intersection_Agg return order-dependent wrong results: non-polygonal input is dropped, and the intersection restarts after becoming empty [sedona-db]
zhangfengcdt opened a new issue, #1459:
URL: https://github.com/apache/sedona-db/issues/1459
`ST_Union_Agg` and `ST_Intersection_Agg` (the `sedona-geo` accumulators in `rust/sedona-geo/src/st_union_agg.rs` and `st_intersection_agg.rs`) can return a wrong result without raising an error. The result also depends on the order rows reach the accumulator, so the same query can give different answers across partitions or runs.
This extends #1093, which covers the all-points case of `ST_Union_Agg` returning `NULL`. Both cases below have the same root cause.
## ST_Union_Agg: non-polygonal input is dropped or rejected depending on order
`init_union` only keeps a Polygon or MultiPolygon. Any other first geometry leaves the state as `None`, so it is silently dropped and the next polygon starts a fresh union. Once the state holds a polygon, a non-polygonal geometry raises an error instead.
```sql
SELECT ST_AsText(ST_Union_Agg(g)) FROM (VALUES
(ST_GeomFromText('POINT (5 5)')),
(ST_GeomFromText('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))'))
) AS t(g);
-- actual: MULTIPOLYGON(((0 0,1 0,1 1,0 1,0 0))) (the point is silently dropped)
-- expected: GEOMETRYCOLLECTION (POINT (5 5), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)))
```
Here is the same input in the other order:
```sql
SELECT ST_AsText(ST_Union_Agg(g)) FROM (VALUES
(ST_GeomFromText('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))')),
(ST_GeomFromText('POINT (5 5)'))
) AS t(g);
-- actual: error "Unsupported geometry type for union operation"
```
A `LINESTRING` behaves the same way. It is dropped if it comes first and raises the error if it comes after a polygon.
## ST_Intersection_Agg: the intersection restarts after it becomes empty
`None` stands both for "no input yet" and for "the intersection is empty". When the running intersection becomes empty (`!intersects`), the state is reset to `None`, so the next geometry starts the intersection over again.
```sql
SELECT ST_AsText(ST_Intersection_Agg(g)) FROM (VALUES
(ST_GeomFromText('POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))')),
(ST_GeomFromText('POLYGON ((2 2, 3 2, 3 3, 2 3, 2 2))')),
(ST_GeomFromText('POLYGON ((10 10, 11 10, 11 11, 10 11, 10 10))'))
) AS t(g);
-- actual: MULTIPOLYGON(((10 10,11 10,11 11,10 11,10 10)))
-- expected: an empty result (the inputs are pairwise disjoint)
```
With only the first two polygons, the result is `NULL`, which matches the existing unit test. The problem is the third input. Over N disjoint inputs, the answer is whatever comes after the last disjoint pair. A non-polygonal first geometry is also dropped here, the same way as in `init_union`.
## Proposal
- Move both aggregates to a GEOS-based implementation that handles every geometry type (`unary_union` / overlay), as suggested in #1093.
- Track "has seen input" separately from the current geometry, so an empty intersection stays empty. A dropped first geometry also becomes impossible.
- Until then, raise an error for any non-polygonal input instead of dropping it, so callers never get a silently wrong result. A test that feeds mixed geometry types in both orders would cover this.
## Environment
Reproduced with `sedonadb` 0.5.0 from PyPI (Python 3.14, macOS arm64). The accumulator code is unchanged on `main`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ST_Union_Agg / ST_Intersection_Agg return order-dependent wrong results: non-polygonal input is dropped, and the intersection restarts after becoming empty [sedona-db]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on issue #1459:
URL: https://github.com/apache/sedona-db/issues/1459#issuecomment-6229347119
> I'd like to work on this, unless one of you is already on it.
I'm not working on it and I haven't seen a PR otherwise...give it a go! One function at a time, if you don't mind (perhaps Feng will want to work on the other one).
> The plan would be GEOS-based st_union_agg and st_intersection_agg accumulators in c/sedona-geos, following st_polygonize_agg: the union over all inputs, and a running intersection that tracks "has seen input" separately so an empty result stays empty.
I think this makes sense. You can test whether it is faster to "buffer" input features and compute a unary union periodically on the collection, or whether it is faster to perform the union on each new geometry.
> Should the sedona-geo versions be removed
I think you can remove the sedona-geo versions because they are so limited in the features they provide
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ST_Union_Agg / ST_Intersection_Agg return order-dependent wrong results: non-polygonal input is dropped, and the intersection restarts after becoming empty [sedona-db]
Posted by "CaptainAni187 (via GitHub)" <gi...@apache.org>.
CaptainAni187 commented on issue #1459:
URL: https://github.com/apache/sedona-db/issues/1459#issuecomment-6225555464
@zhangfengcdt @paleolimbot I'd like to work on this, unless one of you is already on it. The plan would be GEOS-based `st_union_agg` and `st_intersection_agg` accumulators in `c/sedona-geos`, following `st_polygonize_agg`: the union over all inputs, and a running intersection that tracks "has seen input" separately so an empty result stays empty. Tests would cover mixed geometry types in both orders and the three disjoint polygons.
One question first: `sedona-geo`'s aggregate UDFs are registered after GEOS, so its kernels would still win. Should the `sedona-geo` versions be removed, or kept for builds without the `geos` feature and changed to raise an error on non-polygonal input?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] ST_Union_Agg / ST_Intersection_Agg return order-dependent wrong results: non-polygonal input is dropped, and the intersection restarts after becoming empty [sedona-db]
Posted by "CaptainAni187 (via GitHub)" <gi...@apache.org>.
CaptainAni187 commented on issue #1459:
URL: https://github.com/apache/sedona-db/issues/1459#issuecomment-6230446266
thanks! I'll take ST_Union_Agg and leave ST_Intersection_Agg for Feng then.... and remove the sedona-geo union one in the same PR. I'll try the buffered unary union vs union-per-geometry thing too and see which one's faster.
doing a small scalar function first like you suggested in the discussion, so this might be a few days.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org