174{
175 WindowObject winobj = PG_WINDOW_OBJECT();
177 int64 curpos, rowcount;
178
179 rowcount = WinGetPartitionRowCount(winobj);
181 WinGetPartitionLocalMemory(winobj,
183
185 {
186 int i, k, N;
187 bool isnull, isout;
190
191
192 k = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull));
193 if (isnull || k <= 0)
194 {
197 PG_RETURN_NULL();
198 }
199
200
201 N = (int) WinGetPartitionRowCount(winobj);
202 if (N <= 0)
203 {
206 PG_RETURN_NULL();
207 }
208
209
210 if (N<k)
211 {
212 lwpgerror("K (%d) must be smaller than the number of rows in the group (%d)", k, N);
213 }
214
215
216 geoms = palloc(
sizeof(
LWGEOM*) * N);
217 for (i = 0; i < N; i++)
218 {
220 Datum arg = WinGetFuncArgInPartition(winobj, 0, i,
221 WINDOW_SEEK_HEAD, false, &isnull, &isout);
222
223
224 if (isnull)
225 {
226 geoms[i] = NULL;
227 continue;
228 }
229
232 }
233
234
236
237
238 for (i = 0; i < N; i++)
239 if (geoms[i])
241
242 pfree(geoms);
243
245 {
248 PG_RETURN_NULL();
249 }
250
251
252 memcpy(context->
result,
r,
sizeof(
int) * N);
255 }
256
258 PG_RETURN_NULL();
259
260 curpos = WinGetCurrentPosition(winobj);
261 PG_RETURN_INT32(context->
result[curpos]);
262}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
int * lwgeom_cluster_2d_kmeans(const LWGEOM **geoms, uint32_t ngeoms, uint32_t k)
Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster ...