diff --git a/include/pgduckdb/pgduckdb_filter.hpp b/include/pgduckdb/pgduckdb_filter.hpp deleted file mode 100644 index c5f431ef..00000000 --- a/include/pgduckdb/pgduckdb_filter.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "duckdb.hpp" - -extern "C" { -#include "postgres.h" -} - -namespace pgduckdb { - -bool ApplyValueFilter(const duckdb::TableFilter &filter, const Datum &value, bool is_null, Oid type_oid); - -} // namespace pgduckdb diff --git a/src/pgduckdb_filter.cpp b/src/pgduckdb_filter.cpp deleted file mode 100644 index 7b0c4bd3..00000000 --- a/src/pgduckdb_filter.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include "duckdb.hpp" -#include "duckdb/planner/filter/constant_filter.hpp" -#include "pgduckdb/pgduckdb_types.hpp" - -extern "C" { -#include "postgres.h" -#include "catalog/pg_type.h" -#include "utils/builtins.h" -#include "utils/date.h" -#include "utils/timestamp.h" -#if PG_VERSION_NUM >= 160000 -#include "varatt.h" -#endif -} - -#include "pgduckdb/pgduckdb_filter.hpp" -#include "pgduckdb/pgduckdb_detoast.hpp" - -namespace pgduckdb { - -template -bool -TemplatedFilterOperation(const T &value, const duckdb::Value &constant) { - return OP::Operation(value, constant.GetValueUnsafe()); -} - -template -bool -StringFilterOperation(const Datum &value, const duckdb::Value &constant, bool is_bpchar) { - if (value == (Datum)0 || constant.IsNull()) { - return false; // Comparison to NULL always returns false. - } - - bool should_free = false; - const auto detoasted_value = DetoastPostgresDatum(reinterpret_cast(value), &should_free); - - /* bpchar adds zero padding so we need to read true len of bpchar */ - auto detoasted_val_len = is_bpchar ? bpchartruelen(VARDATA_ANY(detoasted_value), VARSIZE_ANY_EXHDR(detoasted_value)) - : VARSIZE_ANY_EXHDR(detoasted_value); - - const auto datum_sv = std::string_view((const char *)VARDATA_ANY(detoasted_value), detoasted_val_len); - const auto val = duckdb::StringValue::Get(constant); - const auto val_sv = std::string_view(val); - const bool res = OP::Operation(datum_sv, val_sv); - - if (should_free) { - duckdb_free(reinterpret_cast(detoasted_value)); - } - return res; -} - -template -static bool -FilterOperationSwitch(const Datum &value, const duckdb::Value &constant, Oid type_oid) { - switch (type_oid) { - case BOOLOID: - return TemplatedFilterOperation(DatumGetBool(value), constant); - case CHAROID: - return TemplatedFilterOperation(DatumGetChar(value), constant); - case INT2OID: - return TemplatedFilterOperation(DatumGetInt16(value), constant); - case INT4OID: - return TemplatedFilterOperation(DatumGetInt32(value), constant); - case INT8OID: - return TemplatedFilterOperation(DatumGetInt64(value), constant); - case FLOAT4OID: - return TemplatedFilterOperation(DatumGetFloat4(value), constant); - case FLOAT8OID: - return TemplatedFilterOperation(DatumGetFloat8(value), constant); - case DATEOID: { - int32_t date = DatumGetDateADT(value) + pgduckdb::PGDUCKDB_DUCK_DATE_OFFSET; - return TemplatedFilterOperation(date, constant); - } - case TIMESTAMPOID: { - int64_t timestamp = DatumGetTimestamp(value) + pgduckdb::PGDUCKDB_DUCK_TIMESTAMP_OFFSET; - return TemplatedFilterOperation(timestamp, constant); - } - case TIMESTAMPTZOID: { - int64_t timestamptz = DatumGetTimestampTz(value) + pgduckdb::PGDUCKDB_DUCK_TIMESTAMP_OFFSET; - return TemplatedFilterOperation(timestamptz, constant); - } - case BPCHAROID: - case TEXTOID: - case VARCHAROID: - return StringFilterOperation(value, constant, type_oid == BPCHAROID); - case BYTEAOID: - return StringFilterOperation(value, constant, false); - default: - throw duckdb::InvalidTypeException( - duckdb::string("(DuckDB/FilterOperationSwitch) Unsupported duckdb type: " + std::to_string(type_oid))); - } -} - -bool -ApplyValueFilter(const duckdb::TableFilter &filter, const Datum &value, bool is_null, Oid type_oid) { - switch (filter.filter_type) { - case duckdb::TableFilterType::CONJUNCTION_AND: { - const auto &conjunction = filter.Cast(); - for (const auto &child_filter : conjunction.child_filters) { - if (!ApplyValueFilter(*child_filter, value, is_null, type_oid)) { - return false; - } - } - return true; - } - case duckdb::TableFilterType::CONSTANT_COMPARISON: { - auto &constant_filter = filter.Cast(); - switch (constant_filter.comparison_type) { - case duckdb::ExpressionType::COMPARE_EQUAL: - return FilterOperationSwitch(value, constant_filter.constant, type_oid); - case duckdb::ExpressionType::COMPARE_LESSTHAN: - return FilterOperationSwitch(value, constant_filter.constant, type_oid); - case duckdb::ExpressionType::COMPARE_LESSTHANOREQUALTO: - return FilterOperationSwitch(value, constant_filter.constant, type_oid); - case duckdb::ExpressionType::COMPARE_GREATERTHAN: - return FilterOperationSwitch(value, constant_filter.constant, type_oid); - case duckdb::ExpressionType::COMPARE_GREATERTHANOREQUALTO: - return FilterOperationSwitch(value, constant_filter.constant, type_oid); - default: - D_ASSERT(0); - } - break; - } - case duckdb::TableFilterType::IS_NOT_NULL: - return is_null == false; - case duckdb::TableFilterType::IS_NULL: - return is_null == true; - default: - D_ASSERT(0); - break; - } -} - -} // namespace pgduckdb diff --git a/src/pgduckdb_types.cpp b/src/pgduckdb_types.cpp index d8041272..db2ad8d0 100644 --- a/src/pgduckdb_types.cpp +++ b/src/pgduckdb_types.cpp @@ -30,7 +30,6 @@ extern "C" { #include "utils/jsonb.h" } -#include "pgduckdb/pgduckdb_filter.hpp" #include "pgduckdb/pgduckdb_detoast.hpp" namespace pgduckdb {