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
|
--
\set VERBOSITY terse
--
--
-- By having a real table with both short and long array values,
-- we get to involve both short varlenas and external toast.
--
create temp table adata (id serial,
a integer[],
b text[],
c numeric[],
d date[]);
insert into adata(a) values
(array[1,2]),
(array[10,20,30,40,50]),
(array(select i from generate_series(1,100) i)),
(array(select i from generate_series(1,100000) i)),
('{}');
insert into adata(b) values
('{}'),
(array['foo','bar','baz']),
(array(select 'val'||i from generate_series(1,100) i)),
(array(select 'val'||i from generate_series(1,10000) i));
insert into adata(c) values
('{}'),
(array[1.234,exp(1.0::numeric(32,30)),factorial(27)]),
(array(select i from generate_series(1,100) i)),
(array(select i from generate_series(1,10000) i));
insert into adata(d) values
('{}'),
(array[date '2017-12-11', '1968-05-10', '1983-09-26', '1962-10-27']),
(array(select date '2017-01-01' + i from generate_series(0,364,18) i));
do language pllua $$
package.preload['myutil'] = function()
local expect_next =
{ string = function(s)
return string.gsub(s, "%d+$",
function(n)
return string.format("%d", n + 1)
end)
end,
number = function(n) return n + 1 end,
[pgtype.numeric] = function(n) return n + 1 end,
}
local function map(a,f)
local r = {}
for i = 1,#a do r[#r+1] = f(a[i]) end
return r
end
local function summarize(a)
if a == nil then return nil end
local expect,first_match,result = nil,nil,{}
for i = 1,#a do
if first_match == nil then
expect,first_match = a[i],i
elseif a[i] ~= expect then
if first_match < i-1 then
result[#result+1] = { a[first_match], a[i-1] }
else
result[#result+1] = a[i-1]
end
expect,first_match = a[i],i
end
--[[ update the "expected" next element ]]
expect = (expect_next[pgtype(expect) or type(expect)]
or function(x) return x end)(expect)
end
if first_match == #a then
result[#result+1] = a[#a]
elseif first_match ~= nil then
result[#result+1] = { a[first_match], a[#a] }
end
return table.concat(map(result, function(e)
if type(e)=='table' then
return string.format("[%s..%s]",
tostring(e[1]),
tostring(e[2]))
else
return tostring(e)
end
end),
',')
end
return {
map = map,
summarize = summarize
}
end
$$;
do language pllua $$
local u = require 'myutil'
for r in spi.rows([[ select a, b,
array_append(a, -1) as xa,
array_append(b, 'wombat') as xb
from adata
where a is not null or b is not null
order by id ]])
do
print(u.summarize(r.a),u.summarize(r.b))
print(u.summarize(r.xa),u.summarize(r.xb))
end
for r in spi.rows([[ select c,
array_append(c, -1.0) as xc
from adata
where c is not null
order by id ]])
do
print(u.summarize(r.c))
print(u.summarize(r.xc))
end
for r in spi.rows([[ select d
from adata
where d is not null
order by id ]])
do
print(r.d)
end
$$;
INFO: [1..2] nil
INFO: [1..2],-1 wombat
INFO: 10,20,30,40,50 nil
INFO: 10,20,30,40,50,-1 wombat
INFO: [1..100] nil
INFO: [1..100],-1 wombat
INFO: [1..100000] nil
INFO: [1..100000],-1 wombat
INFO: nil
INFO: -1 wombat
INFO: nil
INFO: -1 wombat
INFO: nil foo,bar,baz
INFO: -1 foo,bar,baz,wombat
INFO: nil [val1..val100]
INFO: -1 [val1..val100],wombat
INFO: nil [val1..val10000]
INFO: -1 [val1..val10000],wombat
INFO:
INFO: -1.0
INFO: 1.234,2.718281828459045235360287471353,10888869450418352160768000000
INFO: 1.234,2.718281828459045235360287471353,10888869450418352160768000000,-1.0
INFO: [1..100]
INFO: [1..100],-1.0
INFO: [1..10000]
INFO: [1..10000],-1.0
INFO: {}
INFO: {12-11-2017,05-10-1968,09-26-1983,10-27-1962}
INFO: {01-01-2017,01-19-2017,02-06-2017,02-24-2017,03-14-2017,04-01-2017,04-19-2017,05-07-2017,05-25-2017,06-12-2017,06-30-2017,07-18-2017,08-05-2017,08-23-2017,09-10-2017,09-28-2017,10-16-2017,11-03-2017,11-21-2017,12-09-2017,12-27-2017}
create function af1(a anyarray)
returns text
language pllua
stable
as $$
return tostring(u.summarize(a))
end
u = require 'myutil'
do
$$;
-- array_append returns its result as an expanded datum
select af1(a) from adata where a is not null order by id;
af1
----------------
[1..2]
10,20,30,40,50
[1..100]
[1..100000]
(5 rows)
with t as (select a from adata where a is not null order by id)
select af1(array_append(a, -1)) from t;
af1
-------------------
[1..2],-1
10,20,30,40,50,-1
[1..100],-1
[1..100000],-1
-1
(5 rows)
select af1(b) from adata where b is not null order by id;
af1
------------------
foo,bar,baz
[val1..val100]
[val1..val10000]
(4 rows)
with t as (select b from adata where b is not null order by id)
select af1(array_append(b, 'wombat')) from t;
af1
-------------------------
wombat
foo,bar,baz,wombat
[val1..val100],wombat
[val1..val10000],wombat
(4 rows)
select af1(c) from adata where c is not null order by id;
af1
----------------------------------------------------------------------
1.234,2.718281828459045235360287471353,10888869450418352160768000000
[1..100]
[1..10000]
(4 rows)
with t as (select c from adata where c is not null order by id)
select af1(array_append(c, -1.0)) from t;
af1
---------------------------------------------------------------------------
-1.0
1.234,2.718281828459045235360287471353,10888869450418352160768000000,-1.0
[1..100],-1.0
[1..10000],-1.0
(4 rows)
select af1(d) from adata where d is not null order by id;
af1
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
12-11-2017,05-10-1968,09-26-1983,10-27-1962
01-01-2017,01-19-2017,02-06-2017,02-24-2017,03-14-2017,04-01-2017,04-19-2017,05-07-2017,05-25-2017,06-12-2017,06-30-2017,07-18-2017,08-05-2017,08-23-2017,09-10-2017,09-28-2017,10-16-2017,11-03-2017,11-21-2017,12-09-2017,12-27-2017
(3 rows)
with t as (select d from adata where d is not null order by id)
select af1(array_append(d, date '1970-01-01')) from t;
af1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
01-01-1970
12-11-2017,05-10-1968,09-26-1983,10-27-1962,01-01-1970
01-01-2017,01-19-2017,02-06-2017,02-24-2017,03-14-2017,04-01-2017,04-19-2017,05-07-2017,05-25-2017,06-12-2017,06-30-2017,07-18-2017,08-05-2017,08-23-2017,09-10-2017,09-28-2017,10-16-2017,11-03-2017,11-21-2017,12-09-2017,12-27-2017,01-01-1970
(3 rows)
-- conversion edge cases
create function pg_temp.af2() returns integer[] language pllua
as $$
return nil
$$;
select pg_temp.af2();
af2
-----
(1 row)
create function pg_temp.af3() returns integer[] language pllua
as $$
return
$$;
select pg_temp.af3();
af3
-----
(1 row)
create function pg_temp.af4() returns integer[] language pllua
as $$
return 1,2
$$;
select pg_temp.af4();
af4
-------
{1,2}
(1 row)
create function pg_temp.af5() returns integer[] language pllua
as $$
return pgtype(nil,0)()
$$;
select pg_temp.af5();
af5
-----
{}
(1 row)
create function pg_temp.af5b() returns integer[] language pllua
as $$
return {}
$$;
select pg_temp.af5b();
af5b
------
{}
(1 row)
create function pg_temp.af6() returns integer[] language pllua
as $$
return { 1, nil, 3 }
$$;
select pg_temp.af6();
af6
------------
{1,NULL,3}
(1 row)
create function pg_temp.af7() returns integer[] language pllua
as $$
return pgtype.integer(1)
$$;
select pg_temp.af7();
af7
-----
{1}
(1 row)
create function pg_temp.af8() returns integer[] language pllua
as $$
return { pgtype.integer(1) }
$$;
select pg_temp.af8();
af8
-----
{1}
(1 row)
create type acomp1 as (foo integer, bar text);
create function pg_temp.af9() returns acomp1[] language pllua
as $$
return { { foo = 1, bar = "zot" } }
$$;
select pg_temp.af9();
af9
-------------
{"(1,zot)"}
(1 row)
create function pg_temp.af10() returns acomp1[] language pllua
as $$
return { pgtype(nil,0):element()(1, "zot") }
$$;
select pg_temp.af10();
af10
-------------
{"(1,zot)"}
(1 row)
--
|