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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
# name: test/sql/function/list/lambdas/reduce.test
# description: Test list_reduce function
# group: [lambdas]
statement ok
SET lambda_syntax='DISABLE_SINGLE_ARROW'
query I
SELECT list_reduce([1, 2, 3], lambda x, y: x + y);
----
6
query I
SELECT list_reduce([1, 2, 3], lambda x, y: x * y);
----
6
query I
SELECT list_reduce([100, 10, 1], lambda x, y, i: x - y - i);
----
84
query I
SELECT list_reduce([1, 2, 3], lambda x, y: y - x);
----
2
query I
SELECT list_reduce([1, 2, 3], lambda x, y: x - y);
----
-4
query I
SELECT list_reduce([1, 2, 3], lambda x, y, i: x + y + i);
----
11
query I
SELECT list_reduce([NULL], lambda x, y, i: x + y + i);
----
NULL
query I
SELECT list_reduce(NULL, lambda x, y, i: x + y + i);
----
NULL
query I
SELECT list_reduce(['Once', 'upon', 'a', 'time'], lambda x, y: x || ' ' || y);
----
Once upon a time
query I
SELECT list_reduce(['a', 'b', 'c', 'd'], lambda x, y, i: x || ' - ' || CAST(i AS VARCHAR) || ' - ' || y);
----
a - 2 - b - 3 - c - 4 - d
#errors on simple reduce
statement error
SELECT list_reduce([], lambda x, y, i: x + y + i);
----
Cannot perform list_reduce on an empty input list
statement error
SELECT list_reduce([1, 2, 3], lambda x, y: (x * y)::VARCHAR || 'please work');
----
Could not convert
statement error
SELECT list_reduce([1, 2], lambda x: x);
----
list_reduce expects a function with 2 or 3 arguments
statement error
SELECT list_reduce([1, 2], NULL);
----
Invalid lambda expression!
statement error
SELECt list_reduce([1, 2], (len('abc') AS x, y) - > x + y)
----
Parser Error: syntax error at or near "AS"
# simple reduce on a table
statement ok
CREATE table t1(a int[]);
statement ok
INSERT INTO t1 VALUES ([1, 2, 3]);
statement ok
INSERT INTO t1 VALUES ([666]);
statement ok
INSERT INTO t1 VALUES (NULL);
statement ok
INSERT INTO t1 VALUES ([44, 55]);
statement ok
INSERT INTO t1 VALUES ([-1, NULL, -2]);
query I
SELECT list_reduce(a, lambda x, y: x + y) FROM t1;
----
6
666
NULL
99
NULL
query I
SELECT list_reduce(a, lambda x, y, i: x + y + i) FROM t1;
----
11
666
NULL
101
NULL
statement ok
INSERT INTO t1 VALUES ([]);
statement error
SELECT list_reduce(a, lambda x, y: x + y) FROM t1;
----
Cannot perform list_reduce on an empty input list
statement ok
DROP TABLE t1;
# reduce on a table with a list of strings
statement ok
CREATE TABLE t1 (a varchar[]);
statement ok
INSERT INTO t1 VALUES (['Once', 'upon', 'a', 'time']), (NULL), (['there', 'was', 'a', 'table']), (['with', 'a', 'list', 'of', 'strings']), (['and', 'it', 'was', NULL]);
query I
SELECT list_reduce(a, lambda x, y: x || ' ' || y) FROM t1;
----
Once upon a time
NULL
there was a table
with a list of strings
NULL
statement ok
INSERT INTO t1 VALUES ([]);
statement error
SELECT list_reduce(a, lambda x, y: x || ' ' || y) FROM t1;
----
Cannot perform list_reduce on an empty input list
# reduce on a string only using the RHS
statement ok
CREATE TABLE right_only (v varchar[], i int);
statement ok
INSERT INTO right_only VALUES (['blue', 'babbling', 'brook'], 1), (['dogs', 'doing', 'dishes'], 2), (['she', 'sells', 'seashells'], 3);
query I
SELECT list_reduce(v, lambda x, y: y[i]) FROM right_only;
----
b
i
a
# nested functions with ints
query I
SELECT list_reduce([1, 2, 3], lambda x, y: list_reduce([4, 5, 6], lambda a, b: x + y + a + b));
----
63
statement error
SELECT list_reduce([1, 2, 3], lambda x, y: list_reduce([], lambda a, b: x + y + a + b));
----
Cannot perform list_reduce on an empty input list
query I
SELECT list_reduce([1, 2, 3], lambda x, y, x_i: list_reduce([4, 5, 6], lambda a, b, a_i: x + y + a + b + x_i + a_i));
----
92
statement error
SELECT list_reduce([1, 2, 3], lambda x, y, x_i: list_reduce([], lambda a, b, a_i: x + y + a + b + x_i + a_i));
----
Cannot perform list_reduce on an empty input list
query I
SELECT list_reduce([[10, 20], [30, 40], [50, 60]], lambda x, y: list_pack(
list_reduce(x, lambda l, m: l + m) +
list_reduce(y, lambda n, o: n + o)));
----
[210]
query I
SELECT list_reduce([[1, 2, 3], [4, 5, 6], [7, 8, 9]], lambda x, y: list_pack(
list_reduce(x, lambda l, m: l + m) +
list_reduce(y, lambda n, o: n + o)));
----
[45]
query I
SELECT list_reduce([[10, 20], [30, 40], NULL, [NULL, 60], NULL], lambda x, y: list_pack(
list_reduce(x, lambda l, m: l + m) +
list_reduce(y, lambda n, o: n + o)));
----
[NULL]
# nested functions with strings
query I
SELECT list_reduce(['a', 'b', 'c', 'd'], lambda x, y: list_reduce(['1', '2', '3', '4'], lambda a, b: x || y || a || b));
----
ababab1234cababab1234cababab1234c1234dababab1234cababab1234cababab1234c1234dababab1234cababab1234cababab1234c1234d1234
query I
SELECT list_reduce([['a', 'b'], ['c', 'd'], ['e', 'f']], lambda x, y: list_pack(list_reduce(x, lambda a, b: a || b) || list_reduce(y, lambda c, d: c || d)));
----
[abcdef]
# nested functions in a table with ints
statement ok
CREATE TABLE nested (n integer[], l integer[])
statement ok
INSERT INTO nested VALUES ([1, 2, 3], [4, 5, 6]), (NULL, NULL), (NULL, [110, 111, 112]), ([77, 88, 99], [55, 66, NULL]);
query I
SELECT list_reduce(n, lambda x, y: list_reduce(l, lambda a, b: x + y + a + b)) FROM nested;
----
63
NULL
NULL
NULL
query I
SELECT list_reduce(n, lambda x, y, x_i: list_reduce(l, lambda a, b, a_i: x + y + a + b + x_i + a_i)) FROM nested;
----
92
NULL
NULL
NULL
query I
SELECT list_reduce(n, lambda x, y, x_i: list_reduce(l, lambda a, b, a_i: x + y + x_i < a + b + a_i)) FROM nested;
----
1
NULL
NULL
NULL
statement ok
INSERT INTO nested VALUES ([4, 5, 6], []);
statement error
SELECT list_reduce(n, lambda x, y: list_reduce(l, lambda a, b: x + y + a + b)) FROM nested;
----
Cannot perform list_reduce on an empty input list
statement ok
DROP TABLE nested;
statement ok
CREATE TABLE nested (n integer[][])
statement ok
INSERT INTO nested VALUES ([[10, 20], [30, 40], [50, 60]]), ([[1,2,3], [4,5,6], [7,8,9]]), (NULL), ([[NULL, 60], [70, NULL], [NULL, NULL]]);
query I
SELECT list_reduce(n, lambda x, y: list_pack(list_reduce(x, lambda l, m: l + m) + list_reduce(y, lambda j, k: j + k))) from nested;
----
[210]
[45]
NULL
[NULL]
statement ok
INSERT INTO nested VALUES ([[4, 5, 6], []]);
statement error
SELECT list_reduce(n, lambda x, y: list_pack(list_reduce(x, lambda l, m: l + m) + list_reduce(y, lambda j, k: j + k))) from nested;
----
Cannot perform list_reduce on an empty input list
statement ok
DROP TABLE nested;
# nested functions in a table with strings
statement ok
CREATE TABLE nested (n varchar[], l varchar[])
statement ok
INSERT INTO nested VALUES (['a', 'b', 'c', 'd'], ['1', '2', '3', '4']), (NULL, NULL), (NULL, ['110', '111', '112']), (['77', '88', '99'], ['55', '66', NULL]);
query I
SELECT list_reduce(n, lambda x, y: list_reduce(l, lambda a, b: x || y || a || b)) FROM nested;
----
ababab1234cababab1234cababab1234c1234dababab1234cababab1234cababab1234c1234dababab1234cababab1234cababab1234c1234d1234
NULL
NULL
NULL
statement ok
DROP TABLE nested;
# three level nested functions with ints
query I
SELECT
list_reduce([1, 2, 3], lambda x, y:
list_reduce([4, 5, 6], lambda a, b:
list_reduce([7, 8, 9], lambda c, d: x + y + a + b + c + d)));
----
966
query I
SELECT
list_reduce([1, 2, 3], lambda x, y, x_i:
list_reduce([4, 5, 6], lambda a, b, a_i:
list_reduce([7, 8, 9], lambda c, d, c_i: x + y + a + b + c + d + x_i + a_i + c_i)));
----
1259
query I
SELECT
list_reduce([[[10, 20], [100, 200]], [[30, 40], [300, 400]], [[50, 60], [500, 600]]], lambda x, y: list_pack(
list_reduce(x, lambda l, m: list_pack(
list_reduce(l, lambda a, b: a + b) +
list_reduce(m, lambda c, d: c + d))) +
list_reduce(y, lambda n, o: list_pack(
list_reduce(n, lambda a, b: a + b) +
list_reduce(o, lambda c, d: c + d)))));
----
[[330, 770, 1210]]
# three level nested in a table with ints
statement ok
CREATE TABLE nested (n integer[], l integer[], m integer[])
statement ok
INSERT INTO nested VALUES ([1, 2, 3], [4, 5, 6], [7, 8, 9]), (NULL, NULL, NULL), (NULL, [110, 111, 112], [113, 114, 115]), ([77, 88, 99], [55, 66, NULL], [44, 33, 22]);
query I
SELECT
list_reduce(n, lambda x, y:
list_reduce(l, lambda a, b:
list_reduce(m, lambda c, d: x + y + a + b + c + d)))
FROM nested;
----
966
NULL
NULL
NULL
statement ok
DROP TABLE nested;
statement ok
CREATE TABLE nested (n integer[][][])
statement ok
INSERT INTO nested VALUES ([[[10, 20], [100, 200]], [[30, 40], [300, 400]], [[50, 60], [500, 600]]]), ([[[1,2,3], [4,5,6], [7,8,9]]]), (NULL), ([[[NULL, 60], [70, NULL], [NULL, NULL]]]);
query I
SELECT
list_reduce(n, lambda x, y:
list_pack(list_reduce(x, lambda l, m:
list_pack(list_reduce(l, lambda a, b: a + b) +
list_reduce(m, lambda c, d: c + d))) +
list_reduce(y, lambda e, f: list_pack(
list_reduce(e, lambda a, b: a + b) +
list_reduce(f, lambda c, d: c + d)))))
FROM nested;
----
[[330, 770, 1210]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
NULL
[[NULL, 60], [70, NULL], [NULL, NULL]]
# list_reduce in where clause
statement ok
CREATE table where_clause (a int[]);
statement ok
INSERT INTO where_clause VALUES ([10, 2, 1]), ([1, 2, 3]), ([15, 4, 3]), ([3, 4, 5]), ([11, 2, 3, 4, 5]), ([5, 4, 3, 2, 1]), ([100, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
query I
SELECT a FROM where_clause WHERE list_reduce(a, lambda x, y: x - y) > 0;
----
[10, 2, 1]
[15, 4, 3]
[100, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# tests on structs
statement ok
CREATE TABLE t_struct (s STRUCT(v VARCHAR, i INTEGER)[]);
statement ok
INSERT INTO t_struct VALUES ([row('a', 1), row('b', 2)]), ([row('c', 3), row('d', 4)]), ([row('e', 5), row('f', 6)]), ([row('g', 7), row('h', 8)]), ([row('i', 9), row('j', 10)]);
query I
SELECT list_reduce(s, lambda a, b: row(a.v || b.v, a.i + b.i)) FROM t_struct;
----
{'v': ab, 'i': 3}
{'v': cd, 'i': 7}
{'v': ef, 'i': 11}
{'v': gh, 'i': 15}
{'v': ij, 'i': 19}
# issue #11142
statement ok
CREATE OR REPLACE TABLE df(s STRUCT(a INT, b INT)[]);
statement ok
INSERT INTO df VALUES ([row(0, 0), row(0, 1), row(0, 2)]);
query I
SELECT list_reduce(s, lambda curr, next: struct_pack(a:=curr.a + (next.b - curr.b), b:=next.b))
FROM df
----
{'a': 2, 'b': 2}
query I
SELECT
list_reduce(
[struct_pack(a := 0, b := 0), struct_pack(a := 0, b := 1), struct_pack(a := 0, b := 2)],
lambda curr, next: struct_pack(a := curr.a + (next.b - curr.b), b := next.b)
)
----
{'a': 2, 'b': 2}
|