-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb2fce.sql
1191 lines (1042 loc) · 36 KB
/
db2fce.sql
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* contrib/db2fce--@[email protected] */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION db2fce" to load this file. \quit
-- SYSIBM.SYSDUMMY1 emulation
CREATE SCHEMA sysibm;
GRANT USAGE ON SCHEMA sysibm TO PUBLIC;
CREATE VIEW sysibm.sysdummy1 AS SELECT 'X'::char(1) AS ibmreqd;
REVOKE ALL ON sysibm.sysdummy1 FROM PUBLIC;
GRANT SELECT, REFERENCES ON sysibm.sysdummy1 TO PUBLIC;
-- DB2 schema
CREATE SCHEMA db2;
GRANT USAGE ON SCHEMA db2 TO PUBLIC;
SET search_path TO db2;
-- MICROSECOND()/SECOND()/MINUTE()/HOUR() functions
CREATE FUNCTION db2.microsecond(value timestamp)
RETURNS integer
AS $$ SELECT date_part('microsecond', $1)::integer - 1000000 * floor(date_part('second', $1))::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.microsecond(timestamp) IS 'returns microsecond part of specified timestamp';
CREATE FUNCTION db2.second(value timestamp)
RETURNS integer
AS $$ SELECT floor(date_part('second', $1))::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.second(timestamp) IS 'returns second part of specified timestamp';
CREATE FUNCTION db2.minute(value timestamp)
RETURNS integer
AS $$ SELECT date_part('minute', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.minute(timestamp) IS 'returns minute part of specified timestamp';
CREATE FUNCTION db2.hour(value timestamp)
RETURNS integer
AS $$ SELECT date_part('hour', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.hour(timestamp) IS 'returns hour part of specified timestamp';
-- DAY()/MONTH()/YEAR() functions
CREATE FUNCTION db2.day(value date)
RETURNS integer
AS $$ SELECT date_part('day', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.day(date) IS 'returns day part of specified date';
CREATE FUNCTION db2.day(value timestamptz)
RETURNS integer
AS $$ SELECT date_part('day', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.day(timestamptz) IS 'returns day part of specified date';
CREATE FUNCTION db2.day(value timestamp)
RETURNS integer
AS $$ SELECT date_part('day', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.day(timestamp) IS 'returns day part of specified date';
CREATE FUNCTION db2.month(value date)
RETURNS integer
AS $$ SELECT date_part('month', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.month(date) IS 'returns month part of specified date';
CREATE FUNCTION db2.month(value timestamptz)
RETURNS integer
AS $$ SELECT date_part('month', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.month(timestamptz) IS 'returns month part of specified date';
CREATE FUNCTION db2.month(value timestamp)
RETURNS integer
AS $$ SELECT date_part('month', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.month(timestamp) IS 'returns month part of specified date';
CREATE FUNCTION db2.year(value date)
RETURNS integer
AS $$ SELECT date_part('year', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.year(date) IS 'returns year part of specified date';
CREATE FUNCTION db2.year(value timestamptz)
RETURNS integer
AS $$ SELECT date_part('year', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.year(timestamptz) IS 'returns year part of specified date';
CREATE FUNCTION db2.year(value timestamp)
RETURNS integer
AS $$ SELECT date_part('year', $1)::integer; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.year(timestamp) IS 'returns year part of specified date';
-- DAYS() function
CREATE FUNCTION db2.days(value text)
RETURNS integer
AS $$ SELECT to_date($1, 'YYYY-MM-DD') - to_date('0001-01-01', 'YYYY-MM-DD') + 1; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.days(text) IS 'returns days since 0000-01-01';
CREATE FUNCTION db2.days(value date)
RETURNS integer
AS $$ SELECT to_date($1::text, 'YYYY-MM-DD') - to_date('0001-01-01', 'YYYY-MM-DD') + 1; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.days(date) IS 'returns days since 0000-01-01';
CREATE FUNCTION db2.days(value timestamptz)
RETURNS integer
AS $$ SELECT to_date($1::text, 'YYYY-MM-DD') - to_date('0001-01-01', 'YYYY-MM-DD') + 1; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.days(timestamptz) IS 'returns days since 0000-01-01';
CREATE FUNCTION db2.days(value timestamp)
RETURNS integer
AS $$ SELECT to_date($1::text, 'YYYY-MM-DD') - to_date('0001-01-01', 'YYYY-MM-DD') + 1; $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.days(timestamp) IS 'returns days since 0000-01-01';
-- MONTHS_BETWEEN() function
CREATE FUNCTION db2.months_between(date, date)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1) - date_part('year', $2)) * 12 +
date_part('month', $1) - date_part('month', $2)
) + (date_part('day', $1) - date_part('day', $2)) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(date, date) IS 'number of months';
-- TODO: MONTHS_BETWEEN('2008-03-31', '2008-02-29') should return 1.00 because
-- both dates are last-of-month, see also
-- http://www-01.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/com.ibm.db2z11.doc.sqlref/src/tpc/db2z_bif_monthsbetween.dita
CREATE FUNCTION db2.months_between(timestamp, timestamp)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1) - date_part('year', $2)) * 12 +
date_part('month', $1) - date_part('month', $2)
) + (((extract(day from $1) * 24 * 60 * 60 +
extract(hour from $1) * 60 * 60 +
extract(minute from $1) * 60 +
extract(second from $1)) -
(extract(day from $2) * 24 * 60 * 60 +
extract(hour from $2) * 60 * 60 +
extract(minute from $2) * 60 +
extract(second from $2))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(timestamp, timestamp) IS 'number of months';
CREATE FUNCTION db2.months_between(date, timestamp)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1::timestamp) - date_part('year', $2)) * 12 +
date_part('month', $1::timestamp) - date_part('month', $2)
) + (((extract(day from $1::timestamp) * 24 * 60 * 60 +
extract(hour from $1::timestamp) * 60 * 60 +
extract(minute from $1::timestamp) * 60 +
extract(second from $1::timestamp)) -
(extract(day from $2) * 24 * 60 * 60 +
extract(hour from $2) * 60 * 60 +
extract(minute from $2) * 60 +
extract(second from $2))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(date, timestamp) IS 'number of months';
CREATE FUNCTION db2.months_between(timestamp, date)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1) - date_part('year', $2::timestamp)) * 12 +
date_part('month', $1) - date_part('month', $2::timestamp)
) + (((extract(day from $1) * 24 * 60 * 60 +
extract(hour from $1) * 60 * 60 +
extract(minute from $1) * 60 +
extract(second from $1)) -
(extract(day from $2::timestamp) * 24 * 60 * 60 +
extract(hour from $2::timestamp) * 60 * 60 +
extract(minute from $2::timestamp) * 60 +
extract(second from $2::timestamp))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(timestamp, date) IS 'number of months';
CREATE FUNCTION db2.months_between(timestamptz, timestamptz)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1::timestamp) - date_part('year', $2::timestamp)) * 12 +
date_part('month', $1::timestamp) - date_part('month', $2::timestamp)
) + (((extract(day from $1::timestamp) * 24 * 60 * 60 +
extract(hour from $1::timestamp) * 60 * 60 +
extract(minute from $1::timestamp) * 60 +
extract(second from $1::timestamp)) -
(extract(day from $2::timestamp) * 24 * 60 * 60 +
extract(hour from $2::timestamp) * 60 * 60 +
extract(minute from $2::timestamp) * 60 +
extract(second from $2::timestamp))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(timestamptz, timestamptz) IS 'number of months';
CREATE FUNCTION db2.months_between(timestamptz, date)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1::timestamp) - date_part('year', $2::timestamp)) * 12 +
date_part('month', $1::timestamp) - date_part('month', $2::timestamp)
) + (((extract(day from $1::timestamp) * 24 * 60 * 60 +
extract(hour from $1::timestamp) * 60 * 60 +
extract(minute from $1::timestamp) * 60 +
extract(second from $1::timestamp)) -
(extract(day from $2::timestamp) * 24 * 60 * 60 +
extract(hour from $2::timestamp) * 60 * 60 +
extract(minute from $2::timestamp) * 60 +
extract(second from $2::timestamp))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(timestamptz, date) IS 'number of months';
CREATE FUNCTION db2.months_between(date, timestamptz)
RETURNS numeric
AS $$
SELECT (((date_part('year', $1::timestamp) - date_part('year', $2::timestamp)) * 12 +
date_part('month', $1::timestamp) - date_part('month', $2::timestamp)
) + (((extract(day from $1::timestamp) * 24 * 60 * 60 +
extract(hour from $1::timestamp) * 60 * 60 +
extract(minute from $1::timestamp) * 60 +
extract(second from $1::timestamp)) -
(extract(day from $2::timestamp) * 24 * 60 * 60 +
extract(hour from $2::timestamp) * 60 * 60 +
extract(minute from $2::timestamp) * 60 +
extract(second from $2::timestamp))
) / (24 * 60 * 60)
) / 31.0
)::DECIMAL(31,15); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.months_between(date, timestamptz) IS 'number of months';
-- DATE()/TIME() functions
CREATE OR REPLACE FUNCTION db2.date(value timestamp)
RETURNS date
AS $$ SELECT cast(date_trunc('day', $1) as date); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.date(timestamp) IS 'Returns the date part of a timestamp';
CREATE OR REPLACE FUNCTION db2.date(value timestamp with time zone)
RETURNS date
AS $$ SELECT cast(date_trunc('day', $1) as date); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.date(timestamp with time zone) IS 'Returns the date part of a timestamp';
CREATE OR REPLACE FUNCTION db2.date(value text)
RETURNS date
AS $$ SELECT cast(date_trunc('day', $1::timestamp) as date); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.date(text) IS 'Returns the date part of a timestamp';
CREATE OR REPLACE FUNCTION db2.time(value timestamp)
RETURNS time
AS $$ SELECT cast($1 as time); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.time(timestamp) IS 'Returns the time part of a timestamp';
CREATE OR REPLACE FUNCTION db2.time(value timestamp with time zone)
RETURNS time
AS $$ SELECT cast($1 as time); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.time(timestamp with time zone) IS 'Returns the time part of a timestamp';
CREATE OR REPLACE FUNCTION db2.time(value text)
RETURNS time
AS $$ SELECT cast($1::timestamp as time); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.time(text) IS 'Returns the time part of a timestamp';
-- TIMESTAMP_FORMAT(): Converts a text to a timestamp using the supplied time format.
-- Most format strings are compatible between DB2 and PostgreSQL.
CREATE OR REPLACE FUNCTION db2.timestamp_format(text, text)
RETURNS TIMESTAMP WITHOUT TIME ZONE
AS
$FUNC$
select (
-- FFn (n = Number of fractional second digits) with n > 6 is not supported.
-- Digits after the 6th are added to the seconds which is not desirable.
-- So we change the FF format string to US (microseconds) and truncate the digits to 6.
case when $2 ~ '\.FF\d*$' then
to_timestamp(regexp_replace($1, '\.(\d{1,6})\d*$', '.\1'),
regexp_replace($2, '\.FF\d*$', '.US'))
-- Try the original format. Most format strings seem to be identical between PostgreSQL and DB2.
else
to_timestamp($1, $2)
end
)::timestamp without time zone;
$FUNC$
LANGUAGE SQL IMMUTABLE STRICT
COST 50;
COMMENT ON FUNCTION db2.timestamp_format(text, text) IS 'Converts a timestamp given as text in the specified format to an actual timestamp';
-- LOCATE() function
CREATE FUNCTION db2.locate(text, text)
RETURNS integer
AS $$ SELECT position($1 IN $2); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.locate(text, text) IS 'position of substring';
-- TODO: locate(text, text, int)
-- TRANSLATE() function
CREATE FUNCTION db2.translate(text, text, text)
RETURNS text
AS $$ SELECT pg_catalog.translate($1, $3, $2); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.translate(text, text, text) IS 'map a set of characters appearing in string';
-- TODO: translate(text, text, text, pad)
CREATE FUNCTION db2.translate(text)
RETURNS text
AS $$ SELECT upper($1); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.translate(text) IS 'uppercase';
CREATE FUNCTION db2.lcase(value text)
RETURNS text
AS $$ SELECT lower($1); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.lcase(text) IS 'lowercase';
-- UPPER(text, text): DB2 allows the user to specify a language for the UPPER function.
-- This is not really needed with PostgreSQL, as upper() uses Unicode, independent of the language.
-- On DB2, upper('Müller', 'en_US') also returns MÜLLER, just as with de_DE.
CREATE FUNCTION db2.upper(text, text)
RETURNS TEXT
AS $$ SELECT upper($1); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.upper(text, text) IS 'uppercase with language as second parameter (ignored)';
-- LOWER(text, text): DB2 allows the user to specify a language for the LOWER function.
-- This is not really needed with PostgreSQL, as lower() uses Unicode, independent of the language.
CREATE FUNCTION db2.lower(text, text)
RETURNS TEXT
AS $$ SELECT lower($1); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.lower(text, text) IS 'lowercase with language as second parameter (ignored)';
CREATE FUNCTION db2.strip(value text)
RETURNS text
AS $$ SELECT trim($1); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.strip(text) IS 'Removes blanks from the beginning and end of a string. Alias for trim().';
-- CHAR()/INTEGER()/INT()/DOUBLE()/DECIMAL()/DEC() functions (CASTs)
CREATE FUNCTION db2.char(value text)
RETURNS char
AS $$ SELECT cast($1 AS CHAR); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.char(text) IS 'cast to char';
CREATE FUNCTION db2.char(value anyelement)
RETURNS char
AS $$ SELECT cast($1 AS CHAR); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.char(anyelement) IS 'cast to char';
CREATE FUNCTION db2.integer(value text)
RETURNS integer
AS $$ SELECT cast(floor($1::float) AS INTEGER); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.integer(text) IS 'cast to integer';
CREATE FUNCTION db2.integer(value anyelement)
RETURNS integer
AS $$ SELECT cast(floor($1::float) AS INTEGER); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.integer(anyelement) IS 'cast to integer';
CREATE FUNCTION db2.int(value text)
RETURNS integer
AS $$ SELECT cast(floor($1::float) AS INTEGER); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.int(text) IS 'cast to integer';
CREATE FUNCTION db2.int(value anyelement)
RETURNS integer
AS $$ SELECT cast(floor($1::float) AS INTEGER); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.int(anyelement) IS 'cast to integer';
CREATE FUNCTION db2.double(value text)
RETURNS double precision
AS $$ SELECT cast($1 AS DOUBLE PRECISION); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.double(text) IS 'cast to double precision';
CREATE FUNCTION db2.double(value anyelement)
RETURNS double precision
AS $$ SELECT cast($1 AS DOUBLE PRECISION); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.double(anyelement) IS 'cast to double precision';
CREATE FUNCTION db2.decimal(value text)
RETURNS decimal
AS $$ SELECT cast($1 AS DECIMAL); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.decimal(text) IS 'cast to decimal';
CREATE FUNCTION db2.decimal(value anyelement)
RETURNS decimal
AS $$ SELECT cast($1 AS DECIMAL); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.decimal(anyelement) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value text)
RETURNS decimal
AS $$ SELECT cast($1 AS DECIMAL); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(text) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value anyelement)
RETURNS decimal
AS $$ SELECT cast($1 AS DECIMAL); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(anyelement) IS 'cast to decimal';
-- DECIMAL()/DEC() function with precision and optional scale
CREATE FUNCTION db2.decimal(value text, prec integer, scale integer)
RETURNS numeric
AS $$
DECLARE
format text;
i int;
BEGIN
format := '';
FOR i IN 1..$2 LOOP
format := format || '9';
END LOOP;
format := format || '.';
IF $3 > 0 THEN
FOR i IN 1..$3 LOOP
format := format || '9';
END LOOP;
END IF;
RETURN to_number($1::text, format);
END
$$
LANGUAGE PLPGSQL;
COMMENT ON FUNCTION db2.decimal(text, integer, integer) IS 'cast to decimal';
CREATE FUNCTION db2.decimal(value text, prec integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1, $2, 0); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.decimal(text, integer) IS 'cast to decimal';
CREATE FUNCTION db2.decimal(value anyelement, prec integer, scale integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1::text, $2, $3); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.decimal(anyelement, integer, integer) IS 'cast to decimal';
CREATE FUNCTION db2.decimal(value anyelement, prec integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1::text, $2, 0); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.decimal(anyelement, integer) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value text, prec integer, scale integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1, $2, $3); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(text, integer, integer) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value text, prec integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1, $2, 0); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(text, integer) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value anyelement, prec integer, scale integer)
RETURNS decimal
AS $$ SELECT cast($1 AS DECIMAL($2, $3)); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(anyelement, integer, integer) IS 'cast to decimal';
CREATE FUNCTION db2.dec(value anyelement, prec integer)
RETURNS decimal
AS $$ SELECT db2."decimal"($1::text, $2, 0); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.dec(anyelement, integer) IS 'cast to decimal';
-- HEX() function
CREATE FUNCTION db2.hex(value text)
RETURNS text
AS $$ SELECT pg_catalog.encode($1::bytea, 'hex'); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.hex(text) IS 'convert value into hexadecimal string';
-- ROUND() function, additional overloaded signature
CREATE FUNCTION db2.round(value double precision, scale integer)
RETURNS numeric
AS $$ SELECT pg_catalog.round($1::numeric, $2); $$
LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION db2.round(double precision, integer) IS 'value rounded to ''scale''';
-- DIGITS() function
-- https://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_bif_digits.dita
-- Smallint input: return length = 5
CREATE FUNCTION db2.digits(value smallint) RETURNS varchar(5) AS
$func$
SELECT regexp_replace(to_char($1, repeat('0', 5)), '^[ +-]', '');
$func$
LANGUAGE SQL IMMUTABLE STRICT;
-- Integer input: return length = 10
CREATE FUNCTION db2.digits(value integer) RETURNS varchar(10) AS
$func$
SELECT regexp_replace(to_char($1, repeat('0', 10)), '^[ +-]', '');
$func$
LANGUAGE SQL IMMUTABLE STRICT;
-- Bigint input: return length = 19
CREATE FUNCTION db2.digits(value bigint) RETURNS varchar(19) AS
$func$
SELECT regexp_replace(to_char($1, repeat('0', 19)), '^[ +-]', '');
$func$
LANGUAGE SQL IMMUTABLE STRICT;
-- Generic numeric implementation.
-- On numeric(precision,scale) input, the length should depend on the precision of the argument.
-- I haven't found a way to determine that. So the formatted number will be returned without leading zeroes.
-- The result won't be compatible with DB2 if the value has less digits than its type.
CREATE FUNCTION db2.digits(value numeric) RETURNS text as
$func$
SELECT regexp_replace($1::text, '[^0-9]', '', 'g');
$func$
LANGUAGE SQL IMMUTABLE STRICT;
-- VALUE() function (alias for COALESCE())
CREATE FUNCTION db2.value(variadic anyarray)
RETURNS anyelement
AS $$ SELECT $1[i]
FROM generate_subscripts($1, 1) g(i)
WHERE $1[i] IS NOT NULL LIMIT 1; $$
LANGUAGE SQL STRICT;
COMMENT ON FUNCTION db2.value(variadic anyarray) IS 'alias for coalesce';
CREATE FUNCTION db2.value(variadic text[])
RETURNS text
AS $$ SELECT $1[i]
FROM generate_subscripts($1, 1) g(i)
WHERE $1[i] IS NOT NULL LIMIT 1; $$
LANGUAGE SQL STRICT;
COMMENT ON FUNCTION db2.value(variadic text[]) IS 'alias for coalesce';
-- DOUBLE type (alias for DOUBLE PRECISION)
CREATE DOMAIN db2.double AS double precision;
COMMENT ON DOMAIN db2.double IS 'double-precision floating point number, 8-byte storage';
-- ^= operator (alias for <> and !=)
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int48ne,
LEFTARG = integer,
RIGHTARG = bigint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (integer, bigint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.boolne,
LEFTARG = boolean,
RIGHTARG = boolean,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (boolean, boolean) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.tidne,
LEFTARG = tid,
RIGHTARG = tid,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (tid, tid) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int8ne,
LEFTARG = bigint,
RIGHTARG = bigint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bigint, bigint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int84ne,
LEFTARG = bigint,
RIGHTARG = integer,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bigint, integer) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int4ne,
LEFTARG = integer,
RIGHTARG = integer,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (integer, integer) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int2ne,
LEFTARG = smallint,
RIGHTARG = smallint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (smallint, smallint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.textne,
LEFTARG = text,
RIGHTARG = text,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (text, text) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int24ne,
LEFTARG = smallint,
RIGHTARG = integer,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (smallint, integer) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int42ne,
LEFTARG = integer,
RIGHTARG = smallint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (integer, smallint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.oidne,
LEFTARG = oid,
RIGHTARG = oid,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (oid, oid) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.float4ne,
LEFTARG = real,
RIGHTARG = real,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (real, real) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.charne,
LEFTARG = "char",
RIGHTARG = "char",
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= ("char", "char") IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.namene,
LEFTARG = name,
RIGHTARG = name,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (name, name) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.oidvectorne,
LEFTARG = oidvector,
RIGHTARG = oidvector,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (oidvector, oidvector) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.float8ne,
LEFTARG = double precision,
RIGHTARG = double precision,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (double precision, double precision) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.point_ne,
LEFTARG = point,
RIGHTARG = point,
COMMUTATOR = ^=,
NEGATOR = ~=,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (point, point) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.cash_ne,
LEFTARG = money,
RIGHTARG = money,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (money, money) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.bpcharne,
LEFTARG = character,
RIGHTARG = character,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (character, character) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.array_ne,
LEFTARG = anyarray,
RIGHTARG = anyarray,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (anyarray, anyarray) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.date_ne,
LEFTARG = date,
RIGHTARG = date,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (date, date) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.time_ne,
LEFTARG = time without time zone,
RIGHTARG = time without time zone,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (time without time zone, time without time zone) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.float48ne,
LEFTARG = real,
RIGHTARG = double precision,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (real, double precision) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.float84ne,
LEFTARG = double precision,
RIGHTARG = real,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (double precision, real) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.network_ne,
LEFTARG = inet,
RIGHTARG = inet,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (inet, inet) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.macaddr_ne,
LEFTARG = macaddr,
RIGHTARG = macaddr,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (macaddr, macaddr) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.timestamptz_ne,
LEFTARG = timestamp with time zone,
RIGHTARG = timestamp with time zone,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (timestamp with time zone, timestamp with time zone) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.interval_ne,
LEFTARG = interval,
RIGHTARG = interval,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (interval, interval) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.circle_ne,
LEFTARG = circle,
RIGHTARG = circle,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (circle, circle) IS 'not equal by area';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.timetz_ne,
LEFTARG = time with time zone,
RIGHTARG = time with time zone,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (time with time zone, time with time zone) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.lseg_ne,
LEFTARG = lseg,
RIGHTARG = lseg,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (lseg, lseg) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.numeric_ne,
LEFTARG = numeric,
RIGHTARG = numeric,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (numeric, numeric) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.bitne,
LEFTARG = bit,
RIGHTARG = bit,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bit, bit) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.varbitne,
LEFTARG = bit varying,
RIGHTARG = bit varying,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bit varying, bit varying) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int28ne,
LEFTARG = smallint,
RIGHTARG = bigint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (smallint, bigint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.int82ne,
LEFTARG = bigint,
RIGHTARG = smallint,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bigint, smallint) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.byteane,
LEFTARG = bytea,
RIGHTARG = bytea,
COMMUTATOR = ^=,
NEGATOR = =,
RESTRICT = neqsel,
JOIN = neqjoinsel
);
COMMENT ON OPERATOR db2.^= (bytea, bytea) IS 'not equal';
CREATE OPERATOR db2.^= (
PROCEDURE = pg_catalog.timestamp_ne,
LEFTARG = timestamp without time zone,
RIGHTARG = timestamp without time zone,