1#include "duckdb/optimizer/regex_range_filter.hpp"
2
3#include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp"
4
5#include "duckdb/function/scalar/string_functions.hpp"
6
7#include "duckdb/planner/expression.hpp"
8#include "duckdb/planner/expression/bound_comparison_expression.hpp"
9#include "duckdb/planner/expression/bound_conjunction_expression.hpp"
10#include "duckdb/planner/expression/bound_constant_expression.hpp"
11#include "duckdb/planner/expression/bound_function_expression.hpp"
12#include "duckdb/planner/operator/logical_filter.hpp"
13
14using namespace duckdb;
15using namespace std;
16
17unique_ptr<LogicalOperator> RegexRangeFilter::Rewrite(unique_ptr<LogicalOperator> op) {
18
19 for (idx_t child_idx = 0; child_idx < op->children.size(); child_idx++) {
20 op->children[child_idx] = Rewrite(move(op->children[child_idx]));
21 }
22
23 if (op->type != LogicalOperatorType::FILTER) {
24 return op;
25 }
26
27 auto new_filter = make_unique<LogicalFilter>();
28
29 for (auto &expr : op->expressions) {
30 if (expr->type == ExpressionType::BOUND_FUNCTION) {
31 auto &func = (BoundFunctionExpression &)*expr.get();
32 if (func.function.name != "regexp_full_match" || func.children.size() != 2) {
33 continue;
34 }
35 auto &info = (RegexpMatchesBindData &)*func.bind_info;
36 if (!info.range_success) {
37 continue;
38 }
39 auto filter_left = make_unique<BoundComparisonExpression>(
40 ExpressionType::COMPARE_GREATERTHANOREQUALTO, func.children[0]->Copy(),
41 make_unique<BoundConstantExpression>(Value::BLOB(info.range_min)));
42 auto filter_right = make_unique<BoundComparisonExpression>(
43 ExpressionType::COMPARE_LESSTHANOREQUALTO, func.children[0]->Copy(),
44 make_unique<BoundConstantExpression>(Value::BLOB(info.range_max)));
45 auto filter_expr = make_unique<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND,
46 move(filter_left), move(filter_right));
47
48 new_filter->expressions.push_back(move(filter_expr));
49 }
50 }
51
52 if (new_filter->expressions.size() > 0) {
53 new_filter->children = move(op->children);
54 op->children.clear();
55 op->children.push_back(move(new_filter));
56 }
57
58 return op;
59}
60