File: extra_window_functions.c

package info (click to toggle)
extra-window-functions 1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 476 kB
  • sloc: ansic: 281; sql: 205; makefile: 12; sh: 1
file content (367 lines) | stat: -rw-r--r-- 7,388 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "postgres.h"
#include "fmgr.h"

#include "windowapi.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(window_lag_ignore_nulls);
PG_FUNCTION_INFO_V1(window_lag_ignore_nulls_with_offset);
PG_FUNCTION_INFO_V1(window_lag_ignore_nulls_with_offset_with_default);

PG_FUNCTION_INFO_V1(window_lead_ignore_nulls);
PG_FUNCTION_INFO_V1(window_lead_ignore_nulls_with_offset);
PG_FUNCTION_INFO_V1(window_lead_ignore_nulls_with_offset_with_default);

PG_FUNCTION_INFO_V1(window_first_value_ignore_nulls);
PG_FUNCTION_INFO_V1(window_first_value_ignore_nulls_with_default);

PG_FUNCTION_INFO_V1(window_last_value_ignore_nulls);
PG_FUNCTION_INFO_V1(window_last_value_ignore_nulls_with_default);

PG_FUNCTION_INFO_V1(window_nth_value_from_last);
PG_FUNCTION_INFO_V1(window_nth_value_from_last_ignore_nulls);
PG_FUNCTION_INFO_V1(window_nth_value_from_last_ignore_nulls_with_default);
PG_FUNCTION_INFO_V1(window_nth_value_from_last_with_default);
PG_FUNCTION_INFO_V1(window_nth_value_ignore_nulls);
PG_FUNCTION_INFO_V1(window_nth_value_ignore_nulls_with_default);
PG_FUNCTION_INFO_V1(window_nth_value_with_default);

PG_FUNCTION_INFO_V1(window_flip_flop_1);
PG_FUNCTION_INFO_V1(window_flip_flop_2);

typedef struct flip_flop_context
{
	bool	flip_flop;
} flip_flop_context;

/* LEAD / LAG COMMON */

static Datum
lead_lag_common(
		FunctionCallInfo fcinfo,
		bool forward, bool with_offset, bool with_default)
{
	WindowObject winobj = PG_WINDOW_OBJECT();
	int32		offset;
	/*bool		const_offset;*/
	Datum		result;
	bool		isnull;
	bool		isout;
	int			step = forward ? 1 : -1;
	int			runner = step;

	if (with_offset)
	{
		offset = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull));
		if (isnull)
			PG_RETURN_NULL();
	}
	else
		offset = 1;

	if (!forward)
		offset = -offset;

	if (offset == 0)
		runner = 0;

	for (;;)
	{
		result = WinGetFuncArgInPartition(winobj, 0,
										  runner,
										  WINDOW_SEEK_CURRENT,
										  false,
										  &isnull, &isout);

		if (isout)
			break;

		if (isnull)
			offset += step;

		if (runner == offset)
			break;

		runner += step;
	}

	if (isout)
	{
		/*
		 * target row is out of the partition; supply default value if
		 * provided.  otherwise it'll stay NULL
		 */
		if (with_default)
			result = WinGetFuncArgCurrent(winobj, 2, &isnull);
	}

	if (isnull)
		PG_RETURN_NULL();

	PG_RETURN_DATUM(result);
}

Datum
window_lag_ignore_nulls(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, false, false, false);
}

Datum
window_lag_ignore_nulls_with_offset(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, false, true, false);
}

Datum
window_lag_ignore_nulls_with_offset_with_default(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, false, true, true);
}

Datum
window_lead_ignore_nulls(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, true, false, false);
}

Datum
window_lead_ignore_nulls_with_offset(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, true, true, false);
}

Datum
window_lead_ignore_nulls_with_offset_with_default(PG_FUNCTION_ARGS)
{
	return lead_lag_common(fcinfo, true, true, true);
}

/* FIRST_VALUE / LAST_VALUE COMMON */

static Datum
first_last_value_common(
		FunctionCallInfo fcinfo,
		bool from_last, bool with_default)
{
	WindowObject winobj = PG_WINDOW_OBJECT();
	Datum		result;
	bool		isnull;
	bool		isout;
	int			runner = 0;
	int			step = from_last ? -1 : 1;

	for (;;)
	{
		result = WinGetFuncArgInFrame(
					winobj, 0, runner,
					from_last ? WINDOW_SEEK_TAIL : WINDOW_SEEK_HEAD,
					false, &isnull, &isout);

		if (isout || !isnull)
			break;

		runner += step;
	}

	if (isout && with_default)
		result = WinGetFuncArgCurrent(winobj, 1, &isnull);

	if (isnull)
		PG_RETURN_NULL();

	PG_RETURN_DATUM(result);
}

/* FIRST_VALUE */

Datum
window_first_value_ignore_nulls(PG_FUNCTION_ARGS)
{
	return first_last_value_common(fcinfo, false, false);
}

Datum
window_first_value_ignore_nulls_with_default(PG_FUNCTION_ARGS)
{
	return first_last_value_common(fcinfo, false, true);
}

/* LAST_VALUE */

Datum
window_last_value_ignore_nulls(PG_FUNCTION_ARGS)
{
	return first_last_value_common(fcinfo, true, false);
}

Datum
window_last_value_ignore_nulls_with_default(PG_FUNCTION_ARGS)
{
	return first_last_value_common(fcinfo, true, true);
}

/* NTH_VALUE COMMON */

static Datum
nth_value_common(
		FunctionCallInfo fcinfo, const char *fname,
		bool from_last, bool ignore_nulls, bool with_default)
{
	WindowObject winobj = PG_WINDOW_OBJECT();
	Datum		result;
	bool		isnull;
	bool		isout;
	int32		nth;
	int			seektype;

	nth = DatumGetInt32(WinGetFuncArgCurrent(winobj, 1, &isnull)) - 1;
	if (isnull)
		PG_RETURN_NULL();

	if (nth < 0)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_ARGUMENT_FOR_NTH_VALUE),
				 errmsg("argument of %s must be greater than zero", fname)));

	if (from_last)
	{
		seektype = WINDOW_SEEK_TAIL;
		nth = -nth;
	}
	else
		seektype = WINDOW_SEEK_HEAD;

	if (ignore_nulls)
	{
		int		runner = 0;
		int		step = from_last ? -1 : 1;

		for (;;)
		{
			result = WinGetFuncArgInFrame(winobj, 0,
										  runner, seektype, false,
										  &isnull, &isout);

			if (isout)
				break;

			if (isnull)
				nth += step;

			if (runner == nth)
				break;

			runner += step;
		}
	}
	else
	{
		bool	const_offset = get_fn_expr_arg_stable(fcinfo->flinfo, 1);
		result = WinGetFuncArgInFrame(winobj, 0,
									  nth, seektype, const_offset,
									  &isnull, &isout);
	}

	if (isout && with_default)
		result = WinGetFuncArgCurrent(winobj, 2, &isnull);

	if (isnull)
		PG_RETURN_NULL();

	PG_RETURN_DATUM(result);
}

/* NTH_VALUE */

Datum
window_nth_value_from_last(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_from_last", true, false, false);
}

Datum
window_nth_value_from_last_ignore_nulls(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_from_last_ignore_nulls", true, true, false);
}

Datum
window_nth_value_from_last_ignore_nulls_with_default(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_from_last_ignore_nulls", true, true, true);
}

Datum
window_nth_value_from_last_with_default(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_from_last", true, false, true);
}

Datum
window_nth_value_ignore_nulls(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_ignore_nulls", false, true, false);
}

Datum
window_nth_value_ignore_nulls_with_default(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value_ignore_nulls", false, true, true);
}

Datum
window_nth_value_with_default(PG_FUNCTION_ARGS)
{
	return nth_value_common(fcinfo, "nth_value", false, false, true);
}

/* FLIP_FLOP */

static Datum
flip_flop(FunctionCallInfo fcinfo, int flip_argno, int flop_argno)
{
	WindowObject		winobj = PG_WINDOW_OBJECT();
	flip_flop_context  *context;
	bool				isnull;

	context = (flip_flop_context *)
		WinGetPartitionLocalMemory(winobj, sizeof(flip_flop_context));

	if (!context->flip_flop)
	{
		bool	flip;

		flip = WinGetFuncArgCurrent(winobj, flip_argno, &isnull);
		if (!isnull && flip)
		{
			context->flip_flop = true;
			PG_RETURN_BOOL(true);
		}

		PG_RETURN_BOOL(false);
	}
	else
	{
		bool	flop;

		flop = WinGetFuncArgCurrent(winobj, flop_argno, &isnull);
		if (!isnull && flop)
			context->flip_flop = false;

		PG_RETURN_BOOL(true);
	}
}

Datum
window_flip_flop_1(PG_FUNCTION_ARGS)
{
	return flip_flop(fcinfo, 0, 0);
}

Datum
window_flip_flop_2(PG_FUNCTION_ARGS)
{
	return flip_flop(fcinfo, 0, 1);
}