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
|
--Immutable, stable, volatile functions and nextval are allowed in DEFAULT clause
-- IMMUTABLE function is allowed
CREATE FUNCTION xl_add(integer, integer) RETURNS integer
AS 'select $1 + $2;'
LANGUAGE SQL
IMMUTABLE
RETURNS NULL ON NULL INPUT;
CREATE TABLE xl_funct2(
a integer,
b integer,
c integer DEFAULT xl_add(10,12 )
) DISTRIBUTE BY HASH(a);
INSERT INTO xl_funct2(a,b) VALUES (1,2);--c should be 22
INSERT INTO xl_funct2(a,b,c) VALUES (3,4,5);-- c should be 5
SELECT * from xl_funct2;
a | b | c
---+---+----
1 | 2 | 22
3 | 4 | 5
(2 rows)
--STABLE functions
-- checking STABLE function in DEFAULT of non-distribution column
create function xl_nochange(text) returns text
as 'select $1 limit 1' language sql stable;
CREATE TABLE xl_funct3(
a integer,
b integer,
c text DEFAULT xl_nochange('hello')
) DISTRIBUTE BY HASH(a);
INSERT INTO xl_funct3(a,b) VALUES (1,2);--c should be pallavi
INSERT INTO xl_funct3(a,b,c) VALUES (3,4,'qwerty');-- c should be qwerty
SELECT * from xl_funct3;
a | b | c
---+---+--------
1 | 2 | hello
3 | 4 | qwerty
(2 rows)
-- checking STABLE function in DEFAULT of distribution column
CREATE TABLE xl_funct4(
a integer,
b integer,
c text DEFAULT xl_nochange('hello')
) DISTRIBUTE BY HASH(c);
INSERT INTO xl_funct4(a,b) VALUES (1,2);--c should be pallavi
INSERT INTO xl_funct4(a,b,c) VALUES (3,4,'qwerty');-- c should be qwerty
SELECT * from xl_funct4;
a | b | c
---+---+--------
3 | 4 | qwerty
1 | 2 | hello
(2 rows)
--VOLATILE functions
create or replace function xl_get_curr_decade() returns double precision as
$$ SELECT EXTRACT(DECADE FROM NOW()); $$
language sql volatile;
CREATE TABLE xl_funct5(
a integer,
b integer,
c double precision DEFAULT xl_get_curr_decade()
) DISTRIBUTE BY HASH(a);
INSERT INTO xl_funct5(a,b) VALUES (1,2);--c should be e.g. 201 for 2015
INSERT INTO xl_funct5(a,b,c) VALUES (3,4,20);-- c should be 20
SELECT * from xl_funct5;
a | b | c
---+---+-----
1 | 2 | 201
3 | 4 | 20
(2 rows)
--nextval check
SET sequence_range = 1;
CREATE SEQUENCE xl_INSERT_SEQ;
CREATE TABLE xl_funct (
a integer,
b INT DEFAULT nextval('xl_insert_seq')
) DISTRIBUTE BY HASH (a);
INSERT INTO xl_funct (a) VALUES (1);
INSERT INTO xl_funct (a) VALUES (2);
INSERT INTO xl_funct (a) VALUES (3);
SELECT * FROM xl_funct;
a | b
---+---
1 | 1
2 | 2
3 | 3
(3 rows)
CREATE TABLE xl_funct1 (
a integer DEFAULT nextval('xl_insert_seq'),
b INT
) DISTRIBUTE BY HASH (a);
INSERT INTO xl_funct1 (b) VALUES (1);
INSERT INTO xl_funct1 (b) VALUES (2);
INSERT INTO xl_funct1 (b) VALUES (3);
SELECT * FROM xl_funct1;
a | b
---+---
5 | 2
6 | 3
4 | 1
(3 rows)
DROP TABLE xl_funct;
DROP TABLE xl_funct1;
DROP SEQUENCE xl_INSERT_SEQ;
DROP TABLE xl_funct2;
DROP TABLE xl_funct3;
DROP TABLE xl_funct4;
DROP FUNCTION xl_nochange(text);
DROP TABLE xl_funct5;
DROP FUNCTION xl_get_curr_decade();
|