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
|
--
\set VERBOSITY terse
--
create type ctype3 as (fred integer, jim numeric);
do $$
begin
if current_setting('server_version_num')::integer >= 110000 then
execute 'create domain dtype as ctype3 check((VALUE).jim is not null)';
else
execute 'create type dtype as (fred integer, jim numeric)';
end if;
end;
$$;
create type ctype2 as (thingy text, wotsit integer);
create type ctype as (foo text, bar ctype2, baz dtype);
create table tdata (
intcol integer,
textcol text,
charcol char(32),
varcharcol varchar(32),
compcol ctype,
dcompcol dtype
);
insert into tdata
values (1, 'row 1', 'padded with blanks', 'not padded', ('x',('y',1111),(111,11.1)), (11,1.1)),
(2, 'row 2', 'padded with blanks', 'not padded', ('x',('y',2222),(222,22.2)), (22,2.2)),
(3, 'row 3', 'padded with blanks', 'not padded', ('x',('y',3333),(333,33.3)), (33,3.3));
create function tf1() returns setof tdata language pllua as $f$
for i = 1,4 do
coroutine.yield({ intcol = i,
textcol = "row "..i,
charcol = "padded with blanks",
varcharcol = "not padded",
compcol = { foo = "x",
bar = { thingy = "y", wotsit = i*1111 },
baz = { fred = i*111, jim = i*11.1 }
},
dcompcol = { fred = i*11, jim = i*1.1 }
})
end
$f$;
select * from tf1();
intcol | textcol | charcol | varcharcol | compcol | dcompcol
--------+---------+----------------------------------+------------+-----------------------------+----------
1 | row 1 | padded with blanks | not padded | (x,"(y,1111)","(111,11.1)") | (11,1.1)
2 | row 2 | padded with blanks | not padded | (x,"(y,2222)","(222,22.2)") | (22,2.2)
3 | row 3 | padded with blanks | not padded | (x,"(y,3333)","(333,33.3)") | (33,3.3)
4 | row 4 | padded with blanks | not padded | (x,"(y,4444)","(444,44.4)") | (44,4.4)
(4 rows)
--
-- various checks of type handling
--
do language pllua $$ print(pgtype(nil,'ctype3')(1,2)) $$;
INFO: (1,2)
do language pllua $$ print(pgtype(nil,'ctype3')({1,2})) $$;
INFO: (,)
do language pllua $$ print(pgtype(nil,'ctype3')(true,true)) $$;
ERROR: pllua: incompatible value type
do language pllua $$ print(pgtype(nil,'ctype3')("1","2")) $$;
INFO: (1,2)
do language pllua $$ print(pgtype(nil,'ctype3')({fred=1,jim=2})) $$;
INFO: (1,2)
do language pllua $$ print(pgtype(nil,'ctype3')({fred=1,jim={}})) $$;
ERROR: pllua: incompatible value type
do language pllua $$ print(pgtype(nil,'ctype3')({fred=1,jim=nil})) $$;
INFO: (1,)
--do language pllua $$ print(pgtype(nil,'dtype')({fred=1,jim=nil})) $$;
create function tf2() returns setof tdata language pllua as $f$
local t = spi.execute("select * from tdata")
for i,v in ipairs(t) do coroutine.yield(v) end
$f$;
select * from tf2();
intcol | textcol | charcol | varcharcol | compcol | dcompcol
--------+---------+----------------------------------+------------+-----------------------------+----------
1 | row 1 | padded with blanks | not padded | (x,"(y,1111)","(111,11.1)") | (11,1.1)
2 | row 2 | padded with blanks | not padded | (x,"(y,2222)","(222,22.2)") | (22,2.2)
3 | row 3 | padded with blanks | not padded | (x,"(y,3333)","(333,33.3)") | (33,3.3)
(3 rows)
do language pllua $$ print(pgtype.ctype3()) $$;
INFO: (,)
-- ensure detoasting of nested composites works right
do language pllua $f$
for r in spi.rows("select * from tdata") do
print(r.intcol, r.compcol.foo, r.compcol.bar.wotsit, r.dcompcol.jim)
end
$f$;
INFO: 1 x 1111 1.1
INFO: 2 x 2222 2.2
INFO: 3 x 3333 3.3
do language pllua $$
a = pgtype.array.integer({{{1,2}},{{3,4}},{{5,6}}},3,1,2)
print(a)
print(#a,#(a[1]),#(a[1][1]))
print(a[3][1][2],a[1][1][1])
$$;
INFO: {{{1,2}},{{3,4}},{{5,6}}}
INFO: 3 1 2
INFO: 6 1
do language pllua $$ print(pgtype.int4range(123,456)) $$;
INFO: [123,456)
do language pllua $$ print(pgtype.int4range()) $$;
INFO: empty
do language pllua $$ print(pgtype.int4range(123,456,'(]')) $$;
INFO: [124,457)
do language pllua $$ print(pgtype.int4range(nil,456,'(]')) $$;
INFO: (,457)
do language pllua $$ print(pgtype.int4range(nil,nil)) $$;
INFO: (,)
do language pllua $$ print(pgtype.int4range(123,nil)) $$;
INFO: [123,)
do language pllua $$ print(pgtype.int4range('[12,56]')) $$;
INFO: [12,57)
do language pllua $$
local r1,r2,r3 = pgtype.numrange('[12,56]'),
pgtype.numrange('empty'),
pgtype.numrange('(12,)')
print(r1.lower,r1.upper,r1.lower_inc,r1.upper_inc,r1.lower_inf,r1.upper_inf,r1.isempty)
print(r2.lower,r2.upper,r2.lower_inc,r2.upper_inc,r2.lower_inf,r2.upper_inf,r2.isempty)
print(r3.lower,r3.upper,r3.lower_inc,r3.upper_inc,r3.lower_inf,r3.upper_inf,r3.isempty)
$$;
INFO: 12 56 true true false false false
INFO: nil nil false false false false true
INFO: 12 nil false false false true false
create type myenum as enum ('TRUE', 'FALSE', 'FILE_NOT_FOUND');
create function pg_temp.f1(a myenum) returns text language pllua as $$ print(a,type(a)) return a $$;
select pg_temp.f1(x) from unnest(enum_range(null::myenum)) x;
INFO: TRUE string
INFO: FALSE string
INFO: FILE_NOT_FOUND string
f1
----------------
TRUE
FALSE
FILE_NOT_FOUND
(3 rows)
create function pg_temp.f2() returns myenum language pllua as $$ return 'FILE_NOT_FOUND' $$;
select pg_temp.f2();
f2
----------------
FILE_NOT_FOUND
(1 row)
-- domains
create domain mydom1 as varchar(3);
create domain mydom2 as varchar(3) check (value in ('foo','bar','baz'));
create domain mydom3 as varchar(3) not null;
create domain mydom4 as varchar(3) not null check (value in ('foo','bar','baz'));
create function pg_temp.f3(a mydom1) returns void language pllua as $$
print(pgtype(nil,1):name(), type(a), #a)
$$;
select pg_temp.f3('foo') union all select pg_temp.f3('bar ');
INFO: mydom1 string 3
INFO: mydom1 string 3
f3
----
(2 rows)
create function pg_temp.f4d1(a text) returns mydom1 language pllua as $$
return a
$$;
select pg_temp.f4d1('foo');
f4d1
------
foo
(1 row)
select pg_temp.f4d1('bar ');
f4d1
------
bar
(1 row)
select pg_temp.f4d1('toolong');
ERROR: value too long for type character varying(3)
select pg_temp.f4d1(null);
f4d1
------
(1 row)
create function pg_temp.f4d2(a text) returns mydom2 language pllua as $$
return a
$$;
select pg_temp.f4d2('bar ');
f4d2
------
bar
(1 row)
select pg_temp.f4d2('bad');
ERROR: value for domain mydom2 violates check constraint "mydom2_check"
select pg_temp.f4d2('toolong');
ERROR: value too long for type character varying(3)
select pg_temp.f4d2(null);
f4d2
------
(1 row)
create function pg_temp.f4d3(a text) returns mydom3 language pllua as $$
return a
$$;
select pg_temp.f4d3('bar ');
f4d3
------
bar
(1 row)
select pg_temp.f4d3('toolong');
ERROR: value too long for type character varying(3)
select pg_temp.f4d3(null);
ERROR: domain mydom3 does not allow null values
create function pg_temp.f4d4(a text) returns mydom4 language pllua as $$
return a
$$;
select pg_temp.f4d4('bar ');
f4d4
------
bar
(1 row)
select pg_temp.f4d4('bad');
ERROR: value for domain mydom4 violates check constraint "mydom4_check"
select pg_temp.f4d4('toolong');
ERROR: value too long for type character varying(3)
select pg_temp.f4d4(null);
ERROR: domain mydom4 does not allow null values
-- array coercions
-- relabeltype path
do language pllua $$
local a = pgtype.array.varchar("foo","bar")
local b = pgtype.array.text(a)
print(b)
$$;
INFO: {foo,bar}
-- cast function path
do language pllua $$
local a = pgtype.array.boolean(false,true)
local b = pgtype.array.text(a)
print(b)
$$;
INFO: {false,true}
-- IO path
do language pllua $$
local a = pgtype.array.integer(10,20)
local b = pgtype.array.text(a)
print(b)
$$;
INFO: {10,20}
-- array typmod coercions
create temp table atc (a varchar(10)[], b char(10)[]);
do language pllua $$
local a = pgtype.array.varchar('foo','bar','value_too_long_for_type')
local b = pgtype.atc(a,nil)
$$;
ERROR: value too long for type character varying(10)
do language pllua $$
local a = pgtype.array.bpchar('foo','bar','value ')
local b = pgtype.atc(nil,a)
print(b)
$$;
INFO: (,"{""foo "",""bar "",""value ""}")
-- composite type construction edge cases
do language pllua $$
print(pgtype.ctype3())
print(pgtype.ctype3(nil))
$$;
INFO: (,)
INFO: (,)
do language pllua $$
print(pgtype.ctype3(1)) -- error
$$;
ERROR: pllua: incorrect number of arguments for type constructor (expected 2 got 1)
do language pllua $$
print(pgtype.ctype3(1,2))
$$;
INFO: (1,2)
--end
|