Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamchopra committed Dec 5, 2023
1 parent 0f41478 commit cbc1410
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library_unity(
expression_heuristics.cpp
filter_combiner.cpp
filter_pushdown.cpp
filter_pushdown_constants.cpp
filter_pullup.cpp
heuristic_operator_fusion.cpp
in_clause_rewriter.cpp
Expand Down
17 changes: 2 additions & 15 deletions src/optimizer/filter_pushdown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include "duckdb/planner/operator/logical_join.hpp"
#include "duckdb/optimizer/optimizer.hpp"
#include <iostream>

bool disable_filter_pushdown = true;
bool disable_filter_pushdown_get = false;
#include "filter_pushdown_constants.hpp"

namespace duckdb {

Expand All @@ -17,17 +15,6 @@ FilterPushdown::FilterPushdown(Optimizer &optimizer) : optimizer(optimizer), com
}

unique_ptr<LogicalOperator> FilterPushdown::Rewrite(unique_ptr<LogicalOperator> op) {
if (disable_filter_pushdown) {
std::cout << "Optimizer:FilterPushDown:disabled\n";
return std::move(op);
} else {
std::cout << "Optimizer:FilterPushDown:enabled\n";
}
if (disable_filter_pushdown_get) {
std::cout << "Optimizer:FilterFusion:disabled\n";
} else {
std::cout << "Optimizer:FilterFusion:enabled\n";
}

D_ASSERT(!combiner.HasFilters());
switch (op->type) {
Expand Down Expand Up @@ -56,7 +43,7 @@ unique_ptr<LogicalOperator> FilterPushdown::Rewrite(unique_ptr<LogicalOperator>
return op;
}
case LogicalOperatorType::LOGICAL_GET: {
if (disable_filter_pushdown_get) {
if (disable_filter_fusion) {
return FinishPushdown(std::move(op));
} else {
return PushdownGet(std::move(op));
Expand Down
16 changes: 16 additions & 0 deletions src/optimizer/filter_pushdown_constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include "filter_pushdown_constants.hpp"

// Note: Only set these flags
bool filter_pushdown_flag = true;
bool filter_fusion_flag = true;
bool scan_buffering_flag = true;

// Note: Should be less than 0.5 to have any benefits
double caching_threshold = 0.3;


// NOTE: DO NOT TOUCH THESE
bool disable_filter_pushdown = !filter_pushdown_flag;
bool disable_filter_fusion = (disable_filter_pushdown)? true: !filter_fusion_flag;
bool disable_scan_buffering = !scan_buffering_flag;
5 changes: 5 additions & 0 deletions src/optimizer/filter_pushdown_constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

extern bool disable_filter_fusion;
extern bool disable_filter_pushdown;
extern bool disable_scan_buffering;
extern double caching_threshold;
32 changes: 22 additions & 10 deletions src/optimizer/join_order/query_graph_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "duckdb/common/printer.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/assert.hpp"
#include "../filter_pushdown_constants.hpp"

namespace duckdb {

Expand Down Expand Up @@ -58,19 +59,30 @@ const vector<unique_ptr<FilterInfo>> &QueryGraphManager::GetFilterBindings() con
}

static unique_ptr<LogicalOperator> PushFilter(unique_ptr<LogicalOperator> node, unique_ptr<Expression> expr) {
// push an expression into a filter
// first check if we have any filter to push it into
if (node->type != LogicalOperatorType::LOGICAL_FILTER) {
// we don't, we need to create one
if (disable_filter_fusion) {
auto filter = make_uniq<LogicalFilter>();
filter->children.push_back(std::move(node));
node = std::move(filter);
// push the filter into the LogicalFilter
D_ASSERT(node->type == LogicalOperatorType::LOGICAL_FILTER);
auto &new_filter = node->Cast<LogicalFilter>();
new_filter.expressions.push_back(std::move(expr));
return node;
} else {
// push an expression into a filter
// first check if we have any filter to push it into
if (node->type != LogicalOperatorType::LOGICAL_FILTER) {
// we don't, we need to create one
auto filter = make_uniq<LogicalFilter>();
filter->children.push_back(std::move(node));
node = std::move(filter);
}
// push the filter into the LogicalFilter
D_ASSERT(node->type == LogicalOperatorType::LOGICAL_FILTER);
auto &filter = node->Cast<LogicalFilter>();
filter.expressions.push_back(std::move(expr));
return node;
}
// push the filter into the LogicalFilter
D_ASSERT(node->type == LogicalOperatorType::LOGICAL_FILTER);
auto &filter = node->Cast<LogicalFilter>();
filter.expressions.push_back(std::move(expr));
return node;
}

void QueryGraphManager::CreateHyperGraphEdges() {
Expand Down Expand Up @@ -211,7 +223,7 @@ GenerateJoinRelation QueryGraphManager::GenerateJoins(vector<unique_ptr<LogicalO
if (filters_and_bindings[info.filter_index]->filter) {
// now check if the filter is a subset of the current relation
// note that infos with an empty relation set are a special case and we do not push them down
if (info.set.count > 0 && JoinRelationSet::IsSubset(*result_relation, info.set)) {
if (info.set.count > 0 && JoinRelationSet::IsSubset(*result_relation, info.set) && !disable_filter_pushdown) {
auto &filter_and_binding = filters_and_bindings[info.filter_index];
auto filter = std::move(filter_and_binding->filter);
// if it is, we can push the filter
Expand Down
8 changes: 4 additions & 4 deletions src/optimizer/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ unique_ptr<LogicalOperator> Optimizer::Optimize(unique_ptr<LogicalOperator> plan
});

// perform filter pullup
// RunOptimizer(OptimizerType::FILTER_PULLUP, [&]() {
// FilterPullup filter_pullup;
// plan = filter_pullup.Rewrite(std::move(plan));
// });
RunOptimizer(OptimizerType::FILTER_PULLUP, [&]() {
FilterPullup filter_pullup;
plan = filter_pullup.Rewrite(std::move(plan));
});

// perform filter pushdown
RunOptimizer(OptimizerType::FILTER_PUSHDOWN, [&]() {
Expand Down
34 changes: 27 additions & 7 deletions src/optimizer/pushdown/pushdown_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "duckdb/optimizer/filter_pushdown.hpp"
#include "duckdb/planner/operator/logical_empty_result.hpp"
#include "duckdb/planner/operator/logical_filter.hpp"
#include <iostream>
#include "../filter_pushdown_constants.hpp"

namespace duckdb {

Expand All @@ -12,15 +14,33 @@ unique_ptr<LogicalOperator> FilterPushdown::PushdownFilter(unique_ptr<LogicalOpe
if (!filter.projection_map.empty()) {
return FinishPushdown(std::move(op));
}
// filter: gather the filters and remove the filter from the set of operations
for (auto &expression : filter.expressions) {
if (AddFilter(std::move(expression)) == FilterResult::UNSATISFIABLE) {
// filter statically evaluates to false, strip tree
return make_uniq<LogicalEmptyResult>(std::move(op));

if (disable_filter_pushdown) {
vector<unique_ptr<Expression>> exprs;
for (auto &expression : filter.expressions) {
exprs.push_back(std::move(expression));
}
auto child_op = Rewrite(std::move(filter.children[0]));
for (auto &expr: exprs) {
std::cout << "Got expr " << expr->ToString() << std::endl;
vector<unique_ptr<Expression>> v;
v.push_back(std::move(expr));
child_op = AddLogicalFilter(std::move(child_op), std::move(v));
}
child_op->Print();
return child_op;
} else {
for (auto &expression : filter.expressions) {
if (AddFilter(std::move(expression)) == FilterResult::UNSATISFIABLE) {
// filter statically evaluates to false, strip tree
return make_uniq<LogicalEmptyResult>(std::move(op));
}
}
GenerateFilters();
return Rewrite(std::move(filter.children[0]));
}
GenerateFilters();
return Rewrite(std::move(filter.children[0]));
// filter: gather the filters and remove the filter from the set of operations

}

} // namespace duckdb
4 changes: 4 additions & 0 deletions src/optimizer/statistics/operator/propagate_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "duckdb/planner/operator/logical_join.hpp"
#include "duckdb/planner/operator/logical_limit.hpp"
#include "duckdb/planner/operator/logical_positional_join.hpp"
#include <iostream>

namespace duckdb {

Expand Down Expand Up @@ -349,6 +350,9 @@ void StatisticsPropagator::CreateFilterFromJoinStats(unique_ptr<LogicalOperator>
}

FilterPushdown filter_pushdown(optimizer);
std::cout << "JoinOptimizer" << std::endl;
std::cout << "Called FilterPushdown on " << std::endl;
child->Print();
child = filter_pushdown.Rewrite(std::move(child));
PropagateExpression(expr);
}
Expand Down
6 changes: 1 addition & 5 deletions src/storage/table/row_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
#include "duckdb/common/serializer/binary_serializer.hpp"
#include "duckdb/execution/operator/filter/physical_filter.hpp"
#include <iostream>

bool disable_scan_buffering = false;

// Note: Should be less than 0.5 to have any benefits
double caching_threshold = 0.1;
#include "../optimizer/filter_pushdown_constants.hpp"

namespace duckdb {

Expand Down

0 comments on commit cbc1410

Please sign in to comment.