The final function reads the List of LWGEOM* from the aggregate memory context and constructs an Array using construct_md_array()
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
162 Assert(fcinfo->context &&
163 (IsA(fcinfo->context, AggState) ||
164 IsA(fcinfo->context, WindowAggState))
165 );
166
167
168 get_typlenbyvalalign(state->
geomOid, &elmlen, &elmbyval, &elmalign);
169 nelems = list_length(state->
geoms);
170
171
172
173 elems = palloc(nelems * sizeof(Datum));
174 nulls = palloc(nelems * sizeof(bool));
175
176 foreach (l, state->
geoms)
177 {
179 Datum elem = (Datum)0;
180 bool isNull = true;
181 if (geom)
182 {
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
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}