1 | #include "catch.hpp" |
---|---|
2 | #include "expression_helper.hpp" |
3 | #include "duckdb/planner/operator/logical_top_n.hpp" |
4 | #include "duckdb/optimizer/topn_optimizer.hpp" |
5 | #include "test_helpers.hpp" |
6 | |
7 | using namespace duckdb; |
8 | using namespace std; |
9 | |
10 | TEST_CASE("Test Top N optimization", "[topn]") { |
11 | // LogicalTopN *topn; |
12 | ExpressionHelper helper; |
13 | helper.con.Query("CREATE TABLE integers(i INTEGER, j INTEGER)"); |
14 | auto tree = helper.ParseLogicalTree("SELECT i FROM integers ORDER BY i LIMIT 4"); |
15 | |
16 | REQUIRE(tree->type == LogicalOperatorType::LIMIT); |
17 | REQUIRE(tree->children[0]->type == LogicalOperatorType::ORDER_BY); |
18 | |
19 | TopN topn_optimizer; |
20 | auto plan = topn_optimizer.Optimize(move(tree)); |
21 | |
22 | // ORDER BY + LIMIT is now replaced by TOP N optimization |
23 | REQUIRE(plan->type == LogicalOperatorType::TOP_N); |
24 | |
25 | // Same as above but with OFFSET |
26 | tree = helper.ParseLogicalTree("SELECT i FROM integers ORDER BY i DESC LIMIT 4 OFFSET 5"); |
27 | |
28 | REQUIRE(tree->type == LogicalOperatorType::LIMIT); |
29 | REQUIRE(tree->children[0]->type == LogicalOperatorType::ORDER_BY); |
30 | |
31 | plan = topn_optimizer.Optimize(move(tree)); |
32 | REQUIRE(plan->type == LogicalOperatorType::TOP_N); |
33 | |
34 | // Same does not apply when OFFSET is present without LIMIT |
35 | tree = helper.ParseLogicalTree("SELECT i FROM integers ORDER BY i OFFSET 5"); |
36 | |
37 | REQUIRE(tree->type == LogicalOperatorType::LIMIT); |
38 | REQUIRE(tree->children[0]->type == LogicalOperatorType::ORDER_BY); |
39 | |
40 | plan = topn_optimizer.Optimize(move(tree)); |
41 | REQUIRE(plan->type == LogicalOperatorType::LIMIT); |
42 | } |
43 |