Skip to content

Commit

Permalink
Add test coverage for the LIMIT/OFFSET push-down.
Browse files Browse the repository at this point in the history
FDW-131, Vaibhav Dalvi, reviewed by Sravan Velagandula, tested by
Kashif Zeeshan
  • Loading branch information
jeevanchalke committed Oct 13, 2022
1 parent 58bce89 commit 0660759
Show file tree
Hide file tree
Showing 15 changed files with 2,381 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OBJS = connection.o option.o mongo_wrapper_meta.o mongo_fdw.o mongo_query.o depa
EXTENSION = mongo_fdw
DATA = mongo_fdw--1.0.sql mongo_fdw--1.1.sql mongo_fdw--1.0--1.1.sql

REGRESS = server_options connection_validation dml select pushdown join_pushdown aggregate_pushdown
REGRESS = server_options connection_validation dml select pushdown join_pushdown aggregate_pushdown limit_offset_pushdown
REGRESS_OPTS = --load-extension=$(EXTENSION)

#
Expand Down
2 changes: 1 addition & 1 deletion Makefile.meta
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OBJS = connection.o option.o mongo_wrapper_meta.o mongo_fdw.o mongo_query.o depa
EXTENSION = mongo_fdw
DATA = mongo_fdw--1.0.sql mongo_fdw--1.1.sql mongo_fdw--1.0--1.1.sql

REGRESS = server_options connection_validation dml select pushdown join_pushdown aggregate_pushdown
REGRESS = server_options connection_validation dml select pushdown join_pushdown aggregate_pushdown limit_offset_pushdown
REGRESS_OPTS = --load-extension=$(EXTENSION)

#
Expand Down
149 changes: 149 additions & 0 deletions expected/aggregate_pushdown.out
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,155 @@ SELECT a32, sum(a32) FROM f_test_large GROUP BY
132 | 132
(3 rows)

-- FDW-131: Limit and offset pushdown with Aggregate pushdown.
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1;
min | c1
-----+----
10 | 10
20 | 20
30 | 30
40 | 40
50 | 50
|
(6 rows)

EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 1 OFFSET 1;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 1 OFFSET 1;
min | c1
-----+----
10 | 10
(1 row)

-- Limit 0, Offset 0 with aggregates.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 0 OFFSET 0;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 0 OFFSET 0;
min | c1
-----+----
(0 rows)

-- Limit NULL
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT NULL OFFSET 2;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT NULL OFFSET 2;
min | c1
-----+----
20 | 20
30 | 30
40 | 40
50 | 50
(4 rows)

-- Limit ALL
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT ALL OFFSET 2;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT ALL OFFSET 2;
min | c1
-----+----
20 | 20
30 | 30
40 | 40
50 | 50
(4 rows)

-- Limit with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST LIMIT -1;
QUERY PLAN
--------------------------------------------------------------
Limit
Output: c1, (max(c1))
-> GroupAggregate
Output: c1, max(c1)
Group Key: fdw137_t2.c1
-> Foreign Scan on public.fdw137_t2
Output: _id, c1, c2, c3
Foreign Namespace: mongo_fdw_regress.test_tbl2
(8 rows)

-- Should throw an error.
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST LIMIT -1;
ERROR: LIMIT must not be negative
-- Offset with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST OFFSET -2;
QUERY PLAN
---------------------------------------------------------------------------------
Limit
Output: c1, (max(c1))
-> Foreign Scan
Output: c1, (max(c1))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(5 rows)

-- Should throw an error.
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST OFFSET -2;
ERROR: OFFSET must not be negative
-- Limit/Offset with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT -1 OFFSET -2;
QUERY PLAN
--------------------------------------------------------------
Limit
Output: c1, (avg(c1))
-> GroupAggregate
Output: c1, avg(c1)
Group Key: fdw137_t2.c1
-> Foreign Scan on public.fdw137_t2
Output: _id, c1, c2, c3
Foreign Namespace: mongo_fdw_regress.test_tbl2
(8 rows)

-- Should throw an error.
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT -1 OFFSET -2;
ERROR: OFFSET must not be negative
-- Limit with expression evaluating to -ve value.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT (1 - (SELECT COUNT(*) FROM fdw137_t2));
QUERY PLAN
-----------------------------------------------------------------------------------
Limit
Output: fdw137_t2.c1, (avg(fdw137_t2.c1))
InitPlan 1 (returns $0)
-> Foreign Scan
Output: (count(*))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
-> Foreign Scan
Output: fdw137_t2.c1, (avg(fdw137_t2.c1))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(9 rows)

SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT (1 - (SELECT COUNT(*) FROM fdw137_t2));
ERROR: LIMIT must not be negative
-- Cleanup
DELETE FROM fdw137_t1 WHERE c8 IS NULL;
DELETE FROM fdw137_t1 WHERE c8 = 60;
Expand Down
149 changes: 149 additions & 0 deletions expected/aggregate_pushdown_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,155 @@ SELECT a32, sum(a32) FROM f_test_large GROUP BY
132 | 132
(3 rows)

-- FDW-131: Limit and offset pushdown with Aggregate pushdown.
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1;
min | c1
-----+----
10 | 10
20 | 20
30 | 30
40 | 40
50 | 50
|
(6 rows)

EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 1 OFFSET 1;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 1 OFFSET 1;
min | c1
-----+----
10 | 10
(1 row)

-- Limit 0, Offset 0 with aggregates.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 0 OFFSET 0;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT 0 OFFSET 0;
min | c1
-----+----
(0 rows)

-- Limit NULL
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT NULL OFFSET 2;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT NULL OFFSET 2;
min | c1
-----+----
20 | 20
30 | 30
40 | 40
50 | 50
(4 rows)

-- Limit ALL
EXPLAIN (VERBOSE, COSTS OFF)
SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT ALL OFFSET 2;
QUERY PLAN
---------------------------------------------------------------------------
Foreign Scan
Output: (min(c1)), c1
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(3 rows)

SELECT min(c1), c1 FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT ALL OFFSET 2;
min | c1
-----+----
20 | 20
30 | 30
40 | 40
50 | 50
(4 rows)

-- Limit with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST LIMIT -1;
QUERY PLAN
--------------------------------------------------------------
Limit
Output: c1, (max(c1))
-> GroupAggregate
Output: c1, max(c1)
Group Key: fdw137_t2.c1
-> Foreign Scan on public.fdw137_t2
Output: _id, c1, c2, c3
Foreign Namespace: mongo_fdw_regress.test_tbl2
(8 rows)

-- Should throw an error.
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST LIMIT -1;
ERROR: LIMIT must not be negative
-- Offset with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST OFFSET -2;
QUERY PLAN
---------------------------------------------------------------------------------
Limit
Output: c1, (max(c1))
-> Foreign Scan
Output: c1, (max(c1))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(5 rows)

-- Should throw an error.
SELECT c1, max(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY 1 ASC NULLS FIRST OFFSET -2;
ERROR: OFFSET must not be negative
-- Limit/Offset with -ve value. Shouldn't pushdown.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT -1 OFFSET -2;
QUERY PLAN
--------------------------------------------------------------
Limit
Output: c1, (avg(c1))
-> GroupAggregate
Output: c1, avg(c1)
Group Key: fdw137_t2.c1
-> Foreign Scan on public.fdw137_t2
Output: _id, c1, c2, c3
Foreign Namespace: mongo_fdw_regress.test_tbl2
(8 rows)

-- Should throw an error.
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT -1 OFFSET -2;
ERROR: OFFSET must not be negative
-- Limit with expression evaluating to -ve value.
EXPLAIN (VERBOSE, COSTS FALSE)
SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT (1 - (SELECT COUNT(*) FROM fdw137_t2));
QUERY PLAN
-----------------------------------------------------------------------------------
Limit
Output: fdw137_t2.c1, (avg(fdw137_t2.c1))
InitPlan 1 (returns $0)
-> Foreign Scan
Output: (count(*))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
-> Foreign Scan
Output: fdw137_t2.c1, (avg(fdw137_t2.c1))
Foreign Namespace: Aggregate on (mongo_fdw_regress.test_tbl2 fdw137_t2)
(9 rows)

SELECT c1, avg(c1) FROM fdw137_t2 GROUP BY c1 ORDER BY c1 ASC NULLS FIRST LIMIT (1 - (SELECT COUNT(*) FROM fdw137_t2));
ERROR: LIMIT must not be negative
-- Cleanup
DELETE FROM fdw137_t1 WHERE c8 IS NULL;
DELETE FROM fdw137_t1 WHERE c8 = 60;
Expand Down
Loading

0 comments on commit 0660759

Please sign in to comment.