forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_range_filter.cpp
More file actions
62 lines (48 loc) · 2.21 KB
/
regex_range_filter.cpp
File metadata and controls
62 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "duckdb/optimizer/regex_range_filter.hpp"
#include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp"
#include "duckdb/function/scalar/string_functions.hpp"
#include "duckdb/planner/expression.hpp"
#include "duckdb/planner/expression/bound_comparison_expression.hpp"
#include "duckdb/planner/expression/bound_conjunction_expression.hpp"
#include "duckdb/planner/expression/bound_constant_expression.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/planner/operator/logical_filter.hpp"
#include "duckdb/function/scalar/regexp.hpp"
namespace duckdb {
unique_ptr<LogicalOperator> RegexRangeFilter::Rewrite(unique_ptr<LogicalOperator> op) {
for (idx_t child_idx = 0; child_idx < op->children.size(); child_idx++) {
op->children[child_idx] = Rewrite(std::move(op->children[child_idx]));
}
if (op->type != LogicalOperatorType::LOGICAL_FILTER) {
return op;
}
auto new_filter = make_uniq<LogicalFilter>();
for (auto &expr : op->expressions) {
if (expr->GetExpressionType() == ExpressionType::BOUND_FUNCTION) {
auto &func = expr->Cast<BoundFunctionExpression>();
if (func.function.name != "regexp_full_match" || func.children.size() != 2) {
continue;
}
auto &info = func.bind_info->Cast<RegexpMatchesBindData>();
if (!info.range_success) {
continue;
}
auto filter_left = make_uniq<BoundComparisonExpression>(
ExpressionType::COMPARE_GREATERTHANOREQUALTO, func.children[0]->Copy(),
make_uniq<BoundConstantExpression>(Value::BLOB_RAW(info.range_min)));
auto filter_right = make_uniq<BoundComparisonExpression>(
ExpressionType::COMPARE_LESSTHANOREQUALTO, func.children[0]->Copy(),
make_uniq<BoundConstantExpression>(Value::BLOB_RAW(info.range_max)));
auto filter_expr = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND,
std::move(filter_left), std::move(filter_right));
new_filter->expressions.push_back(std::move(filter_expr));
}
}
if (!new_filter->expressions.empty()) {
new_filter->children = std::move(op->children);
op->children.clear();
op->children.push_back(std::move(new_filter));
}
return op;
}
} // namespace duckdb