Skip to content

Commit 9ced60b

Browse files
committed
vendor: Update vendored sources to duckdb/duckdb@ca7af7f
Don't bail on TopN optimization if we don't have a cardinality (duckdb/duckdb#17654)
1 parent 0b19bb4 commit 9ced60b

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

src/duckdb/src/function/table/version/pragma_version.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#ifndef DUCKDB_PATCH_VERSION
2-
#define DUCKDB_PATCH_VERSION "1-dev48"
2+
#define DUCKDB_PATCH_VERSION "1-dev51"
33
#endif
44
#ifndef DUCKDB_MINOR_VERSION
55
#define DUCKDB_MINOR_VERSION 3
@@ -8,10 +8,10 @@
88
#define DUCKDB_MAJOR_VERSION 1
99
#endif
1010
#ifndef DUCKDB_VERSION
11-
#define DUCKDB_VERSION "v1.3.1-dev48"
11+
#define DUCKDB_VERSION "v1.3.1-dev51"
1212
#endif
1313
#ifndef DUCKDB_SOURCE_ID
14-
#define DUCKDB_SOURCE_ID "243eb89447"
14+
#define DUCKDB_SOURCE_ID "ca7af7ffdc"
1515
#endif
1616
#include "duckdb/function/table/system_functions.hpp"
1717
#include "duckdb/main/database.hpp"

src/duckdb/src/include/duckdb/optimizer/topn_optimizer.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include "duckdb/main/client_context.hpp"
1112
#include "duckdb/common/constants.hpp"
1213

1314
namespace duckdb {
@@ -17,13 +18,18 @@ class Optimizer;
1718

1819
class TopN {
1920
public:
21+
explicit TopN(ClientContext &context);
22+
2023
//! Optimize ORDER BY + LIMIT to TopN
2124
unique_ptr<LogicalOperator> Optimize(unique_ptr<LogicalOperator> op);
2225
//! Whether we can perform the optimization on this operator
23-
static bool CanOptimize(LogicalOperator &op);
26+
static bool CanOptimize(LogicalOperator &op, optional_ptr<ClientContext> context = nullptr);
2427

2528
private:
2629
void PushdownDynamicFilters(LogicalTopN &op);
30+
31+
private:
32+
ClientContext &context;
2733
};
2834

2935
} // namespace duckdb

src/duckdb/src/optimizer/optimizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void Optimizer::RunBuiltInOptimizers() {
224224

225225
// transform ORDER BY + LIMIT to TopN
226226
RunOptimizer(OptimizerType::TOP_N, [&]() {
227-
TopN topn;
227+
TopN topn(context);
228228
plan = topn.Optimize(std::move(plan));
229229
});
230230

src/duckdb/src/optimizer/topn_optimizer.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
namespace duckdb {
1616

17-
bool TopN::CanOptimize(LogicalOperator &op) {
17+
TopN::TopN(ClientContext &context_p) : context(context_p) {
18+
}
19+
20+
bool TopN::CanOptimize(LogicalOperator &op, optional_ptr<ClientContext> context) {
1821
if (op.type == LogicalOperatorType::LOGICAL_LIMIT) {
1922
auto &limit = op.Cast<LogicalLimit>();
2023

@@ -28,14 +31,21 @@ bool TopN::CanOptimize(LogicalOperator &op) {
2831
}
2932

3033
auto child_op = op.children[0].get();
34+
if (context) {
35+
// estimate child cardinality if the context is available
36+
child_op->EstimateCardinality(*context);
37+
}
3138

32-
auto constant_limit = static_cast<double>(limit.limit_val.GetConstantValue());
33-
auto child_card = static_cast<double>(child_op->estimated_cardinality);
39+
if (child_op->has_estimated_cardinality) {
40+
// only check if we should switch to full sorting if we have estimated cardinality
41+
auto constant_limit = static_cast<double>(limit.limit_val.GetConstantValue());
42+
auto child_card = static_cast<double>(child_op->estimated_cardinality);
3443

35-
// if the child cardinality is not 98 times more than the
36-
bool limit_is_large = constant_limit > 5000;
37-
if (constant_limit > child_card * 0.007 && limit_is_large) {
38-
return false;
44+
// if the limit is > 0.7% of the child cardinality, sorting the whole table is faster
45+
bool limit_is_large = constant_limit > 5000;
46+
if (constant_limit > child_card * 0.007 && limit_is_large) {
47+
return false;
48+
}
3949
}
4050

4151
while (child_op->type == LogicalOperatorType::LOGICAL_PROJECTION) {
@@ -116,7 +126,7 @@ void TopN::PushdownDynamicFilters(LogicalTopN &op) {
116126
}
117127

118128
unique_ptr<LogicalOperator> TopN::Optimize(unique_ptr<LogicalOperator> op) {
119-
if (CanOptimize(*op)) {
129+
if (CanOptimize(*op, &context)) {
120130

121131
vector<unique_ptr<LogicalOperator>> projections;
122132

0 commit comments

Comments
 (0)