PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
windowapi.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define WINDOW_SEEK_CURRENT   0
 
#define WINDOW_SEEK_HEAD   1
 
#define WINDOW_SEEK_TAIL   2
 
#define PG_WINDOW_OBJECT()   ((WindowObject) fcinfo->context)
 
#define WindowObjectIsValid(winobj)   ((winobj) != NULL && IsA(winobj, WindowObjectData))
 

Typedefs

typedef struct WindowObjectDataWindowObject
 

Functions

void * WinGetPartitionLocalMemory (WindowObject winobj, Size sz)
 
int64 WinGetCurrentPosition (WindowObject winobj)
 
int64 WinGetPartitionRowCount (WindowObject winobj)
 
void WinSetMarkPosition (WindowObject winobj, int64 markpos)
 
bool WinRowsArePeers (WindowObject winobj, int64 pos1, int64 pos2)
 
Datum WinGetFuncArgInPartition (WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
 
Datum WinGetFuncArgInFrame (WindowObject winobj, int argno, int relpos, int seektype, bool set_mark, bool *isnull, bool *isout)
 
Datum WinGetFuncArgCurrent (WindowObject winobj, int argno, bool *isnull)
 

Macro Definition Documentation

#define WINDOW_SEEK_CURRENT   0

Definition at line 32 of file windowapi.h.

Referenced by leadlag_common(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

#define WINDOW_SEEK_HEAD   1
#define WINDOW_SEEK_TAIL   2

Definition at line 34 of file windowapi.h.

Referenced by window_last_value(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

Typedef Documentation

Definition at line 37 of file windowapi.h.

Function Documentation

int64 WinGetCurrentPosition ( WindowObject  winobj)

Definition at line 2485 of file nodeWindowAgg.c.

References Assert, WindowAggState::currentpos, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), window_cume_dist(), window_percent_rank(), window_rank(), and window_row_number().

2486 {
2487  Assert(WindowObjectIsValid(winobj));
2488  return winobj->winstate->currentpos;
2489 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:2018
Datum WinGetFuncArgCurrent ( WindowObject  winobj,
int  argno,
bool isnull 
)

Definition at line 2796 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, ExprContext::ecxt_outertuple, ExecEvalExpr, list_nth(), ScanState::ps, PlanState::ps_ExprContext, WindowAggState::ss, ScanState::ss_ScanTupleSlot, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by leadlag_common(), window_nth_value(), and window_ntile().

2797 {
2798  WindowAggState *winstate;
2799  ExprContext *econtext;
2800 
2801  Assert(WindowObjectIsValid(winobj));
2802  winstate = winobj->winstate;
2803 
2804  econtext = winstate->ss.ps.ps_ExprContext;
2805 
2806  econtext->ecxt_outertuple = winstate->ss.ss_ScanTupleSlot;
2807  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2808  econtext, isnull);
2809 }
ScanState ss
Definition: execnodes.h:2004
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1293
PlanState ps
Definition: execnodes.h:1290
#define ExecEvalExpr(expr, econtext, isNull)
Definition: executor.h:73
void * list_nth(const List *list, int n)
Definition: list.c:410
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:132
#define Assert(condition)
Definition: c.h:675
Datum WinGetFuncArgInFrame ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

Definition at line 2702 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, WindowAggState::currentpos, ExprContext::ecxt_outertuple, elog, ERROR, ExecEvalExpr, WindowAggState::frameheadpos, FRAMEOPTION_RANGE, FRAMEOPTION_START_UNBOUNDED_PRECEDING, WindowAggState::frameOptions, WindowAggState::frametailpos, list_nth(), ScanState::ps, PlanState::ps_ExprContext, row_is_in_frame(), WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, update_frameheadpos(), update_frametailpos(), window_gettupleslot(), WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, WINDOW_SEEK_TAIL, WindowObjectIsValid, WinSetMarkPosition(), and WindowObjectData::winstate.

Referenced by window_first_value(), window_last_value(), and window_nth_value().

2705 {
2706  WindowAggState *winstate;
2707  ExprContext *econtext;
2708  TupleTableSlot *slot;
2709  bool gottuple;
2710  int64 abs_pos;
2711 
2712  Assert(WindowObjectIsValid(winobj));
2713  winstate = winobj->winstate;
2714  econtext = winstate->ss.ps.ps_ExprContext;
2715  slot = winstate->temp_slot_1;
2716 
2717  switch (seektype)
2718  {
2719  case WINDOW_SEEK_CURRENT:
2720  abs_pos = winstate->currentpos + relpos;
2721  break;
2722  case WINDOW_SEEK_HEAD:
2723  update_frameheadpos(winobj, slot);
2724  abs_pos = winstate->frameheadpos + relpos;
2725  break;
2726  case WINDOW_SEEK_TAIL:
2727  update_frametailpos(winobj, slot);
2728  abs_pos = winstate->frametailpos + relpos;
2729  break;
2730  default:
2731  elog(ERROR, "unrecognized window seek type: %d", seektype);
2732  abs_pos = 0; /* keep compiler quiet */
2733  break;
2734  }
2735 
2736  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2737  if (gottuple)
2738  gottuple = row_is_in_frame(winstate, abs_pos, slot);
2739 
2740  if (!gottuple)
2741  {
2742  if (isout)
2743  *isout = true;
2744  *isnull = true;
2745  return (Datum) 0;
2746  }
2747  else
2748  {
2749  if (isout)
2750  *isout = false;
2751  if (set_mark)
2752  {
2753  int frameOptions = winstate->frameOptions;
2754  int64 mark_pos = abs_pos;
2755 
2756  /*
2757  * In RANGE mode with a moving frame head, we must not let the
2758  * mark advance past frameheadpos, since that row has to be
2759  * fetchable during future update_frameheadpos calls.
2760  *
2761  * XXX it is very ugly to pollute window functions' marks with
2762  * this consideration; it could for instance mask a logic bug that
2763  * lets a window function fetch rows before what it had claimed
2764  * was its mark. Perhaps use a separate mark for frame head
2765  * probes?
2766  */
2767  if ((frameOptions & FRAMEOPTION_RANGE) &&
2768  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2769  {
2770  update_frameheadpos(winobj, winstate->temp_slot_2);
2771  if (mark_pos > winstate->frameheadpos)
2772  mark_pos = winstate->frameheadpos;
2773  }
2774  WinSetMarkPosition(winobj, mark_pos);
2775  }
2776  econtext->ecxt_outertuple = slot;
2777  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2778  econtext, isnull);
2779  }
2780 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:2004
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:500
TupleTableSlot * temp_slot_1
Definition: execnodes.h:2055
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1290
int64 frameheadpos
Definition: execnodes.h:2019
#define ExecEvalExpr(expr, econtext, isNull)
Definition: executor.h:73
#define ERROR
Definition: elog.h:43
static bool row_is_in_frame(WindowAggState *winstate, int64 pos, TupleTableSlot *slot)
TupleTableSlot * temp_slot_2
Definition: execnodes.h:2056
void * list_nth(const List *list, int n)
Definition: list.c:410
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
uintptr_t Datum
Definition: postgres.h:372
static void update_frametailpos(WindowObject winobj, TupleTableSlot *slot)
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:132
#define WINDOW_SEEK_CURRENT
Definition: windowapi.h:32
int64 frametailpos
Definition: execnodes.h:2020
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:497
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:2018
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219
Datum WinGetFuncArgInPartition ( WindowObject  winobj,
int  argno,
int  relpos,
int  seektype,
bool  set_mark,
bool isnull,
bool isout 
)

Definition at line 2606 of file nodeWindowAgg.c.

References WindowObjectData::argstates, Assert, WindowAggState::currentpos, ExprContext::ecxt_outertuple, elog, ERROR, ExecEvalExpr, WindowAggState::frameheadpos, FRAMEOPTION_RANGE, FRAMEOPTION_START_UNBOUNDED_PRECEDING, WindowAggState::frameOptions, list_nth(), ScanState::ps, PlanState::ps_ExprContext, spool_tuples(), WindowAggState::spooled_rows, WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, update_frameheadpos(), window_gettupleslot(), WINDOW_SEEK_CURRENT, WINDOW_SEEK_HEAD, WINDOW_SEEK_TAIL, WindowObjectIsValid, WinSetMarkPosition(), and WindowObjectData::winstate.

Referenced by leadlag_common().

2609 {
2610  WindowAggState *winstate;
2611  ExprContext *econtext;
2612  TupleTableSlot *slot;
2613  bool gottuple;
2614  int64 abs_pos;
2615 
2616  Assert(WindowObjectIsValid(winobj));
2617  winstate = winobj->winstate;
2618  econtext = winstate->ss.ps.ps_ExprContext;
2619  slot = winstate->temp_slot_1;
2620 
2621  switch (seektype)
2622  {
2623  case WINDOW_SEEK_CURRENT:
2624  abs_pos = winstate->currentpos + relpos;
2625  break;
2626  case WINDOW_SEEK_HEAD:
2627  abs_pos = relpos;
2628  break;
2629  case WINDOW_SEEK_TAIL:
2630  spool_tuples(winstate, -1);
2631  abs_pos = winstate->spooled_rows - 1 + relpos;
2632  break;
2633  default:
2634  elog(ERROR, "unrecognized window seek type: %d", seektype);
2635  abs_pos = 0; /* keep compiler quiet */
2636  break;
2637  }
2638 
2639  gottuple = window_gettupleslot(winobj, abs_pos, slot);
2640 
2641  if (!gottuple)
2642  {
2643  if (isout)
2644  *isout = true;
2645  *isnull = true;
2646  return (Datum) 0;
2647  }
2648  else
2649  {
2650  if (isout)
2651  *isout = false;
2652  if (set_mark)
2653  {
2654  int frameOptions = winstate->frameOptions;
2655  int64 mark_pos = abs_pos;
2656 
2657  /*
2658  * In RANGE mode with a moving frame head, we must not let the
2659  * mark advance past frameheadpos, since that row has to be
2660  * fetchable during future update_frameheadpos calls.
2661  *
2662  * XXX it is very ugly to pollute window functions' marks with
2663  * this consideration; it could for instance mask a logic bug that
2664  * lets a window function fetch rows before what it had claimed
2665  * was its mark. Perhaps use a separate mark for frame head
2666  * probes?
2667  */
2668  if ((frameOptions & FRAMEOPTION_RANGE) &&
2669  !(frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING))
2670  {
2671  update_frameheadpos(winobj, winstate->temp_slot_2);
2672  if (mark_pos > winstate->frameheadpos)
2673  mark_pos = winstate->frameheadpos;
2674  }
2675  WinSetMarkPosition(winobj, mark_pos);
2676  }
2677  econtext->ecxt_outertuple = slot;
2678  return ExecEvalExpr((ExprState *) list_nth(winobj->argstates, argno),
2679  econtext, isnull);
2680  }
2681 }
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:2004
ExprContext * ps_ExprContext
Definition: execnodes.h:1080
#define FRAMEOPTION_START_UNBOUNDED_PRECEDING
Definition: parsenodes.h:500
TupleTableSlot * temp_slot_1
Definition: execnodes.h:2055
static void spool_tuples(WindowAggState *winstate, int64 pos)
#define WINDOW_SEEK_TAIL
Definition: windowapi.h:34
PlanState ps
Definition: execnodes.h:1290
int64 frameheadpos
Definition: execnodes.h:2019
#define ExecEvalExpr(expr, econtext, isNull)
Definition: executor.h:73
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:2056
void * list_nth(const List *list, int n)
Definition: list.c:410
static void update_frameheadpos(WindowObject winobj, TupleTableSlot *slot)
int64 spooled_rows
Definition: execnodes.h:2017
#define WINDOW_SEEK_HEAD
Definition: windowapi.h:33
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
uintptr_t Datum
Definition: postgres.h:372
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:132
#define WINDOW_SEEK_CURRENT
Definition: windowapi.h:32
#define FRAMEOPTION_RANGE
Definition: parsenodes.h:497
#define Assert(condition)
Definition: c.h:675
int64 currentpos
Definition: execnodes.h:2018
void WinSetMarkPosition(WindowObject winobj, int64 markpos)
#define elog
Definition: elog.h:219
void* WinGetPartitionLocalMemory ( WindowObject  winobj,
Size  sz 
)

Definition at line 2470 of file nodeWindowAgg.c.

References Assert, WindowObjectData::localmem, MemoryContextAllocZero(), NULL, WindowAggState::partcontext, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), window_cume_dist(), window_dense_rank(), window_ntile(), window_percent_rank(), and window_rank().

2471 {
2472  Assert(WindowObjectIsValid(winobj));
2473  if (winobj->localmem == NULL)
2474  winobj->localmem =
2476  return winobj->localmem;
2477 }
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:742
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
MemoryContext partcontext
Definition: execnodes.h:2033
int64 WinGetPartitionRowCount ( WindowObject  winobj)

Definition at line 2500 of file nodeWindowAgg.c.

References Assert, spool_tuples(), WindowAggState::spooled_rows, WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by window_cume_dist(), window_ntile(), and window_percent_rank().

2501 {
2502  Assert(WindowObjectIsValid(winobj));
2503  spool_tuples(winobj->winstate, -1);
2504  return winobj->winstate->spooled_rows;
2505 }
static void spool_tuples(WindowAggState *winstate, int64 pos)
int64 spooled_rows
Definition: execnodes.h:2017
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
bool WinRowsArePeers ( WindowObject  winobj,
int64  pos1,
int64  pos2 
)

Definition at line 2553 of file nodeWindowAgg.c.

References are_peers(), Assert, elog, ERROR, ExecClearTuple(), INT64_FORMAT, WindowAgg::ordNumCols, PlanState::plan, ScanState::ps, WindowAggState::ss, WindowAggState::temp_slot_1, WindowAggState::temp_slot_2, window_gettupleslot(), WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by rank_up(), and window_cume_dist().

2554 {
2555  WindowAggState *winstate;
2556  WindowAgg *node;
2557  TupleTableSlot *slot1;
2558  TupleTableSlot *slot2;
2559  bool res;
2560 
2561  Assert(WindowObjectIsValid(winobj));
2562  winstate = winobj->winstate;
2563  node = (WindowAgg *) winstate->ss.ps.plan;
2564 
2565  /* If no ORDER BY, all rows are peers; don't bother to fetch them */
2566  if (node->ordNumCols == 0)
2567  return true;
2568 
2569  slot1 = winstate->temp_slot_1;
2570  slot2 = winstate->temp_slot_2;
2571 
2572  if (!window_gettupleslot(winobj, pos1, slot1))
2573  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2574  pos1);
2575  if (!window_gettupleslot(winobj, pos2, slot2))
2576  elog(ERROR, "specified position is out of window: " INT64_FORMAT,
2577  pos2);
2578 
2579  res = are_peers(winstate, slot1, slot2);
2580 
2581  ExecClearTuple(slot1);
2582  ExecClearTuple(slot2);
2583 
2584  return res;
2585 }
int ordNumCols
Definition: plannodes.h:777
static bool window_gettupleslot(WindowObject winobj, int64 pos, TupleTableSlot *slot)
ScanState ss
Definition: execnodes.h:2004
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * temp_slot_1
Definition: execnodes.h:2055
PlanState ps
Definition: execnodes.h:1290
#define ERROR
Definition: elog.h:43
TupleTableSlot * temp_slot_2
Definition: execnodes.h:2056
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
Plan * plan
Definition: execnodes.h:1049
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
#define INT64_FORMAT
Definition: c.h:315
static bool are_peers(WindowAggState *winstate, TupleTableSlot *slot1, TupleTableSlot *slot2)
#define elog
Definition: elog.h:219
void WinSetMarkPosition ( WindowObject  winobj,
int64  markpos 
)

Definition at line 2518 of file nodeWindowAgg.c.

References Assert, WindowAggState::buffer, elog, ERROR, WindowObjectData::markpos, WindowObjectData::markptr, WindowObjectData::readptr, WindowObjectData::seekpos, tuplestore_select_read_pointer(), tuplestore_skiptuples(), WindowObjectIsValid, and WindowObjectData::winstate.

Referenced by eval_windowaggregates(), rank_up(), window_row_number(), WinGetFuncArgInFrame(), and WinGetFuncArgInPartition().

2519 {
2520  WindowAggState *winstate;
2521 
2522  Assert(WindowObjectIsValid(winobj));
2523  winstate = winobj->winstate;
2524 
2525  if (markpos < winobj->markpos)
2526  elog(ERROR, "cannot move WindowObject's mark position backward");
2527  tuplestore_select_read_pointer(winstate->buffer, winobj->markptr);
2528  if (markpos > winobj->markpos)
2529  {
2530  tuplestore_skiptuples(winstate->buffer,
2531  markpos - winobj->markpos,
2532  true);
2533  winobj->markpos = markpos;
2534  }
2535  tuplestore_select_read_pointer(winstate->buffer, winobj->readptr);
2536  if (markpos > winobj->seekpos)
2537  {
2538  tuplestore_skiptuples(winstate->buffer,
2539  markpos - winobj->seekpos,
2540  true);
2541  winobj->seekpos = markpos;
2542  }
2543 }
#define ERROR
Definition: elog.h:43
Tuplestorestate * buffer
Definition: execnodes.h:2015
WindowAggState * winstate
Definition: nodeWindowAgg.c:62
#define WindowObjectIsValid(winobj)
Definition: windowapi.h:41
#define Assert(condition)
Definition: c.h:675
bool tuplestore_skiptuples(Tuplestorestate *state, int64 ntuples, bool forward)
Definition: tuplestore.c:1118
void tuplestore_select_read_pointer(Tuplestorestate *state, int ptr)
Definition: tuplestore.c:470
#define elog
Definition: elog.h:219