Skip to content
Merged
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
58 changes: 56 additions & 2 deletions cpp/src/cwrapper/tsfile_cwrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,40 @@ PreparedSeriesHandle tsfile_reader_prepare_series(
return handle;
}

PreparedSeriesHandle tsfile_reader_prepare_table_series(
TsFileReader reader, const char* table_name, const char* column_name,
TagFilterHandle tag_filter, ERRNO* err_code) {
if (err_code == nullptr) {
return nullptr;
}
*err_code = common::E_INVALID_ARG;
if (reader == nullptr || table_name == nullptr || column_name == nullptr) {
return nullptr;
}
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);
if (*err_code != common::E_OK) {
return nullptr;
}
auto* handle = new (std::nothrow)
std::shared_ptr<storage::PreparedSeries>(std::move(prepared));
if (handle == nullptr) {
*err_code = common::E_OOM;
}
return handle;
} catch (const std::bad_alloc&) {
*err_code = common::E_OOM;
return nullptr;
} catch (...) {
*err_code = common::E_FILE_READ_ERR;
return nullptr;
}
}

PreparedSeriesHandle tsfile_reader_prepare_series_with_time_owner(
TsFileReader reader, const TsFilePreparedLocator* locator,
PreparedSeriesHandle aligned_time_owner, bool trust_index,
Expand Down Expand Up @@ -739,6 +773,16 @@ ResultSet tsfile_reader_query_prepared(TsFileReader reader,
PreparedSeriesHandle prepared,
Timestamp start_time, Timestamp end_time,
int offset, int limit, ERRNO* err_code) {
return tsfile_reader_query_prepared_ex(
reader, prepared, start_time, end_time, offset, limit, 0, err_code);
}

ResultSet tsfile_reader_query_prepared_ex(TsFileReader reader,
PreparedSeriesHandle prepared,
Timestamp start_time,
Timestamp end_time, int offset,
int limit, uint32_t block_rows,
ERRNO* err_code) {
if (err_code == nullptr) {
return nullptr;
}
Expand All @@ -750,14 +794,23 @@ ResultSet tsfile_reader_query_prepared(TsFileReader reader,
static_cast<std::shared_ptr<storage::PreparedSeries>*>(prepared);
storage::ResultSet* result = nullptr;
*err_code = static_cast<storage::TsFileReader*>(reader)->query_prepared(
*handle, start_time, end_time, offset, limit, result);
*handle, start_time, end_time, offset, limit, result, block_rows);
return result;
}

ResultSet tsfile_reader_query_prepared_multi(
TsFileReader reader, const PreparedSeriesHandle* prepared,
uint32_t prepared_count, Timestamp start_time, Timestamp end_time,
int offset, int limit, ERRNO* err_code) {
return tsfile_reader_query_prepared_multi_ex(
reader, prepared, prepared_count, start_time, end_time, offset, limit,
0, err_code);
}

ResultSet tsfile_reader_query_prepared_multi_ex(
TsFileReader reader, const PreparedSeriesHandle* prepared,
uint32_t prepared_count, Timestamp start_time, Timestamp end_time,
int offset, int limit, uint32_t block_rows, ERRNO* err_code) {
if (err_code == nullptr) {
return nullptr;
}
Expand All @@ -780,7 +833,8 @@ ResultSet tsfile_reader_query_prepared_multi(
storage::ResultSet* result = nullptr;
*err_code =
static_cast<storage::TsFileReader*>(reader)->query_prepared_multi(
native_prepared, start_time, end_time, offset, limit, result);
native_prepared, start_time, end_time, offset, limit, result,
block_rows);
return result;
}

Expand Down
29 changes: 29 additions & 0 deletions cpp/src/cwrapper/tsfile_cwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,15 @@ PreparedSeriesHandle tsfile_reader_prepare_series_with_time_owner(
*/
void tsfile_prepared_series_free(PreparedSeriesHandle prepared);

/**
* Resolve one table column selected by a tag filter into a reusable series.
* The filter must match exactly one device; otherwise E_NOT_SUPPORT is
* returned and the caller should fall back to the regular table query.
*/
PreparedSeriesHandle tsfile_reader_prepare_table_series(
TsFileReader reader, const char* table_name, const char* column_name,
TagFilterHandle tag_filter, ERRNO* err_code);

/**
* Query a prepared series without traversing the TsFile footer index.
* An inverted time range succeeds with an empty result set.
Expand All @@ -851,6 +860,17 @@ ResultSet tsfile_reader_query_prepared(TsFileReader reader,
Timestamp start_time, Timestamp end_time,
int offset, int limit, ERRNO* err_code);

/**
* Same as tsfile_reader_query_prepared, with an explicit TsBlock row
* capacity. block_rows == 0 keeps the default capacity.
*/
ResultSet tsfile_reader_query_prepared_ex(TsFileReader reader,
PreparedSeriesHandle prepared,
Timestamp start_time,
Timestamp end_time, int offset,
int limit, uint32_t block_rows,
ERRNO* err_code);

/**
* Query multiple aligned prepared value columns sharing one time axis.
* An inverted time range succeeds with an empty result set.
Expand All @@ -860,6 +880,15 @@ ResultSet tsfile_reader_query_prepared_multi(
uint32_t prepared_count, Timestamp start_time, Timestamp end_time,
int offset, int limit, ERRNO* err_code);

/**
* Same as tsfile_reader_query_prepared_multi, with an explicit TsBlock row
* capacity. block_rows == 0 keeps the default capacity.
*/
ResultSet tsfile_reader_query_prepared_multi_ex(
TsFileReader reader, const PreparedSeriesHandle* prepared,
uint32_t prepared_count, Timestamp start_time, Timestamp end_time,
int offset, int limit, uint32_t block_rows, ERRNO* err_code);

/**
* @brief Queries time series data from a specific table within time range.
*
Expand Down
18 changes: 15 additions & 3 deletions cpp/src/file/tsfile_io_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,21 @@ int TsFileIOReader::alloc_prepared_ssi(
if (prepared == nullptr || prepared->index() == nullptr) {
return E_INVALID_ARG;
}
if (time_filter != nullptr &&
!filter_stasify(prepared->index(), time_filter)) {
return E_NO_MORE_DATA;
if (time_filter != nullptr) {
// A device's value statistics exclude NULL cells, but their timestamps
// still belong to the aligned rows the query must return. Prune on the
// shared time index instead, which has no NULLs; this mirrors
// alloc_prepared_multi_ssi() and the aligned page/chunk filtering.
ITimeseriesIndex* filter_index = prepared->index();
if (auto* aligned =
dynamic_cast<AlignedTimeseriesIndex*>(filter_index)) {
if (aligned->time_ts_idx_ != nullptr) {
filter_index = aligned->time_ts_idx_;
}
}
if (!filter_stasify(filter_index, time_filter)) {
return E_NO_MORE_DATA;
}
}
void* memory =
mem_alloc(sizeof(TsFileSeriesScanIterator), MOD_TSFILE_READER);
Expand Down
33 changes: 29 additions & 4 deletions cpp/src/reader/block/prepared_series_tsblock_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ common::TSDataType prepared_value_data_type(ITimeseriesIndex* index) {

int PreparedSeriesTsBlockReader::init(
TsFileIOReader* io_reader, const std::shared_ptr<PreparedSeries>& prepared,
Filter* owned_time_filter, int offset, int limit) {
Filter* owned_time_filter, int offset, int limit, uint32_t block_rows) {
owned_time_filter_ = owned_time_filter;
block_rows_ = block_rows == 0 ? PREPARED_BATCH_ROWS : block_rows;
if (io_reader == nullptr || prepared == nullptr ||
prepared->index() == nullptr || offset < 0 || closed_) {
return common::E_INVALID_ARG;
Expand Down Expand Up @@ -92,8 +93,9 @@ int PreparedSeriesTsBlockReader::init(
int PreparedSeriesTsBlockReader::init_multi(
TsFileIOReader* io_reader,
const std::vector<std::shared_ptr<PreparedSeries>>& prepared,
Filter* owned_time_filter, int offset, int limit) {
Filter* owned_time_filter, int offset, int limit, uint32_t block_rows) {
owned_time_filter_ = owned_time_filter;
block_rows_ = block_rows == 0 ? PREPARED_BATCH_ROWS : block_rows;
if (io_reader == nullptr || prepared.empty() || offset < 0 || closed_) {
return common::E_INVALID_ARG;
}
Expand Down Expand Up @@ -149,9 +151,9 @@ int PreparedSeriesTsBlockReader::has_next(bool& has_next) {
while (true) {
const uint32_t desired_rows =
remaining_limit_ > 0
? std::min<uint32_t>(PREPARED_BATCH_ROWS,
? std::min<uint32_t>(block_rows_,
static_cast<uint32_t>(remaining_limit_))
: PREPARED_BATCH_ROWS;
: block_rows_;
if (block_ != nullptr) {
if (block_->get_max_row_count() != desired_rows) {
ssi_->revert_tsblock();
Expand All @@ -169,6 +171,29 @@ int PreparedSeriesTsBlockReader::has_next(bool& has_next) {
if (ret != common::E_OK) {
return ret;
}
// A page boundary can leave the block short of its capacity, while the
// regular table reader keeps filling it from the next page. Continue
// decoding into the same block so cached queries keep the caller's
// batch_size block boundaries.
while (block_ != nullptr && block_->get_row_count() > 0 &&
block_->get_row_count() < desired_rows) {
const uint32_t rows_before = block_->get_row_count();
int more = ssi_->get_next(block_, true);
if (more == common::E_NO_MORE_DATA) {
break;
}
if (more != common::E_OK) {
return more;
}
if (block_ == nullptr) {
break;
}
if (block_->get_row_count() == rows_before) {
// No row was appended; stop instead of spinning if the reader
// cannot make progress on the same block.
break;
}
}
if (block_ != nullptr && block_->get_row_count() > 0) {
if (remaining_limit_ > 0) {
remaining_limit_ -= static_cast<int>(block_->get_row_count());
Expand Down
7 changes: 5 additions & 2 deletions cpp/src/reader/block/prepared_series_tsblock_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ class PreparedSeriesTsBlockReader final : public TsBlockReader {

int init(TsFileIOReader* io_reader,
const std::shared_ptr<PreparedSeries>& prepared,
Filter* owned_time_filter, int offset, int limit);
Filter* owned_time_filter, int offset, int limit,
uint32_t block_rows = 0);
int init_multi(TsFileIOReader* io_reader,
const std::vector<std::shared_ptr<PreparedSeries>>& prepared,
Filter* owned_time_filter, int offset, int limit);
Filter* owned_time_filter, int offset, int limit,
uint32_t block_rows = 0);

int has_next(bool& has_next) override;
int next(common::TsBlock*& ret_block) override;
Expand All @@ -64,6 +66,7 @@ class PreparedSeriesTsBlockReader final : public TsBlockReader {
common::TsBlock* block_ = nullptr;
common::TSDataType value_data_type_ = common::INVALID_DATATYPE;
std::vector<common::TSDataType> value_data_types_;
uint32_t block_rows_ = 0;
int remaining_limit_ = -1;
bool block_ready_ = false;
bool exhausted_ = false;
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/reader/tsfile_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ int TsFileExecutor::prepare_series(
int TsFileExecutor::execute_prepared(
const std::shared_ptr<PreparedSeries>& prepared, int64_t start_time,
int64_t end_time, int offset, int limit, const std::string& column_name,
ResultSet*& ret_qds) {
ResultSet*& ret_qds, uint32_t block_rows) {
ASSERT(is_inited_);
ret_qds = nullptr;
if (prepared == nullptr || offset < 0) {
Expand All @@ -135,7 +135,7 @@ int TsFileExecutor::execute_prepared(
new PreparedSeriesTsBlockReader());
int ret = tsblock_reader->init(&io_reader_, prepared,
new TimeBetween(start_time, end_time, false),
offset, limit);
offset, limit, block_rows);
if (ret != E_OK) {
return ret;
}
Expand All @@ -150,7 +150,7 @@ int TsFileExecutor::execute_prepared(
int TsFileExecutor::execute_prepared_multi(
const std::vector<std::shared_ptr<PreparedSeries>>& prepared,
int64_t start_time, int64_t end_time, int offset, int limit,
ResultSet*& ret_qds) {
ResultSet*& ret_qds, uint32_t block_rows) {
ASSERT(is_inited_);
ret_qds = nullptr;
if (prepared.empty() || offset < 0) {
Expand All @@ -161,7 +161,7 @@ int TsFileExecutor::execute_prepared_multi(
new PreparedSeriesTsBlockReader());
int ret = tsblock_reader->init_multi(
&io_reader_, prepared, new TimeBetween(start_time, end_time, false),
offset, limit);
offset, limit, block_rows);
if (ret != E_OK) {
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/reader/tsfile_executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class TsFileExecutor // : public QueryExecutor
int execute_prepared(const std::shared_ptr<PreparedSeries>& prepared,
int64_t start_time, int64_t end_time, int offset,
int limit, const std::string& column_name,
ResultSet*& ret_qds);
ResultSet*& ret_qds, uint32_t block_rows = 0);
int execute_prepared_multi(
const std::vector<std::shared_ptr<PreparedSeries>>& prepared,
int64_t start_time, int64_t end_time, int offset, int limit,
ResultSet*& ret_qds);
ResultSet*& ret_qds, uint32_t block_rows = 0);
void destroy_query_data_set(ResultSet* qds);
int get_tsfile_meta(TsFileMeta*& tsfile_meta) {
return io_reader_.get_tsfile_meta(tsfile_meta);
Expand Down
Loading
Loading