Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP complex filter pushdown #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/include/mysql_filter_pushdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
#include "duckdb/planner/table_filter.hpp"
#include "duckdb/planner/filter/conjunction_filter.hpp"
#include "duckdb/planner/filter/constant_filter.hpp"
#include "duckdb/function/table_function.hpp"

namespace duckdb {

class MySQLFilterPushdown {
public:
static string TransformFilters(const vector<column_t> &column_ids, optional_ptr<TableFilterSet> filters,
const vector<string> &names);
static void ComplexFilterPushdown(ClientContext &context, LogicalGet &get, FunctionData *bind_data_p,
vector<unique_ptr<Expression>> &filters);

private:
static string TransformFilter(string &column_name, TableFilter &filter);
Expand Down
2 changes: 2 additions & 0 deletions src/include/mysql_scanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct MySQLBindData : public FunctionData {
vector<MySQLType> mysql_types;
vector<string> names;
vector<LogicalType> types;
//Filter pushdown to apply
vector<unique_ptr<Expression>> filters_to_apply;

public:
unique_ptr<FunctionData> Copy() const override {
Expand Down
1 change: 1 addition & 0 deletions src/include/storage/mysql_table_entry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp"
#include "duckdb/parser/parsed_data/create_table_info.hpp"
#include "mysql_filter_pushdown.hpp"
#include "mysql_utils.hpp"

namespace duckdb {
Expand Down
36 changes: 36 additions & 0 deletions src/mysql_filter_pushdown.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "mysql_filter_pushdown.hpp"
#include "mysql_scanner.hpp"
#include "mysql_utils.hpp"
#include <iostream>

namespace duckdb {

Expand All @@ -25,6 +27,8 @@ string MySQLFilterPushdown::TransformComparision(ExpressionType type) {
return "<=";
case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
return ">=";
case ExpressionType::COMPARE_IN:
return "IN";
default:
throw NotImplementedException("Unsupported expression type");
}
Expand Down Expand Up @@ -57,8 +61,11 @@ string MySQLFilterPushdown::TransformFilter(string &column_name, TableFilter &fi

string MySQLFilterPushdown::TransformFilters(const vector<column_t> &column_ids, optional_ptr<TableFilterSet> filters,
const vector<string> &names) {

std::cout << "TransformFilters" << std::endl;
if (!filters || filters->filters.empty()) {
// no filters
std::cout << "No filter" << std::endl;
return string();
}
string result;
Expand All @@ -68,9 +75,38 @@ string MySQLFilterPushdown::TransformFilters(const vector<column_t> &column_ids,
}
auto column_name = MySQLUtils::WriteIdentifier(names[column_ids[entry.first]]);
auto &filter = *entry.second;
std::cout << "filter: " << filter.ToString(column_name) << std::endl;
result += TransformFilter(column_name, filter);
}
return result;
}

void MySQLFilterPushdown::ComplexFilterPushdown(ClientContext &context, LogicalGet &get, FunctionData *bind_data_p,
vector<unique_ptr<Expression>> &filters) {

auto &data = bind_data_p->Cast<MySQLBindData>();
vector<unique_ptr<Expression>> filters_to_apply;
vector<unique_ptr<Expression>> unsupported_filters;

for (idx_t j = 0; j < filters.size(); j++) {
auto &filter = filters[j];
std::cout << "current filter : " << filter->ToString() << std::endl;
unique_ptr<Expression> filter_copy = filter->Copy();
if (filter->expression_class == ExpressionClass::BOUND_EXPRESSION ||
filter->expression_class == ExpressionClass::BOUND_CONSTANT ||
filter->expression_class == ExpressionClass::BOUND_CONJUNCTION ||
filter->expression_class == ExpressionClass::BOUND_COMPARISON ||
filter->expression_class == ExpressionClass::BOUND_OPERATOR) {
filters_to_apply.emplace_back(std::move(filter_copy));
std::cout << "filters_to_apply : " << filter->ToString() << std::endl;
} else {
unsupported_filters.emplace_back(std::move(filter_copy));
std::cout << "unsupported_filters : " << filter->ToString() << " with class: " << static_cast<int>(filter->expression_class) << std::endl;
}
}

data.filters_to_apply = std::move(filters_to_apply);
filters = std::move(unsupported_filters);
}

} // namespace duckdb
1 change: 1 addition & 0 deletions src/mysql_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static unique_ptr<LocalTableFunctionState> MySQLInitLocalState(ExecutionContext

static void MySQLScan(ClientContext &context, TableFunctionInput &data, DataChunk &output) {
auto &gstate = data.global_state->Cast<MySQLGlobalState>();
data.bind_data->Cast<MySQLBindData>();
idx_t r;
for (r = 0; r < STANDARD_VECTOR_SIZE; r++) {
if (!gstate.result->Next()) {
Expand Down
1 change: 1 addition & 0 deletions src/storage/mysql_table_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ TableFunction MySQLTableEntry::GetScanFunction(ClientContext &context, unique_pt
Value filter_pushdown;
if (context.TryGetCurrentSetting("mysql_experimental_filter_pushdown", filter_pushdown)) {
function.filter_pushdown = BooleanValue::Get(filter_pushdown);
function.pushdown_complex_filter = MySQLFilterPushdown::ComplexFilterPushdown;
}
return function;
}
Expand Down