Skip to content
Open
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
21 changes: 7 additions & 14 deletions cpp/examples/cpp_examples/demo_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

#include <iostream>
#include <memory>
#include <string>
#include <vector>

Expand All @@ -41,17 +40,12 @@ int demo_read() {
columns.emplace_back("id2");
columns.emplace_back("s1");

std::shared_ptr<storage::TableSchema> table_schema;
HANDLE_ERROR(reader.get_table_schema(table_name, table_schema));
storage::Filter* tag_filter1 =
storage::TagFilterBuilder(table_schema.get()).eq("id1", "id1_filed_1");
storage::Filter* tag_filter2 =
storage::TagFilterBuilder(table_schema.get()).eq("id2", "id1_filed_2");
storage::Filter* tag_filter = storage::TagFilterBuilder(table_schema.get())
.and_filter(tag_filter1, tag_filter2);
// Column vector contains the columns you want to select.
HANDLE_ERROR(
reader.query(table_name, columns, 0, 100, temp_ret, tag_filter));
using E = storage::Expression;
auto tag_filter = E::and_filter(
E::compare("id1", E::CompareOp::EQ, E::Literal::string("id1_filed_1")),
E::compare("id2", E::CompareOp::EQ, E::Literal::string("id1_filed_2")));
HANDLE_ERROR(reader.query(table_name, columns, 0, 100, temp_ret,
tag_filter.get(), nullptr));

// Get query handler.
auto ret = dynamic_cast<storage::TableResultSet*>(temp_ret);
Expand Down Expand Up @@ -104,10 +98,9 @@ int demo_read() {
}

// Close query result set.
ret->close();
reader.destroy_query_data_set(ret);

// Close reader.
reader.close();
delete tag_filter;
return 0;
}
4 changes: 4 additions & 0 deletions cpp/src/common/db_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ struct hash<common::TSDataType> {
#endif

namespace common {
FORCE_INLINE bool is_string_type(TSDataType type) {
return type == STRING || type == TEXT || type == BLOB;
}

FORCE_INLINE const char* get_data_type_name(TSDataType type) {
ASSERT(type >= BOOLEAN && type <= STRING);
return s_data_type_names[type];
Expand Down
150 changes: 70 additions & 80 deletions cpp/src/cwrapper/tsfile_cwrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include "common/statistic.h"
#include "common/tablet.h"
#include "common/tsfile_common.h"
#include "reader/filter/tag_filter.h"
#include "reader/result_set.h"
#include "reader/table_result_set.h"
#include "reader/tsfile_reader.h"
Expand Down Expand Up @@ -109,6 +108,49 @@ ERRNO validate_table_schema(const TableSchema* schema) {
return common::E_OK;
}

storage::Expression::Ptr tag_expression(const char* column, const char* value,
TagFilterOp op) {
using E = storage::Expression;
E::CompareOp comparison;
switch (op) {
case TAG_FILTER_EQ:
comparison = E::CompareOp::EQ;
break;
case TAG_FILTER_NEQ:
comparison = E::CompareOp::NE;
break;
case TAG_FILTER_LT:
comparison = E::CompareOp::LT;
break;
case TAG_FILTER_LTEQ:
comparison = E::CompareOp::LE;
break;
case TAG_FILTER_GT:
comparison = E::CompareOp::GT;
break;
case TAG_FILTER_GTEQ:
comparison = E::CompareOp::GE;
break;
case TAG_FILTER_REGEXP:
case TAG_FILTER_NOT_REGEXP:
return E::regexp(column, value, op == TAG_FILTER_NOT_REGEXP);
case TAG_FILTER_IS_NULL:
return E::is_null(column);
case TAG_FILTER_IS_NOT_NULL:
return E::is_not_null(column);
default:
return nullptr;
}
return E::compare(column, comparison, E::Literal::string(value));
}

TagFilterHandle combine_tag_filters(storage::ExpressionType op,
TagFilterHandle left,
TagFilterHandle right) noexcept {
return new storage::Expression(op, static_cast<storage::Expression*>(left),
static_cast<storage::Expression*>(right));
}

} // namespace

#ifdef __cplusplus
Expand Down Expand Up @@ -699,10 +741,10 @@ PreparedSeriesHandle tsfile_reader_prepare_table_series(
}
try {
std::shared_ptr<storage::PreparedSeries> prepared;
*err_code =
static_cast<storage::TsFileReader*>(reader)->prepare_table_series(
table_name, column_name,
static_cast<storage::Filter*>(tag_filter), prepared);
auto* r = static_cast<storage::TsFileReader*>(reader);
*err_code = r->prepare_table_series(
table_name, column_name,
static_cast<const storage::Expression*>(tag_filter), prepared);
if (*err_code != common::E_OK) {
return nullptr;
}
Expand Down Expand Up @@ -947,7 +989,8 @@ ResultSet tsfile_reader_query_table_by_row(

*err_code = r->queryByRow(
table_name == nullptr ? "" : table_name, columns, offset, limit,
result_set, static_cast<storage::Filter*>(tag_filter), batch_size);
result_set, static_cast<const storage::Expression*>(tag_filter),
nullptr, batch_size);
return result_set;
}

Expand Down Expand Up @@ -979,7 +1022,8 @@ ResultSet tsfile_reader_query_table(TsFileReader reader, const char* table_name,
const int normalized_limit = limit < 0 ? -1 : limit;
*err_code = static_cast<storage::TsFileReader*>(reader)->query(
table_name, columns, start_time, end_time, offset, normalized_limit,
result_set, static_cast<storage::Filter*>(tag_filter), batch_size);
result_set, static_cast<const storage::Expression*>(tag_filter),
nullptr, batch_size);
return result_set;
} catch (const std::bad_alloc&) {
*err_code = common::E_OOM;
Expand All @@ -1003,7 +1047,8 @@ ResultSet tsfile_query_table_batch(TsFileReader reader, const char* table_name,
}
*err_code = r->query(table_name, column_names, start_time, end_time,
table_result_set,
static_cast<storage::Filter*>(tag_filter), batch_size);
static_cast<const storage::Expression*>(tag_filter),
nullptr, batch_size);
return table_result_set;
}

Expand Down Expand Up @@ -2836,46 +2881,11 @@ ERRNO tsfile_tag_filter_create_checked(TsFileReader reader,
if (ret != common::E_OK) {
return ret;
}
storage::TagFilterBuilder builder(schema.get());
storage::Filter* filter = nullptr;
switch (op) {
case TAG_FILTER_EQ:
filter = builder.eq(column_name, value);
break;
case TAG_FILTER_NEQ:
filter = builder.neq(column_name, value);
break;
case TAG_FILTER_LT:
filter = builder.lt(column_name, value);
break;
case TAG_FILTER_LTEQ:
filter = builder.lteq(column_name, value);
break;
case TAG_FILTER_GT:
filter = builder.gt(column_name, value);
break;
case TAG_FILTER_GTEQ:
filter = builder.gteq(column_name, value);
break;
case TAG_FILTER_REGEXP:
filter = builder.reg_exp(column_name, value);
break;
case TAG_FILTER_NOT_REGEXP:
filter = builder.not_reg_exp(column_name, value);
break;
case TAG_FILTER_IS_NULL:
filter = builder.is_null(column_name);
break;
case TAG_FILTER_IS_NOT_NULL:
filter = builder.is_not_null(column_name);
break;
default:
return common::E_INVALID_ARG;
}
if (filter == nullptr) {
auto expr = tag_expression(column_name, value, op);
if (!expr) return common::E_INVALID_ARG;
if (schema->find_id_column_order(column_name) < 0)
return common::E_COLUMN_NOT_EXIST;
}
*out_filter = static_cast<void*>(filter);
*out_filter = expr.release();
return common::E_OK;
} catch (const std::bad_alloc&) {
return common::E_OOM;
Expand Down Expand Up @@ -2921,14 +2931,12 @@ ERRNO tsfile_tag_filter_between_checked(TsFileReader reader,
if (ret != common::E_OK) {
return ret;
}
storage::TagFilterBuilder builder(schema.get());
storage::Filter* filter =
is_not ? builder.not_between_and(column_name, lower, upper)
: builder.between_and(column_name, lower, upper);
if (filter == nullptr) {
if (schema->find_id_column_order(column_name) < 0)
return common::E_COLUMN_NOT_EXIST;
}
*out_filter = static_cast<void*>(filter);
using E = storage::Expression;
*out_filter = E::between(column_name, E::Literal::string(lower),
E::Literal::string(upper), is_not)
.release();
return common::E_OK;
} catch (const std::bad_alloc&) {
return common::E_OOM;
Expand Down Expand Up @@ -2956,42 +2964,23 @@ TagFilterHandle tsfile_tag_filter_between(TsFileReader reader,

TagFilterHandle tsfile_tag_filter_and(TagFilterHandle left,
TagFilterHandle right) {
if (!left || !right) return nullptr;
try {
return storage::TagFilterBuilder::and_filter(
static_cast<storage::Filter*>(left),
static_cast<storage::Filter*>(right));
} catch (...) {
return nullptr;
}
if (!left || !right || left == right) return nullptr;
return combine_tag_filters(storage::AND_EXPR, left, right);
}

TagFilterHandle tsfile_tag_filter_or(TagFilterHandle left,
TagFilterHandle right) {
if (!left || !right) return nullptr;
try {
return storage::TagFilterBuilder::or_filter(
static_cast<storage::Filter*>(left),
static_cast<storage::Filter*>(right));
} catch (...) {
return nullptr;
}
if (!left || !right || left == right) return nullptr;
return combine_tag_filters(storage::OR_EXPR, left, right);
}

TagFilterHandle tsfile_tag_filter_not(TagFilterHandle filter) {
if (!filter) return nullptr;
try {
return storage::TagFilterBuilder::not_filter(
static_cast<storage::Filter*>(filter));
} catch (...) {
return nullptr;
}
return combine_tag_filters(storage::ExpressionType::NOT, filter, nullptr);
}

void tsfile_tag_filter_free(TagFilterHandle filter) {
if (filter) {
delete static_cast<storage::Filter*>(filter);
}
delete static_cast<storage::Expression*>(filter);
}

ResultSet tsfile_query_table_with_tag_filter(
Expand All @@ -3006,7 +2995,8 @@ ResultSet tsfile_query_table_with_tag_filter(
}
*err_code = r->query(table_name, column_names, start_time, end_time,
table_result_set,
static_cast<storage::Filter*>(tag_filter), batch_size);
static_cast<const storage::Expression*>(tag_filter),
nullptr, batch_size);
return table_result_set;
}

Expand Down
14 changes: 8 additions & 6 deletions cpp/src/cwrapper/tsfile_cwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -1338,22 +1338,24 @@ TagFilterHandle tsfile_tag_filter_gteq(TsFileReader reader,
const char* value);

/**
* @brief Logical AND of two tag filters. Takes ownership of left and right
* only on success. Returns NULL on failure; the caller still owns the inputs.
* @brief Logical AND of two tag filters. The result owns both inputs.
* Returns NULL for a NULL input or identical handles. In that case the caller
* retains ownership. Allocation failure terminates the process.
*/
TagFilterHandle tsfile_tag_filter_and(TagFilterHandle left,
TagFilterHandle right);

/**
* @brief Logical OR of two tag filters. Takes ownership of left and right
* only on success. Returns NULL on failure; the caller still owns the inputs.
* @brief Logical OR of two tag filters. The result owns both inputs.
* Returns NULL for a NULL input or identical handles. In that case the caller
* retains ownership. Allocation failure terminates the process.
*/
TagFilterHandle tsfile_tag_filter_or(TagFilterHandle left,
TagFilterHandle right);

/**
* @brief Logical NOT of a tag filter. Takes ownership of filter only on
* success. Returns NULL on failure; the caller still owns the input.
* @brief Logical NOT of a tag filter. The result owns the input.
* A NULL input returns NULL. Allocation failure terminates the process.
*/
TagFilterHandle tsfile_tag_filter_not(TagFilterHandle filter);

Expand Down
27 changes: 12 additions & 15 deletions cpp/src/reader/aligned_chunk_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <algorithm>
#include <limits>
#include <set>
#include <type_traits>

#include "common/global.h"
Expand Down Expand Up @@ -1241,7 +1242,8 @@ int AlignedChunkReader::get_next_page(TsBlock* ret_tsblock,
// ══════════════════════════════════════════════════════════════════════════

int AlignedChunkReader::load_by_aligned_meta_multi(
ChunkMeta* time_chunk_meta, const std::vector<ChunkMeta*>& value_metas) {
ChunkMeta* time_chunk_meta, const std::vector<ChunkMeta*>& value_metas,
bool field_all_match) {
int ret = E_OK;
multi_value_mode_ = true;
time_chunk_meta_ = time_chunk_meta;
Expand All @@ -1253,6 +1255,8 @@ int AlignedChunkReader::load_by_aligned_meta_multi(
page_time_count_ = 0;
page_time_cursor_ = 0;

chunk_field_all_match_ = field_all_match;

// ── Load time chunk header ──
file_data_time_buf_size_ = 1024;
int32_t ret_read_len = 0;
Expand Down Expand Up @@ -1525,7 +1529,7 @@ void AlignedChunkReader::build_filter_column_mask() {
filter_column_mask_.assign(value_columns_.size(), false);
if (field_filter_ == nullptr) return;
std::set<std::string> filter_columns;
for (const auto& name : field_filter_->referenced_columns()) {
for (const auto& name : field_filter_->expression().referenced_columns()) {
filter_columns.insert(name);
}
for (uint32_t c = 0; c < value_columns_.size(); ++c) {
Expand Down Expand Up @@ -1592,13 +1596,6 @@ int AlignedChunkReader::build_page_plan(Filter* filter, int& row_offset) {
current_page_plan_index_ = 0;
current_page_loaded_ = false;
page_plan_built_ = false;
// A structurally broken predicate (e.g. an OR missing one operand) must
// fail here rather than be classified by page statistics: statistics-based
// FULL_PASS would otherwise return rows without ever running evaluation.
if (field_filter_ != nullptr && !field_filter_->is_valid()) {
return E_INVALID_ARG;
}

const uint32_t num_cols = value_columns_.size();
// For single-page chunks the times decoded while classifying the page are
// exactly the ones the lazy decode path would decode again; keep them so
Expand Down Expand Up @@ -1654,8 +1651,8 @@ int AlignedChunkReader::build_page_plan(Filter* filter, int& row_offset) {
}

bool field_page_skip = false;
bool field_all_match = false;
if (field_filter_ != nullptr) {
bool field_all_match = chunk_field_all_match_;
if (field_filter_ && !field_all_match) {
std::map<std::string, Statistic*> field_statistics;
for (uint32_t c = 0; c < num_cols; ++c) {
auto* col = value_columns_[c];
Expand All @@ -1667,11 +1664,11 @@ int AlignedChunkReader::build_page_plan(Filter* filter, int& row_offset) {
cur_time_page_header_.statistic_ != nullptr
? cur_time_page_header_.statistic_->count_
: 0;
const auto match =
field_filter_->classify(field_statistics, row_count);
field_page_skip =
!field_filter_->may_match(field_statistics, row_count);
field_all_match =
!field_page_skip &&
field_filter_->all_match(field_statistics, row_count);
match == AlignedFieldFilter::StatisticsMatch::None;
field_all_match = match == AlignedFieldFilter::StatisticsMatch::All;
}

Statistic* stat = cur_time_page_header_.statistic_;
Expand Down
Loading
Loading