Skip to content
Open
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
158 changes: 79 additions & 79 deletions cpp/src/cwrapper/tsfile_cwrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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 +742,19 @@ 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);
std::unique_ptr<storage::TagFilter> bound;
if (tag_filter) {
std::shared_ptr<storage::TableSchema> schema;
*err_code = r->get_table_schema(table_name, schema);
if (*err_code != common::E_OK) return nullptr;
*err_code = storage::TagFilter::bind(
*static_cast<const storage::Expression*>(tag_filter), *schema,
bound);
if (*err_code != common::E_OK) return nullptr;
}
*err_code = r->prepare_table_series(table_name, column_name,
bound.get(), prepared);
if (*err_code != common::E_OK) {
return nullptr;
}
Expand Down Expand Up @@ -947,7 +999,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 +1032,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 +1057,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 +2891,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 +2941,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 +2974,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 +3005,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
10 changes: 2 additions & 8 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 @@ -1525,7 +1526,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 +1593,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
15 changes: 3 additions & 12 deletions cpp/src/reader/column_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,9 @@ class ColumnMapping {
return common::E_OK;
}

int add(const Expression& measurementFilter) {
// TODO: get measurements in the filter and add them to
// field_columns_
return common::E_OK;
}

int add_filter_columns(AlignedFieldFilter& filter, TableSchema& schema) {
const int ret = filter.bind(schema);
if (ret != common::E_OK) return ret;
for (const auto& column_name : filter.referenced_columns()) {
field_columns_.insert(column_name);
}
int add(const Expression& expr) {
for (const auto& name : expr.referenced_columns())
field_columns_.insert(name);
return common::E_OK;
}

Expand Down
7 changes: 4 additions & 3 deletions cpp/src/reader/device_meta_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ int DeviceMetaIterator::load_internal_node(MetaIndexNode* meta_index_node) {
void DeviceMetaIterator::try_setup_direct_lookup(MetaIndexNode* root_node) {
if (id_filter_ == nullptr || root_node == nullptr) return;

const auto* eq = dynamic_cast<const TagEq*>(id_filter_);
if (eq == nullptr) return;
const auto* tag = dynamic_cast<const TagFilter*>(id_filter_);
const auto* segment = tag ? tag->exact_device_id_segment() : nullptr;
if (!segment) return;

if (root_node->children_.empty()) return;

Expand All @@ -190,7 +191,7 @@ void DeviceMetaIterator::try_setup_direct_lookup(MetaIndexNode* root_node) {
for (int i = 1; i < actual_segment_count; i++) {
segs[i] = "";
}
segs[eq->col_idx_] = eq->value_;
segs[1] = *segment;
direct_device_id_ = std::make_shared<StringArrayDeviceID>(segs);
direct_root_node_ = root_node;
}
Expand Down
Loading
Loading