Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 297 additions & 0 deletions cpp/src/cwrapper/tsfile_cwrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "common/statistic.h"
#include "common/tablet.h"
#include "common/tsfile_common.h"
#include "reader/filter/aligned_field_filter.h"
#include "reader/filter/tag_filter.h"
#include "reader/result_set.h"
#include "reader/table_result_set.h"
Expand Down Expand Up @@ -3010,6 +3011,302 @@ ResultSet tsfile_query_table_with_tag_filter(
return table_result_set;
}

// ============== Field Filter API Implementation ==============

namespace {

storage::AlignedFieldFilter::CompareOp to_field_compare_op(FieldFilterOp op,
bool* valid) {
using CompareOp = storage::AlignedFieldFilter::CompareOp;
*valid = true;
switch (op) {
case FIELD_FILTER_EQ:
return CompareOp::EQ;
case FIELD_FILTER_NEQ:
return CompareOp::NE;
case FIELD_FILTER_LT:
return CompareOp::LT;
case FIELD_FILTER_LTEQ:
return CompareOp::LE;
case FIELD_FILTER_GT:
return CompareOp::GT;
case FIELD_FILTER_GTEQ:
return CompareOp::GE;
default:
*valid = false;
return CompareOp::EQ;
}
}

// Builds the native literal for a comparison. Returns false for an unknown
// literal type or a NULL string payload.
bool to_field_literal(const FieldFilterValue& value,
storage::AlignedFieldFilter::Literal* literal) {
using Literal = storage::AlignedFieldFilter::Literal;
switch (value.type) {
case FIELD_FILTER_VALUE_BOOL:
*literal = Literal::boolean(value.bool_value);
return true;
case FIELD_FILTER_VALUE_INT64:
*literal = Literal::int64(value.int64_value);
return true;
case FIELD_FILTER_VALUE_DOUBLE:
*literal = Literal::float64(value.double_value);
return true;
case FIELD_FILTER_VALUE_STRING:
if (value.string_value == nullptr) {
return false;
}
*literal = Literal::string(
std::string(value.string_value, value.string_length));
return true;
default:
return false;
}
}

// Copies a FIELD filter handle into a filter owned by the query call. The
// table query executor (and the result set) owns the filter it receives, so
// the caller's handle cannot be handed over directly.
storage::Filter* clone_field_filter(FieldFilterHandle field_filter,
ERRNO* err_code) {
auto* native = dynamic_cast<storage::AlignedFieldFilter*>(
static_cast<storage::Filter*>(field_filter));
if (native == nullptr) {
*err_code = common::E_INVALID_ARG;
return nullptr;
}
// clone() reports allocation failure by throwing, which the caller turns
// into E_OOM.
return native->clone().release();
}

} // namespace

ERRNO tsfile_field_filter_create_checked(TsFileReader reader,
const char* table_name,
const char* column_name,
FieldFilterOp op,
const FieldFilterValue* value,
FieldFilterHandle* out_filter) {
if (out_filter != nullptr) {
*out_filter = nullptr;
}
if (reader == nullptr || table_name == nullptr || column_name == nullptr ||
out_filter == nullptr) {
return common::E_INVALID_ARG;
}
const bool needs_value =
op != FIELD_FILTER_IS_NULL && op != FIELD_FILTER_IS_NOT_NULL;
if (needs_value && value == nullptr) {
return common::E_INVALID_ARG;
}
using FieldFilter = storage::AlignedFieldFilter;
try {
auto* r = static_cast<storage::TsFileReader*>(reader);
std::shared_ptr<storage::TableSchema> schema;
int ret = r->get_table_schema(table_name, schema);
if (ret != common::E_OK) {
return ret;
}
if (schema == nullptr) {
return common::E_TABLE_NOT_EXIST;
}

FieldFilter::Ptr filter;
if (op == FIELD_FILTER_IS_NULL) {
filter = FieldFilter::is_null(column_name);
} else if (op == FIELD_FILTER_IS_NOT_NULL) {
filter = FieldFilter::is_not_null(column_name);
} else {
bool valid_op = true;
const FieldFilter::CompareOp compare_op =
to_field_compare_op(op, &valid_op);
if (!valid_op) {
return common::E_INVALID_ARG;
}
FieldFilter::Literal literal;
if (!to_field_literal(*value, &literal)) {
return common::E_INVALID_ARG;
}
filter = FieldFilter::compare(column_name, compare_op,
std::move(literal));
}
if (filter == nullptr) {
return common::E_OOM;
}
// Resolve the column and bind the literal against its declared type so
// bad column names, non-FIELD columns and type mismatches surface here
// instead of during iteration.
ret = filter->bind(*schema);
if (ret == common::E_NOT_SUPPORT) {
return common::E_TYPE_NOT_MATCH;
}
if (ret != common::E_OK) {
return ret;
}
*out_filter = static_cast<void*>(filter.release());
return common::E_OK;
} catch (const std::bad_alloc&) {
return common::E_OOM;
} catch (...) {
return common::E_FILE_READ_ERR;
}
}

FieldFilterHandle tsfile_field_filter_and(FieldFilterHandle left,
FieldFilterHandle right) {
if (left == nullptr || right == nullptr) {
return nullptr;
}
// Borrow both inputs: the returned node owns deep copies, so the caller's
// handles stay valid and can be freed independently.
try {
return static_cast<void*>(
storage::AlignedFieldFilter::and_filter(
static_cast<storage::AlignedFieldFilter*>(left)->clone(),
static_cast<storage::AlignedFieldFilter*>(right)->clone())
.release());
} catch (...) {
return nullptr;
}
}

FieldFilterHandle tsfile_field_filter_or(FieldFilterHandle left,
FieldFilterHandle right) {
if (left == nullptr || right == nullptr) {
return nullptr;
}
try {
return static_cast<void*>(
storage::AlignedFieldFilter::or_filter(
static_cast<storage::AlignedFieldFilter*>(left)->clone(),
static_cast<storage::AlignedFieldFilter*>(right)->clone())
.release());
} catch (...) {
return nullptr;
}
}

FieldFilterHandle tsfile_field_filter_not(FieldFilterHandle filter) {
if (filter == nullptr) {
return nullptr;
}
try {
return static_cast<void*>(
storage::AlignedFieldFilter::not_filter(
static_cast<storage::AlignedFieldFilter*>(filter)->clone())
.release());
} catch (...) {
return nullptr;
}
}

void tsfile_field_filter_free(FieldFilterHandle filter) {
if (filter != nullptr) {
delete static_cast<storage::AlignedFieldFilter*>(filter);
}
}

ResultSet tsfile_query_table_with_filters(
TsFileReader reader, const char* table_name, char** columns,
uint32_t column_num, Timestamp start_time, Timestamp end_time, int offset,
int limit, TagFilterHandle tag_filter, FieldFilterHandle field_filter,
int batch_size, ERRNO* err_code) {
if (err_code == nullptr) {
return nullptr;
}
*err_code = common::E_INVALID_ARG;
if (reader == nullptr || table_name == nullptr || columns == nullptr ||
column_num == 0 || offset < 0) {
return nullptr;
}
try {
std::vector<std::string> column_names;
column_names.reserve(column_num);
for (uint32_t i = 0; i < column_num; ++i) {
if (columns[i] == nullptr) {
return nullptr;
}
column_names.emplace_back(columns[i]);
}
storage::ResultSet* result_set = nullptr;
const int normalized_limit = limit < 0 ? -1 : limit;
// batch_size <= 0 selects row-by-row mode, matching query_table's
// behavior without a FIELD filter; clamp negatives so both paths agree.
const int normalized_batch_size = batch_size < 0 ? 0 : batch_size;
storage::Filter* field_copy = nullptr;
if (field_filter != nullptr) {
field_copy = clone_field_filter(field_filter, err_code);
if (field_copy == nullptr) {
return nullptr;
}
}
*err_code = static_cast<storage::TsFileReader*>(reader)->query(
table_name, column_names, start_time, end_time, offset,
normalized_limit, result_set,
static_cast<storage::Filter*>(tag_filter), field_copy,
normalized_batch_size);
if (*err_code != common::E_OK) {
return nullptr;
}
return result_set;
} catch (const std::bad_alloc&) {
*err_code = common::E_OOM;
return nullptr;
} catch (...) {
*err_code = common::E_FILE_READ_ERR;
return nullptr;
}
}

ResultSet tsfile_reader_query_table_by_row_with_filters(
TsFileReader reader, const char* table_name, char** column_names,
int column_names_len, int offset, int limit, TagFilterHandle tag_filter,
FieldFilterHandle field_filter, int batch_size, ERRNO* err_code) {
if (err_code == nullptr) {
return nullptr;
}
*err_code = common::E_INVALID_ARG;
if (reader == nullptr || table_name == nullptr || column_names == nullptr ||
column_names_len <= 0 || offset < 0) {
return nullptr;
}
try {
std::vector<std::string> columns;
columns.reserve(static_cast<size_t>(column_names_len));
for (int i = 0; i < column_names_len; ++i) {
if (column_names[i] == nullptr) {
return nullptr;
}
columns.emplace_back(column_names[i]);
}
storage::ResultSet* result_set = nullptr;
const int normalized_batch_size = batch_size < 0 ? 0 : batch_size;
storage::Filter* field_copy = nullptr;
if (field_filter != nullptr) {
field_copy = clone_field_filter(field_filter, err_code);
if (field_copy == nullptr) {
return nullptr;
}
}
*err_code = static_cast<storage::TsFileReader*>(reader)->queryByRow(
table_name, columns, offset, limit, result_set,
static_cast<storage::Filter*>(tag_filter), field_copy,
normalized_batch_size);
if (*err_code != common::E_OK) {
return nullptr;
}
return result_set;
} catch (const std::bad_alloc&) {
*err_code = common::E_OOM;
return nullptr;
} catch (...) {
*err_code = common::E_FILE_READ_ERR;
return nullptr;
}
}

#ifdef __cplusplus
}
#endif
Loading
Loading