PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ postgis_valid_typmod()

GSERIALIZED* postgis_valid_typmod ( GSERIALIZED gser,
int32_t  typmod 
)

Check the consistency of the metadata we want to enforce in the typmod: srid, type and dimensionality.

If things are inconsistent, shut down the query.

Definition at line 110 of file gserialized_typmod.c.

111 {
112  int32 geom_srid = gserialized_get_srid(gser);
113  int32 geom_type = gserialized_get_type(gser);
114  int32 geom_z = gserialized_has_z(gser);
115  int32 geom_m = gserialized_has_m(gser);
116  int32 typmod_srid = TYPMOD_GET_SRID(typmod);
117  int32 typmod_type = TYPMOD_GET_TYPE(typmod);
118  int32 typmod_z = TYPMOD_GET_Z(typmod);
119  int32 typmod_m = TYPMOD_GET_M(typmod);
120 
121  POSTGIS_DEBUG(2, "Entered function");
122 
123  /* No typmod (-1) => no preferences */
124  if (typmod < 0) return gser;
125 
126  POSTGIS_DEBUGF(3, "Got geom(type = %d, srid = %d, hasz = %d, hasm = %d)", geom_type, geom_srid, geom_z, geom_m);
127  POSTGIS_DEBUGF(3, "Got typmod(type = %d, srid = %d, hasz = %d, hasm = %d)", typmod_type, typmod_srid, typmod_z, typmod_m);
128 
129  /*
130  * #3031: If a user is handing us a MULTIPOINT EMPTY but trying to fit it into
131  * a POINT geometry column, there's a strong chance the reason she has
132  * a MULTIPOINT EMPTY because we gave it to her during data dump,
133  * converting the internal POINT EMPTY into a EWKB MULTIPOINT EMPTY
134  * (because EWKB doesn't have a clean way to represent POINT EMPTY).
135  * In such a case, it makes sense to turn the MULTIPOINT EMPTY back into a
136  * point EMPTY, rather than throwing an error.
137  */
138  if ( typmod_type == POINTTYPE && geom_type == MULTIPOINTTYPE &&
139  gserialized_is_empty(gser) )
140  {
141  LWPOINT *empty_point = lwpoint_construct_empty(geom_srid, geom_z, geom_m);
142  geom_type = POINTTYPE;
143  pfree(gser);
144  if ( gserialized_is_geodetic(gser) )
145  gser = geography_serialize(lwpoint_as_lwgeom(empty_point));
146  else
147  gser = geometry_serialize(lwpoint_as_lwgeom(empty_point));
148  }
149 
150  /* Typmod has a preference for SRID? Geometry SRID had better match. */
151  if ( typmod_srid > 0 && typmod_srid != geom_srid )
152  {
153  ereport(ERROR, (
154  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
155  errmsg("Geometry SRID (%d) does not match column SRID (%d)", geom_srid, typmod_srid) ));
156  }
157 
158  /* Typmod has a preference for geometry type. */
159  if ( typmod_type > 0 &&
160  /* GEOMETRYCOLLECTION column can hold any kind of collection */
161  ((typmod_type == COLLECTIONTYPE && ! (geom_type == COLLECTIONTYPE ||
162  geom_type == MULTIPOLYGONTYPE ||
163  geom_type == MULTIPOINTTYPE ||
164  geom_type == MULTILINETYPE )) ||
165  /* Other types must be strictly equal. */
166  (typmod_type != geom_type)) )
167  {
168  ereport(ERROR, (
169  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
170  errmsg("Geometry type (%s) does not match column type (%s)", lwtype_name(geom_type), lwtype_name(typmod_type)) ));
171  }
172 
173  /* Mismatched Z dimensionality. */
174  if ( typmod_z && ! geom_z )
175  {
176  ereport(ERROR, (
177  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
178  errmsg("Column has Z dimension but geometry does not" )));
179  }
180 
181  /* Mismatched Z dimensionality (other way). */
182  if ( geom_z && ! typmod_z )
183  {
184  ereport(ERROR, (
185  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
186  errmsg("Geometry has Z dimension but column does not" )));
187  }
188 
189  /* Mismatched M dimensionality. */
190  if ( typmod_m && ! geom_m )
191  {
192  ereport(ERROR, (
193  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
194  errmsg("Column has M dimension but geometry does not" )));
195  }
196 
197  /* Mismatched M dimensionality (other way). */
198  if ( geom_m && ! typmod_m )
199  {
200  ereport(ERROR, (
201  errcode(ERRCODE_INVALID_PARAMETER_VALUE),
202  errmsg("Geometry has M dimension but column does not" )));
203  }
204 
205  return gser;
206 
207 }
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: gserialized.c:126
int gserialized_is_geodetic(const GSERIALIZED *g)
Check if a GSERIALIZED is a geography.
Definition: gserialized.c:196
int gserialized_has_m(const GSERIALIZED *g)
Check if a GSERIALIZED has an M ordinate.
Definition: gserialized.c:185
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
int gserialized_has_z(const GSERIALIZED *g)
Check if a GSERIALIZED has a Z ordinate.
Definition: gserialized.c:174
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define TYPMOD_GET_SRID(typmod)
Macros for manipulating the 'typemod' int.
Definition: liblwgeom.h:206
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define TYPMOD_GET_M(typmod)
Definition: liblwgeom.h:212
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:326
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define TYPMOD_GET_TYPE(typmod)
Definition: liblwgeom.h:208
#define TYPMOD_GET_Z(typmod)
Definition: liblwgeom.h:210
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273

References COLLECTIONTYPE, geometry_serialize(), gserialized_get_srid(), gserialized_get_type(), gserialized_has_m(), gserialized_has_z(), gserialized_is_empty(), gserialized_is_geodetic(), lwpoint_as_lwgeom(), lwpoint_construct_empty(), lwtype_name(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, POINTTYPE, TYPMOD_GET_M, TYPMOD_GET_SRID, TYPMOD_GET_TYPE, and TYPMOD_GET_Z.

Referenced by geography_enforce_typmod(), geometry_enforce_typmod(), gserialized_geography_from_lwgeom(), LWGEOM_in(), and LWGEOM_recv().

Here is the call graph for this function:
Here is the caller graph for this function: