mirror of https://github.com/citusdata/citus.git
460 lines
12 KiB
Plaintext
460 lines
12 KiB
Plaintext
--
|
|
-- MULTI_COMPLEX_EXPRESSIONS
|
|
--
|
|
ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 420000;
|
|
ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 420000;
|
|
-- 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
|
|
-------
|
|
|
|
(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
|
|
-- Check that we can issue limit/offset queries
|
|
-- OFFSET in subqueries are not supported
|
|
-- Error in the planner when subquery pushdown is off
|
|
SELECT * FROM (SELECT o_orderkey FROM orders ORDER BY o_orderkey OFFSET 20) sq;
|
|
ERROR: cannot perform distributed planning on this query
|
|
DETAIL: Subqueries with offset are not supported yet
|
|
SET citus.subquery_pushdown TO true;
|
|
-- Error in the optimizer when subquery pushdown is on
|
|
SELECT * FROM (SELECT o_orderkey FROM orders ORDER BY o_orderkey OFFSET 20) sq;
|
|
ERROR: cannot push down this subquery
|
|
DETAIL: Offset clause is currently unsupported
|
|
SET citus.subquery_pushdown TO false;
|
|
-- Simple LIMIT/OFFSET with ORDER BY
|
|
SELECT o_orderkey FROM orders ORDER BY o_orderkey LIMIT 10 OFFSET 20;
|
|
o_orderkey
|
|
------------
|
|
69
|
|
70
|
|
71
|
|
96
|
|
97
|
|
98
|
|
99
|
|
100
|
|
101
|
|
102
|
|
(10 rows)
|
|
|
|
-- LIMIT/OFFSET with a subquery
|
|
SET citus.task_executor_type TO 'task-tracker';
|
|
SELECT
|
|
customer_keys.o_custkey,
|
|
SUM(order_count) AS total_order_count
|
|
FROM
|
|
(SELECT o_custkey, o_orderstatus, COUNT(*) AS order_count
|
|
FROM orders GROUP BY o_custkey, o_orderstatus ) customer_keys
|
|
GROUP BY
|
|
customer_keys.o_custkey
|
|
ORDER BY
|
|
customer_keys.o_custkey DESC
|
|
LIMIT 10 OFFSET 20;
|
|
o_custkey | total_order_count
|
|
-----------+-------------------
|
|
1466 | 1
|
|
1465 | 2
|
|
1463 | 4
|
|
1462 | 10
|
|
1460 | 1
|
|
1459 | 6
|
|
1457 | 1
|
|
1456 | 3
|
|
1454 | 2
|
|
1453 | 5
|
|
(10 rows)
|
|
|
|
SET citus.task_executor_type TO 'real-time';
|
|
SET client_min_messages TO DEBUG1;
|
|
-- Ensure that we push down LIMIT and OFFSET properly
|
|
-- No Group-By -> Push Down
|
|
CREATE TEMP TABLE temp_limit_test_1 AS
|
|
SELECT o_custkey FROM orders LIMIT 10 OFFSET 15;
|
|
DEBUG: push down of limit count: 25
|
|
-- GROUP BY without ORDER BY -> No push-down
|
|
CREATE TEMP TABLE temp_limit_test_2 AS
|
|
SELECT o_custkey FROM orders GROUP BY o_custkey LIMIT 10 OFFSET 15;
|
|
-- GROUP BY and ORDER BY non-aggregate -> push-down
|
|
CREATE TEMP TABLE temp_limit_test_3 AS
|
|
SELECT o_custkey FROM orders GROUP BY o_custkey ORDER BY o_custkey LIMIT 10 OFFSET 15;
|
|
DEBUG: push down of limit count: 25
|
|
-- GROUP BY and ORDER BY aggregate -> No push-down
|
|
CREATE TEMP TABLE temp_limit_test_4 AS
|
|
SELECT o_custkey, COUNT(*) AS ccnt FROM orders GROUP BY o_custkey ORDER BY ccnt DESC LIMIT 10 OFFSET 15;
|
|
-- OFFSET without LIMIT
|
|
SELECT o_custkey FROM orders ORDER BY o_custkey OFFSET 2980;
|
|
o_custkey
|
|
-----------
|
|
1498
|
|
1499
|
|
1499
|
|
1499
|
|
(4 rows)
|
|
|
|
-- LIMIT/OFFSET with Joins
|
|
SELECT
|
|
li.l_partkey,
|
|
o.o_custkey,
|
|
li.l_quantity
|
|
FROM
|
|
lineitem li JOIN orders o ON li.l_orderkey = o.o_orderkey
|
|
WHERE
|
|
li.l_quantity > 25
|
|
ORDER BY
|
|
li.l_quantity
|
|
LIMIT 10 OFFSET 20;
|
|
DEBUG: push down of limit count: 30
|
|
l_partkey | o_custkey | l_quantity
|
|
-----------+-----------+------------
|
|
135912 | 509 | 26.00
|
|
75351 | 1261 | 26.00
|
|
199475 | 1052 | 26.00
|
|
91309 | 8 | 26.00
|
|
53624 | 400 | 26.00
|
|
182736 | 1048 | 26.00
|
|
59694 | 163 | 26.00
|
|
20481 | 173 | 26.00
|
|
78748 | 1499 | 26.00
|
|
7614 | 1397 | 26.00
|
|
(10 rows)
|
|
|
|
RESET client_min_messages;
|