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
|
# name: test/optimizer/topn/topn_optimizer.test
# description: Test Top N optimization
# group: [topn]
statement ok
CREATE TABLE integers(i INTEGER, j INTEGER)
statement ok
PRAGMA explain_output = OPTIMIZED_ONLY;
# standalone limit
query II
EXPLAIN SELECT i FROM integers LIMIT 4
----
logical_opt <REGEX>:.*LIMIT.*
# standalone order by
query II
EXPLAIN SELECT i FROM integers ORDER BY i
----
logical_opt <REGEX>:.*ORDER.*
# order by + limit is transformed into top-n
query II
EXPLAIN SELECT i FROM integers ORDER BY i LIMIT 4
----
logical_opt <REGEX>:.*TOP_N.*
# also works with offset
query II
EXPLAIN SELECT i FROM integers ORDER BY i LIMIT 4 OFFSET 3
----
logical_opt <REGEX>:.*TOP_N.*
# only offset but no limit: no top-n
query II
EXPLAIN SELECT i FROM integers ORDER BY i OFFSET 3
----
logical_opt <!REGEX>:.*TOP_N.*
# only limit, but in subquery with projection pull up optimization
query II
EXPLAIN SELECT * FROM (SELECT * FROM range(100000000) AS _(x) ORDER BY x) AS cte LIMIT 10
----
logical_opt <REGEX>:.*TOP_N.*
require no_alternative_verify
# top n optimization with more complex projection pull up
query II
EXPLAIN
WITH cte1 AS (SELECT range%2000 a,
(range%2000 + 2000) b,
(range%2000 + 4000) c,
(range%2000 + 6000) d
FROM range(10000) ORDER BY range),
cte2 as (select *, a as x, b as y, c as z from cte1),
cte3 as (select *, a as l, b as m, c as n, d as o from cte2)
SELECT * FROM cte3 LIMIT 4;
----
logical_opt <REGEX>:.*TOP_N.*
statement ok
create table large_table as select range a from range(100000);
query II
EXPLAIN select * from large_table order by a limit 100;
----
logical_opt <REGEX>:.*TOP_N.*
query II
EXPLAIN select * from large_table order by a limit 10000;
----
logical_opt <!REGEX>:.*TOP_N.*
|