Opened 12 years ago
Closed 12 years ago
#2242 closed enhancement (fixed)
ST_Union in aggregate form treats NULL as EMPTY
Reported by: | gabrimonfa | Owned by: | robe |
---|---|---|---|
Priority: | medium | Milestone: | PostGIS 2.0.4 |
Component: | documentation | Version: | 2.0.x |
Keywords: | Cc: |
Description ¶
ST_Union of two geometries, one of which is null, returns null, using the same semantic for null used in SQL.
However, the aggregate form of ST_Union returns the other geometry, treating null geometry as an EMPTY geometry.
\pset null '<<NULL>>' SELECT ST_AsText(ST_Union(NULL::geometry,'POLYGON((0 0,0 1,1 1,1 0,0 0))'::geometry)); st_astext ----------- <<NULL>> (1 riga) SELECT ST_AsText(ST_Union(g)) FROM (SELECT NULL::geometry as g UNION SELECT 'POLYGON((0 0,0 1,1 1,1 0,0 0))'::geometry as g) foo; st_astext -------------------------------- POLYGON((0 0,0 1,1 1,1 0,0 0))
Expected result was that it returns null, also because EMPTY geometries do exists. See http://trac.osgeo.org/postgis/wiki/DevWikiEmptyGeometry
If this behavior is intended, we should probably state it clearly in the documentation
Change History (5)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Component: | postgis → documentation |
---|---|
Milestone: | → PostGIS 2.0.4 |
Owner: | changed from | to
The general rule is that NULLs are simply ignored in aggregates (so its not empty), its as if the record is just not there. So PostGIS works as expected.
ST_Union(geom1,geom2) IS NOT an aggregate and as such it doesn't throw away anything. In fact it doesn't even go into the function because its marked as a STRICT (meaning any NULL inputs result in NULL out).
I'll just flag this as a documentation issue to be clarified, but IT IS by design.
comment:3 by , 12 years ago
Ok, thank you all for the clarification. This behaviour is correct as per SQL standard and the same in almost all implementation.
Since NULL handling maybe a source of confusion at first glance, clarifying it in the documentation would be perfect.
comment:5 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Type: | defect → enhancement |
done for 2.0 at r11618.
Aggregates generally ignore nulls where they wouldn't really be meaningful, or keep as a null if that makes sense. I would be surprised if ST_Union returned null if one of the inputs was null.
select string_agg(i,', ') from (select generate_series(1,3)::text as i union all select null union all select generate_series(1,3)::text as i) as i;
(1 row)
I think treating null as empty is the right thing to do here.