|
| 1 | +-- |
| 2 | +-- Tests for PL/pgSQL variable properties: CONSTANT, NOT NULL, initializers |
| 3 | +-- |
| 4 | +create type var_record as (f1 int4, f2 int4); |
| 5 | +create domain int_nn as int not null; |
| 6 | +create domain var_record_nn as var_record not null; |
| 7 | +create domain var_record_colnn as var_record check((value).f2 is not null); |
| 8 | +-- CONSTANT |
| 9 | +do $$ |
| 10 | +declare x constant int := 42; |
| 11 | +begin |
| 12 | + raise notice 'x = %', x; |
| 13 | +end$$; |
| 14 | +NOTICE: x = 42 |
| 15 | +do $$ |
| 16 | +declare x constant int; |
| 17 | +begin |
| 18 | + x := 42; -- fail |
| 19 | +end$$; |
| 20 | +ERROR: variable "x" is declared CONSTANT |
| 21 | +LINE 4: x := 42; -- fail |
| 22 | + ^ |
| 23 | +do $$ |
| 24 | +declare x constant int; y int; |
| 25 | +begin |
| 26 | + for x, y in select 1, 2 loop -- fail |
| 27 | + end loop; |
| 28 | +end$$; |
| 29 | +ERROR: variable "x" is declared CONSTANT |
| 30 | +LINE 4: for x, y in select 1, 2 loop -- fail |
| 31 | + ^ |
| 32 | +do $$ |
| 33 | +declare x constant int[]; |
| 34 | +begin |
| 35 | + x[1] := 42; -- fail |
| 36 | +end$$; |
| 37 | +ERROR: variable "x" is declared CONSTANT |
| 38 | +LINE 4: x[1] := 42; -- fail |
| 39 | + ^ |
| 40 | +do $$ |
| 41 | +declare x constant int[]; y int; |
| 42 | +begin |
| 43 | + for x[1], y in select 1, 2 loop -- fail (currently, unsupported syntax) |
| 44 | + end loop; |
| 45 | +end$$; |
| 46 | +ERROR: syntax error at or near "[" |
| 47 | +LINE 4: for x[1], y in select 1, 2 loop -- fail (currently, unsup... |
| 48 | + ^ |
| 49 | +do $$ |
| 50 | +declare x constant var_record; |
| 51 | +begin |
| 52 | + x.f1 := 42; -- fail |
| 53 | +end$$; |
| 54 | +ERROR: variable "x" is declared CONSTANT |
| 55 | +LINE 4: x.f1 := 42; -- fail |
| 56 | + ^ |
| 57 | +do $$ |
| 58 | +declare x constant var_record; y int; |
| 59 | +begin |
| 60 | + for x.f1, y in select 1, 2 loop -- fail |
| 61 | + end loop; |
| 62 | +end$$; |
| 63 | +ERROR: variable "x" is declared CONSTANT |
| 64 | +LINE 4: for x.f1, y in select 1, 2 loop -- fail |
| 65 | + ^ |
| 66 | +-- initializer expressions |
| 67 | +do $$ |
| 68 | +declare x int := sin(0); |
| 69 | +begin |
| 70 | + raise notice 'x = %', x; |
| 71 | +end$$; |
| 72 | +NOTICE: x = 0 |
| 73 | +do $$ |
| 74 | +declare x int := 1/0; -- fail |
| 75 | +begin |
| 76 | + raise notice 'x = %', x; |
| 77 | +end$$; |
| 78 | +ERROR: division by zero |
| 79 | +CONTEXT: SQL statement "SELECT 1/0" |
| 80 | +PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 81 | +do $$ |
| 82 | +declare x bigint[] := array[1,3,5]; |
| 83 | +begin |
| 84 | + raise notice 'x = %', x; |
| 85 | +end$$; |
| 86 | +NOTICE: x = {1,3,5} |
| 87 | +do $$ |
| 88 | +declare x record := row(1,2,3); |
| 89 | +begin |
| 90 | + raise notice 'x = %', x; |
| 91 | +end$$; |
| 92 | +NOTICE: x = (1,2,3) |
| 93 | +do $$ |
| 94 | +declare x var_record := row(1,2); |
| 95 | +begin |
| 96 | + raise notice 'x = %', x; |
| 97 | +end$$; |
| 98 | +NOTICE: x = (1,2) |
| 99 | +-- NOT NULL |
| 100 | +do $$ |
| 101 | +declare x int not null; -- fail |
| 102 | +begin |
| 103 | + raise notice 'x = %', x; |
| 104 | +end$$; |
| 105 | +ERROR: variable "x" must have a default value, since it's declared NOT NULL |
| 106 | +LINE 2: declare x int not null; -- fail |
| 107 | + ^ |
| 108 | +do $$ |
| 109 | +declare x int not null := 42; |
| 110 | +begin |
| 111 | + raise notice 'x = %', x; |
| 112 | + x := null; -- fail |
| 113 | +end$$; |
| 114 | +NOTICE: x = 42 |
| 115 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 116 | +CONTEXT: PL/pgSQL function inline_code_block line 5 at assignment |
| 117 | +do $$ |
| 118 | +declare x int not null := null; -- fail |
| 119 | +begin |
| 120 | + raise notice 'x = %', x; |
| 121 | +end$$; |
| 122 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 123 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 124 | +do $$ |
| 125 | +declare x record not null; -- fail |
| 126 | +begin |
| 127 | + raise notice 'x = %', x; |
| 128 | +end$$; |
| 129 | +ERROR: variable "x" must have a default value, since it's declared NOT NULL |
| 130 | +LINE 2: declare x record not null; -- fail |
| 131 | + ^ |
| 132 | +do $$ |
| 133 | +declare x record not null := row(42); |
| 134 | +begin |
| 135 | + raise notice 'x = %', x; |
| 136 | + x := row(null); -- ok |
| 137 | + raise notice 'x = %', x; |
| 138 | + x := null; -- fail |
| 139 | +end$$; |
| 140 | +NOTICE: x = (42) |
| 141 | +NOTICE: x = () |
| 142 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 143 | +CONTEXT: PL/pgSQL function inline_code_block line 7 at assignment |
| 144 | +do $$ |
| 145 | +declare x record not null := null; -- fail |
| 146 | +begin |
| 147 | + raise notice 'x = %', x; |
| 148 | +end$$; |
| 149 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 150 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 151 | +do $$ |
| 152 | +declare x var_record not null; -- fail |
| 153 | +begin |
| 154 | + raise notice 'x = %', x; |
| 155 | +end$$; |
| 156 | +ERROR: variable "x" must have a default value, since it's declared NOT NULL |
| 157 | +LINE 2: declare x var_record not null; -- fail |
| 158 | + ^ |
| 159 | +do $$ |
| 160 | +declare x var_record not null := row(41,42); |
| 161 | +begin |
| 162 | + raise notice 'x = %', x; |
| 163 | + x := row(null,null); -- ok |
| 164 | + raise notice 'x = %', x; |
| 165 | + x := null; -- fail |
| 166 | +end$$; |
| 167 | +NOTICE: x = (41,42) |
| 168 | +NOTICE: x = (,) |
| 169 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 170 | +CONTEXT: PL/pgSQL function inline_code_block line 7 at assignment |
| 171 | +do $$ |
| 172 | +declare x var_record not null := null; -- fail |
| 173 | +begin |
| 174 | + raise notice 'x = %', x; |
| 175 | +end$$; |
| 176 | +ERROR: null value cannot be assigned to variable "x" declared NOT NULL |
| 177 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 178 | +-- Check that variables are reinitialized on block re-entry. |
| 179 | +\set VERBOSITY terse \\ -- needed for output stability |
| 180 | +do $$ |
| 181 | +begin |
| 182 | + for i in 1..3 loop |
| 183 | + declare |
| 184 | + x int; |
| 185 | + y int := i; |
| 186 | + r record; |
| 187 | + c var_record; |
| 188 | + begin |
| 189 | + if i = 1 then |
| 190 | + x := 42; |
| 191 | + r := row(i, i+1); |
| 192 | + c := row(i, i+1); |
| 193 | + end if; |
| 194 | + raise notice 'x = %', x; |
| 195 | + raise notice 'y = %', y; |
| 196 | + raise notice 'r = %', r; |
| 197 | + raise notice 'c = %', c; |
| 198 | + end; |
| 199 | + end loop; |
| 200 | +end$$; |
| 201 | +NOTICE: x = 42 |
| 202 | +NOTICE: y = 1 |
| 203 | +NOTICE: r = (1,2) |
| 204 | +NOTICE: c = (1,2) |
| 205 | +NOTICE: x = <NULL> |
| 206 | +NOTICE: y = 2 |
| 207 | +NOTICE: r = <NULL> |
| 208 | +NOTICE: c = <NULL> |
| 209 | +NOTICE: x = <NULL> |
| 210 | +NOTICE: y = 3 |
| 211 | +NOTICE: r = <NULL> |
| 212 | +NOTICE: c = <NULL> |
| 213 | +\set VERBOSITY default |
| 214 | +-- Check enforcement of domain constraints during initialization |
| 215 | +do $$ |
| 216 | +declare x int_nn; -- fail |
| 217 | +begin |
| 218 | + raise notice 'x = %', x; |
| 219 | +end$$; |
| 220 | +ERROR: domain int_nn does not allow null values |
| 221 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 222 | +do $$ |
| 223 | +declare x int_nn := null; -- fail |
| 224 | +begin |
| 225 | + raise notice 'x = %', x; |
| 226 | +end$$; |
| 227 | +ERROR: domain int_nn does not allow null values |
| 228 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 229 | +do $$ |
| 230 | +declare x int_nn := 42; |
| 231 | +begin |
| 232 | + raise notice 'x = %', x; |
| 233 | + x := null; -- fail |
| 234 | +end$$; |
| 235 | +NOTICE: x = 42 |
| 236 | +ERROR: domain int_nn does not allow null values |
| 237 | +CONTEXT: PL/pgSQL function inline_code_block line 5 at assignment |
| 238 | +do $$ |
| 239 | +declare x var_record_nn; -- fail |
| 240 | +begin |
| 241 | + raise notice 'x = %', x; |
| 242 | +end$$; |
| 243 | +ERROR: domain var_record_nn does not allow null values |
| 244 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 245 | +do $$ |
| 246 | +declare x var_record_nn := null; -- fail |
| 247 | +begin |
| 248 | + raise notice 'x = %', x; |
| 249 | +end$$; |
| 250 | +ERROR: domain var_record_nn does not allow null values |
| 251 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 252 | +do $$ |
| 253 | +declare x var_record_nn := row(1,2); |
| 254 | +begin |
| 255 | + raise notice 'x = %', x; |
| 256 | + x := row(null,null); -- ok |
| 257 | + x := null; -- fail |
| 258 | +end$$; |
| 259 | +NOTICE: x = (1,2) |
| 260 | +ERROR: domain var_record_nn does not allow null values |
| 261 | +CONTEXT: PL/pgSQL function inline_code_block line 6 at assignment |
| 262 | +do $$ |
| 263 | +declare x var_record_colnn; -- fail |
| 264 | +begin |
| 265 | + raise notice 'x = %', x; |
| 266 | +end$$; |
| 267 | +ERROR: value for domain var_record_colnn violates check constraint "var_record_colnn_check" |
| 268 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 269 | +do $$ |
| 270 | +declare x var_record_colnn := null; -- fail |
| 271 | +begin |
| 272 | + raise notice 'x = %', x; |
| 273 | +end$$; |
| 274 | +ERROR: value for domain var_record_colnn violates check constraint "var_record_colnn_check" |
| 275 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 276 | +do $$ |
| 277 | +declare x var_record_colnn := row(1,null); -- fail |
| 278 | +begin |
| 279 | + raise notice 'x = %', x; |
| 280 | +end$$; |
| 281 | +ERROR: value for domain var_record_colnn violates check constraint "var_record_colnn_check" |
| 282 | +CONTEXT: PL/pgSQL function inline_code_block line 3 during statement block local variable initialization |
| 283 | +do $$ |
| 284 | +declare x var_record_colnn := row(1,2); |
| 285 | +begin |
| 286 | + raise notice 'x = %', x; |
| 287 | + x := null; -- fail |
| 288 | +end$$; |
| 289 | +NOTICE: x = (1,2) |
| 290 | +ERROR: value for domain var_record_colnn violates check constraint "var_record_colnn_check" |
| 291 | +CONTEXT: PL/pgSQL function inline_code_block line 5 at assignment |
| 292 | +do $$ |
| 293 | +declare x var_record_colnn := row(1,2); |
| 294 | +begin |
| 295 | + raise notice 'x = %', x; |
| 296 | + x := row(null,null); -- fail |
| 297 | +end$$; |
| 298 | +NOTICE: x = (1,2) |
| 299 | +ERROR: value for domain var_record_colnn violates check constraint "var_record_colnn_check" |
| 300 | +CONTEXT: PL/pgSQL function inline_code_block line 5 at assignment |
0 commit comments