171 Datum *result,
bool *isnull);
176 Datum *result,
bool *isnull);
270 foreach(arg, wfuncstate->
args)
286 for (i = 1; i <= numArguments; i++)
339 (
void *) winstate,
NULL);
352 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
353 errmsg(
"moving-aggregate transition function must not return null")));
432 foreach(arg, wfuncstate->
args)
448 for (i = 1; i <= numArguments; i++)
470 elog(
ERROR,
"aggregate transition value is NULL before inverse transition");
495 (
void *) winstate,
NULL);
551 Datum *result,
bool *isnull)
570 (
void *) winstate,
NULL);
576 for (i = 1; i < numFinalArgs; i++)
634 int64 aggregatedupto_nonrestarted;
699 elog(
ERROR,
"window frame head moved backward");
718 for (i = 0; i < numaggs; i++)
720 peraggstate = &winstate->
peragg[
i];
721 wfuncno = peraggstate->
wfuncno;
743 for (i = 0; i < numaggs; i++)
745 peraggstate = &winstate->
peragg[
i];
765 while (numaggs_restart < numaggs &&
774 elog(
ERROR,
"could not re-fetch previously fetched frame row");
783 for (i = 0; i < numaggs; i++)
787 peraggstate = &winstate->
peragg[
i];
791 wfuncno = peraggstate->
wfuncno;
835 if (numaggs_restart > 0)
837 for (i = 0; i < numaggs; i++)
839 peraggstate = &winstate->
peragg[
i];
843 numaggs_restart == 0 ||
848 wfuncno = peraggstate->
wfuncno;
873 if (numaggs_restart > 0 &&
905 for (i = 0; i < numaggs; i++)
907 peraggstate = &winstate->
peragg[
i];
914 wfuncno = peraggstate->
wfuncno;
929 Assert(aggregatedupto_nonrestarted <= winstate->aggregatedupto);
934 for (i = 0; i < numaggs; i++)
939 peraggstate = &winstate->
peragg[
i];
940 wfuncno = peraggstate->
wfuncno;
983 Datum *result,
bool *isnull)
1079 int readptr_flags = 0;
1101 for (i = 0; i < numfuncs; i++)
1207 for (i = 0; i < winstate->
numfuncs; i++)
1212 if (perfuncstate->
winobj)
1224 for (i = 0; i < winstate->
numaggs; i++)
1258 if (pos < winstate->currentpos)
1264 if (pos < winstate->currentpos &&
1281 if (pos < winstate->currentpos + offset)
1287 elog(
ERROR,
"window frame with value offset is not implemented");
1328 elog(
ERROR,
"window frame with value offset is not implemented");
1394 if (fhprev < winstate->frameheadpos)
1434 elog(
ERROR,
"window frame with value offset is not implemented");
1538 elog(
ERROR,
"window frame with value offset is not implemented");
1608 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1609 errmsg(
"frame starting offset must not be null")));
1621 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1622 errmsg(
"frame starting offset must not be negative")));
1634 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1635 errmsg(
"frame ending offset must not be null")));
1647 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1648 errmsg(
"frame ending offset must not be negative")));
1712 elog(
ERROR,
"unexpected end of tuplestore");
1718 for (i = 0; i < numfuncs; i++)
1804 "WindowAgg_Partition",
1817 "WindowAgg_Aggregates",
1895 winstate->
peragg = peragg;
1899 foreach(l, winstate->
funcs)
1908 elog(
ERROR,
"WindowFunc with winref %u assigned to WindowAgg with winref %u",
1912 for (
i = 0;
i <= wfuncno;
i++)
1914 if (
equal(wfunc, perfunc[
i].wfunc) &&
1926 perfuncstate = &perfunc[++wfuncno];
1929 wfuncstate->
wfuncno = wfuncno;
1940 perfuncstate->wfuncstate = wfuncstate;
1941 perfuncstate->wfunc = wfunc;
1951 &perfuncstate->resulttypeLen,
1952 &perfuncstate->resulttypeByVal);
1958 perfuncstate->plain_agg = wfunc->
winagg;
1963 perfuncstate->aggno = ++aggno;
1964 peraggstate = &winstate->
peragg[aggno];
1966 peraggstate->
wfuncno = wfuncno;
1975 perfuncstate->winobj = winobj;
1981 winstate->
numaggs = aggno + 1;
2038 for (i = 0; i < node->
numaggs; i++)
2120 foreach(lc, wfunc->
args)
2127 elog(
ERROR,
"cache lookup failed for aggregate %u",
2145 peraggstate->
transfn_oid = transfn_oid = aggform->aggmtransfn;
2146 peraggstate->
invtransfn_oid = invtransfn_oid = aggform->aggminvtransfn;
2147 peraggstate->
finalfn_oid = finalfn_oid = aggform->aggmfinalfn;
2148 finalextra = aggform->aggmfinalextra;
2149 aggtranstype = aggform->aggmtranstype;
2154 peraggstate->
transfn_oid = transfn_oid = aggform->aggtransfn;
2156 peraggstate->
finalfn_oid = finalfn_oid = aggform->aggfinalfn;
2157 finalextra = aggform->aggfinalextra;
2158 aggtranstype = aggform->aggtranstype;
2175 elog(
ERROR,
"cache lookup failed for function %u",
2285 if (numArguments < 1 ||
2288 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2289 errmsg(
"aggregate %u needs to have compatible input type and transition type",
2304 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
2305 errmsg(
"strictness of aggregate's forward and inverse transition functions must match")));
2324 "WindowAgg_AggregatePrivate",
2397 if (pos < winobj->markpos)
2398 elog(
ERROR,
"cannot fetch row before WindowObject's mark position");
2407 if (winobj->
seekpos < pos - 1)
2412 elog(
ERROR,
"unexpected end of tuplestore");
2415 else if (winobj->
seekpos > pos + 1)
2420 elog(
ERROR,
"unexpected end of tuplestore");
2423 else if (winobj->
seekpos == pos)
2443 elog(
ERROR,
"unexpected end of tuplestore");
2449 elog(
ERROR,
"unexpected end of tuplestore");
2534 if (markpos < winobj->markpos)
2535 elog(
ERROR,
"cannot move WindowObject's mark position backward");
2537 if (markpos > winobj->
markpos)
2545 if (markpos > winobj->
seekpos)
2588 res =
are_peers(winstate, slot1, slot2);
2616 int relpos,
int seektype,
bool set_mark,
2617 bool *isnull,
bool *isout)
2643 elog(
ERROR,
"unrecognized window seek type: %d", seektype);
2664 int64 mark_pos = abs_pos;
2688 econtext, isnull,
NULL);
2712 int relpos,
int seektype,
bool set_mark,
2713 bool *isnull,
bool *isout)
2740 elog(
ERROR,
"unrecognized window seek type: %d", seektype);
2763 int64 mark_pos = abs_pos;
2787 econtext, isnull,
NULL);
2817 econtext, isnull,
NULL);
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Datum WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
bool WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
MemoryContext curaggcontext
TupleTableSlot * ExecProcNode(PlanState *node)
void MemoryContextDelete(MemoryContext context)
void * WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
bool tuplestore_advance(Tuplestorestate *state, bool forward)
struct WindowStatePerAggData * WindowStatePerAgg
int64 WinGetPartitionRowCount(WindowObject winobj)
static void finalize_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate, Datum *result, bool *isnull)
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
ProjectionInfo * ps_ProjInfo
#define Anum_pg_aggregate_agginitval
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
bool equal(const void *a, const void *b)
void tuplestore_trim(Tuplestorestate *state)
void ExecEndNode(PlanState *node)
void tuplestore_set_eflags(Tuplestorestate *state, int eflags)
ExprContext * ps_ExprContext
MemoryContext ecxt_per_tuple_memory
#define FRAMEOPTION_START_VALUE
void ExecReScan(PlanState *node)
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
bool execTuplesMatch(TupleTableSlot *slot1, TupleTableSlot *slot2, int numCols, AttrNumber *matchColIdx, FmgrInfo *eqfunctions, MemoryContext evalContext)
int errcode(int sqlerrcode)
#define MemSet(start, val, len)
struct WindowStatePerFuncData WindowStatePerFuncData
TupleTableSlot * ExecProject(ProjectionInfo *projInfo, ExprDoneCond *isDone)
TupleTableSlot * ss_ScanTupleSlot
static void release_partition(WindowAggState *winstate)
struct WindowStatePerFuncData * WindowStatePerFunc
FmgrInfo * partEqfunctions
void build_aggregate_finalfn_expr(Oid *agg_input_types, int num_finalfn_inputs, Oid agg_state_type, Oid agg_result_type, Oid agg_input_collation, Oid finalfn_oid, Expr **finalfnexpr)
WindowFuncExprState * wfuncstate
bool contain_volatile_functions(Node *clause)
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
TupleTableSlot * temp_slot_1
#define OidIsValid(objectId)
WindowStatePerFunc perfunc
void ExecFreeExprContext(PlanState *planstate)
static void spool_tuples(WindowAggState *winstate, int64 pos)
#define ALLOCSET_DEFAULT_MINSIZE
#define SearchSysCache1(cacheId, key1)
void ExecAssignResultTypeFromTL(PlanState *planstate)
TupleTableSlot * first_part_slot
static bool advance_windowaggregate_base(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
struct WindowObjectData * agg_winobj
bool tuplestore_in_memory(Tuplestorestate *state)
static WindowStatePerAggData * initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc, WindowStatePerAgg peraggstate)
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
void pfree(void *pointer)
TupleTableSlot * ExecWindowAgg(WindowAggState *winstate)
#define ObjectIdGetDatum(X)
char * get_func_name(Oid funcid)
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
struct WindowObjectData WindowObjectData
void fmgr_info(Oid functionId, FmgrInfo *finfo)
static bool row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
#define EXEC_FLAG_BACKWARD
#define outerPlanState(node)
TupleTableSlot * temp_slot_2
Datum WinGetFuncArgInPartition(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
void * list_nth(const List *list, int n)
#define FunctionCallInvoke(fcinfo)
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
void aclcheck_error(AclResult aclerr, AclObjectKind objectkind, const char *objectname)
#define FRAMEOPTION_END_CURRENT_ROW
#define fmgr_info_set_expr(expr, finfo)
#define FRAMEOPTION_START_VALUE_PRECEDING
#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING
bool argnull[FUNC_MAX_ARGS]
MemoryContext CurrentMemoryContext
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
#define FRAMEOPTION_START_CURRENT_ROW
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
#define ereport(elevel, rest)
Datum datumCopy(Datum value, bool typByVal, int typLen)
#define InvokeFunctionExecuteHook(objectId)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
Datum ExecEvalExprSwitchContext(ExprState *expression, ExprContext *econtext, bool *isNull, ExprDoneCond *isDone)
int64 WinGetCurrentPosition(WindowObject winobj)
static void eval_windowaggregates(WindowAggState *winstate)
TupleDesc tts_tupleDescriptor
#define MemoryContextResetAndDeleteChildren(ctx)
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
#define TextDatumGetCString(d)
#define Anum_pg_aggregate_aggminitval
WindowAggState * winstate
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
void * palloc0(Size size)
void ReleaseSysCache(HeapTuple tuple)
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
TupleTableSlot * agg_row_slot
static void initialize_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
FormData_pg_proc * Form_pg_proc
void * MemoryContextAllocZero(MemoryContext context, Size size)
static void update_frametailpos(WindowObject winobj, TupleTableSlot *slot)
static void begin_partition(WindowAggState *winstate)
#define WindowObjectIsValid(winobj)
FmgrInfo * ordEqfunctions
TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
void ExecEndWindowAgg(WindowAggState *node)
TupleTableSlot * ecxt_outertuple
#define WINDOW_SEEK_CURRENT
#define HeapTupleIsValid(tuple)
#define FRAMEOPTION_RANGE
#define Assert(condition)
static void advance_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
FormData_pg_aggregate * Form_pg_aggregate
bool MemoryContextContains(MemoryContext context, void *pointer)
void build_aggregate_transfn_expr(Oid *agg_input_types, int agg_num_inputs, int agg_num_direct_inputs, bool agg_variadic, Oid agg_state_type, Oid agg_input_collation, Oid transfn_oid, Oid invtransfn_oid, Expr **transfnexpr, Expr **invtransfnexpr)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
Oid exprType(const Node *expr)
static int list_length(const List *l)
void tuplestore_end(Tuplestorestate *state)
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
bool tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward)
WindowAggState * ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
MemoryContext ecxt_per_query_memory
Datum WinGetFuncArgInFrame(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
#define FRAMEOPTION_END_VALUE_PRECEDING
static Datum GetAggInitVal(Datum textInitVal, Oid transtype)
#define DatumGetPointer(X)
int tuplestore_alloc_read_pointer(Tuplestorestate *state, int eflags)
static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1, TupleTableSlot *slot2)
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
int errmsg(const char *fmt,...)
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
#define ALLOCSET_DEFAULT_INITSIZE
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
struct WindowStatePerAggData WindowStatePerAggData
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
void ExecReScanWindowAgg(WindowAggState *node)
MemoryContext partcontext
#define ALLOCSET_DEFAULT_MAXSIZE
FmgrInfo * execTuplesMatchPrepare(int numCols, Oid *eqOperators)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
#define FRAMEOPTION_END_VALUE
static void eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate, Datum *result, bool *isnull)
#define ResetExprContext(econtext)
Oid resolve_aggregate_transtype(Oid aggfuncid, Oid aggtranstype, Oid *inputTypes, int numArguments)
#define ExecEvalExpr(expr, econtext, isNull, isDone)