PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ pgis_accum_finalfn() [1/2]

Datum pgis_accum_finalfn ( CollectionBuildState state,
MemoryContext  mctx,
__attribute__((__unused__)) FunctionCallInfo  fcinfo 
)

The final function reads the List of LWGEOM* from the aggregate memory context and constructs an Array using construct_md_array()

Definition at line 147 of file lwgeom_accum.c.

148 {
149  ListCell *l;
150  size_t nelems = 0;
151  Datum *elems;
152  bool *nulls;
153  int16 elmlen;
154  bool elmbyval;
155  char elmalign;
156  size_t i = 0;
157  ArrayType *arr;
158  int dims[1];
159  int lbs[1] = {1};
160 
161  /* cannot be called directly because of internal-type argument */
162  Assert(fcinfo->context &&
163  (IsA(fcinfo->context, AggState) ||
164  IsA(fcinfo->context, WindowAggState))
165  );
166 
167  /* Retrieve geometry type metadata */
168  get_typlenbyvalalign(state->geomOid, &elmlen, &elmbyval, &elmalign);
169  nelems = list_length(state->geoms);
170 
171  /* Build up an array, because that's what we pass to all the */
172  /* specific final functions */
173  elems = palloc(nelems * sizeof(Datum));
174  nulls = palloc(nelems * sizeof(bool));
175 
176  foreach (l, state->geoms)
177  {
178  LWGEOM *geom = (LWGEOM*)(lfirst(l));
179  Datum elem = (Datum)0;
180  bool isNull = true;
181  if (geom)
182  {
183  GSERIALIZED *gser = geometry_serialize(geom);
184  elem = PointerGetDatum(gser);
185  isNull = false;
186  }
187  elems[i] = elem;
188  nulls[i] = isNull;
189  i++;
190 
191  if (i >= nelems)
192  break;
193  }
194 
195  /* Turn element array into PgSQL array */
196  dims[0] = nelems;
197  arr = construct_md_array(elems, nulls, 1, dims, lbs, state->geomOid,
198  elmlen, elmbyval, elmalign);
199 
200  return PointerGetDatum(arr);
201 }

References CollectionBuildState::geomOid, and CollectionBuildState::geoms.