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
use crate::schema_reader::tests;
use crate::test_helpers::TestHelper;
use crate::{
default, FunctionKind, Parallel, PostgresAggregateFunction, PostgresDatabase, PostgresFunction,
PostgresSchema, TimescaleSupport, Volatility,
};
use crate::{test_helpers, PostgresColumn, PostgresTable};
use elefant_test_macros::pg_test;
use ordered_float::NotNan;
#[pg_test(arg(postgres = 12))]
#[pg_test(arg(postgres = 13))]
#[pg_test(arg(postgres = 14))]
#[pg_test(arg(postgres = 15))]
#[pg_test(arg(postgres = 16))]
#[pg_test(arg(timescale_db = 15))]
#[pg_test(arg(timescale_db = 16))]
async fn test_functions(helper: &TestHelper) {
tests::test_introspection(helper,
r#"
create function add(a int4, b int4) returns int4 as $$ begin return a + b; end; $$ language plpgsql;
create function filter_stuff(value text) returns table(id int, name text) as
$$
begin
create temp table temp_table(id int, name text);
insert into temp_table(id, name) values (1, 'foo'), (2, 'bar');
return query select * from temp_table where name = value;
end;
$$ language plpgsql;
create function _group_concat(text, text) returns text
language sql immutable
as $_$
select case
when $2 is null then $1
when $1 is null then $2
else $1 || ', ' || $2
end
$_$;
create aggregate group_concat(text) (
sfunc = _group_concat,
stype = text
);
"#,
PostgresDatabase {
schemas: vec![PostgresSchema {
name: "public".to_string(),
functions: vec![
PostgresFunction {
function_name: "_group_concat".to_string(),
language: "sql".to_string(),
estimated_cost: NotNan::new(100.0).unwrap(),
estimated_rows: NotNan::new(0.0).unwrap(),
support_function: None,
kind: FunctionKind::Function,
security_definer: false,
leak_proof: false,
strict: false,
returns_set: false,
volatility: Volatility::Immutable,
parallel: Parallel::Unsafe,
sql_body: r#"select case
when $2 is null then $1
when $1 is null then $2
else $1 || ', ' || $2
end"#.into(),
arguments: "text, text".to_string(),
result: Some("text".to_string()),
object_id: 2.into(),
..default()
},
PostgresFunction {
function_name: "add".to_string(),
language: "plpgsql".to_string(),
estimated_cost: NotNan::new(100.0).unwrap(),
estimated_rows: NotNan::new(0.0).unwrap(),
support_function: None,
kind: FunctionKind::Function,
security_definer: false,
leak_proof: false,
strict: false,
returns_set: false,
volatility: Volatility::Volatile,
parallel: Parallel::Unsafe,
sql_body: r#"begin return a + b; end;"#
.into(),
configuration: None,
arguments: "a integer, b integer".to_string(),
result: Some("integer".to_string()),
..default()
},
PostgresFunction {
function_name: "filter_stuff".to_string(),
language: "plpgsql".to_string(),
estimated_cost: NotNan::new(100.0).unwrap(),
estimated_rows: NotNan::new(1000.0).unwrap(),
support_function: None,
kind: FunctionKind::Function,
security_definer: false,
leak_proof: false,
strict: false,
returns_set: true,
volatility: Volatility::Volatile,
parallel: Parallel::Unsafe,
sql_body: r#"begin
create temp table temp_table(id int, name text);
insert into temp_table(id, name) values (1, 'foo'), (2, 'bar');
return query select * from temp_table where name = value;
end;"#
.into(),
configuration: None,
arguments: "value text".to_string(),
result: Some("TABLE(id integer, name text)".to_string()),
..default()
},
],
aggregate_functions: vec![
PostgresAggregateFunction {
function_name: "group_concat".to_string(),
state_transition_function: "_group_concat".to_string(),
arguments: "text".to_string(),
transition_type: "text".to_string(),
parallel: Parallel::Unsafe,
depends_on: vec![2.into()],
..default()
}
],
..default()
}],
timescale_support: TimescaleSupport::from_test_helper(helper),
..default()
},
).await;
}
#[pg_test(arg(postgres = 12))]
#[pg_test(arg(postgres = 13))]
#[pg_test(arg(postgres = 14))]
#[pg_test(arg(postgres = 15))]
#[pg_test(arg(postgres = 16))]
#[pg_test(arg(timescale_db = 15))]
#[pg_test(arg(timescale_db = 16))]
async fn functions_returning_tables(helper: &TestHelper) {
tests::test_introspection(
helper,
r#"
create table my_table(id int, name text);
create function my_function() returns setof my_table as $$
begin
return query select 1, 'foo';
end;
$$ language plpgsql;
"#,
PostgresDatabase {
schemas: vec![PostgresSchema {
name: "public".to_string(),
object_id: 1.into(),
tables: vec![PostgresTable {
name: "my_table".to_string(),
object_id: 2.into(),
columns: vec![
PostgresColumn {
name: "id".to_string(),
data_type: "int4".to_string(),
ordinal_position: 1,
..default()
},
PostgresColumn {
name: "name".to_string(),
data_type: "text".to_string(),
ordinal_position: 2,
..default()
},
],
..default()
}],
functions: vec![PostgresFunction {
function_name: "my_function".to_string(),
language: "plpgsql".to_string(),
estimated_cost: NotNan::new(100.0).unwrap(),
estimated_rows: NotNan::new(1000.0).unwrap(),
support_function: None,
kind: FunctionKind::Function,
security_definer: false,
leak_proof: false,
strict: false,
returns_set: true,
volatility: Volatility::Volatile,
parallel: Parallel::Unsafe,
sql_body: r#"begin
return query select 1, 'foo';
end;"#
.into(),
arguments: "".to_string(),
result: Some("SETOF my_table".to_string()),
object_id: 3.into(),
depends_on: vec![2.into()],
..default()
}],
..default()
}],
timescale_support: TimescaleSupport::from_test_helper(helper),
..default()
},
)
.await;
}