|
1 | 1 | #include "mysql_filter_pushdown.hpp"
|
2 | 2 | #include "mysql_utils.hpp"
|
| 3 | +#include "duckdb/planner/filter/optional_filter.hpp" |
| 4 | +#include "duckdb/planner/filter/in_filter.hpp" |
3 | 5 |
|
4 | 6 | namespace duckdb {
|
5 | 7 |
|
@@ -30,12 +32,26 @@ string MySQLFilterPushdown::TransformComparison(ExpressionType type) {
|
30 | 32 | }
|
31 | 33 | }
|
32 | 34 |
|
| 35 | + |
| 36 | +static string TransformBlobToMySQL(const string &val) { |
| 37 | + char const HEX_DIGITS[] = "0123456789ABCDEF"; |
| 38 | + |
| 39 | + string result = "x'"; |
| 40 | + for(idx_t i = 0; i < val.size(); i++) { |
| 41 | + uint8_t byte_val = static_cast<uint8_t>(val[i]); |
| 42 | + result += HEX_DIGITS[(byte_val >> 4) & 0xf]; |
| 43 | + result += HEX_DIGITS[byte_val & 0xf]; |
| 44 | + } |
| 45 | + result += "'"; |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
33 | 49 | string MySQLFilterPushdown::TransformConstant(const Value &val) {
|
34 | 50 | if (val.type().IsNumeric()) {
|
35 | 51 | return val.ToSQLString();
|
36 | 52 | }
|
37 | 53 | if (val.type().id() == LogicalTypeId::BLOB) {
|
38 |
| - throw NotImplementedException("Unsupported type for filter pushdown: BLOB"); |
| 54 | + return TransformBlobToMySQL(StringValue::Get(val)); |
39 | 55 | }
|
40 | 56 | if (val.type().id() == LogicalTypeId::TIMESTAMP_TZ) {
|
41 | 57 | return val.DefaultCastAs(LogicalType::TIMESTAMP).DefaultCastAs(LogicalType::VARCHAR).ToSQLString();
|
@@ -63,6 +79,21 @@ string MySQLFilterPushdown::TransformFilter(string &column_name, TableFilter &fi
|
63 | 79 | auto operator_string = TransformComparison(constant_filter.comparison_type);
|
64 | 80 | return StringUtil::Format("%s %s %s", column_name, operator_string, constant_string);
|
65 | 81 | }
|
| 82 | + case TableFilterType::OPTIONAL_FILTER: { |
| 83 | + auto &optional_filter = filter.Cast<OptionalFilter>(); |
| 84 | + return TransformFilter(column_name, *optional_filter.child_filter); |
| 85 | + } |
| 86 | + case TableFilterType::IN_FILTER: { |
| 87 | + auto &in_filter = filter.Cast<InFilter>(); |
| 88 | + string in_list; |
| 89 | + for(auto &val : in_filter.values) { |
| 90 | + if (!in_list.empty()) { |
| 91 | + in_list += ", "; |
| 92 | + } |
| 93 | + in_list += TransformConstant(val); |
| 94 | + } |
| 95 | + return column_name + " IN (" + in_list + ")"; |
| 96 | + } |
66 | 97 | default:
|
67 | 98 | throw InternalException("Unsupported table filter type");
|
68 | 99 | }
|
|
0 commit comments