mirror of https://github.com/citusdata/citus.git
348 lines
8.1 KiB
Plaintext
348 lines
8.1 KiB
Plaintext
--
|
|
-- MULTI_COMPLEX_EXPRESSIONS
|
|
--
|
|
-- Check that we can correctly handle complex expressions and aggregates.
|
|
SELECT sum(l_quantity) / avg(l_quantity) FROM lineitem;
|
|
?column?
|
|
------------------------
|
|
12000.0000000000000000
|
|
(1 row)
|
|
|
|
SELECT sum(l_quantity) / (10 * avg(l_quantity)) FROM lineitem;
|
|
?column?
|
|
-----------------------
|
|
1200.0000000000000000
|
|
(1 row)
|
|
|
|
SELECT (sum(l_quantity) / (10 * avg(l_quantity))) + 11 FROM lineitem;
|
|
?column?
|
|
-----------------------
|
|
1211.0000000000000000
|
|
(1 row)
|
|
|
|
SELECT avg(l_quantity) as average FROM lineitem;
|
|
average
|
|
---------------------
|
|
25.4462500000000000
|
|
(1 row)
|
|
|
|
SELECT 100 * avg(l_quantity) as average_times_hundred FROM lineitem;
|
|
average_times_hundred
|
|
-----------------------
|
|
2544.6250000000000000
|
|
(1 row)
|
|
|
|
SELECT 100 * avg(l_quantity) / 10 as average_times_ten FROM lineitem;
|
|
average_times_ten
|
|
----------------------
|
|
254.4625000000000000
|
|
(1 row)
|
|
|
|
SELECT l_quantity, 10 * count(*) count_quantity FROM lineitem
|
|
GROUP BY l_quantity ORDER BY count_quantity, l_quantity;
|
|
l_quantity | count_quantity
|
|
------------+----------------
|
|
44.00 | 2150
|
|
38.00 | 2160
|
|
45.00 | 2180
|
|
13.00 | 2190
|
|
47.00 | 2200
|
|
29.00 | 2220
|
|
36.00 | 2230
|
|
49.00 | 2230
|
|
3.00 | 2270
|
|
35.00 | 2280
|
|
18.00 | 2290
|
|
31.00 | 2290
|
|
43.00 | 2290
|
|
14.00 | 2300
|
|
16.00 | 2300
|
|
17.00 | 2300
|
|
26.00 | 2300
|
|
7.00 | 2320
|
|
10.00 | 2340
|
|
34.00 | 2340
|
|
15.00 | 2350
|
|
25.00 | 2360
|
|
33.00 | 2360
|
|
42.00 | 2360
|
|
2.00 | 2370
|
|
12.00 | 2410
|
|
37.00 | 2410
|
|
6.00 | 2420
|
|
22.00 | 2420
|
|
1.00 | 2430
|
|
19.00 | 2430
|
|
4.00 | 2440
|
|
20.00 | 2460
|
|
48.00 | 2460
|
|
41.00 | 2470
|
|
24.00 | 2490
|
|
27.00 | 2490
|
|
8.00 | 2500
|
|
11.00 | 2500
|
|
5.00 | 2540
|
|
21.00 | 2550
|
|
32.00 | 2550
|
|
9.00 | 2580
|
|
39.00 | 2600
|
|
46.00 | 2600
|
|
50.00 | 2600
|
|
23.00 | 2610
|
|
30.00 | 2640
|
|
40.00 | 2690
|
|
28.00 | 2730
|
|
(50 rows)
|
|
|
|
-- Check that we can handle complex select clause expressions.
|
|
SELECT count(*) FROM lineitem
|
|
WHERE octet_length(l_comment || l_comment) > 40;
|
|
count
|
|
-------
|
|
8148
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem
|
|
WHERE octet_length(concat(l_comment, l_comment)) > 40;
|
|
count
|
|
-------
|
|
8148
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem
|
|
WHERE octet_length(l_comment) + octet_length('randomtext'::text) > 40;
|
|
count
|
|
-------
|
|
4611
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem
|
|
WHERE octet_length(l_comment) + 10 > 40;
|
|
count
|
|
-------
|
|
4611
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem
|
|
WHERE (l_receiptdate::timestamp - l_shipdate::timestamp) > interval '5 days';
|
|
count
|
|
-------
|
|
10008
|
|
(1 row)
|
|
|
|
-- can push down queries where no columns present on the WHERE clause
|
|
SELECT count(*) FROM lineitem WHERE random() = -0.1;
|
|
count
|
|
-------
|
|
0
|
|
(1 row)
|
|
|
|
-- boolean tests can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE (l_partkey > 10000) is true;
|
|
count
|
|
-------
|
|
11423
|
|
(1 row)
|
|
|
|
-- scalar array operator expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE l_partkey = ANY(ARRAY[19353, 19354, 19355]);
|
|
count
|
|
-------
|
|
1
|
|
(1 row)
|
|
|
|
-- some more scalar array operator expressions
|
|
SELECT count(*) FROM lineitem
|
|
WHERE l_partkey = ALL(ARRAY[19353]);
|
|
count
|
|
-------
|
|
1
|
|
(1 row)
|
|
|
|
-- operator expressions involving arrays
|
|
SELECT count(*) FROM lineitem
|
|
WHERE ARRAY[19353, 19354, 19355] @> ARRAY[l_partkey];
|
|
count
|
|
-------
|
|
1
|
|
(1 row)
|
|
|
|
-- coerced via io expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE (l_quantity/100)::int::bool::text::bool;
|
|
count
|
|
-------
|
|
260
|
|
(1 row)
|
|
|
|
-- case expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE (CASE WHEN l_orderkey > 4000 THEN l_partkey / 100 > 1 ELSE false END);
|
|
count
|
|
-------
|
|
7948
|
|
(1 row)
|
|
|
|
-- coalesce expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE COALESCE((l_partkey/50000)::bool, false);
|
|
count
|
|
-------
|
|
9122
|
|
(1 row)
|
|
|
|
-- nullif expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE NULLIF((l_partkey/50000)::bool, false);
|
|
count
|
|
-------
|
|
9122
|
|
(1 row)
|
|
|
|
-- null test expressions can be pushed down
|
|
SELECT count(*) FROM orders
|
|
WHERE o_comment IS NOT null;
|
|
count
|
|
-------
|
|
2984
|
|
(1 row)
|
|
|
|
-- functions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE isfinite(l_shipdate);
|
|
count
|
|
-------
|
|
12000
|
|
(1 row)
|
|
|
|
-- constant expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE 0 != 0;
|
|
count
|
|
-------
|
|
0
|
|
(1 row)
|
|
|
|
-- distinct expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE l_partkey IS DISTINCT FROM 50040;
|
|
count
|
|
-------
|
|
11999
|
|
(1 row)
|
|
|
|
-- row compare expression can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE row(l_partkey, 2, 3) > row(2000, 2, 3);
|
|
count
|
|
-------
|
|
11882
|
|
(1 row)
|
|
|
|
-- combination of different expressions can be pushed down
|
|
SELECT count(*) FROM lineitem
|
|
WHERE
|
|
(l_quantity/100)::int::bool::text::bool AND
|
|
CASE WHEN l_orderkey > 4000 THEN l_partkey / 100 > 1 ELSE false END AND
|
|
COALESCE((l_partkey/50000)::bool, false) AND
|
|
NULLIF((l_partkey/50000)::bool, false) AND
|
|
isfinite(l_shipdate) AND
|
|
l_partkey IS DISTINCT FROM 50040 AND
|
|
row(l_partkey, 2, 3) > row(2000, 2, 3);
|
|
count
|
|
-------
|
|
137
|
|
(1 row)
|
|
|
|
-- constant expression in the WHERE clause with a column in the target list
|
|
SELECT l_linenumber FROM lineitem
|
|
WHERE
|
|
1!=0
|
|
ORDER BY
|
|
l_linenumber
|
|
LIMIT 1;
|
|
l_linenumber
|
|
--------------
|
|
1
|
|
(1 row)
|
|
|
|
-- constant expression in the WHERE clause with expressions and a column the target list
|
|
SELECT count(*) * l_discount as total_discount, count(*), sum(l_tax), l_discount FROM lineitem
|
|
WHERE
|
|
1!=0
|
|
GROUP BY
|
|
l_discount
|
|
ORDER BY
|
|
total_discount DESC, sum(l_tax) DESC;
|
|
total_discount | count | sum | l_discount
|
|
----------------+-------+-------+------------
|
|
104.80 | 1048 | 41.08 | 0.10
|
|
98.55 | 1095 | 44.15 | 0.09
|
|
90.64 | 1133 | 45.94 | 0.08
|
|
71.05 | 1015 | 41.19 | 0.07
|
|
69.42 | 1157 | 45.75 | 0.06
|
|
53.60 | 1072 | 42.82 | 0.05
|
|
43.64 | 1091 | 44.40 | 0.04
|
|
32.55 | 1085 | 43.30 | 0.03
|
|
22.22 | 1111 | 45.07 | 0.02
|
|
11.22 | 1122 | 44.54 | 0.01
|
|
0.00 | 1071 | 44.00 | 0.00
|
|
(11 rows)
|
|
|
|
-- distinct expressions in the WHERE clause with a column in the target list
|
|
SELECT l_linenumber FROM lineitem
|
|
WHERE
|
|
l_linenumber IS DISTINCT FROM 1 AND
|
|
l_orderkey IS DISTINCT FROM 8997
|
|
ORDER BY
|
|
l_linenumber
|
|
LIMIT 1;
|
|
l_linenumber
|
|
--------------
|
|
2
|
|
(1 row)
|
|
|
|
-- distinct expressions in the WHERE clause with expressions and a column the target list
|
|
SELECT max(l_linenumber), min(l_discount), l_receiptdate FROM lineitem
|
|
WHERE
|
|
l_linenumber IS DISTINCT FROM 1 AND
|
|
l_orderkey IS DISTINCT FROM 8997
|
|
GROUP BY
|
|
l_receiptdate
|
|
ORDER BY
|
|
l_receiptdate
|
|
LIMIT 1;
|
|
max | min | l_receiptdate
|
|
-----+------+---------------
|
|
3 | 0.07 | 01-09-1992
|
|
(1 row)
|
|
|
|
-- Check that we can handle implicit and explicit join clause definitions.
|
|
SELECT count(*) FROM lineitem, orders
|
|
WHERE l_orderkey = o_orderkey AND l_quantity < 5;
|
|
count
|
|
-------
|
|
951
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem
|
|
JOIN orders ON l_orderkey = o_orderkey AND l_quantity < 5;
|
|
count
|
|
-------
|
|
951
|
|
(1 row)
|
|
|
|
SELECT count(*) FROM lineitem JOIN orders ON l_orderkey = o_orderkey
|
|
WHERE l_quantity < 5;
|
|
count
|
|
-------
|
|
951
|
|
(1 row)
|
|
|
|
-- Check that we make sure local joins are between columns only.
|
|
SELECT count(*) FROM lineitem, orders WHERE l_orderkey + 1 = o_orderkey;
|
|
ERROR: cannot perform local joins that involve expressions
|
|
DETAIL: local joins can be performed between columns only
|