174 Datum *result,
bool *isnull);
179 Datum *result,
bool *isnull);
278 &fcinfo->args[
i].isnull);
289 for (
i = 1;
i <= numArguments;
i++)
291 if (fcinfo->args[
i].isnull)
342 (
void *) winstate, NULL);
343 fcinfo->args[0].value = peraggstate->
transValue;
355 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
356 errmsg(
"moving-aggregate transition function must not return null")));
455 &fcinfo->args[
i].isnull);
466 for (
i = 1;
i <= numArguments;
i++)
468 if (fcinfo->args[
i].isnull)
488 elog(
ERROR,
"aggregate transition value is NULL before inverse transition");
513 (
void *) winstate, NULL);
514 fcinfo->args[0].value = peraggstate->
transValue;
585 Datum *result,
bool *isnull)
604 (
void *) winstate, NULL);
605 fcinfo->args[0].value =
613 for (
i = 1;
i < numFinalArgs;
i++)
615 fcinfo->args[
i].value = (
Datum) 0;
616 fcinfo->args[
i].isnull =
true;
620 if (fcinfo->flinfo->fn_strict && anynull)
630 winstate->curaggcontext = peraggstate->
aggcontext;
632 winstate->curaggcontext = NULL;
633 *isnull = fcinfo->isnull;
670 int64 aggregatedupto_nonrestarted;
738 elog(
ERROR,
"window frame head moved backward");
758 for (
i = 0;
i < numaggs;
i++)
760 peraggstate = &winstate->
peragg[
i];
761 wfuncno = peraggstate->
wfuncno;
784 for (
i = 0;
i < numaggs;
i++)
786 peraggstate = &winstate->
peragg[
i];
807 while (numaggs_restart < numaggs &&
816 elog(
ERROR,
"could not re-fetch previously fetched frame row");
825 for (
i = 0;
i < numaggs;
i++)
829 peraggstate = &winstate->
peragg[
i];
833 wfuncno = peraggstate->
wfuncno;
877 if (numaggs_restart > 0)
879 for (
i = 0;
i < numaggs;
i++)
881 peraggstate = &winstate->
peragg[
i];
885 numaggs_restart == 0 ||
890 wfuncno = peraggstate->
wfuncno;
915 if (numaggs_restart > 0 &&
955 for (
i = 0;
i < numaggs;
i++)
957 peraggstate = &winstate->
peragg[
i];
964 wfuncno = peraggstate->
wfuncno;
980 Assert(aggregatedupto_nonrestarted <= winstate->aggregatedupto);
985 for (
i = 0;
i < numaggs;
i++)
990 peraggstate = &winstate->
peragg[
i];
991 wfuncno = peraggstate->
wfuncno;
1034 Datum *result,
bool *isnull)
1050 (
void *) perfuncstate->
winobj, NULL);
1052 for (
int argno = 0; argno < perfuncstate->
numArguments; argno++)
1053 fcinfo->args[argno].isnull =
true;
1058 *isnull = fcinfo->isnull;
1111 int readptr_flags = 0;
1131 for (
int i = 0;
i < numfuncs;
i++)
1256 for (
int i = 0;
i < numfuncs;
i++)
1385 if (perfuncstate->
winobj)
1437 if (pos < winstate->frameheadpos)
1506 if (pos < winstate->grouptailpos)
1578 elog(
ERROR,
"unexpected end of tuplestore");
1631 int sortCol = node->ordColIdx[0];
1659 elog(
ERROR,
"unexpected end of tuplestore");
1673 if (headisnull || currisnull)
1679 if (!headisnull || currisnull)
1685 if (headisnull || !currisnull)
1735 elog(
ERROR,
"unexpected end of tuplestore");
1831 elog(
ERROR,
"unexpected end of tuplestore");
1885 int sortCol = node->ordColIdx[0];
1913 elog(
ERROR,
"unexpected end of tuplestore");
1927 if (tailisnull || currisnull)
1989 elog(
ERROR,
"unexpected end of tuplestore");
2105 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2106 errmsg(
"frame starting offset must not be null")));
2119 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2120 errmsg(
"frame starting offset must not be negative")));
2132 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2133 errmsg(
"frame ending offset must not be null")));
2146 (
errcode(ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE),
2147 errmsg(
"frame ending offset must not be negative")));
2261 elog(
ERROR,
"unexpected end of tuplestore");
2275 elog(
ERROR,
"unexpected end of tuplestore");
2285 for (
i = 0;
i < numfuncs;
i++)
2383 for (
i = 0;
i < numfuncs;
i++)
2475 "WindowAgg Partition",
2486 "WindowAgg Aggregates",
2579 node->partOperators,
2580 node->partCollations,
2589 node->ordCollations,
2607 winstate->
peragg = peragg;
2611 foreach(l, winstate->
funcs)
2620 elog(
ERROR,
"WindowFunc with winref %u assigned to WindowAgg with winref %u",
2624 for (
i = 0;
i <= wfuncno;
i++)
2626 if (
equal(wfunc, perfunc[
i].wfunc) &&
2638 perfuncstate = &perfunc[++wfuncno];
2641 wfuncstate->
wfuncno = wfuncno;
2653 perfuncstate->
wfunc = wfunc;
2665 perfuncstate->
plain_agg = wfunc->winagg;
2670 perfuncstate->
aggno = ++aggno;
2671 peraggstate = &winstate->
peragg[aggno];
2673 peraggstate->
wfuncno = wfuncno;
2682 perfuncstate->
winobj = winobj;
2693 winstate->
numaggs = aggno + 1;
2745 if (node->
buffer != NULL)
2841 foreach(lc, wfunc->
args)
2848 elog(
ERROR,
"cache lookup failed for aggregate %u",
2871 use_ma_code =
false;
2872 else if (aggform->aggmfinalmodify == AGGMODIFY_READ_ONLY &&
2873 aggform->aggfinalmodify != AGGMODIFY_READ_ONLY)
2876 use_ma_code =
false;
2878 use_ma_code =
false;
2880 use_ma_code =
false;
2885 peraggstate->
transfn_oid = transfn_oid = aggform->aggmtransfn;
2886 peraggstate->
invtransfn_oid = invtransfn_oid = aggform->aggminvtransfn;
2887 peraggstate->
finalfn_oid = finalfn_oid = aggform->aggmfinalfn;
2888 finalextra = aggform->aggmfinalextra;
2889 finalmodify = aggform->aggmfinalmodify;
2890 aggtranstype = aggform->aggmtranstype;
2891 initvalAttNo = Anum_pg_aggregate_aggminitval;
2895 peraggstate->
transfn_oid = transfn_oid = aggform->aggtransfn;
2897 peraggstate->
finalfn_oid = finalfn_oid = aggform->aggfinalfn;
2898 finalextra = aggform->aggfinalextra;
2899 finalmodify = aggform->aggfinalmodify;
2900 aggtranstype = aggform->aggtranstype;
2901 initvalAttNo = Anum_pg_aggregate_agginitval;
2917 elog(
ERROR,
"cache lookup failed for function %u",
2922 aclresult =
object_aclcheck(ProcedureRelationId, transfn_oid, aggOwner,
2931 aclresult =
object_aclcheck(ProcedureRelationId, invtransfn_oid, aggOwner,
2941 aclresult =
object_aclcheck(ProcedureRelationId, finalfn_oid, aggOwner,
2955 if (finalmodify != AGGMODIFY_READ_ONLY)
2957 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2958 errmsg(
"aggregate function %s does not support use as a window function",
3038 if (numArguments < 1 ||
3041 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
3042 errmsg(
"aggregate %u needs to have compatible input type and transition type",
3057 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
3058 errmsg(
"strictness of aggregate's forward and inverse transition functions must match")));
3077 "WindowAgg Per Aggregate",
3120 econtext->ecxt_outertuple = slot1;
3121 econtext->ecxt_innertuple = slot2;
3151 if (pos < winobj->markpos)
3152 elog(
ERROR,
"cannot fetch row before WindowObject's mark position");
3161 if (winobj->
seekpos < pos - 1)
3166 elog(
ERROR,
"unexpected end of tuplestore");
3169 else if (winobj->
seekpos > pos + 1)
3174 elog(
ERROR,
"unexpected end of tuplestore");
3177 else if (winobj->
seekpos == pos)
3201 elog(
ERROR,
"unexpected end of tuplestore");
3207 elog(
ERROR,
"unexpected end of tuplestore");
3292 if (markpos < winobj->markpos)
3293 elog(
ERROR,
"cannot move WindowObject's mark position backward");
3295 if (markpos > winobj->
markpos)
3303 if (markpos > winobj->
seekpos)
3378 int relpos,
int seektype,
bool set_mark,
3379 bool *isnull,
bool *isout)
3405 elog(
ERROR,
"unrecognized window seek type: %d", seektype);
3466 int relpos,
int seektype,
bool set_mark,
3467 bool *isnull,
bool *isout)
3483 elog(
ERROR,
"WINDOW_SEEK_CURRENT is not supported for WinGetFuncArgInFrame");
3484 abs_pos = mark_pos = 0;
3535 if (abs_pos == overlapstart)
3542 elog(
ERROR,
"unrecognized frame option state: 0x%x",
3570 if (abs_pos <= winstate->currentpos &&
3574 if (abs_pos < winstate->frameheadpos)
3580 if (abs_pos < winstate->grouptailpos &&
3589 if (abs_pos < winstate->frameheadpos)
3595 if (abs_pos < winstate->grouptailpos &&
3601 if (abs_pos == overlapend - 1)
3607 if (abs_pos < winstate->frameheadpos)
3612 elog(
ERROR,
"unrecognized frame option state: 0x%x",
3619 elog(
ERROR,
"unrecognized window seek type: %d", seektype);
3620 abs_pos = mark_pos = 0;
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
#define TextDatumGetCString(d)
#define Assert(condition)
#define MemSet(start, val, len)
#define OidIsValid(objectId)
bool contain_subplans(Node *clause)
bool contain_volatile_functions(Node *clause)
Datum datumCopy(Datum value, bool typByVal, int typLen)
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
void ExecReScan(PlanState *node)
ExprState * ExecInitQual(List *qual, PlanState *parent)
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
ExprState * execTuplesMatchPrepare(TupleDesc desc, int numCols, const AttrNumber *keyColIdx, const Oid *eqOperators, const Oid *collations, PlanState *parent)
void ExecEndNode(PlanState *node)
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
const TupleTableSlotOps TTSOpsVirtual
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsMinimalTuple
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
void ExecAssignExprContext(EState *estate, PlanState *planstate)
void ExecAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc)
struct WindowStatePerAggData * WindowStatePerAgg
#define InstrCountFiltered1(node, delta)
#define outerPlanState(node)
@ WINDOWAGG_PASSTHROUGH_STRICT
struct WindowStatePerFuncData * WindowStatePerFunc
#define EXEC_FLAG_BACKWARD
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
#define ResetExprContext(econtext)
static bool ExecQual(ExprState *state, ExprContext *econtext)
static bool ExecQualAndReset(ExprState *state, ExprContext *econtext)
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
static TupleTableSlot * ExecProcNode(PlanState *node)
ExpandedObjectHeader * DatumGetEOHP(Datum d)
void DeleteExpandedObject(Datum d)
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
#define DatumIsReadWriteExpandedObject(d, isnull, typlen)
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Datum OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
Datum FunctionCall5Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5)
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
#define InitFunctionCallInfoData(Fcinfo, Flinfo, Nargs, Collation, Context, Resultinfo)
#define LOCAL_FCINFO(name, nargs)
#define FunctionCallInvoke(fcinfo)
#define fmgr_info_set_expr(expr, finfo)
#define HeapTupleIsValid(tuple)
if(TABLE==NULL||TABLE_index==NULL)
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
char * get_func_name(Oid funcid)
void MemoryContextReset(MemoryContext context)
void pfree(void *pointer)
void * palloc0(Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
MemoryContext CurrentMemoryContext
MemoryContext MemoryContextGetParent(MemoryContext context)
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define CHECK_FOR_INTERRUPTS()
Oid exprType(const Node *expr)
Datum WinGetFuncArgInPartition(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
static void begin_partition(WindowAggState *winstate)
struct WindowObjectData WindowObjectData
static void update_grouptailpos(WindowAggState *winstate)
Datum WinGetFuncArgInFrame(WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
WindowAggState * ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
struct WindowStatePerAggData WindowStatePerAggData
static Datum GetAggInitVal(Datum textInitVal, Oid transtype)
static void spool_tuples(WindowAggState *winstate, int64 pos)
static void advance_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
static int row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
void ExecEndWindowAgg(WindowAggState *node)
static void eval_windowfunction(WindowAggState *winstate, WindowStatePerFunc perfuncstate, Datum *result, bool *isnull)
struct WindowStatePerFuncData WindowStatePerFuncData
static WindowStatePerAggData * initialize_peragg(WindowAggState *winstate, WindowFunc *wfunc, WindowStatePerAgg peraggstate)
static void finalize_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate, Datum *result, bool *isnull)
static bool advance_windowaggregate_base(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
static pg_noinline void prepare_tuplestore(WindowAggState *winstate)
void ExecReScanWindowAgg(WindowAggState *node)
int64 WinGetCurrentPosition(WindowObject winobj)
bool WinRowsArePeers(WindowObject winobj, int64 pos1, int64 pos2)
static pg_noinline void calculate_frame_offsets(PlanState *pstate)
static TupleTableSlot * ExecWindowAgg(PlanState *pstate)
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
static void eval_windowaggregates(WindowAggState *winstate)
static void release_partition(WindowAggState *winstate)
void * WinGetPartitionLocalMemory(WindowObject winobj, Size sz)
static void update_frametailpos(WindowAggState *winstate)
static void update_frameheadpos(WindowAggState *winstate)
static void initialize_windowaggregate(WindowAggState *winstate, WindowStatePerFunc perfuncstate, WindowStatePerAgg peraggstate)
static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1, TupleTableSlot *slot2)
Datum WinGetFuncArgCurrent(WindowObject winobj, int argno, bool *isnull)
int64 WinGetPartitionRowCount(WindowObject winobj)
#define castNode(_type_, nodeptr)
#define InvokeFunctionExecuteHook(objectId)
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)
Oid resolve_aggregate_transtype(Oid aggfuncid, Oid aggtranstype, Oid *inputTypes, int numArguments)
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)
bool IsBinaryCoercible(Oid srctype, Oid targettype)
#define FRAMEOPTION_END_CURRENT_ROW
#define FRAMEOPTION_END_OFFSET
#define FRAMEOPTION_EXCLUDE_CURRENT_ROW
#define FRAMEOPTION_END_OFFSET_PRECEDING
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
#define FRAMEOPTION_START_CURRENT_ROW
#define FRAMEOPTION_START_OFFSET
#define FRAMEOPTION_EXCLUDE_TIES
#define FRAMEOPTION_RANGE
#define FRAMEOPTION_EXCLUDE_GROUP
#define FRAMEOPTION_GROUPS
#define FRAMEOPTION_END_UNBOUNDED_FOLLOWING
#define FRAMEOPTION_START_OFFSET_PRECEDING
#define FRAMEOPTION_EXCLUSION
FormData_pg_aggregate * Form_pg_aggregate
static int list_length(const List *l)
static void * list_nth(const List *list, int n)
FormData_pg_proc * Form_pg_proc
static bool DatumGetBool(Datum X)
static int64 DatumGetInt64(Datum X)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
MemoryContextSwitchTo(old_ctx)
char * format_procedure(Oid procedure_oid)
MemoryContext ecxt_per_tuple_memory
TupleTableSlot * ecxt_innertuple
TupleTableSlot * ecxt_scantuple
MemoryContext ecxt_per_query_memory
TupleTableSlot * ecxt_outertuple
const TupleTableSlotOps * outerops
ExprContext * ps_ExprContext
ProjectionInfo * ps_ProjInfo
ExecProcNodeMtd ExecProcNode
TupleTableSlot * ss_ScanTupleSlot
TupleDesc tts_tupleDescriptor
MemoryContext partcontext
TupleTableSlot * framehead_slot
FmgrInfo startInRangeFunc
TupleTableSlot * frametail_slot
ExprState * ordEqfunction
TupleTableSlot * temp_slot_2
TupleTableSlot * agg_row_slot
struct WindowObjectData * agg_winobj
WindowStatePerFunc perfunc
MemoryContext curaggcontext
ExprState * partEqfunction
TupleTableSlot * first_part_slot
TupleTableSlot * temp_slot_1
WindowAggState * winstate
WindowFuncExprState * wfuncstate
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
void tuplestore_clear(Tuplestorestate *state)
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
int tuplestore_alloc_read_pointer(Tuplestorestate *state, int eflags)
void tuplestore_trim(Tuplestorestate *state)
bool tuplestore_advance(Tuplestorestate *state, bool forward)
bool tuplestore_in_memory(Tuplestorestate *state)
void tuplestore_end(Tuplestorestate *state)
void tuplestore_set_eflags(Tuplestorestate *state, int eflags)
bool tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward)
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
static TupleTableSlot * ExecCopySlot(TupleTableSlot *dstslot, TupleTableSlot *srcslot)
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
#define WindowObjectIsValid(winobj)
#define WINDOW_SEEK_CURRENT