1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-14 18:42:34 +03:00

Support arrays as input to array_agg() and ARRAY(SELECT ...).

These cases formerly failed with errors about "could not find array type
for data type".  Now they yield arrays of the same element type and one
higher dimension.

The implementation involves creating functions with API similar to the
existing accumArrayResult() family.  I (tgl) also extended the base family
by adding an initArrayResult() function, which allows callers to avoid
special-casing the zero-inputs case if they just want an empty array as
result.  (Not all do, so the previous calling convention remains valid.)
This allowed simplifying some existing code in xml.c and plperl.c.

Ali Akbar, reviewed by Pavel Stehule, significantly modified by me
This commit is contained in:
Tom Lane
2014-11-25 12:21:22 -05:00
parent 25976710df
commit bac27394a1
18 changed files with 797 additions and 115 deletions

View File

@ -231,7 +231,7 @@ ExecScanSubPlan(SubPlanState *node,
bool found = false; /* TRUE if got at least one subplan tuple */
ListCell *pvar;
ListCell *l;
ArrayBuildState *astate = NULL;
ArrayBuildStateAny *astate = NULL;
/*
* MULTIEXPR subplans, when "executed", just return NULL; but first we
@ -259,6 +259,11 @@ ExecScanSubPlan(SubPlanState *node,
return (Datum) 0;
}
/* Initialize ArrayBuildStateAny in caller's context, if needed */
if (subLinkType == ARRAY_SUBLINK)
astate = initArrayResultAny(subplan->firstColType,
CurrentMemoryContext);
/*
* We are probably in a short-lived expression-evaluation context. Switch
* to the per-query context for manipulating the child plan's chgParam,
@ -366,8 +371,8 @@ ExecScanSubPlan(SubPlanState *node,
/* stash away current value */
Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
dvalue = slot_getattr(slot, 1, &disnull);
astate = accumArrayResult(astate, dvalue, disnull,
subplan->firstColType, oldcontext);
astate = accumArrayResultAny(astate, dvalue, disnull,
subplan->firstColType, oldcontext);
/* keep scanning subplan to collect all values */
continue;
}
@ -437,10 +442,7 @@ ExecScanSubPlan(SubPlanState *node,
if (subLinkType == ARRAY_SUBLINK)
{
/* We return the result in the caller's context */
if (astate != NULL)
result = makeArrayResult(astate, oldcontext);
else
result = PointerGetDatum(construct_empty_array(subplan->firstColType));
result = makeArrayResultAny(astate, oldcontext, true);
}
else if (!found)
{
@ -951,7 +953,7 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
ListCell *pvar;
ListCell *l;
bool found = false;
ArrayBuildState *astate = NULL;
ArrayBuildStateAny *astate = NULL;
if (subLinkType == ANY_SUBLINK ||
subLinkType == ALL_SUBLINK)
@ -959,6 +961,11 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
if (subLinkType == CTE_SUBLINK)
elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
/* Initialize ArrayBuildStateAny in caller's context, if needed */
if (subLinkType == ARRAY_SUBLINK)
astate = initArrayResultAny(subplan->firstColType,
CurrentMemoryContext);
/*
* Must switch to per-query memory context.
*/
@ -1018,8 +1025,8 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
/* stash away current value */
Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
dvalue = slot_getattr(slot, 1, &disnull);
astate = accumArrayResult(astate, dvalue, disnull,
subplan->firstColType, oldcontext);
astate = accumArrayResultAny(astate, dvalue, disnull,
subplan->firstColType, oldcontext);
/* keep scanning subplan to collect all values */
continue;
}
@ -1072,14 +1079,9 @@ ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
*/
if (node->curArray != PointerGetDatum(NULL))
pfree(DatumGetPointer(node->curArray));
if (astate != NULL)
node->curArray = makeArrayResult(astate,
econtext->ecxt_per_query_memory);
else
{
MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
node->curArray = PointerGetDatum(construct_empty_array(subplan->firstColType));
}
node->curArray = makeArrayResultAny(astate,
econtext->ecxt_per_query_memory,
true);
prm->execPlan = NULL;
prm->value = node->curArray;
prm->isnull = false;