File: pointcloud.sql.in

package info (click to toggle)
pgpointcloud 1.2.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 4,840 kB
  • sloc: sql: 41,137; ansic: 10,943; xml: 935; cpp: 282; perl: 248; makefile: 185; sh: 72
file content (534 lines) | stat: -rw-r--r-- 18,125 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
#include "sqldefines.h"

-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pointcloud" to load this file. \quit


-------------------------------------------------------------------
--  METADATA and SCHEMA
-------------------------------------------------------------------

-- Confirm the XML representation of a schema has everything we need
CREATE OR REPLACE FUNCTION PC_SchemaIsValid(schemaxml text)
	RETURNS boolean AS 'MODULE_PATHNAME','pcschema_is_valid'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Metadata table describing contents of pcpoints
CREATE TABLE IF NOT EXISTS pointcloud_formats (
	pcid INTEGER PRIMARY KEY
		-- PCID == 0 is unknown
		-- PCID > 2^16 is reserved to leave space in typmod
		CHECK (pcid > 0 AND pcid < 65536),
	srid INTEGER, -- REFERENCES spatial_ref_sys(srid)
	schema TEXT
		CHECK ( PC_SchemaIsValid(schema) )
);

-- Register pointcloud_formats table so the contents are included in pg_dump output
SELECT pg_catalog.pg_extension_config_dump('pointcloud_formats', '');

CREATE OR REPLACE FUNCTION PC_SchemaGetNDims(pcid integer)
	RETURNS integer
	AS 'MODULE_PATHNAME','pcschema_get_ndims'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Read typmod number from string
CREATE OR REPLACE FUNCTION pc_typmod_in(cstring[])
	RETURNS integer AS 'MODULE_PATHNAME','pc_typmod_in'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Write typmod number to string
CREATE OR REPLACE FUNCTION pc_typmod_out(typmod integer)
	RETURNS cstring AS 'MODULE_PATHNAME','pc_typmod_out'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Read pcid from typmod number
CREATE OR REPLACE FUNCTION pc_typmod_pcid(typmod integer)
	RETURNS int4 AS 'MODULE_PATHNAME','pc_typmod_pcid'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Return the script version number
CREATE OR REPLACE FUNCTION pc_script_version()
	RETURNS text AS $$ SELECT '#POINTCLOUD_VERSION#'::text $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

-- Return the library version number
CREATE OR REPLACE FUNCTION pc_lib_version()
	RETURNS text AS 'MODULE_PATHNAME', 'pc_version'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Return the pgsql version number
CREATE OR REPLACE FUNCTION pc_pgsql_version()
	RETURNS text AS 'MODULE_PATHNAME', 'pc_pgsql_version'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Return the libxml2 version number
CREATE OR REPLACE FUNCTION pc_libxml2_version()
	RETURNS text AS 'MODULE_PATHNAME', 'pc_libxml2_version'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Return whether lazperf is enabled
CREATE OR REPLACE FUNCTION pc_lazperf_enabled()
	RETURNS boolean AS 'MODULE_PATHNAME', 'pc_lazperf_enabled'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Return the extension version number with the commit hash trimmed, and check sanity
CREATE OR REPLACE FUNCTION _pc_version_no_commit(libver text, scrver text)
	RETURNS text AS
$$
DECLARE
	libver_no_commit TEXT;
BEGIN
	libver_no_commit := split_part(libver, ' ', 1);
	IF scrver != libver_no_commit THEN
		RAISE WARNING 'Library (%) and script (%) version mismatch',
			libver_no_commit, scrver;
	END IF;
	RETURN libver_no_commit;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE STRICT;

-- Return the extension version number, and check sanity
CREATE OR REPLACE FUNCTION pc_version()
	RETURNS text AS
$$
DECLARE
	libver TEXT;
	scrver TEXT;
	version TEXT;
BEGIN
	libver := pc_lib_version();
	scrver := pc_script_version();
	version := _pc_version_no_commit(libver, scrver);
	RETURN version;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE STRICT;

-- Return the full version string
CREATE OR REPLACE FUNCTION pc_full_version()
	RETURNS text AS
$$
DECLARE
	pcver TEXT;
	pclibver TEXT;
	pcscrver TEXT;
	pgsqlver TEXT;
	libxml2ver TEXT;
	lazperfenabled BOOLEAN;
BEGIN
	pclibver := pc_lib_version();
	pcscrver := pc_script_version();
	pcver := _pc_version_no_commit(pclibver, pcscrver);
	pgsqlver := pc_pgsql_version();
	libxml2ver := pc_libxml2_version();
	lazperfenabled := pc_lazperf_enabled();
	RETURN 'POINTCLOUD="' || pclibver || '" PGSQL="' || pgsqlver || '" LIBXML2="' || libxml2ver ||
		' LAZPERF enabled=' || lazperfenabled;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE STRICT;

-- Upgrade pointcloud extension to latest (or specified) version.
-- Takes care of in-development upgrades
CREATE OR REPLACE FUNCTION pc_upgrade(to_version text DEFAULT NULL)
	RETURNS text AS
$$
DECLARE
	ver RECORD;
	target_version text;
BEGIN
	SELECT default_version, installed_version
	FROM pg_catalog.pg_available_extensions
	WHERE name = 'pointcloud'
	INTO ver;

	IF to_version IS NULL THEN
		target_version := ver.default_version;
	ELSE
		target_version := to_version;
	END IF;

	IF target_version != ver.installed_version THEN
		EXECUTE 'ALTER EXTENSION pointcloud UPDATE TO ' ||
			quote_literal(target_version);
	ELSE
		-- Use the "next" trick
		IF target_version LIKE '%next' THEN
			target_version := replace(target_version, 'next', '');
			EXECUTE 'ALTER EXTENSION pointcloud UPDATE TO ' ||
				quote_literal(target_version);
		ELSE
			-- Double upgrade, to get back to "next"-free
			EXECUTE 'ALTER EXTENSION pointcloud UPDATE TO ' ||
				quote_literal(target_version || 'next');
			EXECUTE 'ALTER EXTENSION pointcloud UPDATE TO ' ||
				quote_literal(target_version);
		END IF;
	END IF;

	RETURN pc_version();
END;
$$ LANGUAGE 'plpgsql' VOLATILE;

-------------------------------------------------------------------
--  PCPOINT
-------------------------------------------------------------------

CREATE OR REPLACE FUNCTION pcpoint_in(cstring)
	RETURNS pcpoint AS 'MODULE_PATHNAME', 'pcpoint_in'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION pcpoint_out(pcpoint)
	RETURNS cstring AS 'MODULE_PATHNAME', 'pcpoint_out'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE TYPE pcpoint (
	internallength = variable,
	input = pcpoint_in,
	output = pcpoint_out,
	-- send = geometry_send,
	-- receive = geometry_recv,
	typmod_in = pc_typmod_in,
	typmod_out = pc_typmod_out,
	-- delimiter = ':',
	-- alignment = double,
	-- analyze = geometry_analyze,
	storage = external -- do not try to compress it please
);

CREATE OR REPLACE FUNCTION PC_Get(pt pcpoint, dimname text)
	RETURNS numeric AS 'MODULE_PATHNAME', 'pcpoint_get_value'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_Get(pt pcpoint)
	RETURNS float8[] AS 'MODULE_PATHNAME', 'pcpoint_get_values'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_MakePoint(pcid integer, vals float8[])
	RETURNS pcpoint AS 'MODULE_PATHNAME', 'pcpoint_from_double_array'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_AsText(p pcpoint)
	RETURNS text AS 'MODULE_PATHNAME', 'pcpoint_as_text'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_AsBinary(p pcpoint)
	RETURNS bytea AS 'MODULE_PATHNAME', 'pcpoint_as_bytea'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-------------------------------------------------------------------
--  PCPATCH
-------------------------------------------------------------------


CREATE OR REPLACE FUNCTION PC_MakePatch(pcid integer, vals float8[])
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_from_float_array'
	LANGUAGE 'c' IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION pcpatch_in(cstring)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_in'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION pcpatch_out(pcpatch)
	RETURNS cstring AS 'MODULE_PATHNAME', 'pcpatch_out'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE TYPE pcpatch (
	internallength = variable,
	input = pcpatch_in,
	output = pcpatch_out,
	-- send = geometry_send,
	-- receive = geometry_recv,
	typmod_in = pc_typmod_in,
	typmod_out = pc_typmod_out,
	-- delimiter = ':',
	-- alignment = double,
	-- analyze = geometry_analyze,
	storage = external
);

CREATE OR REPLACE FUNCTION PC_AsText(p pcpatch)
	RETURNS text AS 'MODULE_PATHNAME', 'pcpatch_as_text'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_EnvelopeAsBinary(p pcpatch)
	RETURNS bytea AS 'MODULE_PATHNAME', 'pcpatch_envelope_as_bytea'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- PC_Envelope is an alias to PC_EnvelopeAsBinary, it is kept for
-- backward-compatibility
CREATE OR REPLACE FUNCTION PC_Envelope(p pcpatch)
	RETURNS bytea AS 'MODULE_PATHNAME', 'pcpatch_envelope_as_bytea'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Uncompress(p pcpatch)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_uncompress'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_Compress(p pcpatch, comp text default '', config text default '')
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_compress'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_NumPoints(p pcpatch)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpatch_numpoints'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_PCId(p pcpatch)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpatch_pcid'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_Summary(p pcpatch)
	RETURNS text AS 'MODULE_PATHNAME', 'pcpatch_summary'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Compression(p pcpatch)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpatch_compression'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Intersects(p1 pcpatch, p2 pcpatch)
	RETURNS boolean AS 'MODULE_PATHNAME', 'pcpatch_intersects'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_MemSize(p pcpatch)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpatch_size'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_MemSize(p pcpoint)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpoint_size'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_PCId(p pcpoint)
	RETURNS int4 AS 'MODULE_PATHNAME', 'pcpoint_pcid'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
-- @param statno 0:min 1:max 2:avg
CREATE OR REPLACE FUNCTION _PC_PatchStat(p pcpatch, statno int)
	RETURNS pcpoint AS 'MODULE_PATHNAME', 'pcpatch_get_stat'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_PatchMin(p pcpatch)
	RETURNS pcpoint AS $$ SELECT _PC_PatchStat(p, 0) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_PatchMax(p pcpatch)
	RETURNS pcpoint AS $$ SELECT _PC_PatchStat(p, 1) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
CREATE OR REPLACE FUNCTION PC_PatchAvg(p pcpatch)
	RETURNS pcpoint AS $$ SELECT _PC_PatchStat(p, 2) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

-- Availability: 1.1.0
-- @param statno 0:min 1:max 2:avg
CREATE OR REPLACE FUNCTION _PC_PatchStat(p pcpatch, statno int, attr text)
	RETURNS numeric AS 'MODULE_PATHNAME', 'pcpatch_get_stat'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_PatchMin(p pcpatch, attr text)
	RETURNS numeric AS $$ SELECT _PC_PatchStat(p, 0, attr) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_PatchMax(p pcpatch, attr text)
	RETURNS numeric AS $$ SELECT _PC_PatchStat(p, 1, attr) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_PatchAvg(p pcpatch, attr text)
	RETURNS numeric AS $$ SELECT _PC_PatchStat(p, 2, attr) $$
	LANGUAGE 'sql' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_FilterLessThan(p pcpatch, attr text, v1 float8 default 0.0, v2 float8 default 0.0, mode int4 default 0)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_filter'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_FilterGreaterThan(p pcpatch, attr text, v1 float8 default 0.0, v2 float8 default 0.0, mode int4 default 1)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_filter'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_FilterEquals(p pcpatch, attr text, v1 float8 default 0.0, v2 float8 default 0.0, mode int4 default 2)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_filter'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_FilterBetween(p pcpatch, attr text, v1 float8 default 0.0, v2 float8 default 0.0, mode int4 default 3)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_filter'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_PointN(p pcpatch, n int4)
	RETURNS pcpoint AS 'MODULE_PATHNAME', 'pcpatch_pointn'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Sort(p pcpatch, attr text[])
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_sort'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_IsSorted(p pcpatch, attr text[], strict boolean default false)
	RETURNS boolean AS 'MODULE_PATHNAME', 'pcpatch_is_sorted'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Range(p pcpatch, first int4, count int4)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_range'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_BoundingDiagonalAsBinary(p pcpatch)
	RETURNS bytea AS 'MODULE_PATHNAME', 'pcpatch_bounding_diagonal_as_bytea'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_SetPCId(p pcpatch, pcid int4, def float8 default 0.0)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_setpcid'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION PC_Transform(p pcpatch, pcid int4, def float8 default 0.0)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_transform'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

-------------------------------------------------------------------
--  POINTCLOUD_COLUMNS
-------------------------------------------------------------------

-- Last Changed: 1.0.1
CREATE OR REPLACE VIEW pointcloud_columns AS
	SELECT
		n.nspname AS schema,
		c.relname AS table,
		a.attname AS column,
		pc_typmod_pcid(a.atttypmod) AS pcid,
		p.srid AS srid,
		t.typname AS type
	FROM
		pg_class c,
		pg_type t,
		pg_namespace n,
		pg_attribute a
	LEFT OUTER JOIN pointcloud_formats p
		ON ( pc_typmod_pcid(a.atttypmod) = p.pcid )
	WHERE t.typname IN ('pcpatch','pcpoint')
	AND a.attisdropped = false
	AND a.atttypid = t.oid
	AND a.attrelid = c.oid
	AND c.relnamespace = n.oid
	AND NOT pg_is_other_temp_schema(c.relnamespace)
	AND has_table_privilege( c.oid, 'SELECT'::text );


-- Special cast for enforcing typmod restrictions
CREATE OR REPLACE FUNCTION pcpatch(p pcpatch, typmod integer, explicit boolean)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_enforce_typmod'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE CAST (pcpatch AS pcpatch) WITH FUNCTION pcpatch(pcpatch, integer, boolean) AS IMPLICIT;

CREATE OR REPLACE FUNCTION pcpoint(p pcpoint, typmod integer, explicit boolean)
	RETURNS pcpoint AS 'MODULE_PATHNAME', 'pcpoint_enforce_typmod'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE CAST (pcpoint AS pcpoint) WITH FUNCTION pcpoint(pcpoint, integer, boolean) AS IMPLICIT;

-------------------------------------------------------------------
--  AGGREGATE GENERIC SUPPORT
-------------------------------------------------------------------

CREATE OR REPLACE FUNCTION pointcloud_abs_in(cstring)
	RETURNS pointcloud_abs AS 'MODULE_PATHNAME'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION pointcloud_abs_out(pointcloud_abs)
	RETURNS cstring AS 'MODULE_PATHNAME'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE TYPE pointcloud_abs (
	internallength = 8,
	input = pointcloud_abs_in,
	output = pointcloud_abs_out,
	alignment = double
);

-------------------------------------------------------------------
--  AGGREGATE PCPOINT
-------------------------------------------------------------------

CREATE OR REPLACE FUNCTION PC_Patch(pcpoint[])
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_from_pcpoint_array'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;

CREATE OR REPLACE FUNCTION pcpoint_agg_transfn (pointcloud_abs, pcpoint)
	RETURNS pointcloud_abs AS 'MODULE_PATHNAME', 'pointcloud_agg_transfn'
	LANGUAGE 'c' _PARALLEL;

CREATE OR REPLACE FUNCTION pcpoint_agg_final_array (pointcloud_abs)
	RETURNS pcpoint[] AS 'MODULE_PATHNAME', 'pcpoint_agg_final_array'
	LANGUAGE 'c' _PARALLEL;

CREATE OR REPLACE FUNCTION pcpoint_agg_final_pcpatch (pointcloud_abs)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpoint_agg_final_pcpatch'
	LANGUAGE 'c' _PARALLEL;

CREATE AGGREGATE PC_Patch(pcpoint) (
	SFUNC = pcpoint_agg_transfn,
	STYPE = pointcloud_abs,
#if PGSQL_VERSION >= 96
	PARALLEL = safe,
#endif
	FINALFUNC = pcpoint_agg_final_pcpatch
);

CREATE AGGREGATE PC_Point_Agg(pcpoint) (
	SFUNC = pcpoint_agg_transfn,
	STYPE = pointcloud_abs,
#if PGSQL_VERSION >= 96
	PARALLEL = safe,
#endif
	FINALFUNC = pcpoint_agg_final_array
);

-------------------------------------------------------------------
--  AGGREGATE / EXPLODE PCPATCH
-------------------------------------------------------------------

CREATE OR REPLACE FUNCTION pcpatch_agg_final_array (pointcloud_abs)
	RETURNS pcpatch[] AS 'MODULE_PATHNAME', 'pcpatch_agg_final_array'
	LANGUAGE 'c' _PARALLEL;

CREATE OR REPLACE FUNCTION pcpatch_agg_final_pcpatch (pointcloud_abs)
	RETURNS pcpatch AS 'MODULE_PATHNAME', 'pcpatch_agg_final_pcpatch'
	LANGUAGE 'c' _PARALLEL;

CREATE OR REPLACE FUNCTION pcpatch_agg_transfn (pointcloud_abs, pcpatch)
	RETURNS pointcloud_abs AS 'MODULE_PATHNAME', 'pointcloud_agg_transfn'
	LANGUAGE 'c' _PARALLEL;

CREATE AGGREGATE PC_Patch_Agg(pcpatch) (
	SFUNC = pcpatch_agg_transfn,
	STYPE = pointcloud_abs,
#if PGSQL_VERSION >= 96
	PARALLEL = safe,
#endif
	FINALFUNC = pcpatch_agg_final_array
);

CREATE AGGREGATE PC_Union(pcpatch) (
	SFUNC = pcpatch_agg_transfn,
	STYPE = pointcloud_abs,
#if PGSQL_VERSION >= 96
	PARALLEL = safe,
#endif
	FINALFUNC = pcpatch_agg_final_pcpatch
);

CREATE OR REPLACE FUNCTION PC_Explode(p pcpatch)
	RETURNS setof pcpoint AS 'MODULE_PATHNAME', 'pcpatch_unnest'
	LANGUAGE 'c' IMMUTABLE STRICT _PARALLEL;


-------------------------------------------------------------------
--  SQL Utility Functions
-------------------------------------------------------------------