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
|
--
CREATE EXTENSION pllua;
CREATE EXTENSION plluau;
\set VERBOSITY terse
-- smoke test
do language pllua $$ print "hello world!" $$;
INFO: hello world!
do language plluau $$ print "hello world!" $$;
INFO: hello world!
create function pg_temp.f1() returns text language pllua as $$ return "hello world" $$;
select pg_temp.f1();
f1
-------------
hello world
(1 row)
create function pg_temp.f2() returns text language plluau as $$ return "hello world" $$;
select pg_temp.f2();
f2
-------------
hello world
(1 row)
-- Rest of this file concentrates on simple tests of code paths in
-- compile, exec, and interpreter setup. Tests of other parts of the
-- module are separate.
-- validator
create function pg_temp."bad name"() returns text language pllua as $$ $$;
ERROR: PL/Lua function name "bad name" is not a valid Lua identifier
create function pg_temp.f3("bad arg" integer) returns text language pllua as $$ $$;
ERROR: PL/Lua argument name "bad arg" is not a valid Lua identifier
-- simple params and results (see types.sql for detailed checks)
create function pg_temp.f4(a integer) returns integer language pllua as $$ return a + 1 $$;
select pg_temp.f4(1);
f4
----
2
(1 row)
create function pg_temp.f5(a text) returns text language pllua as $$ return a.."bar" $$;
select pg_temp.f5('foo');
f5
--------
foobar
(1 row)
create function pg_temp.f6(a text, b integer) returns text language pllua as $$ return a..b $$;
select pg_temp.f6('foo',1);
f6
------
foo1
(1 row)
-- try some polymorphism too
create function pg_temp.f7(a anyelement) returns anyelement language pllua as $$ return a $$;
select pg_temp.f7(text 'foo');
f7
-----
foo
(1 row)
select pg_temp.f7(json '{"foo":1}');
f7
-----------
{"foo":1}
(1 row)
--select pg_temp.f7(xml '<foo>bar</foo>'); -- don't bother with this, might be compiled out
select pg_temp.f7(varchar 'foo');
f7
-----
foo
(1 row)
select 'x',pg_temp.f7('foo'::char(20)),'x';
?column? | f7 | ?column?
----------+----------------------+----------
x | foo | x
(1 row)
select pg_temp.f7(cstring 'foo');
f7
-----
foo
(1 row)
select pg_temp.f7(name 'foo');
f7
-----
foo
(1 row)
select pg_temp.f7(bytea 'foo\000bar');
f7
------------------
\x666f6f00626172
(1 row)
select pg_temp.f7(smallint '2');
f7
----
2
(1 row)
select pg_temp.f7(integer '2');
f7
----
2
(1 row)
select pg_temp.f7(bigint '123456789012345');
f7
-----------------
123456789012345
(1 row)
select pg_temp.f7(oid '10');
f7
----
10
(1 row)
select pg_temp.f7(oid '4294967295');
f7
------------
4294967295
(1 row)
select pg_temp.f7(true);
f7
----
t
(1 row)
select pg_temp.f7(false);
f7
----
f
(1 row)
select pg_temp.f7(1.5::float8);
f7
-----
1.5
(1 row)
select pg_temp.f7(1.5::float4);
f7
-----
1.5
(1 row)
-- variadics
create function pg_temp.f8(a text, variadic b integer[]) returns void language pllua as $$ print(a,type(b),b) $$;
select pg_temp.f8('foo', 1, 2, 3);
INFO: foo userdata {1,2,3}
f8
----
(1 row)
create function pg_temp.f9(a integer, variadic b text[]) returns void language pllua as $$ print(a,type(b),b) $$;
select pg_temp.f9(1, 'foo', 'bar', 'baz');
INFO: 1 userdata {foo,bar,baz}
f9
----
(1 row)
create function pg_temp.f10(a integer, variadic "any") returns void language pllua as $$ print(a,...) $$;
select pg_temp.f10(1, 'foo', 2, 'baz');
INFO: 1 foo 2 baz
f10
-----
(1 row)
-- SRF code paths
create function pg_temp.f11(a integer) returns setof text
language pllua as $$ return $$; -- 0 rows
select * from pg_temp.f11(1);
f11
-----
(0 rows)
create function pg_temp.f11b(a integer) returns setof text
language pllua as $$ return 'foo' $$; -- 1 row
select * from pg_temp.f11b(1);
f11b
------
foo
(1 row)
create function pg_temp.f12(a integer) returns setof text
language pllua as $$ coroutine.yield() $$; -- 1 row, null
select * from pg_temp.f12(1);
f12
-----
(1 row)
create function pg_temp.f13(a integer) returns setof text
language pllua as $$ for i = 1,a do coroutine.yield("row "..i) end $$;
select * from pg_temp.f13(4);
f13
-------
row 1
row 2
row 3
row 4
(4 rows)
create function pg_temp.f14(a integer, out x text, out y integer) returns setof record
language pllua as $$ for i = 1,a do coroutine.yield("row "..i, i) end $$;
select * from pg_temp.f14(4);
x | y
-------+---
row 1 | 1
row 2 | 2
row 3 | 3
row 4 | 4
(4 rows)
create function pg_temp.f15(a integer) returns table(x text, y integer)
language pllua as $$ for i = 1,a do coroutine.yield("row "..i, i) end $$;
select * from pg_temp.f15(4);
x | y
-------+---
row 1 | 1
row 2 | 2
row 3 | 3
row 4 | 4
(4 rows)
create function pg_temp.f16(a inout integer, x out text) returns setof record
language pllua as $$ for i = 1,a do coroutine.yield(i, "row "..i) end $$;
select * from pg_temp.f16(4);
a | x
---+-------
1 | row 1
2 | row 2
3 | row 3
4 | row 4
(4 rows)
-- SRF vs null returns
create function pg_temp.f16b(a integer) returns table(x text, y integer)
language pllua as $$ coroutine.yield() $$; -- 1 row, null
select * from pg_temp.f16b(1);
x | y
---+---
|
(1 row)
create function pg_temp.f16c(a integer) returns table(x text, y integer)
language pllua as $$ coroutine.yield() for i = 1,a do coroutine.yield('foo',i) end $$;
select * from pg_temp.f16c(3);
x | y
-----+---
|
foo | 1
foo | 2
foo | 3
(4 rows)
-- compiler and validator code paths
do language pllua $$ _G.rdepth = 40 $$; -- global var hack
-- This function will try and call itself at a point where it is visible
-- but has no definition interned yet; the recursive call will likewise
-- not see an interned definition and recurses again. without any limits
-- this would hit a stack depth check somewhere; we eat about 3 levels of
-- C function recursion inside lua each time, and that gets capped at 200.
-- We don't expect this to be actually useful, the test is just that we
-- don't crash.
create function pg_temp.f17(a integer) returns integer language pllua
as $$
return a
end
do
if _G.rdepth > 0 then
_G.rdepth = _G.rdepth - 1
u = spi.execute("select pg_temp.f17(1)")
end
$$;
select pg_temp.f17(1);
f17
-----
1
(1 row)
create type pg_temp.t1 as (a integer, b text);
create function pg_temp.f18(a integer, b text) returns pg_temp.t1
language pllua as $$ return a,b $$;
select * from pg_temp.f18(123,'foo');
a | b
-----+-----
123 | foo
(1 row)
create function pg_temp.f19(a integer) returns text language pllua as $$ return 'foo '..a $$;
select pg_temp.f19(2);
f19
-------
foo 2
(1 row)
create or replace function pg_temp.f19(a integer) returns text language pllua as $$ return 'bar '..a $$;
select pg_temp.f19(3);
f19
-------
bar 3
(1 row)
-- trusted interpreter setup
-- check we really do have different interpreters
-- this is hard because we intentionally isolate trusted-language code
-- from the normal global env of its interpreter, so we would only be
-- able to verify isolation if we were able to break out of the
-- sandbox, which would rather defeat the point. We have to take the
-- outside view, by generating an interpreter-dependent value and
-- checking that it differs. The stringification of a closure, such as
-- server.error, suffices since this contains an interpreter-dependent
-- address (whereas base C functions do not differ between
-- interpreters in recent lua versions).
create function pg_temp.f20() returns text language pllua as $$ return tostring(spi.error) $$;
create function pg_temp.f21() returns text language plluau as $$ return tostring(spi.error) $$;
select pg_temp.f20() as a intersect select pg_temp.f21(); -- should be empty
a
---
(0 rows)
-- check the global table
do language pllua $$
local gk = { "io", "dofile", "debug" } -- must not exist
for i = 1,#gk do print(gk[i],type(_G[gk[i]])) end
$$;
INFO: io nil
INFO: dofile nil
INFO: debug nil
do language plluau $$
local gk = { "io", "dofile" } -- probably exist
for i = 1,#gk do print(gk[i],type(_G[gk[i]])) end
$$;
INFO: io table
INFO: dofile function
-- check that trusted gets only the restricted os module, even from
-- require
do language pllua $$
local os = require 'os'
local gk = { "time", "difftime", "execute", "getenv", "exit" }
for i = 1,#gk do print(gk[i],type(os[gk[i]])) end
$$;
INFO: time function
INFO: difftime function
INFO: execute nil
INFO: getenv nil
INFO: exit nil
-- check that trusted can't require dangerous core modules
do language pllua $$
print((lpcall(require,"debug")))
print((lpcall(require,"io")))
$$;
INFO: false
INFO: false
--end
|