PostGIS  3.3.9dev-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 146 of file lwgeom_accum.c.

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

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