-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventwindow.c
109 lines (87 loc) · 2.87 KB
/
eventwindow.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "postgres.h"
#include "fmgr.h"
#include "windowapi.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/syscache.h"
#include "nodes/execnodes.h"
#include "parser/parse_type.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(conditional_true_event);
extern Datum conditional_true_event(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(conditional_change_event);
extern Datum conditional_change_event(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(exponential_moving_average);
extern Datum exponential_moving_average(PG_FUNCTION_ARGS);
typedef struct cte_context {
int rank; // count of events for CTE / changes for CCE
} cte_context;
typedef struct ema_context {
float avg;
} ema_context;
// conditional_true_event (CTE)
// Rank increments on any row where predicate is true.
Datum
conditional_true_event(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
cte_context *context;
bool isnull;
bool up;
up = DatumGetBool(WinGetFuncArgCurrent(winobj, 0, &isnull));
context = (cte_context *)
WinGetPartitionLocalMemory(winobj, sizeof(cte_context));
if (isnull)
PG_RETURN_INT64(context->rank);
else if (up)
context->rank++;
PG_RETURN_INT64(context->rank);
}
// utility routine for conditional_change_event,
// loosely modeled after the one in stock postgresql's windowfuncs.c
static bool
rank_up(FunctionCallInfo fcinfo, WindowObject winobj)
{
bool up = false; // should rank increase?
int64 curpos = WinGetCurrentPosition(winobj);
Datum curval, lastval;
bool isnullc, isnullp, isoutp;
Oid typOid;
Type argtype;
bool byVal;
int typLen;
typOid = get_fn_expr_argtype(fcinfo->flinfo, 0);
argtype = typeidType(typOid); // unfortunately I think we have to do this pg_type cache lookup on every call
byVal = typeByVal(argtype);
typLen = typeLen(argtype);
ReleaseSysCache(argtype);
curval = WinGetFuncArgCurrent(winobj, 0, &isnullc);
lastval = WinGetFuncArgInPartition(winobj, 0, -1, WINDOW_SEEK_CURRENT, false, &isnullp, &isoutp);
if(isoutp) // we're on the first row of the partition, CCE of which which should be 0
up = false;
else if (isnullc)
up = false;
// do current and prior tuples differ in the value of the key expression?
else if (!datumIsEqual(curval, lastval, byVal, typLen))
up = true;
// We can advance the mark, but only *after* acccess to prior row
WinSetMarkPosition(winobj, curpos);
return up;
}
// conditional_change_event (CCE)
// Rank increments on any row where key column value differs from that in the prior row.
// Can be emulated with CTE, but providing this function is both more run-time efficient
// and makes queries cleaner.
Datum
conditional_change_event(PG_FUNCTION_ARGS)
{
WindowObject winobj = PG_WINDOW_OBJECT();
cte_context *context;
bool up;
up = rank_up(fcinfo, winobj);
context = (cte_context *)
WinGetPartitionLocalMemory(winobj, sizeof(cte_context));
if (up)
context->rank++;
PG_RETURN_INT64(context->rank);
}