| 553 | } |
| 554 | |
| 555 | Datum |
| 556 | array_agg_combine(PG_FUNCTION_ARGS) |
| 557 | { |
| 558 | ArrayBuildState *state1; |
| 559 | ArrayBuildState *state2; |
| 560 | MemoryContext agg_context; |
| 561 | MemoryContext old_context; |
| 562 | int i; |
| 563 | |
| 564 | if (!AggCheckCallContext(fcinfo, &agg_context)) |
| 565 | elog(ERROR, "aggregate function called in non-aggregate context"); |
| 566 | |
| 567 | state1 = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0); |
| 568 | state2 = PG_ARGISNULL(1) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(1); |
| 569 | |
| 570 | if (state2 == NULL) |
| 571 | { |
| 572 | /* |
| 573 | * NULL state2 is easy, just return state1, which we know is already |
| 574 | * in the agg_context |
| 575 | */ |
| 576 | if (state1 == NULL) |
| 577 | PG_RETURN_NULL(); |
| 578 | PG_RETURN_POINTER(state1); |
| 579 | } |
| 580 | |
| 581 | if (state1 == NULL) |
| 582 | { |
| 583 | /* We must copy state2's data into the agg_context */ |
| 584 | state1 = initArrayResultWithSize(state2->element_type, agg_context, |
| 585 | false, state2->alen); |
| 586 | |
| 587 | old_context = MemoryContextSwitchTo(agg_context); |
| 588 | |
| 589 | for (i = 0; i < state2->nelems; i++) |
| 590 | { |
| 591 | if (!state2->dnulls[i]) |
| 592 | state1->dvalues[i] = datumCopy(state2->dvalues[i], |
| 593 | state1->typbyval, |
| 594 | state1->typlen); |
| 595 | else |
| 596 | state1->dvalues[i] = (Datum) 0; |
| 597 | } |
| 598 | |
| 599 | MemoryContextSwitchTo(old_context); |
| 600 | |
| 601 | memcpy(state1->dnulls, state2->dnulls, sizeof(bool) * state2->nelems); |
| 602 | |
| 603 | state1->nelems = state2->nelems; |
| 604 | |
| 605 | PG_RETURN_POINTER(state1); |
| 606 | } |
| 607 | else if (state2->nelems > 0) |
| 608 | { |
| 609 | /* We only need to combine the two states if state2 has any elements */ |
| 610 | int reqsize = state1->nelems + state2->nelems; |
| 611 | MemoryContext oldContext = MemoryContextSwitchTo(state1->mcontext); |
| 612 |
nothing calls this directly
no test coverage detected