| 359 | |
| 360 | PG_FUNCTION_INFO_V1(svec_unnest); |
| 361 | Datum |
| 362 | svec_unnest(PG_FUNCTION_ARGS) |
| 363 | { |
| 364 | FuncCallContext *funcctx; |
| 365 | svec_unnest_fctx *fctx; |
| 366 | MemoryContext oldcontext; |
| 367 | float8 result; |
| 368 | SvecType *svec; |
| 369 | int run_length=0; |
| 370 | |
| 371 | /* stuff done only on the first call of the function */ |
| 372 | if (SRF_IS_FIRSTCALL()) |
| 373 | { |
| 374 | /* create a function context for cross-call persistence */ |
| 375 | funcctx = SRF_FIRSTCALL_INIT(); |
| 376 | |
| 377 | /* |
| 378 | * switch to memory context appropriate for multiple function calls |
| 379 | */ |
| 380 | oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
| 381 | |
| 382 | /* allocate memory for user context */ |
| 383 | fctx = (svec_unnest_fctx *) |
| 384 | palloc(sizeof(svec_unnest_fctx)); |
| 385 | |
| 386 | svec = PG_GETARG_SVECTYPE_P(0); |
| 387 | fctx->sdata = sdata_from_svec(svec); |
| 388 | |
| 389 | /* set initial index into the sparse vector argument */ |
| 390 | fctx->dimension = svec->dimension; |
| 391 | fctx->absolute_value_position = 0; |
| 392 | fctx->unique_value_position = 0; |
| 393 | fctx->index_position = fctx->sdata->index->data; |
| 394 | fctx->run_position = 1; |
| 395 | |
| 396 | funcctx->user_fctx = fctx; |
| 397 | |
| 398 | MemoryContextSwitchTo(oldcontext); |
| 399 | } |
| 400 | |
| 401 | /* stuff done on every call of the function */ |
| 402 | funcctx = SRF_PERCALL_SETUP(); |
| 403 | |
| 404 | fctx = funcctx->user_fctx; |
| 405 | |
| 406 | run_length = compword_to_int8(fctx->index_position); |
| 407 | |
| 408 | if (fctx->dimension > fctx->absolute_value_position) |
| 409 | { |
| 410 | result = ((float8 *)(fctx->sdata->vals->data)) |
| 411 | [fctx->unique_value_position]; |
| 412 | |
| 413 | fctx->absolute_value_position++; |
| 414 | fctx->run_position++; |
| 415 | if (fctx->run_position > run_length) |
| 416 | { |
| 417 | fctx->run_position = 1; |
| 418 | fctx->unique_value_position++; |
nothing calls this directly
no test coverage detected