From e2e9c3786f9753ec5d5c1b813fd2f1fb0d71d5b8 Mon Sep 17 00:00:00 2001 From: HUANG Xiangdong <1021782+jixuan1989@users.noreply.github.com> Date: Sun, 18 Oct 2026 07:57:48 +0800 Subject: [PATCH 01/16] [C++] Apply field predicates to aligned table query rows --- .../block/single_device_tsblock_reader.cc | 56 ++- .../block/single_device_tsblock_reader.h | 1 + cpp/src/reader/filter/table_field_filter.cc | 61 +++ cpp/src/reader/filter/table_field_filter.h | 90 ++++ cpp/test/reader/table_field_filter_test.cc | 470 ++++++++++++++++++ 5 files changed, 670 insertions(+), 8 deletions(-) create mode 100644 cpp/src/reader/filter/table_field_filter.cc create mode 100644 cpp/src/reader/filter/table_field_filter.h create mode 100644 cpp/test/reader/table_field_filter_test.cc diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index 20431c481..a53b421a8 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -24,6 +24,7 @@ #include #include "common/db_common.h" +#include "reader/filter/table_field_filter.h" namespace storage { @@ -239,7 +240,7 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, break; } } - if (can_multi) { + if (can_multi && field_filter == nullptr) { std::vector meas_names(meas_cols.begin(), meas_cols.end()); // Stable order by first appearance in the result schema so the @@ -365,7 +366,8 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } // Detect aligned fast path: every field column comes from an aligned chunk. - if (!field_column_contexts_.empty() && dense_row_count_ >= 0 && + if (field_filter == nullptr && !field_column_contexts_.empty() && + dense_row_count_ >= 0 && aligned_col_count_ == field_column_contexts_.size()) { all_aligned_ = true; aligned_vec_.reserve(field_column_contexts_.size()); @@ -476,7 +478,7 @@ int SingleDeviceTsBlockReader::has_next(bool& has_next) { break; } - if (remaining_offset_ > 0) { + if (remaining_offset_ > 0 && field_filter_ == nullptr) { for (auto* col_ctx : min_time_columns) { int advance_ret = advance_column(col_ctx); if (IS_FAIL(advance_ret)) return advance_ret; @@ -636,8 +638,16 @@ int SingleDeviceTsBlockReader::has_next_aligned(bool& result_has_next) { int SingleDeviceTsBlockReader::fill_measurements( std::vector& column_contexts) { int ret = common::E_OK; - if (field_filter_ == - nullptr /*TODO: || field_filter_->satisfy(column_contexts)*/) { + bool satisfied = true; + if (field_filter_ != nullptr) { + if (RET_FAIL(satisfy_field_filter(satisfied))) return ret; + // OFFSET and LIMIT count rows that passed the predicate. + if (satisfied && remaining_offset_ > 0) { + --remaining_offset_; + satisfied = false; + } + } + if (satisfied) { row_appender_->add_row(); if (!col_appenders_[time_column_index_]->add_row()) { assert(false); @@ -654,9 +664,7 @@ int SingleDeviceTsBlockReader::fill_measurements( } for (auto& column_context : column_contexts) { column_context->fill_into(col_appenders_); - if (RET_FAIL(advance_column(column_context))) { - break; - } + if (RET_FAIL(advance_column(column_context))) return ret; } // Align all columns, filling with nulls where data is missing. @@ -673,10 +681,42 @@ int SingleDeviceTsBlockReader::fill_measurements( col_appender->append_null(); } } + } else { + // Rejected rows must also advance, including the last row in a page. + for (auto* column_context : column_contexts) { + if (RET_FAIL(advance_column(column_context))) return ret; + } } return ret; } +int SingleDeviceTsBlockReader::satisfy_field_filter(bool& satisfied) { + int ret = common::E_OK; + std::vector fields; + const auto& names = + device_query_task_->get_column_mapping()->get_measurement_columns(); + fields.reserve(names.size()); + for (const auto& name : names) { + char* value = nullptr; + uint32_t length = 0; + auto context = field_column_contexts_.find(name); + if (context != field_column_contexts_.end()) { + int64_t time = 0; + ret = context->second->get_current_time(time); + if (ret != common::E_OK && ret != common::E_NO_MORE_DATA) + return ret; + if (ret == common::E_OK && time == next_time_ && + RET_FAIL(context->second->get_current_value(value, length))) + return ret; + } + auto schema = + device_query_task_->get_table_schema()->get_column_schema(name); + fields.push_back({name, schema.data_type_, value, length}); + } + return evaluate_table_field_filter(*field_filter_, {next_time_, fields}, + satisfied); +} + int SingleDeviceTsBlockReader::advance_column( MeasurementColumnContext* column_context) { int ret = column_context->move_iter(); diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index 4bf0cca0f..eb58d40ad 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -62,6 +62,7 @@ class SingleDeviceTsBlockReader : public TsBlockReader { int fill_measurements( std::vector& column_contexts); int fill_ids(); + int satisfy_field_filter(bool& satisfied); int advance_column(MeasurementColumnContext* column_context); int32_t compute_dense_row_count( const std::vector& ts_indexes); diff --git a/cpp/src/reader/filter/table_field_filter.cc b/cpp/src/reader/filter/table_field_filter.cc new file mode 100644 index 000000000..5c385139d --- /dev/null +++ b/cpp/src/reader/filter/table_field_filter.cc @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "reader/filter/table_field_filter.h" + +namespace storage { +namespace { +template +int evaluate_integer(Filter& filter, const TableFieldRow& row, + bool& satisfied) { + T value; + if (!row.fields[0].get(value)) return common::E_DECODE_ERR; + satisfied = filter.satisfy(row.timestamp, static_cast(value)); + return common::E_OK; +} +} // namespace + +int evaluate_table_field_filter(Filter& filter, const TableFieldRow& row, + bool& satisfied) { + satisfied = false; + if (auto* row_filter = dynamic_cast(&filter)) { + return row_filter->satisfy_row(row, satisfied); + } + if (row.fields.size() != 1) return common::E_NOT_SUPPORT; + const auto& field = row.fields[0]; + if (field.is_null()) return common::E_OK; + switch (field.type) { + case common::BOOLEAN: + return evaluate_integer(filter, row, satisfied); + case common::DATE: + case common::INT32: + return evaluate_integer(filter, row, satisfied); + case common::TIMESTAMP: + case common::INT64: + return evaluate_integer(filter, row, satisfied); + case common::TEXT: + case common::STRING: + case common::BLOB: + satisfied = filter.satisfy(row.timestamp, + common::String(field.data, field.size)); + return common::E_OK; + default: + return common::E_NOT_SUPPORT; + } +} +} // namespace storage diff --git a/cpp/src/reader/filter/table_field_filter.h b/cpp/src/reader/filter/table_field_filter.h new file mode 100644 index 000000000..64ca4f17c --- /dev/null +++ b/cpp/src/reader/filter/table_field_filter.h @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#ifndef READER_TABLE_FIELD_FILTER_H +#define READER_TABLE_FIELD_FILTER_H + +#include +#include +#include +#include + +#include "reader/filter/filter.h" +#include "utils/errno_define.h" + +namespace storage { + +// Borrowed views: names and bytes are valid only during satisfy_row(). +// A null data pointer denotes NULL; size preserves embedded NUL bytes. +struct TableFieldValue { + const std::string& name; + common::TSDataType type; + const char* data; + uint32_t size; + + bool is_null() const { return data == nullptr; } + + // Exact native primitive types; string-like values use data and size. + template + bool get(T& value) const { + const bool matches = + (std::is_same::value && type == common::BOOLEAN) || + (std::is_same::value && + (type == common::INT32 || type == common::DATE)) || + (std::is_same::value && + (type == common::INT64 || type == common::TIMESTAMP)) || + (std::is_same::value && type == common::FLOAT) || + (std::is_same::value && type == common::DOUBLE); + if (is_null() || !matches || size != sizeof(T)) { + return false; + } + std::memcpy(&value, data, sizeof(T)); + return true; + } +}; + +struct TableFieldRow { + int64_t timestamp; + // Unique projected FIELDs with normalized names, in unspecified order. + const std::vector& fields; + + const TableFieldValue* find(const std::string& name) const { + for (const auto& field : fields) { + if (field.name == name) return &field; + } + return nullptr; + } +}; + +// Row predicates can inspect all projected FIELD columns, including NULLs. +// TAG predicates belong in query()'s tag_filter argument. Unprojected fields +// are not loaded automatically. An error is propagated through ResultSet. +class TableFieldFilter : public Filter { + public: + virtual int satisfy_row(const TableFieldRow& row, + bool& satisfied) const = 0; +}; + +// Legacy Filter callbacks support one selected integer/Boolean or string-like +// FIELD. NULL does not satisfy a legacy value predicate. Other legacy shapes +// return E_NOT_SUPPORT; use TableFieldFilter for floats or multiple fields. +int evaluate_table_field_filter(Filter& filter, const TableFieldRow& row, + bool& satisfied); + +} // namespace storage +#endif // READER_TABLE_FIELD_FILTER_H diff --git a/cpp/test/reader/table_field_filter_test.cc b/cpp/test/reader/table_field_filter_test.cc new file mode 100644 index 000000000..b22b2547f --- /dev/null +++ b/cpp/test/reader/table_field_filter_test.cc @@ -0,0 +1,470 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "reader/filter/table_field_filter.h" + +#include + +#include +#include +#include +#include + +#include "common/config/config.h" +#include "common/tablet.h" +#include "file/local_random_access_read_file.h" +#include "file/write_file.h" +#include "reader/filter/tag_filter.h" +#include "reader/filter/time_filter.h" +#include "reader/filter/time_operator.h" +#include "reader/table_query_executor.h" +#include "writer/tsfile_table_writer.h" + +namespace { +using namespace common; +using namespace storage; +using Predicate = std::function; + +class RowPredicate : public TableFieldFilter { + public: + explicit RowPredicate(Predicate predicate) : predicate_(predicate) {} + int satisfy_row(const TableFieldRow& row, bool& accepted) const override { + return predicate_(row, accepted); + } + + private: + Predicate predicate_; +}; + +class LegacyPredicate : public Filter { + public: + int calls = 0; + int64_t time = 0; + int64_t number = 0; + std::string bytes; + bool accept = true; + bool satisfy(int64_t timestamp, int64_t value) override { + ++calls; + time = timestamp; + number = value; + return accept; + } + bool satisfy(int64_t timestamp, String value) override { + ++calls; + time = timestamp; + bytes.assign(value.buf_, value.len_); + return accept; + } +}; + +TEST(TableFieldFilterValueTest, LegacyDispatchPreservesTypesAndBytes) { + LegacyPredicate filter; + bool accepted = false; + const int64_t timestamp = INT64_C(1099511627776); + const std::string name = "v"; + const bool flag = true; + const int32_t small = -123; + const int64_t large = INT64_MIN; + for (TSDataType type : {BOOLEAN, INT32, DATE, INT64, TIMESTAMP}) { + const char* value = type == BOOLEAN + ? reinterpret_cast(&flag) + : type == INT32 || type == DATE + ? reinterpret_cast(&small) + : reinterpret_cast(&large); + const uint32_t size = type == BOOLEAN ? sizeof(flag) + : type == INT32 || type == DATE ? sizeof(small) + : sizeof(large); + std::vector fields{{name, type, value, size}}; + EXPECT_EQ( + evaluate_table_field_filter(filter, {timestamp, fields}, accepted), + E_OK); + EXPECT_TRUE(accepted); + EXPECT_EQ(filter.time, timestamp); + EXPECT_EQ(filter.number, type == BOOLEAN ? 1 + : type == INT32 || type == DATE ? small + : large); + fields[0].size = 0; + EXPECT_EQ( + evaluate_table_field_filter(filter, {timestamp, fields}, accepted), + E_DECODE_ERR); + EXPECT_FALSE(accepted); + } + for (TSDataType type : {STRING, TEXT, BLOB}) { + const std::string bytes("a\0b", 3); + std::vector fields{{name, type, bytes.data(), 3}}; + EXPECT_EQ( + evaluate_table_field_filter(filter, {timestamp, fields}, accepted), + E_OK); + EXPECT_EQ(filter.bytes, bytes); + fields[0].size = 0; + EXPECT_EQ( + evaluate_table_field_filter(filter, {timestamp, fields}, accepted), + E_OK); + EXPECT_TRUE(filter.bytes.empty()); + } +} + +TEST(TableFieldFilterValueTest, InvalidLegacyShapesAndSafeGetters) { + LegacyPredicate filter; + bool accepted = true; + const std::string name = "v"; + int64_t number = 42; + std::vector fields{{name, INT64, nullptr, 8}}; + EXPECT_EQ(evaluate_table_field_filter(filter, {0, fields}, accepted), E_OK); + EXPECT_FALSE(accepted); + EXPECT_EQ(filter.calls, 0); + int64_t output = 7; + EXPECT_FALSE(fields[0].get(output)); + fields[0].data = reinterpret_cast(&number); + EXPECT_TRUE(fields[0].get(output)); + EXPECT_EQ(output, 42); + float wrong_type = 0; + EXPECT_FALSE(fields[0].get(wrong_type)); + fields[0].size = sizeof(int32_t); + int32_t truncated = 0; + EXPECT_FALSE(fields[0].get(truncated)); + fields[0].type = DOUBLE; + EXPECT_FALSE(fields[0].get(wrong_type)); + fields[0].type = FLOAT; + EXPECT_EQ(evaluate_table_field_filter(filter, {0, fields}, accepted), + E_NOT_SUPPORT); + fields.clear(); + EXPECT_EQ(evaluate_table_field_filter(filter, {0, fields}, accepted), + E_NOT_SUPPORT); + fields.push_back({name, INT64, nullptr, 0}); + fields.push_back({name, INT64, nullptr, 0}); + EXPECT_EQ(evaluate_table_field_filter(filter, {0, fields}, accepted), + E_NOT_SUPPORT); + EXPECT_EQ((TableFieldRow{0, fields}).find("absent"), nullptr); +} + +// A small page and explicit flush force rejection/acceptance at page and +// chunk boundaries. Both devices contain sparse NULLs and binary values. +class TableFieldFilterTest + : public ::testing::TestWithParam> { + protected: + void SetUp() override { + libtsfile_init(); + saved_ = g_config_value_; + g_config_value_.page_writer_max_point_num_ = 3; + g_config_value_.parallel_read_enabled_ = std::get<0>(GetParam()); + filename_ = + "table_field_filter_" + + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()) + + ".tsfile"; + WriteFile file; + int flags = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + flags |= O_BINARY; +#endif + ASSERT_EQ(file.create(filename_, flags, 0666), E_OK); + std::vector columns; + columns.emplace_back("id", STRING, UNCOMPRESSED, PLAIN, + ColumnCategory::TAG); + const std::vector names{"v", "i", "flag", "f", + "d", "s", "text", "blob", + "date", "stamp", "empty"}; + const std::vector types{INT64, INT32, BOOLEAN, FLOAT, + DOUBLE, STRING, TEXT, BLOB, + DATE, TIMESTAMP, INT64}; + for (size_t i = 0; i < names.size(); ++i) + columns.emplace_back(names[i], types[i], UNCOMPRESSED, PLAIN, + ColumnCategory::FIELD); + TableSchema schema("t", columns); + TsFileTableWriter writer(&file, &schema); + for (int chunk = 0; chunk < 2; ++chunk) { + Tablet tablet("t", schema.get_measurement_names(), + schema.get_data_types(), + schema.get_column_categories(), 14); + for (int device = 0; device < 2; ++device) { + for (int row = 0; row < 7; ++row) { + int index = device * 7 + row; + int t = chunk * 7 + row; + ASSERT_EQ(tablet.add_timestamp(index, base_ + t), E_OK); + const std::string id = "d" + std::to_string(device); + ASSERT_EQ(tablet.add_value(index, "id", id.c_str()), E_OK); + ASSERT_EQ( + tablet.add_value(index, "v", int64_t(t + device * 100)), + E_OK); + if (t % 3 != 0) + ASSERT_EQ(tablet.add_value(index, "i", int32_t(-t)), + E_OK); + ASSERT_EQ(tablet.add_value(index, "flag", bool(t % 2)), + E_OK); + ASSERT_EQ(tablet.add_value(index, "f", float(t) + 0.25f), + E_OK); + ASSERT_EQ(tablet.add_value(index, "d", -double(t) - 0.5), + E_OK); + const std::string bytes = + t % 2 ? std::string("a\0b", 3) : std::string(); + for (const std::string& name : {"s", "text", "blob"}) + ASSERT_EQ(tablet.add_value( + index, name, + String(bytes.data(), bytes.size())), + E_OK); + ASSERT_EQ( + tablet.add_value(index, "date", int32_t(20260101 + t)), + E_OK); + ASSERT_EQ( + tablet.add_value(index, "stamp", int64_t(-base_ - t)), + E_OK); + } + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.flush(), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + } + void TearDown() override { + std::remove(filename_.c_str()); + g_config_value_ = saved_; + libtsfile_destroy(); + } + std::vector scan(const std::vector& columns, + Filter* filter, int offset = 0, int limit = -1, + Filter* time = nullptr, Filter* tag = nullptr, + int expected_error = E_OK) { + std::vector times; + LocalRandomAccessReadFile file; + EXPECT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + EXPECT_EQ(executor.query("t", columns, time, tag, filter, offset, limit, + result), + E_OK); + if (!result) return times; + bool next = false; + int ret = E_OK; + while ((ret = result->next(next)) == E_OK && next) { + if (std::get<1>(GetParam()) <= 0) { + times.push_back(result->get_value(1)); + } else { + // has_next is idempotent until the block is consumed. + EXPECT_EQ(result->next(next), E_OK); + EXPECT_TRUE(next); + TsBlock* block = nullptr; + EXPECT_EQ(result->get_next_tsblock(block), E_OK); + if (!block) break; + RowIterator rows(block); + while (rows.has_next()) { + uint32_t size = 0; + bool null = false; + int64_t timestamp = 0; + std::memcpy(×tamp, rows.read(0, &size, &null), + sizeof(timestamp)); + times.push_back(timestamp); + rows.next(); + } + } + } + EXPECT_EQ(ret, expected_error); + EXPECT_FALSE(next); + // EOF and errors stay stable on repeated calls. + EXPECT_EQ(result->next(next), expected_error); + EXPECT_FALSE(next); + executor.destroy_query_data_set(result); + return times; + } + std::vector expected(const std::function& predicate, + int devices = 2) { + std::vector out; + for (int d = 0; d < devices; ++d) + for (int t = 0; t < 14; ++t) + if (predicate(t)) out.push_back(base_ + t); + return out; + } + ConfigValue saved_; + std::string filename_; + const int64_t base_ = INT64_C(1099511627776); +}; + +TEST_P(TableFieldFilterTest, NullTrueFalseAndRejectWholePages) { + EXPECT_EQ(scan({"v"}, nullptr), expected([](int) { return true; })); + for (bool accept : {false, true}) { + int calls = 0; + auto* filter = new RowPredicate([&](const TableFieldRow&, bool& ok) { + ++calls; + ok = accept; + return E_OK; + }); + EXPECT_EQ(scan({"v"}, filter), + expected([accept](int) { return accept; })); + EXPECT_EQ(calls, 28); + } + int calls = 0; + EXPECT_EQ( + scan({"v"}, new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + ok = row.timestamp >= base_ + 10; + return E_OK; + })), + expected([](int t) { return t >= 10; })); + EXPECT_EQ(calls, 28); + // Preserve the original ordinary Filter callback as well. + auto* reject = new LegacyPredicate(); + reject->accept = false; + EXPECT_TRUE(scan({"v"}, reject).empty()); + EXPECT_EQ(scan({"v"}, new LegacyPredicate()), + expected([](int) { return true; })); +} + +TEST_P(TableFieldFilterTest, TypedRowsPreserveNullsFloatsBinaryAndTimestamp) { + int calls = 0; + const std::vector columns{"id", "v", "i", "flag", + "f", "d", "s", "text", + "blob", "date", "stamp", "v"}; + auto* filter = new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + const int t = int(row.timestamp - base_); + EXPECT_EQ(row.fields.size(), + 10u); // Duplicate projections are one FIELD. + EXPECT_EQ(row.find("id"), nullptr); + int64_t v = 0, stamp = 0; + EXPECT_TRUE(row.find("v")->get(v)); + EXPECT_TRUE(v == t || v == 100 + t); + EXPECT_TRUE(row.find("stamp")->get(stamp)); + EXPECT_EQ(stamp, -row.timestamp); + const auto* i = row.find("i"); + EXPECT_EQ(i->is_null(), t % 3 == 0); + int32_t integer = 0; + EXPECT_EQ(i->get(integer), t % 3 != 0); + if (!i->is_null()) EXPECT_EQ(integer, -t); + float f = 0; + double d = 0; + bool flag = false; + int32_t date = 0; + EXPECT_TRUE(row.find("f")->get(f)); + EXPECT_EQ(f, float(t) + 0.25f); + EXPECT_TRUE(row.find("d")->get(d)); + EXPECT_EQ(d, -double(t) - 0.5); + EXPECT_TRUE(row.find("flag")->get(flag)); + EXPECT_EQ(flag, bool(t % 2)); + EXPECT_TRUE(row.find("date")->get(date)); + EXPECT_EQ(date, 20260101 + t); + for (const std::string& name : {"s", "text", "blob"}) { + const auto* value = row.find(name); + EXPECT_FALSE(value->is_null()); + EXPECT_EQ(std::string(value->data, value->size), + t % 2 ? std::string("a\0b", 3) : std::string()); + } + ok = i->is_null() || (v % 2 == 0 && f > 4); + return E_OK; + }); + EXPECT_EQ(scan(columns, filter), expected([](int t) { + return t % 3 == 0 || (t % 2 == 0 && t >= 4); + })); + EXPECT_EQ(calls, 28); +} + +TEST_P(TableFieldFilterTest, OffsetLimitApplyAfterFilterAcrossDevices) { + auto all = expected([](int t) { return t % 2 == 1; }); + for (int offset : {0, 3, 8, 20}) { + for (int limit : {0, 1, 5, -1}) { + auto begin = all.begin() + std::min(offset, all.size()); + auto end = limit < 0 + ? all.end() + : begin + std::min(limit, all.end() - begin); + std::vector wanted(begin, end); + EXPECT_EQ( + scan({"v"}, + new RowPredicate([](const TableFieldRow& row, bool& ok) { + int64_t v = 0; + row.find("v")->get(v); + ok = v % 2 == 1; + return E_OK; + }), + offset, limit), + wanted); + } + } +} + +TEST_P(TableFieldFilterTest, TimeAndTagFiltersComposeWithFieldFilter) { + auto* time = TimeFilter::gt_eq(base_ + 5); + auto* tag = new TagEq(1, "d1"); + EXPECT_EQ(scan({"id", "v"}, + new RowPredicate([](const TableFieldRow& row, bool& ok) { + int64_t v = 0; + row.find("v")->get(v); + ok = v >= 100 && v % 2 == 0; + return E_OK; + }), + 1, 3, time, tag), + std::vector({base_ + 8, base_ + 10, base_ + 12})); +} + +TEST_P(TableFieldFilterTest, CallbackErrorsAreTerminal) { + int calls = 0; + auto* filter = new RowPredicate([&](const TableFieldRow&, bool& ok) { + ok = true; + return ++calls == 5 ? E_DECODE_ERR : E_OK; + }); + scan({"v"}, filter, 0, -1, nullptr, nullptr, E_DECODE_ERR); + EXPECT_EQ(calls, 5); + scan({"f"}, new LegacyPredicate(), 0, -1, nullptr, nullptr, E_NOT_SUPPORT); + scan({"v", "i"}, new LegacyPredicate(), 0, -1, nullptr, nullptr, + E_NOT_SUPPORT); +} + +TEST_P(TableFieldFilterTest, LegacyNullAndAllIntegerStringTypes) { + EXPECT_EQ(scan({"i"}, new LegacyPredicate()), + expected([](int t) { return t % 3 != 0; })); + for (const std::string& name : + {"flag", "date", "stamp", "s", "text", "blob"}) + EXPECT_EQ(scan({name}, new LegacyPredicate()), + expected([](int) { return true; })); +} + +TEST_P(TableFieldFilterTest, AllNullFieldDoesNotSuppressOtherFields) { + int calls = 0; + EXPECT_EQ(scan({"v", "empty"}, + new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + const auto* empty = row.find("empty"); + EXPECT_NE(empty, nullptr); + if (!empty) return E_DECODE_ERR; + EXPECT_EQ(empty->type, INT64); + EXPECT_TRUE(empty->is_null()); + int64_t v = 0; + EXPECT_TRUE(row.find("v")->get(v)); + ok = v % 2 == 0; + return E_OK; + })), + expected([](int t) { return t % 2 == 0; })); + EXPECT_EQ(calls, 28); +} + +TEST_P(TableFieldFilterTest, TimeOnlyRowsDoNotInventFieldValues) { + int calls = 0; + EXPECT_EQ( + scan({"id"}, new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + EXPECT_TRUE(row.fields.empty()); + EXPECT_EQ(row.find("v"), nullptr); + ok = row.timestamp >= base_ + 10; + return E_OK; + })), + expected([](int t) { return t >= 10; })); + EXPECT_EQ(calls, 28); +} + +INSTANTIATE_TEST_SUITE_P(RowAndBatchParallelModes, TableFieldFilterTest, + ::testing::Combine(::testing::Bool(), + ::testing::Values(-1, 1, 4))); +} // namespace From 249b0d51a6bfe202185aa21b794ff21ccdfafcf3 Mon Sep 17 00:00:00 2001 From: HUANG Xiangdong <1021782+jixuan1989@users.noreply.github.com> Date: Sun, 18 Oct 2026 09:47:42 +0800 Subject: [PATCH 02/16] Preserve aligned NULL rows in table FIELD filtering --- cpp/src/reader/aligned_chunk_reader.cc | 16 +-- .../block/single_device_tsblock_reader.cc | 27 +++-- cpp/test/reader/table_field_filter_test.cc | 108 ++++++++++++++++-- 3 files changed, 126 insertions(+), 25 deletions(-) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index ff4552cc8..18a46f1e8 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -510,13 +510,10 @@ int AlignedChunkReader::read_from_file_and_rewrap( // page cannot be ruled out, while "fully satisfies" means every row is known // to match and the page count can safely be used for offset pushdown. bool AlignedChunkReader::cur_page_may_satisfy_filter(Filter* filter) { - bool value_satisfy = filter == nullptr || - cur_value_page_header_.statistic_ == nullptr || - filter->satisfy(cur_value_page_header_.statistic_); - bool time_satisfy = filter == nullptr || - cur_time_page_header_.statistic_ == nullptr || - filter->satisfy(cur_time_page_header_.statistic_); - return time_satisfy && value_satisfy; + // Value statistics exclude NULLs, whose timestamps may still match. + // Only the shared time page can rule out every aligned row. + return filter == nullptr || cur_time_page_header_.statistic_ == nullptr || + filter->satisfy(cur_time_page_header_.statistic_); } bool AlignedChunkReader::cur_page_fully_satisfies_filter(Filter* filter) { @@ -738,6 +735,11 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( if (ret != E_OK) { \ break; \ } \ + if (filter != nullptr && \ + !filter->satisfy_start_end_time(time, time)) { \ + row_appender.backoff_add_row(); \ + continue; \ + } \ row_appender.append(0, (char*)&time, sizeof(time)); \ row_appender.append_null(1); \ continue; \ diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index a53b421a8..a47bd7dbb 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -324,22 +324,29 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } if (field_column_contexts_.empty()) { - // If value columns were actually requested but none produced a - // context, every one of them read empty under the current filter - // (e.g. an empty/inverted time range, or a filter that matches no - // rows). The result is simply empty -- return it directly. The - // time-only fallback below is only for genuine time-only queries (no - // value columns); routing an all-empty value query through it would - // call alloc_multi_ssi(), which is aligned-only and returns - // E_NOT_SUPPORT on non-aligned devices. + // An all-NULL aligned FIELD has no value context, but its shared + // timestamps must still reach a NULL-aware FIELD predicate. Preserve + // the empty-result shortcut for other filtered-out value queries, + // particularly non-aligned devices that cannot use alloc_multi_ssi(). bool any_value_column_requested = false; + bool all_values_null_aligned = true; for (const auto* ts_idx : time_series_indexs) { if (ts_idx != nullptr) { any_value_column_requested = true; - break; + const auto* aligned = + dynamic_cast(ts_idx); + const auto* value_stat = + aligned != nullptr && aligned->value_ts_idx_ != nullptr + ? aligned->value_ts_idx_->get_statistic() + : nullptr; + if (value_stat == nullptr || value_stat->count_ != 0) { + all_values_null_aligned = false; + break; + } } } - if (any_value_column_requested) { + if (any_value_column_requested && + (field_filter == nullptr || !all_values_null_aligned)) { return common::E_OK; } diff --git a/cpp/test/reader/table_field_filter_test.cc b/cpp/test/reader/table_field_filter_test.cc index b22b2547f..06b6d19a2 100644 --- a/cpp/test/reader/table_field_filter_test.cc +++ b/cpp/test/reader/table_field_filter_test.cc @@ -177,12 +177,13 @@ class TableFieldFilterTest std::vector columns; columns.emplace_back("id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG); - const std::vector names{"v", "i", "flag", "f", - "d", "s", "text", "blob", - "date", "stamp", "empty"}; - const std::vector types{INT64, INT32, BOOLEAN, FLOAT, - DOUBLE, STRING, TEXT, BLOB, - DATE, TIMESTAMP, INT64}; + const std::vector names{ + "v", "i", "flag", "f", "d", + "s", "text", "blob", "date", "stamp", + "empty", "tail_null", "nullable_flag"}; + const std::vector types{ + INT64, INT32, BOOLEAN, FLOAT, DOUBLE, STRING, TEXT, + BLOB, DATE, TIMESTAMP, INT64, INT64, BOOLEAN}; for (size_t i = 0; i < names.size(); ++i) columns.emplace_back(names[i], types[i], UNCOMPRESSED, PLAIN, ColumnCategory::FIELD); @@ -211,6 +212,14 @@ class TableFieldFilterTest E_OK); ASSERT_EQ(tablet.add_value(index, "d", -double(t) - 0.5), E_OK); + if (t % 3 == 0) + ASSERT_EQ( + tablet.add_value(index, "tail_null", int64_t(t)), + E_OK); + if (t % 3 != 0) + ASSERT_EQ(tablet.add_value(index, "nullable_flag", + bool(t % 2)), + E_OK); const std::string bytes = t % 2 ? std::string("a\0b", 3) : std::string(); for (const std::string& name : {"s", "text", "blob"}) @@ -397,7 +406,7 @@ TEST_P(TableFieldFilterTest, OffsetLimitApplyAfterFilterAcrossDevices) { TEST_P(TableFieldFilterTest, TimeAndTagFiltersComposeWithFieldFilter) { auto* time = TimeFilter::gt_eq(base_ + 5); - auto* tag = new TagEq(1, "d1"); + TagEq tag(1, "d1"); EXPECT_EQ(scan({"id", "v"}, new RowPredicate([](const TableFieldRow& row, bool& ok) { int64_t v = 0; @@ -405,7 +414,7 @@ TEST_P(TableFieldFilterTest, TimeAndTagFiltersComposeWithFieldFilter) { ok = v >= 100 && v % 2 == 0; return E_OK; }), - 1, 3, time, tag), + 1, 3, time, &tag), std::vector({base_ + 8, base_ + 10, base_ + 12})); } @@ -464,6 +473,89 @@ TEST_P(TableFieldFilterTest, TimeOnlyRowsDoNotInventFieldValues) { EXPECT_EQ(calls, 28); } +TEST_P(TableFieldFilterTest, NullRowsOutsideValueStatisticsReachPredicate) { + int calls = 0; + auto* filter = new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + const auto* field = row.find("tail_null"); + EXPECT_NE(field, nullptr); + ok = field != nullptr && field->is_null(); + return E_OK; + }); + // Each page starts with one non-NULL value followed by NULLs. The first + // value page has range [base, base], but its time page includes base+1/2. + EXPECT_EQ(scan({"tail_null"}, filter, 0, -1, + TimeFilter::between(base_ + 1, base_ + 2, false)), + expected([](int t) { return t == 1 || t == 2; })); + EXPECT_EQ(calls, 4); +} + +TEST_P(TableFieldFilterTest, NullBooleanRowsRespectTimeBeforeFieldAndOffset) { + for (int offset : {0, 1}) { + int calls = 0; + auto* filter = + new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + EXPECT_GE(row.timestamp, base_ + 1); + EXPECT_LE(row.timestamp, base_ + 2); + bool value = false; + const auto* field = row.find("nullable_flag"); + EXPECT_NE(field, nullptr); + EXPECT_TRUE(field != nullptr && field->get(value)); + ok = true; + return E_OK; + }); + std::vector wanted = + offset == 0 ? expected([](int t) { return t == 1 || t == 2; }) + : std::vector{base_ + 2, base_ + 1}; + EXPECT_EQ(scan({"nullable_flag"}, filter, offset, offset == 0 ? -1 : 2, + TimeFilter::between(base_ + 1, base_ + 2, false)), + wanted); + EXPECT_GE(calls, int(wanted.size())); + } +} + +TEST_P(TableFieldFilterTest, OnlyAllNullFieldStillSuppliesTimestampRows) { + for (bool accept : {true, false}) { + int calls = 0; + auto* filter = + new RowPredicate([&](const TableFieldRow& row, bool& ok) { + ++calls; + EXPECT_EQ(row.fields.size(), 1u); + const auto* empty = row.find("empty"); + EXPECT_NE(empty, nullptr); + if (!empty) return E_DECODE_ERR; + EXPECT_EQ(empty->type, INT64); + EXPECT_TRUE(empty->is_null()); + ok = accept && empty->is_null(); + return E_OK; + }); + EXPECT_EQ(scan({"empty"}, filter), + expected([accept](int) { return accept; })); + EXPECT_EQ(calls, 28); + } + // Legacy value predicates reject NULL instead of receiving a made-up 0. + EXPECT_TRUE(scan({"empty"}, new LegacyPredicate()).empty()); +} + +TEST_P(TableFieldFilterTest, AllNullProjectionAppliesTimeOffsetLimit) { + auto predicate = []() -> Filter* { + return new RowPredicate([](const TableFieldRow& row, bool& ok) { + const auto* field = row.find("empty"); + ok = field != nullptr && field->is_null(); + return E_OK; + }); + }; + EXPECT_EQ(scan({"empty"}, predicate(), 15, 2), + std::vector({base_ + 1, base_ + 2})); + EXPECT_EQ(scan({"empty"}, predicate(), 1, 2, + TimeFilter::between(base_ + 1, base_ + 2, false)), + std::vector({base_ + 2, base_ + 1})); + EXPECT_TRUE(scan({"empty"}, predicate(), 0, -1, + TimeFilter::between(base_ + 20, base_ + 21, false)) + .empty()); +} + INSTANTIATE_TEST_SUITE_P(RowAndBatchParallelModes, TableFieldFilterTest, ::testing::Combine(::testing::Bool(), ::testing::Values(-1, 1, 4))); From f2e157ed3a5e16d2ea1e3d3d6b8735bae7c44e77 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Wed, 21 Oct 2026 11:44:01 +0800 Subject: [PATCH 03/16] Preserve aligned NULL rows outside value statistics A FIELD predicate must see rows whose timestamps lie in the selected time range even when the column's non-NULL values all lie outside it. Replace the all-values-NULL check in the empty-context shortcut with an aligned-source check so such devices fall through to the time-only context instead of returning an empty result. --- .../block/single_device_tsblock_reader.cc | 24 ++++++------- cpp/test/reader/table_field_filter_test.cc | 34 ++++++++++++++++--- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index a47bd7dbb..5586a03df 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -324,29 +324,25 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } if (field_column_contexts_.empty()) { - // An all-NULL aligned FIELD has no value context, but its shared - // timestamps must still reach a NULL-aware FIELD predicate. Preserve - // the empty-result shortcut for other filtered-out value queries, - // particularly non-aligned devices that cannot use alloc_multi_ssi(). + // An aligned device can still have rows in its shared time pages even + // when none of the requested FIELDs yields a value context: all values + // may be NULL in the selected time range, or non-NULL values may exist + // only outside it. A row predicate must see those rows as NULL. + // Preserve the empty-result shortcut for non-aligned sources, which + // cannot use the aligned time-only fallback. bool any_value_column_requested = false; - bool all_values_null_aligned = true; + bool all_value_columns_aligned = true; for (const auto* ts_idx : time_series_indexs) { if (ts_idx != nullptr) { any_value_column_requested = true; - const auto* aligned = - dynamic_cast(ts_idx); - const auto* value_stat = - aligned != nullptr && aligned->value_ts_idx_ != nullptr - ? aligned->value_ts_idx_->get_statistic() - : nullptr; - if (value_stat == nullptr || value_stat->count_ != 0) { - all_values_null_aligned = false; + if (!ts_idx->is_aligned()) { + all_value_columns_aligned = false; break; } } } if (any_value_column_requested && - (field_filter == nullptr || !all_values_null_aligned)) { + (field_filter == nullptr || !all_value_columns_aligned)) { return common::E_OK; } diff --git a/cpp/test/reader/table_field_filter_test.cc b/cpp/test/reader/table_field_filter_test.cc index 06b6d19a2..861cb0fa7 100644 --- a/cpp/test/reader/table_field_filter_test.cc +++ b/cpp/test/reader/table_field_filter_test.cc @@ -178,12 +178,12 @@ class TableFieldFilterTest columns.emplace_back("id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG); const std::vector names{ - "v", "i", "flag", "f", "d", - "s", "text", "blob", "date", "stamp", - "empty", "tail_null", "nullable_flag"}; + "v", "i", "flag", "f", "d", + "s", "text", "blob", "date", "stamp", + "empty", "tail_null", "nullable_flag", "chunk_null"}; const std::vector types{ - INT64, INT32, BOOLEAN, FLOAT, DOUBLE, STRING, TEXT, - BLOB, DATE, TIMESTAMP, INT64, INT64, BOOLEAN}; + INT64, INT32, BOOLEAN, FLOAT, DOUBLE, STRING, TEXT, + BLOB, DATE, TIMESTAMP, INT64, INT64, BOOLEAN, INT64}; for (size_t i = 0; i < names.size(); ++i) columns.emplace_back(names[i], types[i], UNCOMPRESSED, PLAIN, ColumnCategory::FIELD); @@ -220,6 +220,10 @@ class TableFieldFilterTest ASSERT_EQ(tablet.add_value(index, "nullable_flag", bool(t % 2)), E_OK); + if (t < 7) + ASSERT_EQ( + tablet.add_value(index, "chunk_null", int64_t(t)), + E_OK); const std::string bytes = t % 2 ? std::string("a\0b", 3) : std::string(); for (const std::string& name : {"s", "text", "blob"}) @@ -538,6 +542,26 @@ TEST_P(TableFieldFilterTest, OnlyAllNullFieldStillSuppliesTimestampRows) { EXPECT_TRUE(scan({"empty"}, new LegacyPredicate()).empty()); } +TEST_P(TableFieldFilterTest, NullOnlyChunkInTimeRangeReachesPredicate) { + int calls = 0; + auto predicate = [&calls]() -> Filter* { + return new RowPredicate([&calls](const TableFieldRow& row, bool& ok) { + ++calls; + const auto* field = row.find("chunk_null"); + ok = field != nullptr && field->is_null(); + return E_OK; + }); + }; + EXPECT_EQ(scan({"chunk_null"}, predicate()), + expected([](int t) { return t >= 7; })); + EXPECT_EQ(calls, 28); + calls = 0; + EXPECT_EQ(scan({"chunk_null"}, predicate(), 0, -1, + TimeFilter::between(base_ + 7, base_ + 13, false)), + expected([](int t) { return t >= 7; })); + EXPECT_EQ(calls, 14); +} + TEST_P(TableFieldFilterTest, AllNullProjectionAppliesTimeOffsetLimit) { auto predicate = []() -> Filter* { return new RowPredicate([](const TableFieldRow& row, bool& ok) { From 6f4a971eefffcc81cf62a75b6ad8bc91460b0fa6 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Wed, 21 Oct 2026 11:45:42 +0800 Subject: [PATCH 04/16] Remove unused per-value i32 decode in AlignedChunkReader decode_tv_batch replaced this loop in #823, leaving the function uncalled. Its NULL branch also predates the time-filter fix applied to the other decode paths in this series. --- cpp/src/reader/aligned_chunk_reader.cc | 47 -------------------------- cpp/src/reader/aligned_chunk_reader.h | 4 --- 2 files changed, 51 deletions(-) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 18a46f1e8..a2005cdac 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -767,53 +767,6 @@ int AlignedChunkReader::decode_time_value_buf_into_tsblock( } \ } while (false) -int AlignedChunkReader::i32_DECODE_TYPED_TV_INTO_TSBLOCK( - ByteStream& time_in, ByteStream& value_in, RowAppender& row_appender, - Filter* filter) { - int ret = E_OK; - uint32_t mask = 1 << 7; - int64_t time = 0; - int32_t value; - while (time_decoder_->has_remaining(time_in)) { - cur_value_index++; - if (value_page_col_notnull_bitmap_.empty() || - ((value_page_col_notnull_bitmap_[cur_value_index / 8] & 0xFF) & - (mask >> (cur_value_index % 8))) == 0) { - ret = time_decoder_->read_int64(time, time_in); - if (ret != E_OK) { - break; - } - if (UNLIKELY(!row_appender.add_row())) { - ret = E_OVERFLOW; - break; - } - row_appender.append(0, (char*)&time, sizeof(time)); - row_appender.append_null(1); - continue; - } - assert(value_decoder_->has_remaining(value_in)); - if (!value_decoder_->has_remaining(value_in)) { - return common::E_DATA_INCONSISTENCY; - } - if (UNLIKELY(!row_appender.add_row())) { - ret = E_OVERFLOW; - cur_value_index--; - break; - } else if (RET_FAIL(time_decoder_->read_int64(time, time_in))) { - } else if (RET_FAIL(value_decoder_->read_int32(value, value_in))) { - } else if (filter != nullptr && !filter->satisfy(time, value)) { - row_appender.backoff_add_row(); - continue; - } else { - /*std::cout << "decoder: time=" << time << ", value=" << value - * << std::endl;*/ - row_appender.append(0, (char*)&time, sizeof(time)); - row_appender.append(1, (char*)&value, sizeof(value)); - } - } - return ret; -} - namespace { // Type-dispatched exact read / skip for decode_tv_batch. Overload // resolution on the value pointer selects the matching Decoder method. diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index 5f6b0b94e..44e28fda3 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -219,10 +219,6 @@ class AlignedChunkReader : public IChunkReader { common::TsBlock* ret_tsblock, Filter* filter, common::PageArena* pa); - int i32_DECODE_TYPED_TV_INTO_TSBLOCK(common::ByteStream& time_in, - common::ByteStream& value_in, - common::RowAppender& row_appender, - Filter* filter); // Unified fixed-width aligned time+value page decode for // INT32/INT64/FLOAT/DOUBLE. Defined in the .cc; instantiated there for // each value type by decode_tv_buf_into_tsblock_by_datatype(). From f5d1243a44fd8a6adddb24d23e8718ab8ccd0fed Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 22 Oct 2026 09:54:08 +0800 Subject: [PATCH 05/16] [C++] Batch-evaluate FIELD predicates on aligned table rows Add AlignedFieldFilter (a columnar predicate tree) and SelectionMask (a row bitmask), and thread them through the aligned chunk/block readers. Single-threaded flow: page statistics (may_match/all_match) prune pages that cannot match or mark pages that fully match; only filter-referenced columns are decoded first; output columns are decoded lazily and only for pages with at least one matching row; the predicate is evaluated once per page into a bitmask, and the scatter loop only tests bits. NOT is pushed down to leaf comparisons so NULL rows keep SQL three-valued semantics, get_data_type_size is no longer called on string columns, and query() error paths free the field filter. --- cpp/src/file/tsfile_io_reader.cc | 6 +- cpp/src/file/tsfile_io_reader.h | 3 +- cpp/src/reader/aligned_chunk_reader.cc | 230 +++++- cpp/src/reader/aligned_chunk_reader.h | 14 + .../block/single_device_tsblock_reader.cc | 205 ++++- .../block/single_device_tsblock_reader.h | 19 +- cpp/src/reader/column_mapping.h | 17 + cpp/src/reader/filter/aligned_field_filter.cc | 775 ++++++++++++++++++ cpp/src/reader/filter/aligned_field_filter.h | 129 +++ cpp/src/reader/filter/selection_mask.cc | 157 ++++ cpp/src/reader/filter/selection_mask.h | 65 ++ cpp/src/reader/table_query_executor.cc | 29 +- cpp/src/reader/tsfile_series_scan_iterator.cc | 4 +- cpp/src/reader/tsfile_series_scan_iterator.h | 13 +- .../filter/aligned_field_filter_test.cc | 147 ++++ cpp/test/reader/table_field_filter_test.cc | 94 +++ 16 files changed, 1848 insertions(+), 59 deletions(-) create mode 100644 cpp/src/reader/filter/aligned_field_filter.cc create mode 100644 cpp/src/reader/filter/aligned_field_filter.h create mode 100644 cpp/src/reader/filter/selection_mask.cc create mode 100644 cpp/src/reader/filter/selection_mask.h create mode 100644 cpp/test/reader/filter/aligned_field_filter_test.cc diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 37225677c..bc0fa62f6 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -297,8 +297,8 @@ int TsFileIOReader::alloc_prepared_multi_ssi( int TsFileIOReader::alloc_multi_ssi( std::shared_ptr device_id, const std::vector& measurement_names, - TsFileSeriesScanIterator*& ssi, common::PageArena& pa, - Filter* time_filter) { + TsFileSeriesScanIterator*& ssi, common::PageArena& pa, Filter* time_filter, + AlignedFieldFilter* field_filter) { int ret = E_OK; if (RET_FAIL(load_tsfile_meta_if_necessary())) return ret; @@ -307,7 +307,7 @@ int TsFileIOReader::alloc_multi_ssi( if (IS_NULL(ssi_buf)) return E_OOM; ssi = new (ssi_buf) TsFileSeriesScanIterator; ssi->init(device_id, measurement_names.empty() ? "" : measurement_names[0], - read_file_, time_filter, pa); + read_file_, time_filter, pa, field_filter); auto& ssi_pa = ssi->timeseries_index_pa_; diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index f0c18f60f..a7105739d 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -89,7 +89,8 @@ class TsFileIOReader { int alloc_multi_ssi(std::shared_ptr device_id, const std::vector& measurement_names, TsFileSeriesScanIterator*& ssi, common::PageArena& pa, - Filter* time_filter = nullptr); + Filter* time_filter = nullptr, + AlignedFieldFilter* field_filter = nullptr); int prepare_series(const FileGeneration& generation, const PreparedLocator& locator, diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index a2005cdac..2fa694079 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -1421,6 +1421,68 @@ int AlignedChunkReader::decode_time_page_with(const ChunkPageInfo& page_info, return ret; } +void AlignedChunkReader::build_filter_column_mask() { + filter_column_mask_.assign(value_columns_.size(), false); + if (field_filter_ == nullptr) return; + std::set filter_columns; + for (const auto& name : field_filter_->referenced_columns()) { + filter_columns.insert(name); + } + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + filter_column_mask_[c] = + filter_columns.count( + value_columns_[c] + ->chunk_meta->measurement_name_.to_std_string()) > 0; + } +} + +int AlignedChunkReader::evaluate_page_field_mask(size_t page_idx) { + if (field_filter_ == nullptr) return E_OK; + const ChunkPageInfo& page_info = chunk_pages_[page_idx]; + std::vector views; + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + if (!filter_column_mask_[c]) continue; + auto* col = value_columns_[c]; + auto& pps = col->per_page_state[page_idx]; + AlignedFieldFilter::ColumnView view; + view.name = col->chunk_meta->measurement_name_.to_std_string(); + view.type = col->chunk_header.data_type_; + view.notnull_bitmap = + pps.notnull_bitmap.empty() ? nullptr : pps.notnull_bitmap.data(); + view.notnull_bitmap_size = + static_cast(pps.notnull_bitmap.size()); + view.row_offset = static_cast(page_info.row_begin); + view.nonnull_start = static_cast( + count_non_null_prefix(pps.notnull_bitmap, page_info.row_begin)); + if (view.type == common::STRING || view.type == common::TEXT || + view.type == common::BLOB) { + view.string_values = pps.predecoded_strings.data(); + } else { + view.fixed_values = pps.predecoded_values.data(); + } + views.push_back(std::move(view)); + } + + const uint32_t row_count = + static_cast(page_info.row_end - page_info.row_begin); + current_page_field_mask_.reset(row_count); + int ret = field_filter_->evaluate_columns(views, row_count, + current_page_field_mask_); + if (ret != E_OK) return ret; + current_page_field_mask_valid_ = true; + return E_OK; +} + +int AlignedChunkReader::decode_output_columns_for_page(size_t page_idx) { + if (field_filter_ == nullptr) return E_OK; + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + if (filter_column_mask_[c]) continue; + int ret = decode_value_page_for_slot(c, page_idx); + if (IS_FAIL(ret)) return ret; + } + return E_OK; +} + int AlignedChunkReader::build_page_plan(Filter* filter, int& row_offset) { int ret = E_OK; chunk_pages_.clear(); @@ -1475,44 +1537,74 @@ int AlignedChunkReader::build_page_plan(Filter* filter, int& row_offset) { break; } + bool field_page_skip = false; + bool field_all_match = false; + if (field_filter_ != nullptr) { + std::map field_statistics; + for (uint32_t c = 0; c < num_cols; ++c) { + auto* col = value_columns_[c]; + field_statistics[col->chunk_meta->measurement_name_ + .to_std_string()] = + col->cur_page_header.statistic_; + } + const int64_t row_count = + cur_time_page_header_.statistic_ != nullptr + ? cur_time_page_header_.statistic_->count_ + : 0; + 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); + } + Statistic* stat = cur_time_page_header_.statistic_; - if (filter == nullptr) { - page_info.pass_type = PagePassType::FULL_PASS; - page_info.row_begin = 0; - page_info.row_end = stat != nullptr ? stat->count_ : 0; - } else if (stat != nullptr && !filter->satisfy(stat)) { + const bool time_full_match = + filter == nullptr || + (stat != nullptr && filter->contain_start_end_time( + stat->start_time_, stat->end_time_)); + if (field_page_skip || + (stat != nullptr && filter != nullptr && !filter->satisfy(stat))) { page_info.pass_type = PagePassType::SKIP; - } else if (stat != nullptr && filter->contain_start_end_time( - stat->start_time_, stat->end_time_)) { + } else if (time_full_match && + (field_filter_ == nullptr || field_all_match)) { page_info.pass_type = PagePassType::FULL_PASS; page_info.row_begin = 0; - page_info.row_end = stat->count_; + page_info.row_end = stat != nullptr ? stat->count_ : 0; } else { page_info.pass_type = PagePassType::BOUNDARY; - std::vector times; - if (RET_FAIL(decode_time_page_direct(page_info, times))) { - break; - } - int32_t first = -1; - int32_t last = -1; - for (int32_t i = 0; i < static_cast(times.size()); i++) { - if (filter->satisfy_start_end_time(times[i], times[i])) { - // Offset is defined on the filtered row stream. Consume - // matching rows only; holes inside a boundary page do not - // count toward it. - if (row_offset > 0) { - row_offset--; - continue; + if (filter == nullptr) { + // There is no time predicate to narrow the rows; keep the + // whole page and let the FIELD mask decide exact rows. + page_info.row_begin = 0; + page_info.row_end = stat != nullptr ? stat->count_ : 0; + } else { + std::vector times; + if (RET_FAIL(decode_time_page_direct(page_info, times))) { + break; + } + int32_t first = -1; + int32_t last = -1; + for (int32_t i = 0; i < static_cast(times.size()); + i++) { + if (filter->satisfy_start_end_time(times[i], times[i])) { + // Offset is defined on the filtered row stream. Consume + // matching rows only; holes inside a boundary page do + // not count toward it. + if (field_filter_ == nullptr && row_offset > 0) { + row_offset--; + continue; + } + if (first < 0) first = i; + last = i; } - if (first < 0) first = i; - last = i; } - } - if (first >= 0) { - page_info.row_begin = first; - page_info.row_end = last + 1; - } else { - page_info.pass_type = PagePassType::SKIP; + if (first >= 0) { + page_info.row_begin = first; + page_info.row_end = last + 1; + } else { + page_info.pass_type = PagePassType::SKIP; + } } } @@ -1774,6 +1866,7 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, // (see decode_page_lazy() / release_page_slot()). Bounds memory to one page. int AlignedChunkReader::decode_all_planned_pages() { if (chunk_pages_.empty()) return E_OK; + build_filter_column_mask(); #ifdef ENABLE_THREADS if (decode_pool_ != nullptr && value_columns_.size() > 1) { @@ -1803,6 +1896,7 @@ int AlignedChunkReader::decode_all_planned_pages() { std::vector> futures; std::vector col_rets(value_columns_.size(), E_OK); for (uint32_t c = 0; c < value_columns_.size(); c++) { + if (field_filter_ != nullptr && !filter_column_mask_[c]) continue; int* col_ret = &col_rets[c]; futures.push_back(decode_pool_->submit([this, c, col_ret]() { for (size_t p = 0; p < chunk_pages_.size(); p++) { @@ -1854,20 +1948,47 @@ int AlignedChunkReader::decode_all_planned_pages() { return E_OK; } -// Decode time + all value columns for a single page slot on demand. -// Used by the single-thread path to keep memory bounded to one page. +// Decode one page slot on demand. With a FIELD filter, decode only its filter +// columns first and leave output-only columns untouched when the page has no +// matching row. int AlignedChunkReader::decode_page_lazy(size_t page_idx) { int ret = E_OK; if (RET_FAIL(decode_time_page_direct(chunk_pages_[page_idx], per_page_times_[page_idx]))) { return ret; } - for (uint32_t c = 0; c < value_columns_.size(); c++) { - if (RET_FAIL(decode_value_page_for_slot(c, page_idx))) { - return ret; + if (field_filter_ != nullptr && + filter_column_mask_.size() != value_columns_.size()) { + build_filter_column_mask(); + } + if (field_filter_ == nullptr) { + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + if (RET_FAIL(decode_value_page_for_slot(c, page_idx))) return ret; } + return E_OK; } - return E_OK; + if (chunk_pages_[page_idx].pass_type == PagePassType::FULL_PASS) { + // Every row matches; no mask evaluation is needed, so decode all + // columns once instead of first decoding the filter columns. + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + if (RET_FAIL(decode_value_page_for_slot(c, page_idx))) return ret; + } + current_page_field_mask_.reset(static_cast( + chunk_pages_[page_idx].row_end - chunk_pages_[page_idx].row_begin)); + current_page_field_mask_.set_all(); + current_page_field_mask_valid_ = true; + return E_OK; + } + + // BOUNDARY page: decode only the filter columns, evaluate the mask, then + // decode output columns only when at least one row survives. + for (uint32_t c = 0; c < value_columns_.size(); ++c) { + if (!filter_column_mask_[c]) continue; + if (RET_FAIL(decode_value_page_for_slot(c, page_idx))) return ret; + } + if (RET_FAIL(evaluate_page_field_mask(page_idx))) return ret; + if (current_page_field_mask_.empty()) return E_OK; + return decode_output_columns_for_page(page_idx); } // Release the decoded buffers of one page slot so they can be reused by the @@ -1911,7 +2032,8 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, (prev_time_page_not_finish() || prev_any_value_page_not_finish_multi()); const bool use_page_plan = page_plan_built_ || (!serial_page_in_progress && - (row_offset > 0 || use_parallel_page_plan)); + (row_offset > 0 || use_parallel_page_plan || + field_filter_ != nullptr)); if (!use_page_plan) { return get_next_page_multi_serial(ret_tsblock, filter, pa, row_offset); } @@ -1947,7 +2069,33 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, page_time_cursor_ = page_info.row_begin; page_time_count_ = page_info.row_end; current_page_loaded_ = true; + current_page_field_mask_valid_ = false; + } + + if (field_filter_ != nullptr && !current_page_field_mask_valid_) { + if (page_info.pass_type == PagePassType::FULL_PASS) { + current_page_field_mask_.reset(static_cast( + page_info.row_end - page_info.row_begin)); + current_page_field_mask_.set_all(); + current_page_field_mask_valid_ = true; + } else if (RET_FAIL(evaluate_page_field_mask( + current_page_plan_index_))) { + return ret; + } + if (current_page_field_mask_.empty()) { + if (single_thread_lazy) + release_page_slot(current_page_plan_index_); + current_page_plan_index_++; + current_page_loaded_ = false; + current_page_field_mask_valid_ = false; + continue; + } + if (!single_thread_lazy && RET_FAIL(decode_output_columns_for_page( + current_page_plan_index_))) { + return ret; + } } + const std::vector& times = per_page_times_[current_page_plan_index_]; @@ -2033,7 +2181,12 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, return E_OK; } int64_t ts = times[page_time_cursor_]; - if (boundary_filter && !filter->satisfy_start_end_time(ts, ts)) { + const uint32_t field_row = page_time_cursor_ - page_info.row_begin; + const bool field_rejected = + field_filter_ != nullptr && + !current_page_field_mask_.test(field_row); + if (field_rejected || + (boundary_filter && !filter->satisfy_start_end_time(ts, ts))) { for (uint32_t c = 0; c < num_cols; c++) { auto* col = value_columns_[c]; auto& pps = col->per_page_state[current_page_plan_index_]; @@ -2095,6 +2248,7 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, } current_page_plan_index_++; current_page_loaded_ = false; + current_page_field_mask_valid_ = false; } return E_NO_MORE_DATA; } diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index 44e28fda3..81311febf 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -25,6 +25,7 @@ #include "compress/compressor.h" #include "encoding/decoder.h" #include "file/random_access_read_file.h" +#include "reader/filter/aligned_field_filter.h" #include "reader/filter/filter.h" #include "reader/ichunk_reader.h" @@ -122,6 +123,12 @@ class AlignedChunkReader : public IChunkReader { cur_value_index(-1) {} int init(RandomAccessReadFile* read_file, common::String m_name, common::TSDataType data_type, Filter* time_filter) override; + void set_field_filter(AlignedFieldFilter* field_filter) { + field_filter_ = field_filter; + } + bool field_filter_applied() const { + return field_filter_ != nullptr && page_plan_built_; + } void reset() override; void destroy() override; ~AlignedChunkReader() override = default; @@ -257,6 +264,9 @@ class AlignedChunkReader : public IChunkReader { common::RowAppender& row_appender, Filter* filter, common::PageArena* pa); int build_page_plan(Filter* filter, int& row_offset); + void build_filter_column_mask(); + int evaluate_page_field_mask(size_t page_idx); + int decode_output_columns_for_page(size_t page_idx); int decode_time_page_direct(const ChunkPageInfo& page_info, std::vector& out_times); int decode_time_page_with(const ChunkPageInfo& page_info, @@ -292,6 +302,7 @@ class AlignedChunkReader : public IChunkReader { Compressor* time_compressor_; Compressor* value_compressor_; Filter* time_filter_; + AlignedFieldFilter* field_filter_ = nullptr; Decoder* time_decoder_; Decoder* value_decoder_; @@ -319,6 +330,9 @@ class AlignedChunkReader : public IChunkReader { bool page_plan_built_ = false; bool current_page_loaded_ = false; size_t current_page_plan_index_ = 0; + std::vector filter_column_mask_; + SelectionMask current_page_field_mask_; + bool current_page_field_mask_valid_ = false; #ifdef ENABLE_THREADS common::ThreadPool* decode_pool_ = nullptr; // borrowed, not owned diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index 5586a03df..8330997de 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -40,7 +40,9 @@ SingleDeviceTsBlockReader::SingleDeviceTsBlockReader( field_filter_(field_filter), block_size_(block_size), tuple_desc_(), - tsfile_io_reader_(tsfile_io_reader) {} + tsfile_io_reader_(tsfile_io_reader) { + aligned_field_filter_ = dynamic_cast(field_filter_); +} int SingleDeviceTsBlockReader::init(DeviceQueryTask* device_query_task, uint32_t block_size, Filter* time_filter, @@ -240,7 +242,8 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, break; } } - if (can_multi && field_filter == nullptr) { + if (can_multi && + (field_filter == nullptr || aligned_field_filter_ != nullptr)) { std::vector meas_names(meas_cols.begin(), meas_cols.end()); // Stable order by first appearance in the result schema so the @@ -280,8 +283,9 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, dense_row_count_ >= 0) ? remaining_offset_ : 0; - int ctx_ret = ctx->init(device_query_task_, meas_names, time_filter, - pos_list, pa_, ssi_offset, -1); + int ctx_ret = + ctx->init(device_query_task_, meas_names, time_filter, pos_list, + pa_, aligned_field_filter_, ssi_offset, -1); if (ctx_ret == common::E_OK) { if (ssi_offset > 0) { row_offset_pushed_to_ssi_ = true; @@ -299,6 +303,13 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, used_multi = true; } else { delete ctx; + if (ctx_ret == common::E_NO_MORE_DATA && + aligned_field_filter_ != nullptr) { + // Page statistics can prove that no row in this device can + // pass the structured FIELD filter. This is a valid empty + // result, not a reason to fall back to the row path. + return common::E_OK; + } if (ctx_ret != common::E_NO_MORE_DATA && ctx_ret != common::E_NOT_SUPPORT) { return ctx_ret; @@ -342,7 +353,8 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } } if (any_value_column_requested && - (field_filter == nullptr || !all_value_columns_aligned)) { + ((field_filter == nullptr && aligned_field_filter_ == nullptr) || + !all_value_columns_aligned)) { return common::E_OK; } @@ -350,9 +362,9 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, std::vector> empty_positions; auto* time_only_ctx = new VectorMeasurementColumnContext(tsfile_io_reader_); - int time_only_ret = - time_only_ctx->init(device_query_task_, empty_measurements, - time_filter, empty_positions, pa_); + int time_only_ret = time_only_ctx->init( + device_query_task_, empty_measurements, time_filter, + empty_positions, pa_, nullptr, 0, -1); if (common::E_OK == time_only_ret) { field_column_contexts_.insert( std::make_pair(kTimeOnlyContextName, time_only_ctx)); @@ -369,8 +381,9 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } // Detect aligned fast path: every field column comes from an aligned chunk. - if (field_filter == nullptr && !field_column_contexts_.empty() && - dense_row_count_ >= 0 && + if ((field_filter == nullptr || aligned_field_filter_ != nullptr) && + !field_column_contexts_.empty() && + (dense_row_count_ >= 0 || aligned_field_filter_ != nullptr) && aligned_col_count_ == field_column_contexts_.size()) { all_aligned_ = true; aligned_vec_.reserve(field_column_contexts_.size()); @@ -386,6 +399,13 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } } + if (aligned_field_filter_ != nullptr && !all_aligned_ && + !field_column_contexts_.empty()) { + // The structured batch filter is aligned-only. Legacy Filter objects + // continue to use the row-oriented path. + return common::E_NOT_SUPPORT; + } + if (field_column_contexts_.empty()) { return common::E_OK; } @@ -526,7 +546,79 @@ int SingleDeviceTsBlockReader::has_next(bool& has_next) { return ret; } +int SingleDeviceTsBlockReader::has_next_aligned_filtered( + bool& result_has_next) { + int ret = common::E_OK; + if (aligned_vec_.empty()) { + result_has_next = false; + return common::E_OK; + } + auto* context = + dynamic_cast(aligned_vec_[0]); + if (context == nullptr) { + return common::E_NOT_SUPPORT; + } + + const int time_in_query_index = tuple_desc_.get_time_column_index(); + common::ColAppender* extra_time_appender = + time_in_query_index == -1 ? nullptr + : col_appenders_[time_in_query_index]; + + while (current_block_->get_row_count() < block_size_) { + if (remaining_limit_ == 0) break; + + if (!aligned_filter_mask_valid_) { + if (context->available_rows() == 0) { + int next_ret = context->get_next_tsblock(false); + if (next_ret == common::E_NO_MORE_DATA) break; + if (next_ret != common::E_OK) return next_ret; + } + if (RET_FAIL(context->evaluate_filter(*aligned_field_filter_, + aligned_filter_mask_))) { + return ret; + } + aligned_filter_next_row_ = 0; + aligned_filter_mask_valid_ = true; + if (aligned_filter_mask_.empty()) { + const uint32_t skip = context->available_rows(); + if (RET_FAIL(context->skip_rows(skip))) return ret; + aligned_filter_mask_valid_ = false; + continue; + } + } + + uint32_t copied = 0; + bool block_exhausted = false; + if (RET_FAIL(context->filtered_bulk_copy_into( + col_appenders_, col_appenders_[time_column_index_], + extra_time_appender, row_appender_, aligned_filter_mask_, + aligned_filter_next_row_, remaining_offset_, remaining_limit_, + copied, block_exhausted))) { + return ret; + } + if (block_exhausted) { + aligned_filter_mask_valid_ = false; + } + if (remaining_limit_ == 0) break; + if (current_block_->get_row_count() >= block_size_) break; + if (copied == 0 && !block_exhausted) break; + } + + if (current_block_->get_row_count() > 0) { + if (RET_FAIL(fill_ids())) return ret; + current_block_->fill_trailling_nulls(); + last_block_returned_ = false; + result_has_next = true; + } else { + result_has_next = false; + } + return ret; +} + int SingleDeviceTsBlockReader::has_next_aligned(bool& result_has_next) { + if (aligned_field_filter_ != nullptr) { + return has_next_aligned_filtered(result_has_next); + } int ret = common::E_OK; int time_in_query_index = tuple_desc_.get_time_column_index(); @@ -1081,13 +1173,13 @@ int VectorMeasurementColumnContext::init( DeviceQueryTask* device_query_task, const std::vector& measurement_names, Filter* time_filter, std::vector>& pos_in_result, common::PageArena& pa, - int ssi_offset, int ssi_limit) { + AlignedFieldFilter* field_filter, int ssi_offset, int ssi_limit) { int ret = common::E_OK; pos_in_result_ = pos_in_result; column_names_ = measurement_names; if (RET_FAIL(tsfile_io_reader_->alloc_multi_ssi( device_query_task->get_device_id(), measurement_names, ssi_, pa, - time_filter))) { + time_filter, field_filter))) { return ret; } ssi_->set_row_range(ssi_offset, ssi_limit); @@ -1099,6 +1191,7 @@ int VectorMeasurementColumnContext::init( int VectorMeasurementColumnContext::get_next_tsblock(bool alloc_mem) { int ret = common::E_OK; + if (exhausted_) return common::E_NO_MORE_DATA; if (tsblock_ != nullptr) { if (time_iter_) { delete time_iter_; @@ -1111,6 +1204,7 @@ int VectorMeasurementColumnContext::get_next_tsblock(bool alloc_mem) { tsblock_->reset(); } if (RET_FAIL(ssi_->get_next(tsblock_, alloc_mem))) { + if (ret == common::E_NO_MORE_DATA) exhausted_ = true; if (time_iter_) { delete time_iter_; time_iter_ = nullptr; @@ -1199,10 +1293,95 @@ void VectorMeasurementColumnContext::remove_from( } uint32_t VectorMeasurementColumnContext::available_rows() const { - if (!time_iter_ || time_iter_->end()) return 0; + if (exhausted_ || !time_iter_ || time_iter_->end()) return 0; return time_iter_->remaining(); } +int VectorMeasurementColumnContext::evaluate_filter( + const AlignedFieldFilter& filter, SelectionMask& mask) { + if (tsblock_ == nullptr) return common::E_NO_MORE_DATA; + if (ssi_ != nullptr && ssi_->field_filter_applied_in_chunk()) { + mask.reset(tsblock_->get_row_count()); + mask.set_all(); + return common::E_OK; + } + return filter.evaluate(*tsblock_, column_names_, mask); +} + +int VectorMeasurementColumnContext::filtered_bulk_copy_into( + std::vector& col_appenders, + common::ColAppender* time_appender, + common::ColAppender* extra_time_appender, common::RowAppender* row_appender, + const SelectionMask& mask, uint32_t& source_row, int& remaining_offset, + int& remaining_limit, uint32_t& copied, bool& block_exhausted) { + copied = 0; + block_exhausted = false; + if (tsblock_ == nullptr || time_iter_ == nullptr || time_iter_->end()) { + block_exhausted = true; + return common::E_OK; + } + + const uint32_t row_count = tsblock_->get_row_count(); + while (source_row < row_count) { + if (remaining_limit == 0) break; + if (row_appender != nullptr && row_appender->remaining() == 0) break; + + uint32_t time_len = 0; + int64_t time = *reinterpret_cast(time_iter_->read(&time_len)); + bool selected = mask.test(source_row); + if (selected && remaining_offset > 0) { + --remaining_offset; + selected = false; + } + + if (selected) { + if (time_appender != nullptr) { + time_appender->add_row(); + time_appender->append(reinterpret_cast(&time), + sizeof(time)); + } + if (extra_time_appender != nullptr) { + extra_time_appender->add_row(); + extra_time_appender->append(reinterpret_cast(&time), + sizeof(time)); + } + for (uint32_t c = 0; + c < value_iters_.size() && c < pos_in_result_.size(); ++c) { + uint32_t len = 0; + bool is_null = false; + char* value = value_iters_[c]->read(&len, &is_null); + for (int32_t pos : pos_in_result_[c]) { + auto* appender = col_appenders[pos + 1]; + appender->add_row(); + if (is_null) { + appender->append_null(); + } else { + appender->append(value, len); + } + } + } + if (row_appender != nullptr) row_appender->add_row(); + ++copied; + if (remaining_limit > 0) --remaining_limit; + } else { + // Reading is required even for rejected rows because variable + // length cursors use the current row length when advancing. + for (auto* value_iter : value_iters_) { + uint32_t len = 0; + bool is_null = false; + value_iter->read(&len, &is_null); + } + } + + time_iter_->next(); + for (auto* value_iter : value_iters_) value_iter->next(); + ++source_row; + } + + if (source_row >= row_count) block_exhausted = true; + return common::E_OK; +} + int VectorMeasurementColumnContext::bulk_copy_into( std::vector& col_appenders, common::ColAppender* time_appender, common::RowAppender* row_appender, diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index eb58d40ad..b028fe2b9 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -21,7 +21,9 @@ #include "common/tsblock/tsblock.h" #include "reader/block/tsblock_reader.h" +#include "reader/filter/aligned_field_filter.h" #include "reader/filter/filter.h" +#include "reader/filter/selection_mask.h" #include "reader/imeta_data_querier.h" #include "reader/task/device_query_task.h" @@ -69,9 +71,11 @@ class SingleDeviceTsBlockReader : public TsBlockReader { // Fast path for aligned data: all columns share the same timestamps, // so no per-row merge-sort is needed. int has_next_aligned(bool& has_next); + int has_next_aligned_filtered(bool& has_next); DeviceQueryTask* device_query_task_; Filter* field_filter_; + AlignedFieldFilter* aligned_field_filter_ = nullptr; uint32_t block_size_; common::TsBlock* current_block_ = nullptr; std::vector col_appenders_; @@ -93,6 +97,9 @@ class SingleDeviceTsBlockReader : public TsBlockReader { bool all_aligned_ = false; uint32_t aligned_col_count_ = 0; std::vector aligned_vec_; + SelectionMask aligned_filter_mask_; + uint32_t aligned_filter_next_row_ = 0; + bool aligned_filter_mask_valid_ = false; }; class MeasurementColumnContext { @@ -194,7 +201,8 @@ class VectorMeasurementColumnContext final : public MeasurementColumnContext { const std::vector& measurement_names, Filter* time_filter, std::vector>& pos_in_result, - common::PageArena& pa, int ssi_offset = 0, int ssi_limit = -1); + common::PageArena& pa, AlignedFieldFilter* field_filter = nullptr, + int ssi_offset = 0, int ssi_limit = -1); int get_next_tsblock(bool alloc_mem) override; int get_current_time(int64_t& time) override; int get_current_value(char*& value, uint32_t& len) override; @@ -205,11 +213,20 @@ class VectorMeasurementColumnContext final : public MeasurementColumnContext { common::RowAppender* row_appender, uint32_t count) override; int skip_rows(uint32_t count) override; + int evaluate_filter(const AlignedFieldFilter& filter, SelectionMask& mask); + int filtered_bulk_copy_into( + std::vector& col_appenders, + common::ColAppender* time_appender, + common::ColAppender* extra_time_appender, + common::RowAppender* row_appender, const SelectionMask& mask, + uint32_t& source_row, int& remaining_offset, int& remaining_limit, + uint32_t& copied, bool& block_exhausted); private: std::vector column_names_; std::vector> pos_in_result_; std::vector value_iters_; + bool exhausted_ = false; }; class IdColumnContext { diff --git a/cpp/src/reader/column_mapping.h b/cpp/src/reader/column_mapping.h index 99e153030..33e76f47e 100644 --- a/cpp/src/reader/column_mapping.h +++ b/cpp/src/reader/column_mapping.h @@ -21,6 +21,7 @@ #include "common/schema.h" #include "expression.h" +#include "reader/filter/aligned_field_filter.h" namespace storage { class ColumnMapping { public: @@ -51,6 +52,22 @@ class ColumnMapping { return common::E_OK; } + int add_filter_columns(const AlignedFieldFilter& filter, + TableSchema& schema) { + for (const auto& column_name : filter.referenced_columns()) { + const int column_index = schema.find_column_index(column_name); + if (column_index < 0) { + return common::E_COLUMN_NOT_EXIST; + } + if (schema.get_column_categories()[column_index] != + common::ColumnCategory::FIELD) { + return common::E_INVALID_ARG; + } + field_columns_.insert(column_name); + } + return common::E_OK; + } + const std::vector& get_column_pos( const std::string& column_name) const { static const std::vector empty; diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc new file mode 100644 index 000000000..ebdcfd3f3 --- /dev/null +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -0,0 +1,775 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "reader/filter/aligned_field_filter.h" + +#include +#include +#include +#include + +#include "common/statistic.h" + +namespace storage { +namespace { + +template +bool compare_values(T left, T right, AlignedFieldFilter::CompareOp op) { + switch (op) { + case AlignedFieldFilter::CompareOp::EQ: + return left == right; + case AlignedFieldFilter::CompareOp::NE: + return left != right; + case AlignedFieldFilter::CompareOp::LT: + return left < right; + case AlignedFieldFilter::CompareOp::LE: + return left <= right; + case AlignedFieldFilter::CompareOp::GT: + return left > right; + case AlignedFieldFilter::CompareOp::GE: + return left >= right; + } + return false; +} + +int compare_string(const char* data, uint32_t len, const std::string& literal, + AlignedFieldFilter::CompareOp op) { + const std::string value(data, len); + switch (op) { + case AlignedFieldFilter::CompareOp::EQ: + return value == literal; + case AlignedFieldFilter::CompareOp::NE: + return value != literal; + case AlignedFieldFilter::CompareOp::LT: + return value < literal; + case AlignedFieldFilter::CompareOp::LE: + return value <= literal; + case AlignedFieldFilter::CompareOp::GT: + return value > literal; + case AlignedFieldFilter::CompareOp::GE: + return value >= literal; + } + return false; +} + +bool is_integer_type(common::TSDataType type) { + return type == common::INT32 || type == common::DATE || + type == common::INT64 || type == common::TIMESTAMP; +} + +bool is_string_type(common::TSDataType type) { + return type == common::STRING || type == common::TEXT || + type == common::BLOB; +} + +std::string normalize_column_name(const std::string& column) { + std::string normalized(column); + for (char& ch : normalized) { + ch = static_cast(std::tolower(static_cast(ch))); + } + return normalized; +} + +} // namespace + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::boolean(bool value) { + Literal literal; + literal.type = common::BOOLEAN; + literal.bool_value = value; + return literal; +} + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::int32(int32_t value) { + Literal literal; + literal.type = common::INT32; + literal.int32_value = value; + return literal; +} + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::int64(int64_t value) { + Literal literal; + literal.type = common::INT64; + literal.int64_value = value; + return literal; +} + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::float32(float value) { + Literal literal; + literal.type = common::FLOAT; + literal.float_value = value; + return literal; +} + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::float64(double value) { + Literal literal; + literal.type = common::DOUBLE; + literal.double_value = value; + return literal; +} + +AlignedFieldFilter::Literal AlignedFieldFilter::Literal::string( + std::string value) { + Literal literal; + literal.type = common::STRING; + literal.string_value = std::move(value); + return literal; +} + +AlignedFieldFilter::AlignedFieldFilter(Kind kind, std::string column, + CompareOp op, Literal literal) + : kind_(kind), + column_(std::move(column)), + compare_op_(op), + literal_(std::move(literal)) {} + +AlignedFieldFilter::AlignedFieldFilter(Kind kind, Ptr left, Ptr right) + : kind_(kind), left_(std::move(left)), right_(std::move(right)) {} + +AlignedFieldFilter::Ptr AlignedFieldFilter::compare(std::string column, + CompareOp op, + Literal literal) { + return Ptr(new AlignedFieldFilter(Kind::COMPARISON, + normalize_column_name(column), op, + std::move(literal))); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::is_null(std::string column) { + return Ptr(new AlignedFieldFilter(Kind::IS_NULL, + normalize_column_name(column), + CompareOp::EQ, Literal())); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::is_not_null(std::string column) { + return Ptr(new AlignedFieldFilter(Kind::IS_NOT_NULL, + normalize_column_name(column), + CompareOp::EQ, Literal())); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::and_filter(Ptr left, Ptr right) { + return Ptr( + new AlignedFieldFilter(Kind::AND, std::move(left), std::move(right))); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::or_filter(Ptr left, Ptr right) { + return Ptr( + new AlignedFieldFilter(Kind::OR, std::move(left), std::move(right))); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::not_filter(Ptr child) { + return negate(std::move(child)); +} + +AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { + if (node == nullptr) return nullptr; + switch (node->kind_) { + case Kind::COMPARISON: { + CompareOp neg_op = CompareOp::EQ; + switch (node->compare_op_) { + case CompareOp::EQ: + neg_op = CompareOp::NE; + break; + case CompareOp::NE: + neg_op = CompareOp::EQ; + break; + case CompareOp::LT: + neg_op = CompareOp::GE; + break; + case CompareOp::LE: + neg_op = CompareOp::GT; + break; + case CompareOp::GT: + neg_op = CompareOp::LE; + break; + case CompareOp::GE: + neg_op = CompareOp::LT; + break; + } + return Ptr(new AlignedFieldFilter(Kind::COMPARISON, node->column_, + neg_op, node->literal_)); + } + case Kind::IS_NULL: + return is_not_null(node->column_); + case Kind::IS_NOT_NULL: + return is_null(node->column_); + case Kind::NOT: + return std::move(node->left_); + case Kind::AND: + return or_filter(negate(std::move(node->left_)), + negate(std::move(node->right_))); + case Kind::OR: + return and_filter(negate(std::move(node->left_)), + negate(std::move(node->right_))); + } + return nullptr; +} + +std::vector AlignedFieldFilter::referenced_columns() const { + std::vector columns; + std::set seen_columns; + std::set seen_nodes; + std::vector stack{this}; + while (!stack.empty()) { + const AlignedFieldFilter* node = stack.back(); + stack.pop_back(); + if (node == nullptr || !seen_nodes.insert(node).second) continue; + if (node->kind_ == Kind::COMPARISON || node->kind_ == Kind::IS_NULL || + node->kind_ == Kind::IS_NOT_NULL) { + if (seen_columns.insert(node->column_).second) { + columns.push_back(node->column_); + } + continue; + } + if (node->right_) stack.push_back(node->right_.get()); + if (node->left_) stack.push_back(node->left_.get()); + } + return columns; +} + +bool AlignedFieldFilter::may_match( + const std::map& statistics, + int64_t row_count) const { + if (kind_ == Kind::AND) { + return !left_ || !right_ || + (left_->may_match(statistics, row_count) && + right_->may_match(statistics, row_count)); + } + if (kind_ == Kind::OR) { + return !left_ || !right_ || + (left_->may_match(statistics, row_count) || + right_->may_match(statistics, row_count)); + } + if (kind_ == Kind::NOT) { + // NOT can be pushed down to leaves in a future planner. Until then, + // never use page statistics to prune a negated predicate. + return true; + } + + const auto found = statistics.find(column_); + if (found == statistics.end() || found->second == nullptr) return true; + Statistic* statistic = found->second; + if (kind_ == Kind::IS_NULL) { + return row_count <= 0 || statistic->count_ < row_count; + } + if (kind_ == Kind::IS_NOT_NULL) { + return statistic->count_ > 0; + } + if (statistic->count_ <= 0) return false; + + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::INT32) return true; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ <= literal_.int32_value && + literal_.int32_value <= typed->max_value_; + case CompareOp::NE: + return !(typed->min_value_ == typed->max_value_ && + typed->min_value_ == literal_.int32_value && + typed->count_ == row_count); + case CompareOp::LT: + return typed->min_value_ < literal_.int32_value; + case CompareOp::LE: + return typed->min_value_ <= literal_.int32_value; + case CompareOp::GT: + return typed->max_value_ > literal_.int32_value; + case CompareOp::GE: + return typed->max_value_ >= literal_.int32_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::INT64) return true; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ <= literal_.int64_value && + literal_.int64_value <= typed->max_value_; + case CompareOp::NE: + return !(typed->min_value_ == typed->max_value_ && + typed->min_value_ == literal_.int64_value && + typed->count_ == row_count); + case CompareOp::LT: + return typed->min_value_ < literal_.int64_value; + case CompareOp::LE: + return typed->min_value_ <= literal_.int64_value; + case CompareOp::GT: + return typed->max_value_ > literal_.int64_value; + case CompareOp::GE: + return typed->max_value_ >= literal_.int64_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::FLOAT) return true; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ <= literal_.float_value && + literal_.float_value <= typed->max_value_; + case CompareOp::NE: + return !(typed->min_value_ == typed->max_value_ && + typed->min_value_ == literal_.float_value && + typed->count_ == row_count); + case CompareOp::LT: + return typed->min_value_ < literal_.float_value; + case CompareOp::LE: + return typed->min_value_ <= literal_.float_value; + case CompareOp::GT: + return typed->max_value_ > literal_.float_value; + case CompareOp::GE: + return typed->max_value_ >= literal_.float_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::DOUBLE) return true; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ <= literal_.double_value && + literal_.double_value <= typed->max_value_; + case CompareOp::NE: + return !(typed->min_value_ == typed->max_value_ && + typed->min_value_ == literal_.double_value && + typed->count_ == row_count); + case CompareOp::LT: + return typed->min_value_ < literal_.double_value; + case CompareOp::LE: + return typed->min_value_ <= literal_.double_value; + case CompareOp::GT: + return typed->max_value_ > literal_.double_value; + case CompareOp::GE: + return typed->max_value_ >= literal_.double_value; + } + } + return true; +} + +bool AlignedFieldFilter::all_match( + const std::map& statistics, + int64_t row_count) const { + if (row_count <= 0) return false; + if (kind_ == Kind::AND) { + return left_ && right_ && left_->all_match(statistics, row_count) && + right_->all_match(statistics, row_count); + } + if (kind_ == Kind::OR) { + return (left_ && left_->all_match(statistics, row_count)) || + (right_ && right_->all_match(statistics, row_count)); + } + if (kind_ == Kind::NOT) { + // Physical plans may keep a NOT node for now. Do not infer a + // page-wide match from its child statistics. + return false; + } + + const auto found = statistics.find(column_); + if (found == statistics.end() || found->second == nullptr) return false; + Statistic* statistic = found->second; + if (kind_ == Kind::IS_NULL) { + return statistic->count_ == 0; + } + if (kind_ == Kind::IS_NOT_NULL) { + return statistic->count_ == row_count; + } + // Ordinary comparisons do not match NULL. A page-wide match requires that + // every row has a non-NULL value and that the whole min/max range passes. + if (statistic->count_ != row_count) return false; + + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::INT32) return false; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ == literal_.int32_value && + typed->max_value_ == literal_.int32_value; + case CompareOp::NE: + return literal_.int32_value < typed->min_value_ || + literal_.int32_value > typed->max_value_; + case CompareOp::LT: + return typed->max_value_ < literal_.int32_value; + case CompareOp::LE: + return typed->max_value_ <= literal_.int32_value; + case CompareOp::GT: + return typed->min_value_ > literal_.int32_value; + case CompareOp::GE: + return typed->min_value_ >= literal_.int32_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::INT64) return false; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ == literal_.int64_value && + typed->max_value_ == literal_.int64_value; + case CompareOp::NE: + return literal_.int64_value < typed->min_value_ || + literal_.int64_value > typed->max_value_; + case CompareOp::LT: + return typed->max_value_ < literal_.int64_value; + case CompareOp::LE: + return typed->max_value_ <= literal_.int64_value; + case CompareOp::GT: + return typed->min_value_ > literal_.int64_value; + case CompareOp::GE: + return typed->min_value_ >= literal_.int64_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::FLOAT) return false; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ == literal_.float_value && + typed->max_value_ == literal_.float_value; + case CompareOp::NE: + return literal_.float_value < typed->min_value_ || + literal_.float_value > typed->max_value_; + case CompareOp::LT: + return typed->max_value_ < literal_.float_value; + case CompareOp::LE: + return typed->max_value_ <= literal_.float_value; + case CompareOp::GT: + return typed->min_value_ > literal_.float_value; + case CompareOp::GE: + return typed->min_value_ >= literal_.float_value; + } + } + if (auto* typed = dynamic_cast(statistic)) { + if (literal_.type != common::DOUBLE) return false; + switch (compare_op_) { + case CompareOp::EQ: + return typed->min_value_ == literal_.double_value && + typed->max_value_ == literal_.double_value; + case CompareOp::NE: + return literal_.double_value < typed->min_value_ || + literal_.double_value > typed->max_value_; + case CompareOp::LT: + return typed->max_value_ < literal_.double_value; + case CompareOp::LE: + return typed->max_value_ <= literal_.double_value; + case CompareOp::GT: + return typed->min_value_ > literal_.double_value; + case CompareOp::GE: + return typed->min_value_ >= literal_.double_value; + } + } + return false; +} + +int AlignedFieldFilter::evaluate(common::TsBlock& block, + const std::vector& column_names, + SelectionMask& mask) const { + std::map column_index; + for (uint32_t i = 0; i < column_names.size(); ++i) { + column_index[column_names[i]] = i + 1; + } + + struct Frame { + const AlignedFieldFilter* node; + uint8_t state; + }; + std::vector traversal; + std::vector postorder; + traversal.push_back({this, 0}); + while (!traversal.empty()) { + Frame& frame = traversal.back(); + const AlignedFieldFilter* node = frame.node; + if (node->kind_ == Kind::COMPARISON || node->kind_ == Kind::IS_NULL || + node->kind_ == Kind::IS_NOT_NULL) { + postorder.push_back(node); + traversal.pop_back(); + } else if (node->kind_ == Kind::NOT) { + if (!node->left_) return common::E_INVALID_ARG; + if (frame.state == 0) { + frame.state = 1; + traversal.push_back({node->left_.get(), 0}); + } else { + postorder.push_back(node); + traversal.pop_back(); + } + } else { + if (!node->left_ || !node->right_) return common::E_INVALID_ARG; + if (frame.state == 0) { + frame.state = 1; + traversal.push_back({node->left_.get(), 0}); + } else if (frame.state == 1) { + frame.state = 2; + traversal.push_back({node->right_.get(), 0}); + } else { + postorder.push_back(node); + traversal.pop_back(); + } + } + } + + std::vector masks; + masks.reserve(postorder.size()); + for (const auto* node : postorder) { + if (node->kind_ == Kind::COMPARISON || node->kind_ == Kind::IS_NULL || + node->kind_ == Kind::IS_NOT_NULL) { + SelectionMask leaf_mask(block.get_row_count()); + int ret = node->evaluate_leaf(block, column_index, leaf_mask); + if (ret != common::E_OK) return ret; + masks.push_back(std::move(leaf_mask)); + } else if (node->kind_ == Kind::NOT) { + if (masks.empty()) return common::E_INVALID_ARG; + masks.back().invert(); + } else { + if (masks.size() < 2) return common::E_INVALID_ARG; + SelectionMask right = std::move(masks.back()); + masks.pop_back(); + if (node->kind_ == Kind::AND) { + masks.back().and_with(right); + } else { + masks.back().or_with(right); + } + } + } + if (masks.size() != 1) return common::E_INVALID_ARG; + mask = std::move(masks.back()); + return common::E_OK; +} + +int AlignedFieldFilter::evaluate_columns(const std::vector& columns, + uint32_t row_count, + SelectionMask& mask) const { + std::map column_index; + for (uint32_t i = 0; i < columns.size(); ++i) { + column_index[columns[i].name] = i; + } + + struct Frame { + const AlignedFieldFilter* node; + uint8_t state; + }; + std::vector traversal{{this, 0}}; + std::vector postorder; + while (!traversal.empty()) { + Frame& frame = traversal.back(); + const AlignedFieldFilter* node = frame.node; + if (node->kind_ == Kind::COMPARISON || node->kind_ == Kind::IS_NULL || + node->kind_ == Kind::IS_NOT_NULL) { + postorder.push_back(node); + traversal.pop_back(); + } else if (node->kind_ == Kind::NOT) { + if (!node->left_) return common::E_INVALID_ARG; + if (frame.state == 0) { + frame.state = 1; + traversal.push_back({node->left_.get(), 0}); + } else { + postorder.push_back(node); + traversal.pop_back(); + } + } else { + if (!node->left_ || !node->right_) return common::E_INVALID_ARG; + if (frame.state == 0) { + frame.state = 1; + traversal.push_back({node->left_.get(), 0}); + } else if (frame.state == 1) { + frame.state = 2; + traversal.push_back({node->right_.get(), 0}); + } else { + postorder.push_back(node); + traversal.pop_back(); + } + } + } + + std::vector masks; + masks.reserve(postorder.size()); + for (const auto* node : postorder) { + if (node->kind_ == Kind::COMPARISON || node->kind_ == Kind::IS_NULL || + node->kind_ == Kind::IS_NOT_NULL) { + const auto found = column_index.find(node->column_); + if (found == column_index.end()) return common::E_COLUMN_NOT_EXIST; + SelectionMask leaf_mask(row_count); + int ret = node->evaluate_leaf(columns[found->second], row_count, + leaf_mask); + if (ret != common::E_OK) return ret; + masks.push_back(std::move(leaf_mask)); + } else if (node->kind_ == Kind::NOT) { + if (masks.empty()) return common::E_INVALID_ARG; + masks.back().invert(); + } else { + if (masks.size() < 2) return common::E_INVALID_ARG; + SelectionMask right = std::move(masks.back()); + masks.pop_back(); + if (node->kind_ == Kind::AND) { + masks.back().and_with(right); + } else { + masks.back().or_with(right); + } + } + } + if (masks.size() != 1) return common::E_INVALID_ARG; + mask = std::move(masks.back()); + return common::E_OK; +} + +int AlignedFieldFilter::evaluate_leaf(const ColumnView& column, + uint32_t row_count, + SelectionMask& mask) const { + if (column.type == common::INVALID_DATATYPE) return common::E_INVALID_ARG; + uint32_t nonnull_index = column.nonnull_start; + for (uint32_t row = 0; row < row_count; ++row) { + const uint32_t source_row = column.row_offset + row; + bool is_null = true; + if (column.notnull_bitmap != nullptr && + source_row < column.notnull_bitmap_size * 8U) { + is_null = (column.notnull_bitmap[source_row / 8U] & + (0x80U >> (source_row % 8U))) == 0; + } + + const char* data = nullptr; + uint32_t len = 0; + if (!is_null) { + if (column.type == common::STRING || column.type == common::TEXT || + column.type == common::BLOB) { + if (column.string_values == nullptr) { + return common::E_INVALID_ARG; + } + data = column.string_values[nonnull_index].buf_; + len = column.string_values[nonnull_index].len_; + } else { + if (column.fixed_values == nullptr) { + return common::E_INVALID_ARG; + } + const uint32_t elem_size = + common::get_data_type_size(column.type); + data = column.fixed_values + + static_cast(nonnull_index) * elem_size; + len = elem_size; + } + ++nonnull_index; + } + + bool satisfied = false; + if (kind_ == Kind::IS_NULL) { + satisfied = is_null; + } else if (kind_ == Kind::IS_NOT_NULL) { + satisfied = !is_null; + } else if (!is_null) { + if (column.type == common::BOOLEAN && + literal_.type == common::BOOLEAN) { + bool value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.bool_value, compare_op_); + } else if ((column.type == common::INT32 || + column.type == common::DATE) && + literal_.type == common::INT32) { + int32_t value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.int32_value, compare_op_); + } else if ((column.type == common::INT64 || + column.type == common::TIMESTAMP) && + literal_.type == common::INT64) { + int64_t value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.int64_value, compare_op_); + } else if (column.type == common::FLOAT && + literal_.type == common::FLOAT) { + float value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.float_value, compare_op_); + } else if (column.type == common::DOUBLE && + literal_.type == common::DOUBLE) { + double value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.double_value, compare_op_); + } else if (is_string_type(column.type) && + is_string_type(literal_.type)) { + satisfied = compare_string(data, len, literal_.string_value, + compare_op_); + } else { + return common::E_NOT_SUPPORT; + } + } + if (satisfied) mask.set_one(row); + } + return common::E_OK; +} + +int AlignedFieldFilter::evaluate_leaf( + common::TsBlock& block, const std::map& column_index, + SelectionMask& mask) const { + const auto found = column_index.find(column_); + if (found == column_index.end()) return common::E_COLUMN_NOT_EXIST; + + const uint32_t vector_index = found->second; + if (vector_index >= block.get_column_count()) return common::E_INVALID_ARG; + common::Vector* vector = block.get_vector(vector_index); + if (vector == nullptr) return common::E_INVALID_ARG; + + common::ColIterator iter(vector_index, &block); + const common::TSDataType column_type = vector->get_vector_type(); + for (uint32_t row = 0; row < block.get_row_count(); ++row) { + uint32_t len = 0; + bool is_null = false; + char* data = iter.read(&len, &is_null); + + bool satisfied = false; + if (kind_ == Kind::IS_NULL) { + satisfied = is_null; + } else if (kind_ == Kind::IS_NOT_NULL) { + satisfied = !is_null; + } else if (!is_null) { + if (column_type == common::BOOLEAN && + literal_.type == common::BOOLEAN) { + bool value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.bool_value, compare_op_); + } else if ((column_type == common::INT32 || + column_type == common::DATE) && + literal_.type == common::INT32) { + int32_t value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.int32_value, compare_op_); + } else if ((column_type == common::INT64 || + column_type == common::TIMESTAMP) && + literal_.type == common::INT64) { + int64_t value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.int64_value, compare_op_); + } else if (column_type == common::FLOAT && + literal_.type == common::FLOAT) { + float value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.float_value, compare_op_); + } else if (column_type == common::DOUBLE && + literal_.type == common::DOUBLE) { + double value; + std::memcpy(&value, data, sizeof(value)); + satisfied = + compare_values(value, literal_.double_value, compare_op_); + } else if (is_string_type(column_type) && + is_string_type(literal_.type)) { + satisfied = compare_string(data, len, literal_.string_value, + compare_op_); + } else { + return common::E_NOT_SUPPORT; + } + } + if (satisfied) mask.set_one(row); + iter.next(); + } + return common::E_OK; +} + +} // namespace storage diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h new file mode 100644 index 000000000..64f571dcf --- /dev/null +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef READER_FILTER_ALIGNED_FIELD_FILTER_H +#define READER_FILTER_ALIGNED_FIELD_FILTER_H + +#include +#include +#include +#include +#include +#include + +#include "common/tsblock/tsblock.h" +#include "reader/filter/filter.h" +#include "reader/filter/selection_mask.h" + +namespace storage { + +// Column-aware predicate used by aligned batch readers. It deliberately does +// not implement TableFieldFilter::satisfy_row(); aligned execution evaluates +// it against a whole TsBlock selection. +class AlignedFieldFilter : public Filter { + public: + enum class Kind { COMPARISON, IS_NULL, IS_NOT_NULL, AND, OR, NOT }; + + enum class CompareOp { EQ, NE, LT, LE, GT, GE }; + + struct ColumnView { + std::string name; + common::TSDataType type = common::INVALID_DATATYPE; + const char* fixed_values = nullptr; + const common::String* string_values = nullptr; + const uint8_t* notnull_bitmap = nullptr; + uint32_t notnull_bitmap_size = 0; + uint32_t row_offset = 0; + uint32_t nonnull_start = 0; + }; + + struct Literal { + common::TSDataType type = common::INVALID_DATATYPE; + bool bool_value = false; + int32_t int32_value = 0; + int64_t int64_value = 0; + float float_value = 0.0F; + double double_value = 0.0; + std::string string_value; + + static Literal boolean(bool value); + static Literal int32(int32_t value); + static Literal int64(int64_t value); + static Literal float32(float value); + static Literal float64(double value); + static Literal string(std::string value); + }; + + using Ptr = std::unique_ptr; + + ~AlignedFieldFilter() override = default; + + static Ptr compare(std::string column, CompareOp op, Literal literal); + static Ptr is_null(std::string column); + static Ptr is_not_null(std::string column); + static Ptr and_filter(Ptr left, Ptr right); + static Ptr or_filter(Ptr left, Ptr right); + static Ptr not_filter(Ptr child); + + Kind kind() const { return kind_; } + CompareOp compare_op() const { return compare_op_; } + const std::string& column() const { return column_; } + const Literal& literal() const { return literal_; } + + std::vector referenced_columns() const; + + // Conservatively decides whether any row in a page can satisfy the filter. + // Missing or unsupported statistics always return true. + bool may_match(const std::map& statistics, + int64_t row_count) const; + bool all_match(const std::map& statistics, + int64_t row_count) const; + + // Evaluates against block.get_vector(0) as time and column_names[i] as + // block.get_vector(i + 1). The mask is reset to block row count. + int evaluate(common::TsBlock& block, + const std::vector& column_names, + SelectionMask& mask) const; + int evaluate_columns(const std::vector& columns, + uint32_t row_count, SelectionMask& mask) const; + + private: + AlignedFieldFilter(Kind kind, std::string column, CompareOp op, + Literal literal); + AlignedFieldFilter(Kind kind, Ptr left, Ptr right); + // Push NOT down to the leaves so negation keeps SQL three-valued NULL + // semantics; a single SelectionMask bit cannot represent NULL vs FALSE. + static Ptr negate(Ptr node); + + int evaluate_leaf(common::TsBlock& block, + const std::map& column_index, + SelectionMask& mask) const; + int evaluate_leaf(const ColumnView& column, uint32_t row_count, + SelectionMask& mask) const; + Kind kind_; + std::string column_; + CompareOp compare_op_ = CompareOp::EQ; + Literal literal_; + Ptr left_; + Ptr right_; +}; + +} // namespace storage + +#endif // READER_FILTER_ALIGNED_FIELD_FILTER_H diff --git a/cpp/src/reader/filter/selection_mask.cc b/cpp/src/reader/filter/selection_mask.cc new file mode 100644 index 000000000..8ee93c1f3 --- /dev/null +++ b/cpp/src/reader/filter/selection_mask.cc @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "reader/filter/selection_mask.h" + +#include +#include + +namespace storage { +namespace { +uint32_t count_bits(uint64_t value) { +#if defined(__GNUC__) || defined(__clang__) + return static_cast(__builtin_popcountll(value)); +#else + uint32_t count = 0; + while (value != 0) { + value &= value - 1; + ++count; + } + return count; +#endif +} + +uint32_t trailing_zeros(uint64_t value) { + assert(value != 0); +#if defined(__GNUC__) || defined(__clang__) + return static_cast(__builtin_ctzll(value)); +#else + uint32_t count = 0; + while ((value & 1U) == 0) { + value >>= 1U; + ++count; + } + return count; +#endif +} +} // namespace + +SelectionMask::SelectionMask() : row_count_(0) {} + +SelectionMask::SelectionMask(uint32_t row_count) { reset(row_count); } + +void SelectionMask::reset(uint32_t row_count) { + row_count_ = row_count; + words_.assign(word_count(row_count), 0); +} + +void SelectionMask::set_all(bool value) { + const uint64_t fill = value ? ~static_cast(0) : 0; + for (auto& word : words_) { + word = fill; + } + clear_unused_bits(); +} + +void SelectionMask::set_one(uint32_t row) { + if (row >= row_count_) return; + words_[row >> 6U] |= (static_cast(1) << (row & 63U)); +} + +bool SelectionMask::test(uint32_t row) const { + if (row >= row_count_) return false; + return (words_[row >> 6U] & (static_cast(1) << (row & 63U))) != 0; +} + +void SelectionMask::and_with(const SelectionMask& other) { + assert(row_count_ == other.row_count_); + const size_t common = std::min(words_.size(), other.words_.size()); + for (size_t i = 0; i < common; ++i) { + words_[i] &= other.words_[i]; + } + // Rows past the shorter mask have no counterpart; ANDing with nothing + // clears them so a mismatched operand cannot leave stale bits. + for (size_t i = common; i < words_.size(); ++i) { + words_[i] = 0; + } +} + +void SelectionMask::or_with(const SelectionMask& other) { + assert(row_count_ == other.row_count_); + const size_t common = std::min(words_.size(), other.words_.size()); + for (size_t i = 0; i < common; ++i) { + words_[i] |= other.words_[i]; + } + // Rows past the shorter mask keep their value (ORing with nothing). +} + +void SelectionMask::invert() { + for (auto& word : words_) { + word = ~word; + } + clear_unused_bits(); +} + +uint32_t SelectionMask::count_ones() const { + uint32_t count = 0; + for (uint64_t word : words_) { + count += count_bits(word); + } + return count; +} + +bool SelectionMask::next_set_bit(uint32_t from, uint32_t& next) const { + if (from >= row_count_) return false; + uint32_t word_index = from >> 6U; + uint64_t word = + words_[word_index] & (~static_cast(0) << (from & 63U)); + while (true) { + if (word != 0) { + const uint32_t candidate = + (word_index << 6U) + trailing_zeros(word); + if (candidate >= row_count_) return false; + next = candidate; + return true; + } + ++word_index; + if (word_index >= words_.size()) return false; + word = words_[word_index]; + } +} + +void SelectionMask::clear_from(uint32_t row) { + if (row >= row_count_) return; + const uint32_t first_word = row >> 6U; + const uint32_t bit = row & 63U; + words_[first_word] &= + bit == 0 ? 0 : ((static_cast(1) << bit) - 1U); + for (size_t i = first_word + 1; i < words_.size(); ++i) { + words_[i] = 0; + } +} + +void SelectionMask::clear_unused_bits() { + if (words_.empty()) return; + const uint32_t used = row_count_ & 63U; + if (used != 0) { + words_.back() &= ((static_cast(1) << used) - 1U); + } +} + +} // namespace storage diff --git a/cpp/src/reader/filter/selection_mask.h b/cpp/src/reader/filter/selection_mask.h new file mode 100644 index 000000000..ed091e9a8 --- /dev/null +++ b/cpp/src/reader/filter/selection_mask.h @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef READER_FILTER_SELECTION_MASK_H +#define READER_FILTER_SELECTION_MASK_H + +#include +#include +#include + +namespace storage { + +// A compact row-selection bitmap. Bit i corresponds to row i in an aligned +// batch. All operations require masks with the same row count. +class SelectionMask { + public: + SelectionMask(); + explicit SelectionMask(uint32_t row_count); + + void reset(uint32_t row_count); + void set_all(bool value = true); + + uint32_t row_count() const { return row_count_; } + bool empty() const { return count_ones() == 0; } + + void set_one(uint32_t row); + bool test(uint32_t row) const; + + void and_with(const SelectionMask& other); + void or_with(const SelectionMask& other); + void invert(); + + uint32_t count_ones() const; + bool next_set_bit(uint32_t from, uint32_t& next) const; + void clear_from(uint32_t row); + + private: + static size_t word_count(uint32_t row_count) { + return (static_cast(row_count) + 63U) >> 6U; + } + void clear_unused_bits(); + + std::vector words_; + uint32_t row_count_; +}; + +} // namespace storage + +#endif // READER_FILTER_SELECTION_MASK_H diff --git a/cpp/src/reader/table_query_executor.cc b/cpp/src/reader/table_query_executor.cc index 5d708baae..084e1b363 100644 --- a/cpp/src/reader/table_query_executor.cc +++ b/cpp/src/reader/table_query_executor.cc @@ -31,6 +31,7 @@ int TableQueryExecutor::query(const std::string& table_name, ret_qds = nullptr; if (RET_FAIL(tsfile_io_reader_->get_tsfile_meta(file_metadata))) { delete time_filter; + delete field_filter; return ret; } common::PageArena pa; @@ -44,6 +45,7 @@ int TableQueryExecutor::query(const std::string& table_name, if (RET_FAIL(file_metadata->get_table_schema(table_name, table_schema))) { ret_qds = nullptr; delete time_filter; + delete field_filter; return ret; } file_metadata->get_table_metaindex_node(table_name, table_root); @@ -63,11 +65,21 @@ int TableQueryExecutor::query(const std::string& table_name, auto ind = table_schema->find_column_index(lower_case_column_names[i]); if (ind < 0) { delete time_filter; + delete field_filter; return common::E_COLUMN_NOT_EXIST; } data_types.push_back(table_schema->get_data_types()[ind]); } - // column_mapping.add(*measurement_filter); + if (auto* aligned_filter = + dynamic_cast(field_filter)) { + ret = + column_mapping->add_filter_columns(*aligned_filter, *table_schema); + if (ret != common::E_OK) { + delete time_filter; + delete field_filter; + return ret; + } + } auto device_task_iterator = std::unique_ptr(new DeviceTaskIterator( @@ -85,6 +97,7 @@ int TableQueryExecutor::query(const std::string& table_name, case TableQueryOrdering::TIME: default: delete time_filter; + delete field_filter; ret_qds = nullptr; return common::E_UNSUPPORTED_ORDER; } @@ -105,6 +118,7 @@ int TableQueryExecutor::query(const std::string& table_name, ret_qds = nullptr; if (RET_FAIL(tsfile_io_reader_->get_tsfile_meta(file_metadata))) { delete time_filter; + delete field_filter; return ret; } common::PageArena pa; @@ -114,6 +128,7 @@ int TableQueryExecutor::query(const std::string& table_name, if (RET_FAIL(file_metadata->get_table_schema(table_name, table_schema))) { ret_qds = nullptr; delete time_filter; + delete field_filter; return ret; } file_metadata->get_table_metaindex_node(table_name, table_root); @@ -133,10 +148,21 @@ int TableQueryExecutor::query(const std::string& table_name, auto ind = table_schema->find_column_index(lower_case_column_names[i]); if (ind < 0) { delete time_filter; + delete field_filter; return common::E_COLUMN_NOT_EXIST; } data_types.push_back(table_schema->get_data_types()[ind]); } + if (auto* aligned_filter = + dynamic_cast(field_filter)) { + ret = + column_mapping->add_filter_columns(*aligned_filter, *table_schema); + if (ret != common::E_OK) { + delete time_filter; + delete field_filter; + return ret; + } + } auto device_task_iterator = std::unique_ptr(new DeviceTaskIterator( @@ -155,6 +181,7 @@ int TableQueryExecutor::query(const std::string& table_name, case TableQueryOrdering::TIME: default: delete time_filter; + delete field_filter; ret_qds = nullptr; return common::E_UNSUPPORTED_ORDER; } diff --git a/cpp/src/reader/tsfile_series_scan_iterator.cc b/cpp/src/reader/tsfile_series_scan_iterator.cc index fbf8f93db..948d1a553 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.cc +++ b/cpp/src/reader/tsfile_series_scan_iterator.cc @@ -304,7 +304,8 @@ int TsFileSeriesScanIterator::get_next(TsBlock*& ret_tsblock, bool alloc, if (should_skip_chunk_by_time(time_cm, min_time_hint)) { continue; } - if (chunk_fully_satisfies_filter(time_cm, filter) && + if (field_filter_ == nullptr && + chunk_fully_satisfies_filter(time_cm, filter) && should_skip_multi_aligned_chunk_by_offset(time_cm, value_cms)) { continue; @@ -512,6 +513,7 @@ int TsFileSeriesScanIterator::init_chunk_reader_multi() { itimeseries_index_->get_data_type(), time_filter_))) { return ret; } + acr->set_field_filter(field_filter_); // No chunks → nothing to load; iteration short-circuits via // has_next_chunk() returning false. diff --git a/cpp/src/reader/tsfile_series_scan_iterator.h b/cpp/src/reader/tsfile_series_scan_iterator.h index 449f240b4..19c261694 100644 --- a/cpp/src/reader/tsfile_series_scan_iterator.h +++ b/cpp/src/reader/tsfile_series_scan_iterator.h @@ -29,6 +29,7 @@ #include "file/random_access_read_file.h" #include "file/tsfile_io_reader.h" #include "reader/chunk_reader.h" +#include "reader/filter/aligned_field_filter.h" #include "reader/filter/filter.h" #include "utils/util_define.h" @@ -51,6 +52,7 @@ class TsFileSeriesScanIterator { tuple_desc_(), tsblock_(nullptr), time_filter_(nullptr), + field_filter_(nullptr), prepared_(), is_aligned_(false), is_multi_value_(false), @@ -61,12 +63,14 @@ class TsFileSeriesScanIterator { int init(std::shared_ptr device_id, const std::string& measurement_name, RandomAccessReadFile* read_file, Filter* time_filter, - common::PageArena& data_pa) { + common::PageArena& data_pa, + AlignedFieldFilter* field_filter = nullptr) { ASSERT(read_file != nullptr); device_id_ = device_id; measurement_name_ = measurement_name; read_file_ = read_file; time_filter_ = time_filter; + field_filter_ = field_filter; data_pa_ = &data_pa; return common::E_OK; } @@ -115,6 +119,12 @@ class TsFileSeriesScanIterator { int64_t min_time_hint = std::numeric_limits::min()); void revert_tsblock(); + bool field_filter_applied_in_chunk() const { + if (!is_multi_value_ || chunk_reader_ == nullptr) return false; + auto* aligned = dynamic_cast(chunk_reader_); + return aligned != nullptr && aligned->field_filter_applied(); + } + // Multi-value: number of value columns in the TsBlock uint32_t get_value_column_count() const { if (is_multi_value_ && chunk_reader_) { @@ -225,6 +235,7 @@ class TsFileSeriesScanIterator { common::TupleDesc tuple_desc_; common::TsBlock* tsblock_; Filter* time_filter_; + AlignedFieldFilter* field_filter_; // Keeps the arena-backed index alive until the chunk reader has released // every pointer into it. Empty for the legacy path-owned metadata arena. std::shared_ptr prepared_; diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc new file mode 100644 index 000000000..880053112 --- /dev/null +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "reader/filter/aligned_field_filter.h" + +#include + +#include "common/statistic.h" +#include "reader/filter/selection_mask.h" + +namespace storage { +namespace { + +TEST(SelectionMaskTest, BooleanOperationsAndTrailingBits) { + SelectionMask left(70); + SelectionMask right(70); + for (uint32_t row : {0U, 2U, 63U, 64U, 69U}) left.set_one(row); + for (uint32_t row : {1U, 2U, 64U}) right.set_one(row); + + left.and_with(right); + EXPECT_EQ(left.count_ones(), 2U); + EXPECT_TRUE(left.test(2)); + EXPECT_TRUE(left.test(64)); + + left.or_with(right); + EXPECT_EQ(left.count_ones(), 3U); + left.invert(); + EXPECT_EQ(left.count_ones(), 67U); + EXPECT_FALSE(left.test(2)); + EXPECT_TRUE(left.test(69)); +} + +TEST(SelectionMaskTest, NextSetBitAndClearFrom) { + SelectionMask mask(130); + for (uint32_t row : {0U, 63U, 64U, 129U}) mask.set_one(row); + uint32_t value = 0; + EXPECT_TRUE(mask.next_set_bit(0, value)); + EXPECT_EQ(value, 0U); + EXPECT_TRUE(mask.next_set_bit(1, value)); + EXPECT_EQ(value, 63U); + EXPECT_TRUE(mask.next_set_bit(64, value)); + EXPECT_EQ(value, 64U); + EXPECT_TRUE(mask.next_set_bit(65, value)); + EXPECT_EQ(value, 129U); + mask.clear_from(64); + EXPECT_EQ(mask.count_ones(), 2U); + EXPECT_FALSE(mask.test(64)); + EXPECT_FALSE(mask.test(129)); +} + +TEST(AlignedFieldFilterTest, PageStatisticsAreConservative) { + Int64Statistic statistic; + statistic.count_ = 5; + statistic.min_value_ = 10; + statistic.max_value_ = 20; + const std::map stats{{"a", &statistic}}; + + auto below = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::LT, + AlignedFieldFilter::Literal::int64(5)); + EXPECT_FALSE(below->may_match(stats, 5)); + + auto overlap = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int64(10)); + EXPECT_TRUE(overlap->may_match(stats, 5)); + + auto is_null = AlignedFieldFilter::is_null("a"); + EXPECT_FALSE(is_null->may_match(stats, 5)); + statistic.count_ = 4; + EXPECT_TRUE(is_null->may_match(stats, 5)); + + auto missing = AlignedFieldFilter::compare( + "missing", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::int64(1)); + EXPECT_TRUE(missing->may_match(stats, 5)); +} + +TEST(AlignedFieldFilterTest, PageStatisticsAllMatchIsConservative) { + Int64Statistic statistic; + statistic.count_ = 5; + statistic.min_value_ = 10; + statistic.max_value_ = 20; + const std::map stats{{"a", &statistic}}; + + auto ge = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::GE, + AlignedFieldFilter::Literal::int64(10)); + EXPECT_TRUE(ge->all_match(stats, 5)); + + auto lt = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::LT, + AlignedFieldFilter::Literal::int64(21)); + EXPECT_TRUE(lt->all_match(stats, 5)); + + auto strict_lt = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::LT, + AlignedFieldFilter::Literal::int64(20)); + EXPECT_FALSE(strict_lt->all_match(stats, 5)); + + auto not_null = AlignedFieldFilter::is_not_null("a"); + EXPECT_TRUE(not_null->all_match(stats, 5)); + statistic.count_ = 4; + EXPECT_FALSE(not_null->all_match(stats, 5)); + auto is_null = AlignedFieldFilter::is_null("a"); + statistic.count_ = 0; + EXPECT_TRUE(is_null->all_match(stats, 5)); + + statistic.count_ = 5; + auto combined = AlignedFieldFilter::and_filter( + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::GE, + AlignedFieldFilter::Literal::int64(10)), + AlignedFieldFilter::is_not_null("a")); + EXPECT_TRUE(combined->all_match(stats, 5)); +} + +TEST(AlignedFieldFilterTest, CollectsReferencedColumnsOnce) { + auto left = + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::int32(1)); + auto right = AlignedFieldFilter::or_filter( + AlignedFieldFilter::is_null("b"), + AlignedFieldFilter::compare("a", AlignedFieldFilter::CompareOp::GT, + AlignedFieldFilter::Literal::int32(3))); + auto root = + AlignedFieldFilter::and_filter(std::move(left), std::move(right)); + EXPECT_EQ(root->referenced_columns(), (std::vector{"a", "b"})); +} + +} // namespace +} // namespace storage diff --git a/cpp/test/reader/table_field_filter_test.cc b/cpp/test/reader/table_field_filter_test.cc index 861cb0fa7..62b495a83 100644 --- a/cpp/test/reader/table_field_filter_test.cc +++ b/cpp/test/reader/table_field_filter_test.cc @@ -29,6 +29,7 @@ #include "common/tablet.h" #include "file/local_random_access_read_file.h" #include "file/write_file.h" +#include "reader/filter/aligned_field_filter.h" #include "reader/filter/tag_filter.h" #include "reader/filter/time_filter.h" #include "reader/filter/time_operator.h" @@ -307,6 +308,99 @@ class TableFieldFilterTest const int64_t base_ = INT64_C(1099511627776); }; +TEST_P(TableFieldFilterTest, AlignedBatchFilterAndOrUsesFilterOnlyColumns) { + auto left = AlignedFieldFilter::and_filter( + AlignedFieldFilter::compare("i", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int32(-4)), + AlignedFieldFilter::compare( + "flag", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::boolean(true))); + auto right = AlignedFieldFilter::is_not_null("tail_null"); + auto filter = + AlignedFieldFilter::or_filter(std::move(left), std::move(right)); + + EXPECT_EQ(scan({"v"}, filter.release()), expected([](int t) { + const bool and_branch = t % 3 != 0 && t >= 4 && (t % 2 == 1); + const bool or_branch = t % 3 == 0; + return and_branch || or_branch; + })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterSupportsNotAndChunkNulls) { + auto filter = AlignedFieldFilter::not_filter( + AlignedFieldFilter::is_null("chunk_null")); + EXPECT_EQ(scan({"v"}, filter.release()), + expected([](int t) { return t < 7; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterNotComparisonExcludesNulls) { + // NOT over a nullable comparison must exclude NULL rows (SQL three-valued + // logic): NOT(i <= -5) == i > -5, and NULL > -5 is still NULL, not TRUE. + auto filter = AlignedFieldFilter::not_filter( + AlignedFieldFilter::compare("i", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int32(-5))); + EXPECT_EQ(scan({"v"}, filter.release()), + expected([](int t) { return t % 3 != 0 && t < 5; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterAppliesOffsetAfterMask) { + for (int offset : {0, 1, 3, 8}) { + for (int limit : {0, 1, 4, -1}) { + auto filter = AlignedFieldFilter::compare( + "i", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int32(-5)); + auto all = expected([](int t) { return t % 3 != 0 && t >= 5; }); + auto begin = all.begin() + std::min(offset, all.size()); + auto end = limit < 0 + ? all.end() + : begin + std::min(limit, all.end() - begin); + std::vector wanted(begin, end); + EXPECT_EQ( + scan({"v"}, filter.release(), offset, limit, nullptr, nullptr), + wanted); + } + } +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterSharedProjectionColumn) { + auto filter = + AlignedFieldFilter::compare("i", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int32(-5)); + EXPECT_EQ(scan({"i"}, filter.release()), + expected([](int t) { return t % 3 != 0 && t >= 5; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterAllMatchFastPath) { + auto filter = + AlignedFieldFilter::compare("v", AlignedFieldFilter::CompareOp::GE, + AlignedFieldFilter::Literal::int64(0)); + EXPECT_EQ(scan({"v"}, filter.release()), + expected([](int) { return true; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterAllNullFilterOnlyColumn) { + auto filter = AlignedFieldFilter::is_null("empty"); + EXPECT_EQ(scan({"v"}, filter.release()), + expected([](int) { return true; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterCombinesTimeAndFieldMask) { + auto filter = + AlignedFieldFilter::compare("i", AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::Literal::int32(-5)); + EXPECT_EQ(scan({"v"}, filter.release(), 0, -1, + TimeFilter::between(base_ + 7, base_ + 10, false)), + expected([](int t) { return t >= 7 && t <= 10 && t % 3 != 0; })); +} + +TEST_P(TableFieldFilterTest, AlignedBatchFilterSupportsStringLiterals) { + auto filter = AlignedFieldFilter::compare( + "s", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::string(std::string("a\0b", 3))); + EXPECT_EQ(scan({"v"}, filter.release()), + expected([](int t) { return t % 2 == 1; })); +} + TEST_P(TableFieldFilterTest, NullTrueFalseAndRejectWholePages) { EXPECT_EQ(scan({"v"}, nullptr), expected([](int) { return true; })); for (bool accept : {false, true}) { From 11bfc35c99f8e845939a6753f004ecee38c7a2ec Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 22 Oct 2026 14:21:28 +0800 Subject: [PATCH 06/16] [C++] Bulk-copy aligned FIELD selections and fix edge cases Copy selected bitmap runs with dense fixed-width bulk appends while preserving NULL/string fallbacks, duplicate projections, post-filter row ranges, and source-block cursor ownership. Keep page-plan decode mode stable across TsBlocks, retain NaN rows when floating statistics cannot prove a match, and evaluate missing aligned columns as NULL. Add focused and integration regression coverage. --- cpp/src/reader/aligned_chunk_reader.cc | 9 +- cpp/src/reader/aligned_chunk_reader.h | 1 + .../block/single_device_tsblock_reader.cc | 220 ++++++---- .../block/single_device_tsblock_reader.h | 9 + cpp/src/reader/filter/aligned_field_filter.cc | 24 +- cpp/src/reader/filter/selection_mask.cc | 20 + cpp/src/reader/filter/selection_mask.h | 3 + .../aligned_field_filter_regression_test.cc | 384 ++++++++++++++++++ .../reader/block/masked_bulk_copy_test.cc | 283 +++++++++++++ .../filter/aligned_field_filter_test.cc | 107 +++++ 10 files changed, 961 insertions(+), 99 deletions(-) create mode 100644 cpp/test/reader/aligned_field_filter_regression_test.cc create mode 100644 cpp/test/reader/block/masked_bulk_copy_test.cc diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 2fa694079..284c0347f 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -1865,11 +1865,16 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, // empty so the scatter loop decodes on demand and releases after each page // (see decode_page_lazy() / release_page_slot()). Bounds memory to one page. int AlignedChunkReader::decode_all_planned_pages() { + // Keep this decision stable while a page spans multiple SSI TsBlocks. + // A populated first-page buffer does not imply that later pages were + // eagerly decoded. + page_plan_lazy_ = true; if (chunk_pages_.empty()) return E_OK; build_filter_column_mask(); #ifdef ENABLE_THREADS if (decode_pool_ != nullptr && value_columns_.size() > 1) { + page_plan_lazy_ = false; // Lazily grow the per-worker time decoder/compressor pool. Both // factories can return nullptr on OOM/unsupported config; without // checking, the worker task below dereferences null when calling @@ -2053,9 +2058,7 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, const uint32_t null_mask_base = 1 << 7; const uint32_t num_cols = value_columns_.size(); RowAppender row_appender(ret_tsblock); - // Detect single-thread lazy mode by whether decode_all_planned_pages left - // per_page_times_ empty (it leaves slots empty when there's no pool). - const bool single_thread_lazy = per_page_times_[0].empty(); + const bool single_thread_lazy = page_plan_lazy_; while (current_page_plan_index_ < chunk_pages_.size()) { const ChunkPageInfo& page_info = chunk_pages_[current_page_plan_index_]; diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index 81311febf..bd19c8263 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -328,6 +328,7 @@ class AlignedChunkReader : public IChunkReader { std::vector chunk_pages_; std::vector> per_page_times_; bool page_plan_built_ = false; + bool page_plan_lazy_ = true; bool current_page_loaded_ = false; size_t current_page_plan_index_ = 0; std::vector filter_column_mask_; diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index 8330997de..0d9e43ffd 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -381,7 +381,8 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } // Detect aligned fast path: every field column comes from an aligned chunk. - if ((field_filter == nullptr || aligned_field_filter_ != nullptr) && + if ((field_filter == nullptr || + (aligned_field_filter_ != nullptr && used_multi)) && !field_column_contexts_.empty() && (dense_row_count_ >= 0 || aligned_field_filter_ != nullptr) && aligned_col_count_ == field_column_contexts_.size()) { @@ -401,9 +402,15 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, if (aligned_field_filter_ != nullptr && !all_aligned_ && !field_column_contexts_.empty()) { - // The structured batch filter is aligned-only. Legacy Filter objects - // continue to use the row-oriented path. - return common::E_NOT_SUPPORT; + // Missing metadata (or unequal chunk counts) can make an aligned + // device use per-column contexts. Keep the row merge for that case, + // with absent FIELDs exposed as NULL, but still reject non-aligned + // sources for the aligned-only structured filter. + for (const auto* ts_index : time_series_indexs) { + if (ts_index != nullptr && !ts_index->is_aligned()) { + return common::E_NOT_SUPPORT; + } + } } if (field_column_contexts_.empty()) { @@ -808,6 +815,36 @@ int SingleDeviceTsBlockReader::satisfy_field_filter(bool& satisfied) { device_query_task_->get_table_schema()->get_column_schema(name); fields.push_back({name, schema.data_type_, value, length}); } + if (aligned_field_filter_ != nullptr) { + // Aligned devices missing one of the requested measurements cannot + // use the shared multi-column SSI. Evaluate their merged row through + // the same typed column-view evaluator, including explicit NULL + // views for measurements that have no metadata on this device. + const uint8_t notnull = 0x80; + std::vector strings(fields.size()); + std::vector views; + views.reserve(fields.size()); + for (size_t i = 0; i < fields.size(); ++i) { + const auto& field = fields[i]; + AlignedFieldFilter::ColumnView view; + view.name = field.name; + view.type = field.type; + view.notnull_bitmap = field.is_null() ? nullptr : ¬null; + view.notnull_bitmap_size = 1; + if (field.type == common::STRING || field.type == common::TEXT || + field.type == common::BLOB) { + strings[i] = common::String(field.data, field.size); + view.string_values = &strings[i]; + } else { + view.fixed_values = field.data; + } + views.push_back(std::move(view)); + } + SelectionMask mask; + ret = aligned_field_filter_->evaluate_columns(views, 1, mask); + satisfied = ret == common::E_OK && mask.test(0); + return ret; + } return evaluate_table_field_filter(*field_filter_, {next_time_, fields}, satisfied); } @@ -1308,85 +1345,11 @@ int VectorMeasurementColumnContext::evaluate_filter( return filter.evaluate(*tsblock_, column_names_, mask); } -int VectorMeasurementColumnContext::filtered_bulk_copy_into( +void VectorMeasurementColumnContext::copy_rows_in_current_block( std::vector& col_appenders, common::ColAppender* time_appender, common::ColAppender* extra_time_appender, common::RowAppender* row_appender, - const SelectionMask& mask, uint32_t& source_row, int& remaining_offset, - int& remaining_limit, uint32_t& copied, bool& block_exhausted) { - copied = 0; - block_exhausted = false; - if (tsblock_ == nullptr || time_iter_ == nullptr || time_iter_->end()) { - block_exhausted = true; - return common::E_OK; - } - - const uint32_t row_count = tsblock_->get_row_count(); - while (source_row < row_count) { - if (remaining_limit == 0) break; - if (row_appender != nullptr && row_appender->remaining() == 0) break; - - uint32_t time_len = 0; - int64_t time = *reinterpret_cast(time_iter_->read(&time_len)); - bool selected = mask.test(source_row); - if (selected && remaining_offset > 0) { - --remaining_offset; - selected = false; - } - - if (selected) { - if (time_appender != nullptr) { - time_appender->add_row(); - time_appender->append(reinterpret_cast(&time), - sizeof(time)); - } - if (extra_time_appender != nullptr) { - extra_time_appender->add_row(); - extra_time_appender->append(reinterpret_cast(&time), - sizeof(time)); - } - for (uint32_t c = 0; - c < value_iters_.size() && c < pos_in_result_.size(); ++c) { - uint32_t len = 0; - bool is_null = false; - char* value = value_iters_[c]->read(&len, &is_null); - for (int32_t pos : pos_in_result_[c]) { - auto* appender = col_appenders[pos + 1]; - appender->add_row(); - if (is_null) { - appender->append_null(); - } else { - appender->append(value, len); - } - } - } - if (row_appender != nullptr) row_appender->add_row(); - ++copied; - if (remaining_limit > 0) --remaining_limit; - } else { - // Reading is required even for rejected rows because variable - // length cursors use the current row length when advancing. - for (auto* value_iter : value_iters_) { - uint32_t len = 0; - bool is_null = false; - value_iter->read(&len, &is_null); - } - } - - time_iter_->next(); - for (auto* value_iter : value_iters_) value_iter->next(); - ++source_row; - } - - if (source_row >= row_count) block_exhausted = true; - return common::E_OK; -} - -int VectorMeasurementColumnContext::bulk_copy_into( - std::vector& col_appenders, - common::ColAppender* time_appender, common::RowAppender* row_appender, uint32_t count) { - int ret = common::E_OK; const uint32_t time_elem_size = sizeof(int64_t); // Bulk copy time column (only when time_appender is provided). @@ -1395,6 +1358,11 @@ int VectorMeasurementColumnContext::bulk_copy_into( time_elem_size); } + if (extra_time_appender != nullptr) { + extra_time_appender->bulk_append_fixed(time_iter_->data_ptr(), count, + time_elem_size); + } + // Advance output row count. if (row_appender) { row_appender->add_rows(count); @@ -1453,34 +1421,108 @@ int VectorMeasurementColumnContext::bulk_copy_into( // Variable-length iterators and fixed-length iterators with nulls were // already advanced in the copy loop above. } +} - // If source TsBlock exhausted, load next. - if (time_iter_->end()) { - if (RET_FAIL(get_next_tsblock(false))) return ret; +int VectorMeasurementColumnContext::filtered_bulk_copy_into( + std::vector& col_appenders, + common::ColAppender* time_appender, + common::ColAppender* extra_time_appender, common::RowAppender* row_appender, + const SelectionMask& mask, uint32_t& source_row, int& remaining_offset, + int& remaining_limit, uint32_t& copied, bool& block_exhausted) { + copied = 0; + block_exhausted = false; + if (tsblock_ == nullptr || time_iter_ == nullptr || time_iter_->end()) { + block_exhausted = true; + return common::E_OK; } - return ret; + + const uint32_t row_count = tsblock_->get_row_count(); + if (mask.row_count() != row_count || + source_row != time_iter_->get_row_id()) { + return common::E_INVALID_ARG; + } + while (source_row < row_count) { + if (remaining_limit == 0) break; + if (row_appender != nullptr && row_appender->remaining() == 0) break; + + uint32_t begin = 0, end = 0; + if (!mask.next_selected_run(source_row, begin, end)) { + skip_rows_in_current_block(row_count - source_row); + source_row = row_count; + break; + } + // Reject gaps without copying them. Do not prefetch another source + // block: mask/source_row must remain bound to this one until exhausted. + if (begin > source_row) { + skip_rows_in_current_block(begin - source_row); + source_row = begin; + } + uint32_t count = end - source_row; + if (remaining_offset > 0) { + const uint32_t skip = + std::min(count, static_cast(remaining_offset)); + skip_rows_in_current_block(skip); + source_row += skip; + remaining_offset -= static_cast(skip); + count -= skip; + if (count == 0) continue; + } + if (row_appender != nullptr) { + count = std::min(count, row_appender->remaining()); + } + if (remaining_limit > 0) { + count = std::min(count, static_cast(remaining_limit)); + } + + // A full mask is one run. Dense fixed-width columns use memcpy for + // the whole retained run; NULL/string columns retain scalar copying. + copy_rows_in_current_block(col_appenders, time_appender, + extra_time_appender, row_appender, count); + source_row += count; + copied += count; + if (remaining_limit > 0) remaining_limit -= static_cast(count); + } + block_exhausted = source_row == row_count; + return common::E_OK; } -int VectorMeasurementColumnContext::skip_rows(uint32_t count) { - if (!time_iter_ || time_iter_->end()) return common::E_OK; +int VectorMeasurementColumnContext::bulk_copy_into( + std::vector& col_appenders, + common::ColAppender* time_appender, common::RowAppender* row_appender, + uint32_t count) { + copy_rows_in_current_block(col_appenders, time_appender, nullptr, + row_appender, count); + // Preserve eager refill for the unfiltered caller. Masked copies must + // instead let their caller invalidate the old mask before loading more. + if (time_iter_->end()) return get_next_tsblock(false); + return common::E_OK; +} + +void VectorMeasurementColumnContext::skip_rows_in_current_block( + uint32_t count) { const uint32_t time_elem_size = sizeof(int64_t); - uint32_t to_skip = std::min(count, time_iter_->remaining()); - time_iter_->advance(to_skip, time_elem_size); + time_iter_->advance(count, time_elem_size); for (uint32_t c = 0; c < value_iters_.size(); c++) { auto dt = value_iters_[c]->get_data_type(); bool is_varlen = (dt == common::STRING || dt == common::TEXT || dt == common::BLOB); if (!is_varlen && !value_iters_[c]->has_null()) { uint32_t val_elem_size = common::get_data_type_size(dt); - value_iters_[c]->advance(to_skip, val_elem_size); + value_iters_[c]->advance(count, val_elem_size); } else { // Variable-length and fixed-length-with-null vectors need next() // to keep the payload offset aligned with non-null rows. - for (uint32_t r = 0; r < to_skip; r++) { + for (uint32_t r = 0; r < count; r++) { value_iters_[c]->next(); } } } +} + +int VectorMeasurementColumnContext::skip_rows(uint32_t count) { + if (!time_iter_ || time_iter_->end()) return common::E_OK; + const uint32_t to_skip = std::min(count, time_iter_->remaining()); + skip_rows_in_current_block(to_skip); consume_ssi_row_offset(static_cast(to_skip)); if (time_iter_->end()) { int r = get_next_tsblock(false); diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index b028fe2b9..19a6823d9 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -223,6 +223,15 @@ class VectorMeasurementColumnContext final : public MeasurementColumnContext { uint32_t& copied, bool& block_exhausted); private: + // These helpers never load another SSI TsBlock: callers may still hold a + // selection mask and source-row cursor belonging to the current block. + void copy_rows_in_current_block( + std::vector& col_appenders, + common::ColAppender* time_appender, + common::ColAppender* extra_time_appender, + common::RowAppender* row_appender, uint32_t count); + void skip_rows_in_current_block(uint32_t count); + std::vector column_names_; std::vector> pos_in_result_; std::vector value_iters_; diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index ebdcfd3f3..375f81bcb 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -68,9 +69,14 @@ int compare_string(const char* data, uint32_t len, const std::string& literal, return false; } -bool is_integer_type(common::TSDataType type) { - return type == common::INT32 || type == common::DATE || - type == common::INT64 || type == common::TIMESTAMP; +// NaN values count as non-NULL, but may not change finite min/max values. +// Their presence is retained in the sum, including after statistics merge. +// A NaN sum can also result from opposite infinities; conservatively falling +// back to row evaluation is safe in that case as well. +template +bool has_nan_statistics(const S& statistic) { + return std::isnan(statistic.sum_value_) || + std::isnan(statistic.min_value_) || std::isnan(statistic.max_value_); } bool is_string_type(common::TSDataType type) { @@ -312,7 +318,8 @@ bool AlignedFieldFilter::may_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::FLOAT) return true; + if (literal_.type != common::FLOAT || has_nan_statistics(*typed)) + return true; switch (compare_op_) { case CompareOp::EQ: return typed->min_value_ <= literal_.float_value && @@ -332,7 +339,8 @@ bool AlignedFieldFilter::may_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::DOUBLE) return true; + if (literal_.type != common::DOUBLE || has_nan_statistics(*typed)) + return true; switch (compare_op_) { case CompareOp::EQ: return typed->min_value_ <= literal_.double_value && @@ -424,7 +432,8 @@ bool AlignedFieldFilter::all_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::FLOAT) return false; + if (literal_.type != common::FLOAT || has_nan_statistics(*typed)) + return false; switch (compare_op_) { case CompareOp::EQ: return typed->min_value_ == literal_.float_value && @@ -443,7 +452,8 @@ bool AlignedFieldFilter::all_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::DOUBLE) return false; + if (literal_.type != common::DOUBLE || has_nan_statistics(*typed)) + return false; switch (compare_op_) { case CompareOp::EQ: return typed->min_value_ == literal_.double_value && diff --git a/cpp/src/reader/filter/selection_mask.cc b/cpp/src/reader/filter/selection_mask.cc index 8ee93c1f3..fe38955fc 100644 --- a/cpp/src/reader/filter/selection_mask.cc +++ b/cpp/src/reader/filter/selection_mask.cc @@ -135,6 +135,26 @@ bool SelectionMask::next_set_bit(uint32_t from, uint32_t& next) const { } } +bool SelectionMask::next_selected_run(uint32_t from, uint32_t& begin, + uint32_t& end) const { + if (!next_set_bit(from, begin)) return false; + uint32_t word_index = begin >> 6U; + uint64_t unselected = + ~words_[word_index] & (~static_cast(0) << (begin & 63U)); + while (true) { + if (unselected != 0) { + end = std::min(row_count_, + (word_index << 6U) + trailing_zeros(unselected)); + return true; + } + if (++word_index >= words_.size()) { + end = row_count_; + return true; + } + unselected = ~words_[word_index]; + } +} + void SelectionMask::clear_from(uint32_t row) { if (row >= row_count_) return; const uint32_t first_word = row >> 6U; diff --git a/cpp/src/reader/filter/selection_mask.h b/cpp/src/reader/filter/selection_mask.h index ed091e9a8..784e6d0dc 100644 --- a/cpp/src/reader/filter/selection_mask.h +++ b/cpp/src/reader/filter/selection_mask.h @@ -48,6 +48,9 @@ class SelectionMask { uint32_t count_ones() const; bool next_set_bit(uint32_t from, uint32_t& next) const; + // Find the next contiguous selected interval [begin, end), starting at + // or after from. The end is clamped to row_count(), excluding padding. + bool next_selected_run(uint32_t from, uint32_t& begin, uint32_t& end) const; void clear_from(uint32_t row); private: diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc new file mode 100644 index 000000000..0a1c27db4 --- /dev/null +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -0,0 +1,384 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "common/config/config.h" +#include "common/tablet.h" +#include "file/local_random_access_read_file.h" +#include "file/write_file.h" +#include "reader/filter/aligned_field_filter.h" +#include "reader/filter/time_filter.h" +#include "reader/filter/time_operator.h" +#include "reader/table_query_executor.h" +#include "writer/tsfile_table_writer.h" + +namespace { +using namespace common; +using namespace storage; +using A = AlignedFieldFilter; +using Rows = std::vector>; +const int64_t kNull = INT64_MIN; + +bool compare(double value, double literal, A::CompareOp op) { + switch (op) { + case A::CompareOp::EQ: + return value == literal; + case A::CompareOp::NE: + return value != literal; + case A::CompareOp::LT: + return value < literal; + case A::CompareOp::LE: + return value <= literal; + case A::CompareOp::GT: + return value > literal; + case A::CompareOp::GE: + return value >= literal; + } + return false; +} + +class AlignedFieldFilterRegressionTest + : public ::testing::TestWithParam> { + protected: + void SetUp() override { + libtsfile_init(); + saved_ = g_config_value_; + g_config_value_.page_writer_max_point_num_ = 3; + g_config_value_.parallel_read_enabled_ = std::get<0>(GetParam()); + filename_ = + "aligned_field_filter_regression_" + + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()) + + ".tsfile"; + } + void TearDown() override { + std::remove(filename_.c_str()); + g_config_value_ = saved_; + libtsfile_destroy(); + } + void create(WriteFile& file) { + int flags = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + flags |= O_BINARY; +#endif + ASSERT_EQ(file.create(filename_, flags, 0666), E_OK); + } + Rows scan(const std::vector& columns, A::Ptr filter, + int offset = 0, int limit = -1, Filter* time_filter = nullptr) { + LocalRandomAccessReadFile file; + EXPECT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + EXPECT_EQ(executor.query("t", columns, time_filter, nullptr, + filter.release(), offset, limit, result), + E_OK); + Rows rows; + if (!result) return rows; + EXPECT_EQ(result->get_metadata()->get_column_count(), + columns.size() + 1); + bool next = false; + int ret; + while ((ret = result->next(next)) == E_OK && next) { + if (std::get<1>(GetParam()) < 0) { + std::vector values; + for (uint32_t c = 1; c <= columns.size() + 1; ++c) + values.push_back( + result->is_null(c) ? kNull : result->get_int64_at(c)); + rows.push_back(std::move(values)); + } else { + EXPECT_EQ(result->next(next), E_OK); + EXPECT_TRUE(next); + TsBlock* block = nullptr; + EXPECT_EQ(result->get_next_tsblock(block), E_OK); + if (!block) break; + RowIterator iter(block); + while (iter.has_next()) { + std::vector values; + for (uint32_t c = 0; c <= columns.size(); ++c) { + uint32_t len = 0; + bool null = false; + char* data = iter.read(c, &len, &null); + int64_t value = kNull; + if (!null) { + EXPECT_EQ(len, sizeof(value)); + std::memcpy(&value, data, sizeof(value)); + } + values.push_back(value); + } + rows.push_back(std::move(values)); + iter.next(); + } + } + } + EXPECT_EQ(ret, E_OK); + EXPECT_FALSE(next); + EXPECT_EQ(result->next(next), E_OK); + EXPECT_FALSE(next); + executor.destroy_query_data_set(result); + return rows; + } + void write_floats() { + const std::vector columns{ + {"id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG}, + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"f", FLOAT, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"d", DOUBLE, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + TableSchema schema("t", columns); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + for (int device = 0; device < 2; ++device) { + Tablet tablet("t", schema.get_measurement_names(), + schema.get_data_types(), + schema.get_column_categories(), float_values_.size()); + for (uint32_t r = 0; r < float_values_.size(); ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "id", device == 0 ? "d0" : "d1"), + E_OK); + ASSERT_EQ(tablet.add_value(r, "v", int64_t(device * 100 + r)), + E_OK); + ASSERT_EQ(tablet.add_value(r, "f", float(float_values_[r])), + E_OK); + ASSERT_EQ(tablet.add_value(r, "d", float_values_[r]), E_OK); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.flush(), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + } + void write_missing_columns() { + const std::vector columns{ + {"id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG}, + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"absent", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + TableSchema schema("t", columns); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + for (int device = 0; device < 3; ++device) { + std::vector names{"id"}; + std::vector types{STRING}; + std::vector categories{ColumnCategory::TAG}; + if (device != 2) { + names.push_back("v"); + types.push_back(INT64); + categories.push_back(ColumnCategory::FIELD); + } + if (device != 0) { + names.push_back("absent"); + types.push_back(INT64); + categories.push_back(ColumnCategory::FIELD); + names.push_back("s"); + types.push_back(STRING); + categories.push_back(ColumnCategory::FIELD); + } + Tablet tablet("t", names, types, categories, 6); + for (int r = 0; r < 6; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + const std::string id = "d" + std::to_string(device); + ASSERT_EQ(tablet.add_value(r, "id", id.c_str()), E_OK); + if (device != 2) + ASSERT_EQ( + tablet.add_value(r, "v", int64_t(device * 100 + r)), + E_OK); + if (device != 0) { + ASSERT_EQ(tablet.add_value(r, "absent", + int64_t(device * 1000 + r)), + E_OK); + const std::string bytes = + r % 2 == 0 ? std::string("a\0b", 3) : "other"; + ASSERT_EQ(tablet.add_value( + r, "s", String(bytes.data(), bytes.size())), + E_OK); + } + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.flush(), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + } + ConfigValue saved_; + std::string filename_; + const std::vector float_values_{ + 1, + std::numeric_limits::quiet_NaN(), + 1, + std::numeric_limits::quiet_NaN(), + 1, + 1, + 1, + std::numeric_limits::infinity(), + 1, + 1, + -std::numeric_limits::infinity(), + std::numeric_limits::quiet_NaN()}; +}; + +TEST_P(AlignedFieldFilterRegressionTest, FloatingStatisticsKeepNaNRowsCorrect) { + write_floats(); + for (bool is_float : {true, false}) { + for (auto op : {A::CompareOp::EQ, A::CompareOp::NE, A::CompareOp::LT, + A::CompareOp::LE, A::CompareOp::GT, A::CompareOp::GE}) { + for (double literal : + {1.0, 2.0, std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), + -std::numeric_limits::infinity()}) { + SCOPED_TRACE(::testing::Message() + << is_float << ":" << int(op) << ":" << literal); + auto filter = + A::compare(is_float ? "f" : "d", op, + is_float ? A::Literal::float32(float(literal)) + : A::Literal::float64(literal)); + Rows expected; + for (int device = 0; device < 2; ++device) + for (uint32_t r = 0; r < float_values_.size(); ++r) + if (compare(float_values_[r], literal, op)) + expected.push_back({r, device * 100 + r}); + EXPECT_EQ(scan({"v"}, std::move(filter)), expected); + } + } + } +} + +TEST_P(AlignedFieldFilterRegressionTest, PagePlanResumesAcrossSmallTsBlocks) { + g_config_value_.page_writer_max_point_num_ = 32; + g_config_value_.tsblock_max_memory_ = 128; + const std::vector columns{ + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"w", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + TableSchema schema("t", columns); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + Tablet tablet("t", schema.get_measurement_names(), schema.get_data_types(), + schema.get_column_categories(), 96); + Rows all, selected; + for (int r = 0; r < 96; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "v", int64_t(r)), E_OK); + ASSERT_EQ(tablet.add_value(r, "w", int64_t(-r)), E_OK); + all.push_back({r, r, -r, r}); + if (r >= 31 && r < 65) selected.push_back({r, r, -r, r}); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + EXPECT_EQ(scan({"v", "w", "v"}, nullptr), all); + EXPECT_EQ(scan({"v", "w", "v"}, A::is_not_null("v")), all); + auto filter = + A::and_filter(A::compare("v", A::CompareOp::GE, A::Literal::int64(31)), + A::compare("v", A::CompareOp::LT, A::Literal::int64(65))); + EXPECT_EQ(scan({"v", "w", "v"}, std::move(filter)), selected); + Rows single_column; + for (int r = 0; r < 96; ++r) single_column.push_back({r, r}); + EXPECT_EQ(scan({"v"}, A::is_not_null("v")), single_column); + EXPECT_EQ(scan({"v", "w", "v"}, A::is_not_null("v"), 29, 38), + Rows(all.begin() + 29, all.begin() + 67)); +} + +TEST_P(AlignedFieldFilterRegressionTest, + MissingFilterAndProjectionColumnsAreNull) { + write_missing_columns(); + Rows null_filter, missing_projection; + for (int d = 0; d < 2; ++d) + for (int r = 0; r < 6; ++r) { + if (d == 0) null_filter.push_back({r, r, kNull, r}); + missing_projection.push_back( + {r, d == 0 ? kNull : d * 1000 + r, d * 100 + r}); + } + EXPECT_EQ(scan({"v", "absent", "v"}, A::is_null("absent")), null_filter); + EXPECT_EQ(scan({"absent", "v"}, A::is_not_null("v")), missing_projection); + auto filter = + A::or_filter(A::is_null("absent"), + A::compare("v", A::CompareOp::LT, A::Literal::int64(102))); + Rows either; + for (int r = 0; r < 6; ++r) either.push_back({r, r}); + either.push_back({0, 100}); + either.push_back({1, 101}); + EXPECT_EQ(scan({"v"}, std::move(filter)), either); + EXPECT_EQ(scan({"v"}, A::not_filter(A::compare("v", A::CompareOp::LE, + A::Literal::int64(1)))), + (Rows{{2, 2}, + {3, 3}, + {4, 4}, + {5, 5}, + {0, 100}, + {1, 101}, + {2, 102}, + {3, 103}, + {4, 104}, + {5, 105}})); +} + +TEST_P(AlignedFieldFilterRegressionTest, + MissingStringFilterPreservesBinaryValues) { + write_missing_columns(); + Rows expected; + for (int d = 1; d < 3; ++d) + for (int r : {0, 2, 4}) + expected.push_back({r, d == 2 ? kNull : d * 100 + r}); + EXPECT_EQ( + scan({"v"}, A::compare("s", A::CompareOp::EQ, + A::Literal::string(std::string("a\0b", 3)))), + expected); +} + +TEST_P(AlignedFieldFilterRegressionTest, AllMissingColumnsUseSharedTimestamps) { + write_missing_columns(); + Rows expected; + for (int d = 0; d < 3; ++d) + for (int r = 0; r < 6; ++r) expected.push_back({r, kNull}); + EXPECT_EQ(scan({"ghost"}, A::is_null("ghost")), expected); + EXPECT_TRUE(scan({"ghost"}, A::is_not_null("ghost")).empty()); + EXPECT_EQ(scan({"ghost"}, A::is_null("ghost"), 3, 4, + TimeFilter::between(1, 4, false)), + (Rows{{4, kNull}, {1, kNull}, {2, kNull}, {3, kNull}})); +} + +TEST_P(AlignedFieldFilterRegressionTest, + MissingColumnsApplyOffsetAfterFilterAcrossDevices) { + write_missing_columns(); + for (int offset : {0, 3, 5, 6, 8, 20}) { + auto filter = A::or_filter( + A::is_null("absent"), + A::compare("v", A::CompareOp::LT, A::Literal::int64(102))); + Rows expected{{0, 0}, {1, 1}, {2, 2}, {3, 3}, + {4, 4}, {5, 5}, {0, 100}, {1, 101}}; + const size_t start = std::min(expected.size(), size_t(offset)); + expected = + Rows(expected.begin() + start, + expected.begin() + std::min(expected.size(), start + 3)); + EXPECT_EQ(scan({"v"}, std::move(filter), offset, 3), expected); + } +} + +INSTANTIATE_TEST_SUITE_P(RowAndBatchParallelModes, + AlignedFieldFilterRegressionTest, + ::testing::Combine(::testing::Bool(), + ::testing::Values(-1, 1, 4))); +} // namespace diff --git a/cpp/test/reader/block/masked_bulk_copy_test.cc b/cpp/test/reader/block/masked_bulk_copy_test.cc new file mode 100644 index 000000000..21a9e1b50 --- /dev/null +++ b/cpp/test/reader/block/masked_bulk_copy_test.cc @@ -0,0 +1,283 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "common/config/config.h" +#include "common/tablet.h" +#include "file/local_random_access_read_file.h" +#include "file/write_file.h" +#include "reader/block/single_device_tsblock_reader.h" +#include "writer/tsfile_table_writer.h" + +namespace { +using namespace common; +using namespace storage; + +class MaskedBulkCopyTest : public ::testing::TestWithParam { + protected: + void SetUp() override { + libtsfile_init(); + saved_ = g_config_value_; + g_config_value_.parallel_read_enabled_ = GetParam(); + g_config_value_.page_writer_max_point_num_ = 31; + // Source tuple: time + v + n + s + hidden + empty = 72 bytes/row. + // Each source TsBlock has 137 rows, crossing two bitmap words. + g_config_value_.tsblock_max_memory_ = 72 * 137; + filename_ = + "masked_bulk_copy_" + + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()) + + ".tsfile"; + const std::vector columns{ + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"n", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"hidden", TEXT, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"empty", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + schema_ = std::make_shared("t", columns); + WriteFile file; + int flags = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + flags |= O_BINARY; +#endif + ASSERT_EQ(file.create(filename_, flags, 0666), E_OK); + TsFileTableWriter writer(&file, schema_.get()); + Tablet tablet("t", schema_->get_measurement_names(), + schema_->get_data_types(), + schema_->get_column_categories(), kRows); + for (int r = 0; r < kRows; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "v", int64_t(r * 7)), E_OK); + if (r % 5 != 0) + ASSERT_EQ(tablet.add_value(r, "n", int64_t(-r)), E_OK); + if (r % 7 != 0) { + const auto bytes = string_at(r); + ASSERT_EQ(tablet.add_value(r, "s", + String(bytes.data(), bytes.size())), + E_OK); + } + const std::string hidden = "hidden-" + std::to_string(r); + ASSERT_EQ(tablet.add_value(r, "hidden", hidden.c_str()), E_OK); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + } + void TearDown() override { + std::remove(filename_.c_str()); + g_config_value_ = saved_; + libtsfile_destroy(); + } + static std::string string_at(int row) { + return row % 3 == 0 ? std::string() + : row % 3 == 1 ? std::string("a\0b", 3) + : "value-" + std::to_string(row); + } + void run_selection(int pattern, int offset, int limit, uint32_t capacity) { + SCOPED_TRACE(::testing::Message() << pattern << ":" << offset << ":" + << limit << ":" << capacity); + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TsFileIOReader reader; + ASSERT_EQ(reader.init(&file), E_OK); + PageArena arena; + arena.init(512, MOD_TSFILE_READER); + auto mapping = std::make_shared(); + const auto names = schema_->get_measurement_names(); + auto device = std::make_shared( + std::vector{"t"}); + DeviceQueryTask task(device, names, mapping, nullptr, schema_); + auto source_filter = AlignedFieldFilter::is_not_null("v"); + VectorMeasurementColumnContext context(&reader); + std::vector> positions{{0, 4}, {1}, {2}, {}, {3}}; + ASSERT_EQ(context.init(&task, names, nullptr, positions, arena, + source_filter.get()), + E_OK); + ASSERT_EQ(context.available_rows(), 137U); + TupleDesc tuple; + tuple.push_back(ColumnSchema("time", INT64, UNCOMPRESSED, PLAIN)); + for (const auto& name : {"v", "n", "s", "empty"}) + tuple.push_back(schema_->get_column_schema(name)); + tuple.push_back(ColumnSchema("duplicate", INT64, UNCOMPRESSED, PLAIN)); + tuple.push_back(ColumnSchema("extra_time", INT64, UNCOMPRESSED, PLAIN)); + TsBlock output(&tuple, capacity); + ASSERT_EQ(output.init(), E_OK); + RowAppender rows(&output); + std::vector> owned; + std::vector appenders; + for (uint32_t c = 0; c < tuple.get_column_count(); ++c) { + owned.emplace_back(new ColAppender(c, &output)); + appenders.push_back(owned.back().get()); + } + if (pattern == 4) { + SelectionMask bad_size(context.available_rows() - 1); + bad_size.set_all(); + uint32_t source_row = 0, copied = 0; + int remaining_offset = 0, remaining_limit = -1; + bool exhausted = false; + EXPECT_EQ(context.filtered_bulk_copy_into( + appenders, appenders[0], appenders[6], &rows, + bad_size, source_row, remaining_offset, + remaining_limit, copied, exhausted), + E_INVALID_ARG); + EXPECT_EQ(context.available_rows(), 137U); + EXPECT_EQ(output.get_row_count(), 0U); + output.reset(); + for (auto* appender : appenders) appender->reset(); + SelectionMask valid(137); + valid.set_all(); + source_row = 1; // The source iterators are still at row zero. + EXPECT_EQ(context.filtered_bulk_copy_into( + appenders, appenders[0], appenders[6], &rows, valid, + source_row, remaining_offset, remaining_limit, copied, + exhausted), + E_INVALID_ARG); + EXPECT_EQ(output.get_row_count(), 0U); + return; + } + int remaining_offset = offset, remaining_limit = limit; + int base_row = 0; + std::vector actual, expected; + while (remaining_limit != 0) { + const uint32_t count = context.available_rows(); + ASSERT_GT(count, 0U); + SelectionMask mask(count); + for (uint32_t r = 0; r < count; ++r) { + const bool selected = + pattern == 0 || + (pattern == 1 && + (((r >= 3 && r < 80) || r == 100 || r >= 120) && + r + 3 < count)) || + (pattern == 2 && r % 2 == 0); + if (selected) { + mask.set_one(r); + expected.push_back(base_row + r); + } + } + uint32_t source_row = 0; + bool exhausted = false; + while (!exhausted && remaining_limit != 0) { + output.reset(); + for (auto* appender : appenders) appender->reset(); + uint32_t copied = 0; + ASSERT_EQ(context.filtered_bulk_copy_into( + appenders, appenders[0], appenders[6], &rows, + mask, source_row, remaining_offset, + remaining_limit, copied, exhausted), + E_OK); + EXPECT_EQ(copied, output.get_row_count()); + EXPECT_LE(copied, capacity); + EXPECT_EQ(context.available_rows(), count - source_row); + EXPECT_EQ(exhausted, source_row == count); + if (!exhausted && remaining_limit != 0) EXPECT_GT(copied, 0U); + RowIterator iter(&output); + while (iter.has_next()) { + uint32_t len = 0; + bool null = false; + int64_t time = 0; + std::memcpy(&time, iter.read(0, &len, &null), sizeof(time)); + ASSERT_FALSE(null); + actual.push_back(time); + for (uint32_t c : {1U, 5U, 6U}) { + int64_t value = 0; + std::memcpy(&value, iter.read(c, &len, &null), + sizeof(value)); + EXPECT_FALSE(null); + EXPECT_EQ(value, c == 6 ? time : time * 7); + } + char* n = iter.read(2, &len, &null); + EXPECT_EQ(null, time % 5 == 0); + if (!null) { + int64_t value = 0; + std::memcpy(&value, n, sizeof(value)); + EXPECT_EQ(value, -time); + } + char* text = iter.read(3, &len, &null); + EXPECT_EQ(null, time % 7 == 0); + if (!null) + EXPECT_EQ(std::string(text, len), string_at(time)); + iter.read(4, &len, &null); + EXPECT_TRUE(null); + iter.next(); + } + } + if (remaining_limit == 0) break; + // Copying/skipping to the end must not prefetch and invalidate + // mask. + EXPECT_EQ(context.available_rows(), 0U); + base_row += count; + const int ret = context.get_next_tsblock(false); + if (ret == E_NO_MORE_DATA) break; + ASSERT_EQ(ret, E_OK); + int64_t time = -1; + ASSERT_EQ(context.get_current_time(time), E_OK); + EXPECT_EQ(time, base_row); + } + EXPECT_EQ(remaining_offset, + std::max(0, offset - static_cast(expected.size()))); + if (limit >= 0) + EXPECT_EQ(remaining_limit, limit - static_cast(actual.size())); + const size_t begin = std::min(expected.size(), size_t(offset)); + const size_t end = + limit < 0 ? expected.size() + : std::min(expected.size(), begin + size_t(limit)); + EXPECT_EQ(actual, std::vector(expected.begin() + begin, + expected.begin() + end)); + } + static constexpr int kRows = 421; + ConfigValue saved_; + std::string filename_; + std::shared_ptr schema_; +}; + +TEST_P(MaskedBulkCopyTest, + AllSelectedRunsResumeAcrossDestinationAndSourceBlocks) { + for (uint32_t capacity : {1U, 4U, 67U, 1024U}) + for (int offset : {0, 63, 138, 1000}) + for (int limit : {-1, 0, 1, 80}) + run_selection(0, offset, limit, capacity); +} +TEST_P(MaskedBulkCopyTest, + PartialRunsPreserveNullsStringsDuplicatesAndOffsets) { + for (uint32_t capacity : {1U, 4U, 67U, 1024U}) + for (int offset : {0, 63, 138, 1000}) + for (int limit : {-1, 0, 1, 80}) + run_selection(1, offset, limit, capacity); +} +TEST_P(MaskedBulkCopyTest, IsolatedSelectionsAndEmptyMasksDoNotPrefetch) { + for (int pattern : {2, 3}) + for (uint32_t capacity : {1U, 4U, 67U}) + for (int offset : {0, 65}) + run_selection(pattern, offset, -1, capacity); +} + +TEST_P(MaskedBulkCopyTest, InvalidMaskAndCursorCannotModifyDestination) { + run_selection(4, 0, -1, 4); +} + +INSTANTIATE_TEST_SUITE_P(DefaultAndAlternateReadModes, MaskedBulkCopyTest, + ::testing::Bool()); +} // namespace diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc index 880053112..e33be81c0 100644 --- a/cpp/test/reader/filter/aligned_field_filter_test.cc +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -21,6 +21,10 @@ #include +#include +#include +#include + #include "common/statistic.h" #include "reader/filter/selection_mask.h" @@ -64,6 +68,61 @@ TEST(SelectionMaskTest, NextSetBitAndClearFrom) { EXPECT_FALSE(mask.test(129)); } +TEST(SelectionMaskTest, SelectedRunsCrossWordBoundariesAndStopAtRowCount) { + SelectionMask mask(137); + for (uint32_t row = 3; row < 130; ++row) mask.set_one(row); + mask.set_one(136); + uint32_t begin = 0, end = 0; + ASSERT_TRUE(mask.next_selected_run(0, begin, end)); + EXPECT_EQ(begin, 3U); + EXPECT_EQ(end, 130U); + ASSERT_TRUE(mask.next_selected_run(63, begin, end)); + EXPECT_EQ(begin, 63U); + EXPECT_EQ(end, 130U); + ASSERT_TRUE(mask.next_selected_run(end, begin, end)); + EXPECT_EQ(begin, 136U); + EXPECT_EQ(end, 137U); + EXPECT_FALSE(mask.next_selected_run(end, begin, end)); + mask.set_all(); + ASSERT_TRUE(mask.next_selected_run(0, begin, end)); + EXPECT_EQ(begin, 0U); + EXPECT_EQ(end, 137U); + ASSERT_TRUE(mask.next_selected_run(64, begin, end)); + EXPECT_EQ(begin, 64U); + EXPECT_EQ(end, 137U); + mask.invert(); + EXPECT_FALSE(mask.next_selected_run(0, begin, end)); + EXPECT_FALSE(SelectionMask(0).next_selected_run(0, begin, end)); +} + +TEST(SelectionMaskTest, SelectedRunsMatchScalarOracle) { + std::mt19937 random(1026); + for (uint32_t size : {1U, 63U, 64U, 65U, 127U, 130U, 257U, 1025U}) { + for (int iteration = 0; iteration < 8; ++iteration) { + SelectionMask mask(size); + for (uint32_t row = 0; row < size; ++row) { + if ((random() & 7U) < static_cast(iteration)) + mask.set_one(row); + } + for (uint32_t from = 0; from <= size; ++from) { + uint32_t expected_begin = from; + while (expected_begin < size && !mask.test(expected_begin)) + ++expected_begin; + uint32_t expected_end = expected_begin; + while (expected_end < size && mask.test(expected_end)) + ++expected_end; + uint32_t begin = 0, end = 0; + const bool found = mask.next_selected_run(from, begin, end); + ASSERT_EQ(found, expected_begin < size); + if (found) { + EXPECT_EQ(begin, expected_begin); + EXPECT_EQ(end, expected_end); + } + } + } + } +} + TEST(AlignedFieldFilterTest, PageStatisticsAreConservative) { Int64Statistic statistic; statistic.count_ = 5; @@ -143,5 +202,53 @@ TEST(AlignedFieldFilterTest, CollectsReferencedColumnsOnce) { EXPECT_EQ(root->referenced_columns(), (std::vector{"a", "b"})); } +TEST(AlignedFieldFilterTest, NaNStatisticsCannotProvePageMatchesOrExclusion) { + const auto check = [](Statistic& stat, + const AlignedFieldFilter::Literal& literal) { + const std::map stats{{"v", &stat}}; + for (auto op : {AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::CompareOp::NE, + AlignedFieldFilter::CompareOp::LT, + AlignedFieldFilter::CompareOp::LE, + AlignedFieldFilter::CompareOp::GT, + AlignedFieldFilter::CompareOp::GE}) { + auto filter = AlignedFieldFilter::compare("v", op, literal); + EXPECT_TRUE(filter->may_match(stats, 3)); + EXPECT_FALSE(filter->all_match(stats, 3)); + } + }; + FloatStatistic floats; + DoubleStatistic doubles; + for (int r = 0; r < 3; ++r) { + floats.update(r, + r == 1 ? std::numeric_limits::quiet_NaN() : 1.0f); + doubles.update(r, + r == 1 ? std::numeric_limits::quiet_NaN() : 1.0); + } + check(floats, AlignedFieldFilter::Literal::float32(1)); + check(doubles, AlignedFieldFilter::Literal::float64(1)); +} + +TEST(AlignedFieldFilterTest, FiniteFloatingStatisticsRetainFastPaths) { + FloatStatistic floats; + DoubleStatistic doubles; + for (int r = 0; r < 3; ++r) { + floats.update(r, 1.0f); + doubles.update(r, 1.0); + } + auto f = + AlignedFieldFilter::compare("v", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::float32(1)); + auto d = + AlignedFieldFilter::compare("v", AlignedFieldFilter::CompareOp::EQ, + AlignedFieldFilter::Literal::float64(1)); + EXPECT_TRUE(f->all_match({{"v", &floats}}, 3)); + EXPECT_TRUE(d->all_match({{"v", &doubles}}, 3)); + auto below = + AlignedFieldFilter::compare("v", AlignedFieldFilter::CompareOp::LT, + AlignedFieldFilter::Literal::float32(0)); + EXPECT_FALSE(below->may_match({{"v", &floats}}, 3)); +} + } // namespace } // namespace storage From fcab7437130421bed2604311461a7a7aa6e9a255 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 23 Oct 2026 17:11:42 +0800 Subject: [PATCH 07/16] [C++] Fix floating NOT, malformed predicates, and reader teardown Negating an ordered FLOAT/DOUBLE comparison flipped the operator (NOT (x < 5) -> x >= 5). NaN makes every ordered comparison false, so the negation must be true for NaN rows while the flipped form is false and dropped them; when the literal itself was NaN, every non-NULL row was lost. Ordered floating-point leaves now keep the operator and record the negation, and evaluation computes !(value literal) for non-NULL rows. Page statistics no longer prove a full-page match or a skip for such leaves. A composite node missing an operand could be classified as a full-page match through its remaining operand. Page statistics then skipped evaluation, so a malformed OR silently returned rows (and did not terminate in batch mode). all_match() now requires both operands, and build_page_plan() rejects a structurally invalid filter with E_INVALID_ARG before classifying pages. AlignedChunkReader is placement-new'd and torn down through destroy() without running its destructor, so the page-plan filter state added for FIELD filtering leaked its backing buffers. destroy() now releases filter_column_mask_ and the current page mask. Tests cover NOT over all six operators and five literals (including NaN and both infinities) for FLOAT and DOUBLE, with NULL rows from a device that lacks the float columns; and a malformed OR that was previously accepted. --- cpp/src/reader/aligned_chunk_reader.cc | 11 +++ cpp/src/reader/filter/aligned_field_filter.cc | 60 +++++++++++- cpp/src/reader/filter/aligned_field_filter.h | 12 ++- cpp/src/reader/filter/selection_mask.cc | 5 + cpp/src/reader/filter/selection_mask.h | 3 + .../aligned_field_filter_regression_test.cc | 97 +++++++++++++++++-- 6 files changed, 174 insertions(+), 14 deletions(-) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 284c0347f..692b98973 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -218,6 +218,11 @@ void AlignedChunkReader::destroy() { // page_all_times_ handling above. std::vector().swap(value_columns_); std::vector>().swap(per_page_times_); + // Page-plan filter state added for FIELD filtering. These two own their + // heap buffers and, like the vectors above, are only reachable through + // this destroy() path. + std::vector().swap(filter_column_mask_); + current_page_field_mask_.release_storage(); #ifdef ENABLE_THREADS decode_pool_ = nullptr; // borrowed, not owned for (auto* d : time_decoder_pool_) { @@ -1489,6 +1494,12 @@ 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(); while (IS_SUCC(ret)) { diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index 375f81bcb..c478a35fa 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -138,11 +138,13 @@ AlignedFieldFilter::Literal AlignedFieldFilter::Literal::string( } AlignedFieldFilter::AlignedFieldFilter(Kind kind, std::string column, - CompareOp op, Literal literal) + CompareOp op, Literal literal, + bool negated) : kind_(kind), column_(std::move(column)), compare_op_(op), - literal_(std::move(literal)) {} + literal_(std::move(literal)), + negated_(negated) {} AlignedFieldFilter::AlignedFieldFilter(Kind kind, Ptr left, Ptr right) : kind_(kind), left_(std::move(left)), right_(std::move(right)) {} @@ -185,6 +187,23 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { if (node == nullptr) return nullptr; switch (node->kind_) { case Kind::COMPARISON: { + // Flipping an ordered operator is not the same as negating the + // comparison when the value is NaN: every ordered comparison with + // NaN is false, so NOT (x < 5) is true for a NaN value while + // x >= 5 stays false. Keep the operand and mark the leaf negated; + // evaluation then computes !(value literal) for non-null + // rows. Equality negation stays exact (NOT (x == v) is x != v for + // every value), so it keeps its operator flip and its pruning. + const bool ordered = node->compare_op_ == CompareOp::LT || + node->compare_op_ == CompareOp::LE || + node->compare_op_ == CompareOp::GT || + node->compare_op_ == CompareOp::GE; + if (ordered && (node->literal_.type == common::FLOAT || + node->literal_.type == common::DOUBLE)) { + return Ptr(new AlignedFieldFilter( + Kind::COMPARISON, node->column_, node->compare_op_, + node->literal_, !node->negated_)); + } CompareOp neg_op = CompareOp::EQ; switch (node->compare_op_) { case CompareOp::EQ: @@ -247,6 +266,22 @@ std::vector AlignedFieldFilter::referenced_columns() const { return columns; } +bool AlignedFieldFilter::is_valid() const { + switch (kind_) { + case Kind::COMPARISON: + case Kind::IS_NULL: + case Kind::IS_NOT_NULL: + return true; + case Kind::NOT: + return left_ != nullptr && left_->is_valid(); + case Kind::AND: + case Kind::OR: + return left_ != nullptr && right_ != nullptr && left_->is_valid() && + right_->is_valid(); + } + return false; +} + bool AlignedFieldFilter::may_match( const std::map& statistics, int64_t row_count) const { @@ -277,6 +312,11 @@ bool AlignedFieldFilter::may_match( } if (statistic->count_ <= 0) return false; + // A negated comparison admits rows that the page's min/max statistics do + // not describe (NaN is non-NULL but never extends either bound), so it + // cannot rule the page out. Evaluation resolves the exact rows. + if (negated_) return true; + if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return true; switch (compare_op_) { @@ -371,8 +411,12 @@ bool AlignedFieldFilter::all_match( right_->all_match(statistics, row_count); } if (kind_ == Kind::OR) { - return (left_ && left_->all_match(statistics, row_count)) || - (right_ && right_->all_match(statistics, row_count)); + // Both operands are required: a half-built OR must fall through to + // row evaluation, which reports E_INVALID_ARG, instead of matching a + // page through its remaining operand as a full-page pass. + return left_ && right_ && + (left_->all_match(statistics, row_count) || + right_->all_match(statistics, row_count)); } if (kind_ == Kind::NOT) { // Physical plans may keep a NOT node for now. Do not infer a @@ -393,6 +437,10 @@ bool AlignedFieldFilter::all_match( // every row has a non-NULL value and that the whole min/max range passes. if (statistic->count_ != row_count) return false; + // The negated form depends on values (NaN) rather than min/max bounds; + // leave the page to row evaluation. + if (negated_) return false; + if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return false; switch (compare_op_) { @@ -706,6 +754,8 @@ int AlignedFieldFilter::evaluate_leaf(const ColumnView& column, } else { return common::E_NOT_SUPPORT; } + // NULL never satisfies a comparison, negated or not. + if (negated_) satisfied = !satisfied; } if (satisfied) mask.set_one(row); } @@ -775,6 +825,8 @@ int AlignedFieldFilter::evaluate_leaf( } else { return common::E_NOT_SUPPORT; } + // NULL never satisfies a comparison, negated or not. + if (negated_) satisfied = !satisfied; } if (satisfied) mask.set_one(row); iter.next(); diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h index 64f571dcf..a5ec3cc56 100644 --- a/cpp/src/reader/filter/aligned_field_filter.h +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -88,6 +88,11 @@ class AlignedFieldFilter : public Filter { std::vector referenced_columns() const; + // False when a composite node is missing one of its operands. Callers use + // this to reject a malformed tree before page statistics can classify it + // as a full-page match and skip evaluation. + bool is_valid() const; + // Conservatively decides whether any row in a page can satisfy the filter. // Missing or unsupported statistics always return true. bool may_match(const std::map& statistics, @@ -105,7 +110,7 @@ class AlignedFieldFilter : public Filter { private: AlignedFieldFilter(Kind kind, std::string column, CompareOp op, - Literal literal); + Literal literal, bool negated = false); AlignedFieldFilter(Kind kind, Ptr left, Ptr right); // Push NOT down to the leaves so negation keeps SQL three-valued NULL // semantics; a single SelectionMask bit cannot represent NULL vs FALSE. @@ -120,6 +125,11 @@ class AlignedFieldFilter : public Filter { std::string column_; CompareOp compare_op_ = CompareOp::EQ; Literal literal_; + // True when this comparison leaf negates an ordered FLOAT/DOUBLE + // comparison. Flipping the operator is not equivalent there: NaN makes + // every ordered comparison false, so the negation must be evaluated as + // !(value literal). NULL still never satisfies either form. + bool negated_ = false; Ptr left_; Ptr right_; }; diff --git a/cpp/src/reader/filter/selection_mask.cc b/cpp/src/reader/filter/selection_mask.cc index fe38955fc..087249b0c 100644 --- a/cpp/src/reader/filter/selection_mask.cc +++ b/cpp/src/reader/filter/selection_mask.cc @@ -61,6 +61,11 @@ void SelectionMask::reset(uint32_t row_count) { words_.assign(word_count(row_count), 0); } +void SelectionMask::release_storage() { + std::vector().swap(words_); + row_count_ = 0; +} + void SelectionMask::set_all(bool value) { const uint64_t fill = value ? ~static_cast(0) : 0; for (auto& word : words_) { diff --git a/cpp/src/reader/filter/selection_mask.h b/cpp/src/reader/filter/selection_mask.h index 784e6d0dc..bd810af70 100644 --- a/cpp/src/reader/filter/selection_mask.h +++ b/cpp/src/reader/filter/selection_mask.h @@ -35,6 +35,9 @@ class SelectionMask { void reset(uint32_t row_count); void set_all(bool value = true); + // Releases the backing storage. Readers are torn down through an explicit + // destroy() that never runs destructors, so clear() alone would leak it. + void release_storage(); uint32_t row_count() const { return row_count_; } bool empty() const { return count_ones() == 0; } diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index 0a1c27db4..e172ccbd2 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -151,19 +151,33 @@ class AlignedFieldFilterRegressionTest WriteFile file; create(file); TsFileTableWriter writer(&file, &schema); - for (int device = 0; device < 2; ++device) { - Tablet tablet("t", schema.get_measurement_names(), - schema.get_data_types(), - schema.get_column_categories(), float_values_.size()); + // d0/d1 carry the floating-point values; d2 omits both float columns, + // so every row of d2 presents NULL to the predicate. + for (int device = 0; device < 3; ++device) { + std::vector names{"id", "v"}; + std::vector types{STRING, INT64}; + std::vector categories{ColumnCategory::TAG, + ColumnCategory::FIELD}; + if (device < 2) { + names.push_back("f"); + types.push_back(FLOAT); + categories.push_back(ColumnCategory::FIELD); + names.push_back("d"); + types.push_back(DOUBLE); + categories.push_back(ColumnCategory::FIELD); + } + Tablet tablet("t", names, types, categories, float_values_.size()); + const std::string id = "d" + std::to_string(device); for (uint32_t r = 0; r < float_values_.size(); ++r) { ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); - ASSERT_EQ(tablet.add_value(r, "id", device == 0 ? "d0" : "d1"), - E_OK); + ASSERT_EQ(tablet.add_value(r, "id", id.c_str()), E_OK); ASSERT_EQ(tablet.add_value(r, "v", int64_t(device * 100 + r)), E_OK); - ASSERT_EQ(tablet.add_value(r, "f", float(float_values_[r])), - E_OK); - ASSERT_EQ(tablet.add_value(r, "d", float_values_[r]), E_OK); + if (device < 2) { + ASSERT_EQ(tablet.add_value(r, "f", float(float_values_[r])), + E_OK); + ASSERT_EQ(tablet.add_value(r, "d", float_values_[r]), E_OK); + } } ASSERT_EQ(writer.write_table(tablet), E_OK); ASSERT_EQ(writer.flush(), E_OK); @@ -266,6 +280,71 @@ TEST_P(AlignedFieldFilterRegressionTest, FloatingStatisticsKeepNaNRowsCorrect) { } } +TEST_P(AlignedFieldFilterRegressionTest, NotPreservesFloatingNaNRowSemantics) { + write_floats(); + // Control: d2 has no float data at all, so IS NULL must see all of its + // rows. This proves the NULL device is actually part of the result stream + // and that the NOT expectations below are not vacuous. + Rows null_rows; + for (uint32_t r = 0; r < float_values_.size(); ++r) { + null_rows.push_back({r, 200 + r}); + } + EXPECT_EQ(scan({"v"}, A::is_null("f")), null_rows); + for (bool is_float : {true, false}) { + for (auto op : {A::CompareOp::EQ, A::CompareOp::NE, A::CompareOp::LT, + A::CompareOp::LE, A::CompareOp::GT, A::CompareOp::GE}) { + for (double literal : + {1.0, 2.0, std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity(), + -std::numeric_limits::infinity()}) { + SCOPED_TRACE(::testing::Message() << "not:" << is_float << ":" + << int(op) << ":" << literal); + auto filter = A::not_filter( + A::compare(is_float ? "f" : "d", op, + is_float ? A::Literal::float32(float(literal)) + : A::Literal::float64(literal))); + Rows expected; + // Negation follows IEEE comparison semantics: NaN never + // satisfies a comparison, so it always satisfies its + // negation. NULL rows (device d2) satisfy neither form. + for (int device = 0; device < 2; ++device) + for (uint32_t r = 0; r < float_values_.size(); ++r) + if (!compare(float_values_[r], literal, op)) + expected.push_back({r, device * 100 + r}); + EXPECT_EQ(scan({"v"}, std::move(filter)), expected); + } + } + } +} + +TEST_P(AlignedFieldFilterRegressionTest, IncompleteOrCannotBypassEvaluation) { + write_floats(); + // Every row of every page satisfies the left operand, so page statistics + // classify the page as a full match. The missing right operand must still + // surface as an error instead of being silently accepted. + auto filter = A::or_filter( + A::compare("v", A::CompareOp::GE, A::Literal::int64(0)), nullptr); + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + int ret = executor.query("t", {"v"}, nullptr, nullptr, filter.release(), 0, + -1, result); + if (ret == E_OK) { + ASSERT_NE(result, nullptr); + bool next = false; + int rows = 0; + while ((ret = result->next(next)) == E_OK && next) { + // Bound the loop: the broken path can stream rows indefinitely + // instead of failing, which would otherwise hang the test. + if (++rows > 1000) break; + } + executor.destroy_query_data_set(result); + EXPECT_EQ(rows, 0) << "incomplete OR returned rows"; + } + EXPECT_EQ(ret, E_INVALID_ARG); +} + TEST_P(AlignedFieldFilterRegressionTest, PagePlanResumesAcrossSmallTsBlocks) { g_config_value_.page_writer_max_point_num_ = 32; g_config_value_.tsblock_max_memory_ = 128; From f98ff7bd52466dcae2dc8122d847be0de9595d59 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sun, 25 Oct 2026 15:56:13 +0800 Subject: [PATCH 08/16] [CI] Pin C++ and Python Linux tests to Ubuntu 24.04 --- .github/workflows/unit-test-cpp.yml | 20 +++++++------------- .github/workflows/unit-test-python.yml | 7 ++++--- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.github/workflows/unit-test-cpp.yml b/.github/workflows/unit-test-cpp.yml index 7a0605c8b..6c125091f 100644 --- a/.github/workflows/unit-test-cpp.yml +++ b/.github/workflows/unit-test-cpp.yml @@ -44,17 +44,17 @@ jobs: max-parallel: 15 matrix: include: - # Linux all - - os: ubuntu-latest + # Pin the Linux image to keep the CI toolchain stable. + - os: ubuntu-24.04 build_type: Debug enable_asan: Asan - - os: ubuntu-latest + - os: ubuntu-24.04 build_type: Debug enable_asan: NoAsan - - os: ubuntu-latest + - os: ubuntu-24.04 build_type: Release enable_asan: Asan - - os: ubuntu-latest + - os: ubuntu-24.04 build_type: Release enable_asan: NoAsan @@ -115,16 +115,10 @@ jobs: shell: bash run: | if [[ "$RUNNER_OS" == "Linux" ]]; then - if command -v apt-get >/dev/null 2>&1; then - sudo apt-get update - sudo apt-get install -y uuid-dev - elif command -v yum >/dev/null 2>&1; then - sudo yum install -y libuuid-devel - fi + sudo apt-get update + sudo apt-get install -y clang-format-17 uuid-dev sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100 sudo update-alternatives --set clang-format /usr/bin/clang-format-17 - sudo apt-get update - sudo apt-get install -y uuid-dev elif [[ "$RUNNER_OS" == "Windows" ]]; then choco install llvm --version 17.0.6 --force else diff --git a/.github/workflows/unit-test-python.yml b/.github/workflows/unit-test-python.yml index b01c09341..fbe772ee7 100644 --- a/.github/workflows/unit-test-python.yml +++ b/.github/workflows/unit-test-python.yml @@ -43,7 +43,8 @@ jobs: fail-fast: false max-parallel: 15 matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] + # Keep Python 3.9 available on Linux as ubuntu-latest advances. + os: [ ubuntu-24.04, macos-latest, windows-latest ] python-version: ["3.9", "3.11", "3.14"] runs-on: ${{ matrix.os }} @@ -92,10 +93,10 @@ jobs: shell: bash run: | if [[ "$RUNNER_OS" == "Linux" ]]; then + sudo apt-get update + sudo apt-get install -y clang-format-17 uuid-dev sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-17 100 sudo update-alternatives --set clang-format /usr/bin/clang-format-17 - sudo apt-get update - sudo apt-get install -y uuid-dev elif [[ "$RUNNER_OS" == "Windows" ]]; then choco install llvm --version 17.0.6 --force else From c8b27546f61d5880e714612379556cf860f18467 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sun, 25 Oct 2026 23:24:50 +0800 Subject: [PATCH 09/16] [C++] Validate field filters and restore aligned query fast paths --- cpp/src/reader/aligned_chunk_reader.cc | 24 +-- .../block/single_device_tsblock_reader.cc | 5 +- .../block/single_device_tsblock_reader.h | 2 + cpp/src/reader/filter/aligned_field_filter.cc | 85 +++++----- cpp/src/reader/table_query_executor.cc | 10 +- .../aligned_field_filter_regression_test.cc | 160 +++++++++++++++++- .../filter/aligned_field_filter_test.cc | 79 +++++++++ 7 files changed, 308 insertions(+), 57 deletions(-) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 692b98973..6e4f0f04d 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -2075,6 +2075,7 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, const ChunkPageInfo& page_info = chunk_pages_[current_page_plan_index_]; if (!current_page_loaded_) { + current_page_field_mask_valid_ = false; if (single_thread_lazy) { if (RET_FAIL(decode_page_lazy(current_page_plan_index_))) { return ret; @@ -2083,7 +2084,6 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, page_time_cursor_ = page_info.row_begin; page_time_count_ = page_info.row_end; current_page_loaded_ = true; - current_page_field_mask_valid_ = false; } if (field_filter_ != nullptr && !current_page_field_mask_valid_) { @@ -2096,19 +2096,21 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, current_page_plan_index_))) { return ret; } - if (current_page_field_mask_.empty()) { - if (single_thread_lazy) - release_page_slot(current_page_plan_index_); - current_page_plan_index_++; - current_page_loaded_ = false; - current_page_field_mask_valid_ = false; - continue; - } - if (!single_thread_lazy && RET_FAIL(decode_output_columns_for_page( - current_page_plan_index_))) { + if (!single_thread_lazy && !current_page_field_mask_.empty() && + RET_FAIL( + decode_output_columns_for_page(current_page_plan_index_))) { return ret; } } + // Lazy decoding may already have produced the mask, including an + // empty one that deliberately left output columns undecoded. + if (field_filter_ != nullptr && current_page_field_mask_.empty()) { + if (single_thread_lazy) release_page_slot(current_page_plan_index_); + current_page_plan_index_++; + current_page_loaded_ = false; + current_page_field_mask_valid_ = false; + continue; + } const std::vector& times = per_page_times_[current_page_plan_index_]; diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index 0d9e43ffd..def0ec5fc 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -368,6 +368,7 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, if (common::E_OK == time_only_ret) { field_column_contexts_.insert( std::make_pair(kTimeOnlyContextName, time_only_ctx)); + time_only_context_ = time_only_ctx; } else { delete time_only_ctx; // Only treat "no data" as an acceptable empty result; I/O @@ -802,7 +803,8 @@ int SingleDeviceTsBlockReader::satisfy_field_filter(bool& satisfied) { char* value = nullptr; uint32_t length = 0; auto context = field_column_contexts_.find(name); - if (context != field_column_contexts_.end()) { + if (context != field_column_contexts_.end() && + context->second != time_only_context_) { int64_t time = 0; ret = context->second->get_current_time(time); if (ret != common::E_OK && ret != common::E_NO_MORE_DATA) @@ -914,6 +916,7 @@ int SingleDeviceTsBlockReader::next(common::TsBlock*& ret_block) { void SingleDeviceTsBlockReader::close() { aligned_vec_.clear(); // non-owning; owned by field_column_contexts_ + time_only_context_ = nullptr; // De-duplicate pointers before deleting: VectorMeasurementColumnContext // has multiple map entries pointing to the same object. std::set unique_contexts; diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index 19a6823d9..291991015 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -83,6 +83,8 @@ class SingleDeviceTsBlockReader : public TsBlockReader { common::TupleDesc tuple_desc_; bool last_block_returned_ = true; std::map field_column_contexts_; + // Borrowed from field_column_contexts_; it supplies timestamps, not values. + MeasurementColumnContext* time_only_context_ = nullptr; std::map id_column_contexts_; int64_t next_time_ = 0; int64_t time_column_index_ = 0; diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index c478a35fa..7a188c6a2 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -49,6 +49,26 @@ bool compare_values(T left, T right, AlignedFieldFilter::CompareOp op) { return false; } +AlignedFieldFilter::CompareOp negate_comparison( + AlignedFieldFilter::CompareOp op) { + using Op = AlignedFieldFilter::CompareOp; + switch (op) { + case Op::EQ: + return Op::NE; + case Op::NE: + return Op::EQ; + case Op::LT: + return Op::GE; + case Op::LE: + return Op::GT; + case Op::GT: + return Op::LE; + case Op::GE: + return Op::LT; + } + return Op::EQ; +} + int compare_string(const char* data, uint32_t len, const std::string& literal, AlignedFieldFilter::CompareOp op) { const std::string value(data, len); @@ -204,29 +224,9 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { Kind::COMPARISON, node->column_, node->compare_op_, node->literal_, !node->negated_)); } - CompareOp neg_op = CompareOp::EQ; - switch (node->compare_op_) { - case CompareOp::EQ: - neg_op = CompareOp::NE; - break; - case CompareOp::NE: - neg_op = CompareOp::EQ; - break; - case CompareOp::LT: - neg_op = CompareOp::GE; - break; - case CompareOp::LE: - neg_op = CompareOp::GT; - break; - case CompareOp::GT: - neg_op = CompareOp::LE; - break; - case CompareOp::GE: - neg_op = CompareOp::LT; - break; - } - return Ptr(new AlignedFieldFilter(Kind::COMPARISON, node->column_, - neg_op, node->literal_)); + return Ptr(new AlignedFieldFilter( + Kind::COMPARISON, node->column_, + negate_comparison(node->compare_op_), node->literal_)); } case Kind::IS_NULL: return is_not_null(node->column_); @@ -312,11 +312,6 @@ bool AlignedFieldFilter::may_match( } if (statistic->count_ <= 0) return false; - // A negated comparison admits rows that the page's min/max statistics do - // not describe (NaN is non-NULL but never extends either bound), so it - // cannot rule the page out. Evaluation resolves the exact rows. - if (negated_) return true; - if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return true; switch (compare_op_) { @@ -358,9 +353,14 @@ bool AlignedFieldFilter::may_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::FLOAT || has_nan_statistics(*typed)) + if (literal_.type != common::FLOAT || has_nan_statistics(*typed) || + (negated_ && std::isnan(literal_.float_value))) return true; - switch (compare_op_) { + // Complementing an ordered operator is safe only when neither the + // page nor the literal can contain NaN. + const CompareOp op = + negated_ ? negate_comparison(compare_op_) : compare_op_; + switch (op) { case CompareOp::EQ: return typed->min_value_ <= literal_.float_value && literal_.float_value <= typed->max_value_; @@ -379,9 +379,12 @@ bool AlignedFieldFilter::may_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::DOUBLE || has_nan_statistics(*typed)) + if (literal_.type != common::DOUBLE || has_nan_statistics(*typed) || + (negated_ && std::isnan(literal_.double_value))) return true; - switch (compare_op_) { + const CompareOp op = + negated_ ? negate_comparison(compare_op_) : compare_op_; + switch (op) { case CompareOp::EQ: return typed->min_value_ <= literal_.double_value && literal_.double_value <= typed->max_value_; @@ -437,10 +440,6 @@ bool AlignedFieldFilter::all_match( // every row has a non-NULL value and that the whole min/max range passes. if (statistic->count_ != row_count) return false; - // The negated form depends on values (NaN) rather than min/max bounds; - // leave the page to row evaluation. - if (negated_) return false; - if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return false; switch (compare_op_) { @@ -480,9 +479,12 @@ bool AlignedFieldFilter::all_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::FLOAT || has_nan_statistics(*typed)) + if (literal_.type != common::FLOAT || has_nan_statistics(*typed) || + (negated_ && std::isnan(literal_.float_value))) return false; - switch (compare_op_) { + const CompareOp op = + negated_ ? negate_comparison(compare_op_) : compare_op_; + switch (op) { case CompareOp::EQ: return typed->min_value_ == literal_.float_value && typed->max_value_ == literal_.float_value; @@ -500,9 +502,12 @@ bool AlignedFieldFilter::all_match( } } if (auto* typed = dynamic_cast(statistic)) { - if (literal_.type != common::DOUBLE || has_nan_statistics(*typed)) + if (literal_.type != common::DOUBLE || has_nan_statistics(*typed) || + (negated_ && std::isnan(literal_.double_value))) return false; - switch (compare_op_) { + const CompareOp op = + negated_ ? negate_comparison(compare_op_) : compare_op_; + switch (op) { case CompareOp::EQ: return typed->min_value_ == literal_.double_value && typed->max_value_ == literal_.double_value; diff --git a/cpp/src/reader/table_query_executor.cc b/cpp/src/reader/table_query_executor.cc index 084e1b363..8ecc1256d 100644 --- a/cpp/src/reader/table_query_executor.cc +++ b/cpp/src/reader/table_query_executor.cc @@ -72,8 +72,9 @@ int TableQueryExecutor::query(const std::string& table_name, } if (auto* aligned_filter = dynamic_cast(field_filter)) { - ret = - column_mapping->add_filter_columns(*aligned_filter, *table_schema); + ret = aligned_filter->is_valid() ? column_mapping->add_filter_columns( + *aligned_filter, *table_schema) + : common::E_INVALID_ARG; if (ret != common::E_OK) { delete time_filter; delete field_filter; @@ -155,8 +156,9 @@ int TableQueryExecutor::query(const std::string& table_name, } if (auto* aligned_filter = dynamic_cast(field_filter)) { - ret = - column_mapping->add_filter_columns(*aligned_filter, *table_schema); + ret = aligned_filter->is_valid() ? column_mapping->add_filter_columns( + *aligned_filter, *table_schema) + : common::E_INVALID_ARG; if (ret != common::E_OK) { delete time_filter; delete field_filter; diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index e172ccbd2..1f4e1b546 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -190,7 +190,9 @@ class AlignedFieldFilterRegressionTest {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, {"absent", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, - {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"__time_only_aligned_context__", INT64, UNCOMPRESSED, PLAIN, + ColumnCategory::FIELD}}; TableSchema schema("t", columns); WriteFile file; create(file); @@ -345,6 +347,122 @@ TEST_P(AlignedFieldFilterRegressionTest, IncompleteOrCannotBypassEvaluation) { EXPECT_EQ(ret, E_INVALID_ARG); } +TEST_P(AlignedFieldFilterRegressionTest, + InvalidTreesCannotBypassEmptyTableOrZeroLimit) { + for (bool populated : {false, true}) { + { + TableSchema schema("t", {{"v", INT64, UNCOMPRESSED, PLAIN, + ColumnCategory::FIELD}}); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + if (populated) { + Tablet tablet("t", {"v"}, {INT64}, {ColumnCategory::FIELD}, 1); + ASSERT_EQ(tablet.add_timestamp(0, 0), E_OK); + ASSERT_EQ(tablet.add_value(0, "v", int64_t(1)), E_OK); + ASSERT_EQ(writer.write_table(tablet), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + } + for (int mode : {0, 1, 2}) { + for (bool conjunction : {false, true}) { + for (bool malformed : {false, true}) { + SCOPED_TRACE(::testing::Message() + << populated << ":" << mode << ":" + << conjunction << ":" << malformed); + auto left = A::is_not_null("v"); + auto right = malformed ? A::Ptr() : A::is_null("v"); + auto filter = + conjunction + ? A::and_filter(std::move(left), std::move(right)) + : A::or_filter(std::move(left), std::move(right)); + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + const int ret = + mode == 0 ? executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), result) + : executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), 0, + mode == 2 ? 0 : -1, result); + EXPECT_EQ(ret, malformed ? E_INVALID_ARG : E_OK); + if (malformed) EXPECT_EQ(result, nullptr); + if (result != nullptr) { + if (!malformed) { + bool next = false; + EXPECT_EQ(result->next(next), E_OK); + EXPECT_EQ(next, + populated && mode != 2 && !conjunction); + } + executor.destroy_query_data_set(result); + } + } + } + } + } +} + +TEST_P(AlignedFieldFilterRegressionTest, + MissingFieldNamedLikeInternalContextRemainsNull) { + write_missing_columns(); + const std::string internal_name = "__time_only_aligned_context__"; + for (const auto& columns : + {std::vector{}, std::vector{"ghost"}, + std::vector{internal_name}, + std::vector{"ghost", internal_name}, + std::vector{"v", internal_name}}) { + Rows expected; + for (int device = 0; device < 3; ++device) { + for (int r = 0; r < 6; ++r) { + std::vector row{r}; + for (const auto& column : columns) + row.push_back(column == "v" && device != 2 + ? device * 100 + r + : kNull); + expected.push_back(std::move(row)); + } + } + EXPECT_EQ(scan(columns, A::is_null(internal_name)), expected); + EXPECT_EQ(scan(columns, A::is_null("ghost")), expected); + EXPECT_TRUE(scan(columns, A::is_not_null(internal_name)).empty()); + } +} + +TEST_P(AlignedFieldFilterRegressionTest, + RealFieldNamedLikeInternalContextRetainsValues) { + const std::string internal_name = "__time_only_aligned_context__"; + TableSchema schema( + "t", + {{"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {internal_name, INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + Tablet tablet("t", {"v", internal_name}, {INT64, INT64}, + {ColumnCategory::FIELD, ColumnCategory::FIELD}, 6); + Rows nulls, nonnulls; + for (int r = 0; r < 6; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "v", int64_t(r)), E_OK); + if (r % 3 == 0) { + nulls.push_back({r, r, kNull, kNull}); + } else { + ASSERT_EQ(tablet.add_value(r, internal_name, int64_t(100 + r)), + E_OK); + nonnulls.push_back({r, r, kNull, 100 + r}); + } + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + // The absent ghost column forces the row fallback, where the actual + // same-named FIELD must still be read rather than treated as internal. + const std::vector columns{"v", "ghost", internal_name}; + EXPECT_EQ(scan(columns, A::is_null(internal_name)), nulls); + EXPECT_EQ(scan(columns, A::is_not_null(internal_name)), nonnulls); +} + TEST_P(AlignedFieldFilterRegressionTest, PagePlanResumesAcrossSmallTsBlocks) { g_config_value_.page_writer_max_point_num_ = 32; g_config_value_.tsblock_max_memory_ = 128; @@ -380,6 +498,46 @@ TEST_P(AlignedFieldFilterRegressionTest, PagePlanResumesAcrossSmallTsBlocks) { Rows(all.begin() + 29, all.begin() + 67)); } +TEST_P(AlignedFieldFilterRegressionTest, + EmptyFieldMaskResumesIntoPartialAndFullPages) { + g_config_value_.page_writer_max_point_num_ = 8; + g_config_value_.tsblock_max_memory_ = 64; + TableSchema schema( + "t", {{"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"flag", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"w", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + Tablet tablet("t", schema.get_measurement_names(), schema.get_data_types(), + schema.get_column_categories(), 24); + Rows expected; + for (int r = 0; r < 24; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + const int64_t value = r < 16 ? r % 8 : r - 6; + const int64_t flag = r < 8 ? int64_t(r < 4) : 1; + ASSERT_EQ(tablet.add_value(r, "v", value), E_OK); + ASSERT_EQ(tablet.add_value(r, "flag", flag), E_OK); + ASSERT_EQ(tablet.add_value(r, "w", int64_t(1000 + r)), E_OK); + if (value >= 4 && flag == 1) expected.push_back({r, 1000 + r}); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + const auto make_filter = [] { + return A::and_filter( + A::compare("v", A::CompareOp::GE, A::Literal::int64(4)), + A::compare("flag", A::CompareOp::EQ, A::Literal::int64(1))); + }; + // The first page passes both independent statistics checks, but the + // conjunction matches no row. Its output-only w column stays undecoded. + EXPECT_EQ(scan({"w"}, make_filter()), expected); + EXPECT_EQ(scan({"w"}, make_filter(), 3, 6), + Rows(expected.begin() + 3, expected.begin() + 9)); + EXPECT_EQ( + scan({"w"}, make_filter(), 1, 4, TimeFilter::between(14, 20, false)), + Rows(expected.begin() + 3, expected.begin() + 7)); +} + TEST_P(AlignedFieldFilterRegressionTest, MissingFilterAndProjectionColumnsAreNull) { write_missing_columns(); diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc index e33be81c0..43e9f9409 100644 --- a/cpp/test/reader/filter/aligned_field_filter_test.cc +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -215,6 +215,10 @@ TEST(AlignedFieldFilterTest, NaNStatisticsCannotProvePageMatchesOrExclusion) { auto filter = AlignedFieldFilter::compare("v", op, literal); EXPECT_TRUE(filter->may_match(stats, 3)); EXPECT_FALSE(filter->all_match(stats, 3)); + auto negated = AlignedFieldFilter::not_filter( + AlignedFieldFilter::compare("v", op, literal)); + EXPECT_TRUE(negated->may_match(stats, 3)); + EXPECT_FALSE(negated->all_match(stats, 3)); } }; FloatStatistic floats; @@ -250,5 +254,80 @@ TEST(AlignedFieldFilterTest, FiniteFloatingStatisticsRetainFastPaths) { EXPECT_FALSE(below->may_match({{"v", &floats}}, 3)); } +TEST(AlignedFieldFilterTest, NegatedFloatingStatisticsPreserveSafeShortcuts) { + using A = AlignedFieldFilter; + FloatStatistic floats; + DoubleStatistic doubles; + for (int value : {10, 15, 20}) { + floats.update(value, static_cast(value)); + doubles.update(value, static_cast(value)); + } + for (bool is_float : {true, false}) { + const std::map stats{ + {"v", is_float ? static_cast(&floats) + : static_cast(&doubles)}}; + for (auto op : {A::CompareOp::LT, A::CompareOp::LE, A::CompareOp::GT, + A::CompareOp::GE}) { + for (double threshold : + {-std::numeric_limits::infinity(), 0.0, 10.0, 15.0, + 20.0, 30.0, std::numeric_limits::infinity()}) { + SCOPED_TRACE(::testing::Message() + << is_float << ":" << int(op) << ":" << threshold); + int matches = 0; + for (double value : {10.0, 15.0, 20.0}) { + bool comparison = false; + switch (op) { + case A::CompareOp::LT: + comparison = value < threshold; + break; + case A::CompareOp::LE: + comparison = value <= threshold; + break; + case A::CompareOp::GT: + comparison = value > threshold; + break; + case A::CompareOp::GE: + comparison = value >= threshold; + break; + default: + FAIL() << "unexpected ordered comparison"; + } + matches += !comparison; + } + const auto literal = + is_float + ? A::Literal::float32(static_cast(threshold)) + : A::Literal::float64(threshold); + auto filter = A::not_filter(A::compare("v", op, literal)); + EXPECT_EQ(filter->may_match(stats, 3), matches != 0); + EXPECT_EQ(filter->all_match(stats, 3), matches == 3); + // The same non-NULL statistics can describe a page with + // another NULL row, which must never match a comparison. + EXPECT_EQ(filter->may_match(stats, 4), matches != 0); + EXPECT_FALSE(filter->all_match(stats, 4)); + auto original = A::compare("v", op, literal); + auto twice = + A::not_filter(A::not_filter(A::compare("v", op, literal))); + EXPECT_EQ(twice->may_match(stats, 3), + original->may_match(stats, 3)); + EXPECT_EQ(twice->all_match(stats, 3), + original->all_match(stats, 3)); + } + const auto nan = + is_float ? A::Literal::float32( + std::numeric_limits::quiet_NaN()) + : A::Literal::float64( + std::numeric_limits::quiet_NaN()); + auto filter = A::not_filter(A::compare("v", op, nan)); + EXPECT_TRUE(filter->may_match(stats, 3)); + EXPECT_FALSE(filter->all_match(stats, 3)); + } + auto mismatched = A::not_filter( + A::compare("v", A::CompareOp::LT, A::Literal::int32(15))); + EXPECT_TRUE(mismatched->may_match(stats, 3)); + EXPECT_FALSE(mismatched->all_match(stats, 3)); + } +} + } // namespace } // namespace storage From c01ee105d397b1ed16a4daf335db9d8176036bac Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 09:30:11 +0800 Subject: [PATCH 10/16] [C++] Release aligned chunk header names during reader teardown --- cpp/src/reader/aligned_chunk_reader.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 6e4f0f04d..a5c506cc0 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -183,6 +183,10 @@ void AlignedChunkReader::destroy() { } cur_value_page_header_.reset(); chunk_header_.~ChunkHeader(); + // The placement-new reader is freed without member destructors. These + // single-value headers also own string buffers for long measurement names. + std::string().swap(time_chunk_header_.measurement_name_); + std::string().swap(value_chunk_header_.measurement_name_); // Multi-value destroy for (size_t ci = 0; ci < value_columns_.size(); ci++) { From 80bdef8ded18c9a9f87520dbfa94c5d20a2fee0b Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 14:10:03 +0800 Subject: [PATCH 11/16] [C++] Align field predicate literals with Java numeric conversions --- cpp/src/reader/column_mapping.h | 15 +- cpp/src/reader/filter/aligned_field_filter.cc | 164 ++++++++++++++++-- cpp/src/reader/filter/aligned_field_filter.h | 13 +- .../aligned_field_filter_regression_test.cc | 120 +++++++++++++ .../filter/aligned_field_filter_test.cc | 78 +++++++++ 5 files changed, 364 insertions(+), 26 deletions(-) diff --git a/cpp/src/reader/column_mapping.h b/cpp/src/reader/column_mapping.h index 33e76f47e..621f8756c 100644 --- a/cpp/src/reader/column_mapping.h +++ b/cpp/src/reader/column_mapping.h @@ -52,17 +52,10 @@ class ColumnMapping { return common::E_OK; } - int add_filter_columns(const AlignedFieldFilter& filter, - TableSchema& schema) { + 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()) { - const int column_index = schema.find_column_index(column_name); - if (column_index < 0) { - return common::E_COLUMN_NOT_EXIST; - } - if (schema.get_column_categories()[column_index] != - common::ColumnCategory::FIELD) { - return common::E_INVALID_ARG; - } field_columns_.insert(column_name); } return common::E_OK; @@ -106,4 +99,4 @@ class ColumnMapping { } // namespace storage -#endif // READER_COLUMN_MAPPING_H \ No newline at end of file +#endif // READER_COLUMN_MAPPING_H diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index 7a188c6a2..0dc5ed60e 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -23,8 +23,10 @@ #include #include #include +#include #include +#include "common/schema.h" #include "common/statistic.h" namespace storage { @@ -104,6 +106,121 @@ bool is_string_type(common::TSDataType type) { type == common::BLOB; } +bool is_numeric_type(common::TSDataType type) { + return type == common::INT32 || type == common::INT64 || + type == common::FLOAT || type == common::DOUBLE || + type == common::DATE || type == common::TIMESTAMP; +} + +template +T java_number_cast(S value) { + return static_cast(value); +} + +// Java narrows integral values by retaining the low bits, while narrowing a +// floating-point value saturates and maps NaN to zero. Avoid C++'s undefined +// floating-to-integer overflow and implementation-defined signed narrowing. +template <> +int32_t java_number_cast(int64_t value) { + const uint32_t bits = static_cast(value); + int32_t result; + std::memcpy(&result, &bits, sizeof(result)); + return result; +} + +template +T java_floating_to_integer(S value) { + if (std::isnan(value)) return 0; + if (value >= static_cast(std::numeric_limits::max())) + return std::numeric_limits::max(); + if (value <= static_cast(std::numeric_limits::min())) + return std::numeric_limits::min(); + return static_cast(value); +} + +template <> +int32_t java_number_cast(float value) { + return java_floating_to_integer(value); +} +template <> +int32_t java_number_cast(double value) { + return java_floating_to_integer(value); +} +template <> +int64_t java_number_cast(float value) { + return java_floating_to_integer(value); +} +template <> +int64_t java_number_cast(double value) { + return java_floating_to_integer(value); +} + +template <> +float java_number_cast(double value) { + const double maximum = std::numeric_limits::max(); + // Round values just above Float.MAX_VALUE back to the maximum; the + // halfway point to the next exponent rounds to infinity in Java. + const double overflow = + maximum + std::ldexp(1.0, std::numeric_limits::max_exponent - + std::numeric_limits::digits - 1); + if (value > maximum) + return value >= overflow ? std::numeric_limits::infinity() + : std::numeric_limits::max(); + if (value < -maximum) + return value <= -overflow ? -std::numeric_limits::infinity() + : -std::numeric_limits::max(); + return static_cast(value); +} + +template +T java_number_value(const AlignedFieldFilter::Literal& literal) { + switch (literal.type) { + case common::DATE: + case common::INT32: + return java_number_cast(literal.int32_value); + case common::TIMESTAMP: + case common::INT64: + return java_number_cast(literal.int64_value); + case common::FLOAT: + return java_number_cast(literal.float_value); + case common::DOUBLE: + return java_number_cast(literal.double_value); + default: + ASSERT(false); + return T(); + } +} + +int bind_literal(AlignedFieldFilter::Literal& literal, + common::TSDataType column_type) { + using Literal = AlignedFieldFilter::Literal; + if (is_numeric_type(column_type) && is_numeric_type(literal.type)) { + switch (column_type) { + case common::DATE: + case common::INT32: + literal = Literal::int32(java_number_value(literal)); + break; + case common::TIMESTAMP: + case common::INT64: + literal = Literal::int64(java_number_value(literal)); + break; + case common::FLOAT: + literal = Literal::float32(java_number_value(literal)); + break; + case common::DOUBLE: + literal = Literal::float64(java_number_value(literal)); + break; + default: + return common::E_NOT_SUPPORT; + } + return common::E_OK; + } + if ((column_type == common::BOOLEAN && literal.type == common::BOOLEAN) || + (is_string_type(column_type) && is_string_type(literal.type))) + return common::E_OK; + return common::E_NOT_SUPPORT; +} + std::string normalize_column_name(const std::string& column) { std::string normalized(column); for (char& ch : normalized) { @@ -207,19 +324,15 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { if (node == nullptr) return nullptr; switch (node->kind_) { case Kind::COMPARISON: { - // Flipping an ordered operator is not the same as negating the - // comparison when the value is NaN: every ordered comparison with - // NaN is false, so NOT (x < 5) is true for a NaN value while - // x >= 5 stays false. Keep the operand and mark the leaf negated; - // evaluation then computes !(value literal) for non-null - // rows. Equality negation stays exact (NOT (x == v) is x != v for - // every value), so it keeps its operator flip and its pruning. + // The literal type does not identify the column type: an integer + // constant can bind to a floating-point column. Preserve ordered + // negation until binding so NOT (NaN < 5) remains true, while NULL + // remains unknown. Equality can still flip its operator directly. const bool ordered = node->compare_op_ == CompareOp::LT || node->compare_op_ == CompareOp::LE || node->compare_op_ == CompareOp::GT || node->compare_op_ == CompareOp::GE; - if (ordered && (node->literal_.type == common::FLOAT || - node->literal_.type == common::DOUBLE)) { + if (ordered) { return Ptr(new AlignedFieldFilter( Kind::COMPARISON, node->column_, node->compare_op_, node->literal_, !node->negated_)); @@ -282,6 +395,31 @@ bool AlignedFieldFilter::is_valid() const { return false; } +int AlignedFieldFilter::bind(TableSchema& schema) { + if (kind_ == Kind::AND || kind_ == Kind::OR || kind_ == Kind::NOT) { + if (!left_ || (kind_ != Kind::NOT && !right_)) + return common::E_INVALID_ARG; + const int ret = left_->bind(schema); + if (ret != common::E_OK || kind_ == Kind::NOT) return ret; + return right_->bind(schema); + } + const common::ColumnSchema column = schema.get_column_schema(column_); + if (!column.is_valid()) return common::E_COLUMN_NOT_EXIST; + if (column.column_category_ != common::ColumnCategory::FIELD) + return common::E_INVALID_ARG; + if (kind_ != Kind::COMPARISON) return common::E_OK; + const int ret = bind_literal(literal_, column.data_type_); + if (ret != common::E_OK) return ret; + // Integral, Boolean, and string columns have no NaN values. Canonicalize + // their bound operators once, retaining the existing execution fast path. + if (negated_ && column.data_type_ != common::FLOAT && + column.data_type_ != common::DOUBLE) { + compare_op_ = negate_comparison(compare_op_); + negated_ = false; + } + return common::E_OK; +} + bool AlignedFieldFilter::may_match( const std::map& statistics, int64_t row_count) const { @@ -314,7 +452,7 @@ bool AlignedFieldFilter::may_match( if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return true; - switch (compare_op_) { + switch (negated_ ? negate_comparison(compare_op_) : compare_op_) { case CompareOp::EQ: return typed->min_value_ <= literal_.int32_value && literal_.int32_value <= typed->max_value_; @@ -334,7 +472,7 @@ bool AlignedFieldFilter::may_match( } if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT64) return true; - switch (compare_op_) { + switch (negated_ ? negate_comparison(compare_op_) : compare_op_) { case CompareOp::EQ: return typed->min_value_ <= literal_.int64_value && literal_.int64_value <= typed->max_value_; @@ -442,7 +580,7 @@ bool AlignedFieldFilter::all_match( if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT32) return false; - switch (compare_op_) { + switch (negated_ ? negate_comparison(compare_op_) : compare_op_) { case CompareOp::EQ: return typed->min_value_ == literal_.int32_value && typed->max_value_ == literal_.int32_value; @@ -461,7 +599,7 @@ bool AlignedFieldFilter::all_match( } if (auto* typed = dynamic_cast(statistic)) { if (literal_.type != common::INT64) return false; - switch (compare_op_) { + switch (negated_ ? negate_comparison(compare_op_) : compare_op_) { case CompareOp::EQ: return typed->min_value_ == literal_.int64_value && typed->max_value_ == literal_.int64_value; diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h index a5ec3cc56..0ac092c0b 100644 --- a/cpp/src/reader/filter/aligned_field_filter.h +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -32,6 +32,7 @@ #include "reader/filter/selection_mask.h" namespace storage { +class TableSchema; // Column-aware predicate used by aligned batch readers. It deliberately does // not implement TableFieldFilter::satisfy_row(); aligned execution evaluates @@ -93,6 +94,13 @@ class AlignedFieldFilter : public Filter { // as a full-page match and skip evaluation. bool is_valid() const; + // Bind once before execution: validate FIELD references and literal types, + // and convert numeric constants to the declared column type using Java's + // Number.intValue/longValue/floatValue/doubleValue rules. Comparisons and + // statistics pruning then use the same normalized constant, even on empty + // or all-NULL inputs. + int bind(TableSchema& schema); + // Conservatively decides whether any row in a page can satisfy the filter. // Missing or unsupported statistics always return true. bool may_match(const std::map& statistics, @@ -125,8 +133,9 @@ class AlignedFieldFilter : public Filter { std::string column_; CompareOp compare_op_ = CompareOp::EQ; Literal literal_; - // True when this comparison leaf negates an ordered FLOAT/DOUBLE - // comparison. Flipping the operator is not equivalent there: NaN makes + // True when this leaf negates an ordered comparison. Preserve it until + // binding identifies the column type. Flipping the operator is not + // equivalent for FLOAT/DOUBLE: NaN makes // every ordered comparison false, so the negation must be evaluated as // !(value literal). NULL still never satisfies either form. bool negated_ = false; diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index 1f4e1b546..d2d7bbcbb 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -319,6 +319,126 @@ TEST_P(AlignedFieldFilterRegressionTest, NotPreservesFloatingNaNRowSemantics) { } } +TEST_P(AlignedFieldFilterRegressionTest, + JavaNumericLiteralsUseTheDeclaredColumnType) { + write_floats(); + for (const std::string column : {"v", "f", "d"}) { + for (const auto& literal : + {A::Literal::int32(1), A::Literal::int64(1), + A::Literal::float32(1), A::Literal::float64(1)}) { + for (auto op : + {A::CompareOp::EQ, A::CompareOp::NE, A::CompareOp::LT, + A::CompareOp::LE, A::CompareOp::GT, A::CompareOp::GE}) { + for (bool negate : {false, true}) { + SCOPED_TRACE(::testing::Message() + << column << ":" << int(literal.type) << ":" + << int(op) << ":" << negate); + auto filter = A::compare(column, op, literal); + if (negate) filter = A::not_filter(std::move(filter)); + Rows expected; + for (int device = 0; device < 3; ++device) { + for (uint32_t r = 0; r < float_values_.size(); ++r) { + if (column != "v" && device == 2) continue; + const double value = column == "v" + ? device * 100 + r + : float_values_[r]; + if (compare(value, 1, op) != negate) + expected.push_back({r, device * 100 + r}); + } + } + EXPECT_EQ(scan({"v"}, std::move(filter)), expected); + } + } + } + } +} + +TEST_P(AlignedFieldFilterRegressionTest, + InvalidLiteralTypesFailBeforeReadingAnyData) { + for (int contents : {0, 1, 2}) { + { + TableSchema schema( + "t", + {{"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"anchor", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"flag", BOOLEAN, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + WriteFile file; + create(file); + TsFileTableWriter writer(&file, &schema); + if (contents > 0) { + Tablet tablet("t", {"anchor", "v", "flag", "s"}, + {INT64, INT64, BOOLEAN, STRING}, + {ColumnCategory::FIELD, ColumnCategory::FIELD, + ColumnCategory::FIELD, ColumnCategory::FIELD}, + 2); + for (int r = 0; r < 2; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "anchor", int64_t(r)), E_OK); + if (contents == 2) { + ASSERT_EQ(tablet.add_value(r, "v", int64_t(r + 1)), + E_OK); + ASSERT_EQ(tablet.add_value(r, "flag", true), E_OK); + ASSERT_EQ(tablet.add_value(r, "s", "value"), E_OK); + } + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + } + ASSERT_EQ(writer.close(), E_OK); + } + // Schema-only, all-NULL, and populated tables must reject the same + // invalid types, including LIMIT 0 and both query overloads. + for (int mode : {0, 1, 2}) { + for (int shape = 0; shape < 6; ++shape) { + SCOPED_TRACE(::testing::Message() + << contents << ":" << mode << ":" << shape); + A::Ptr filter; + if (shape == 0) { + filter = A::compare("v", A::CompareOp::EQ, + A::Literal::string("wrong")); + } else if (shape == 1) { + filter = A::compare("v", A::CompareOp::EQ, + A::Literal::boolean(true)); + } else if (shape == 2) { + filter = + A::compare("s", A::CompareOp::EQ, A::Literal::int64(1)); + } else if (shape == 3) { + filter = A::compare("flag", A::CompareOp::EQ, + A::Literal::int32(1)); + } else if (shape == 4) { + filter = + A::and_filter(A::compare("v", A::CompareOp::GT, + A::Literal::int64(100)), + A::compare("v", A::CompareOp::EQ, + A::Literal::string("wrong"))); + } else { + filter = A::or_filter( + A::compare("v", A::CompareOp::GE, A::Literal::int64(0)), + A::compare("v", A::CompareOp::EQ, + A::Literal::boolean(true))); + } + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, std::get<1>(GetParam())); + ResultSet* result = nullptr; + const int ret = + mode == 0 ? executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), result) + : executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), 0, + mode == 1 ? 0 : -1, result); + EXPECT_EQ(ret, E_NOT_SUPPORT); + EXPECT_EQ(result, nullptr); + if (result != nullptr) executor.destroy_query_data_set(result); + } + } + const Rows expected = contents == 2 ? Rows{{0, 1}, {1, 2}} : Rows{}; + EXPECT_EQ(scan({"v"}, + A::compare("v", A::CompareOp::GE, A::Literal::int32(1))), + expected); + } +} + TEST_P(AlignedFieldFilterRegressionTest, IncompleteOrCannotBypassEvaluation) { write_floats(); // Every row of every page satisfies the left operand, so page statistics diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc index 43e9f9409..8c1ec67cd 100644 --- a/cpp/test/reader/filter/aligned_field_filter_test.cc +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -24,13 +24,91 @@ #include #include #include +#include +#include "common/schema.h" #include "common/statistic.h" #include "reader/filter/selection_mask.h" namespace storage { namespace { +TEST(AlignedFieldFilterTest, NumericBindingMatchesJavaNumberConversions) { + using namespace common; + using A = AlignedFieldFilter; + using L = A::Literal; + using Case = std::tuple; + const double infinity = std::numeric_limits::infinity(); + const double nan = std::numeric_limits::quiet_NaN(); + const float maximum = std::numeric_limits::max(); + const double overflow = double(maximum) + std::ldexp(1.0, 103); + // Golden results verified against Java ValueFilterApi.eq, including + // narrowing, saturation, rounding, NaN, signed zero, and date aliases. + const std::vector cases{ + {INT64, L::int32(10), L::int64(10)}, + {INT32, L::int64(4294967297LL), L::int32(1)}, + {INT32, L::int64(INT64_MAX), L::int32(-1)}, + {INT32, L::int64(INT64_MIN), L::int32(0)}, + {INT32, L::float64(1.9), L::int32(1)}, + {INT32, L::float32(-1.9F), L::int32(-1)}, + {INT32, L::float64(infinity), L::int32(INT32_MAX)}, + {INT32, L::float64(-infinity), L::int32(INT32_MIN)}, + {INT64, L::float64(nan), L::int64(0)}, + {INT64, L::float64(infinity), L::int64(INT64_MAX)}, + {INT64, L::float32(-infinity), L::int64(INT64_MIN)}, + {INT64, L::float64(std::ldexp(1.0, 63)), L::int64(INT64_MAX)}, + {INT64, L::float64(std::nextafter(std::ldexp(1.0, 63), 0.0)), + L::int64(9223372036854774784LL)}, + {DATE, L::int64(4294967297LL), L::int32(1)}, + {TIMESTAMP, L::int32(10), L::int64(10)}, + {FLOAT, L::int64(16777217), L::float32(16777216)}, + {DOUBLE, L::int64(9007199254740993LL), L::float64(9007199254740992.0)}, + {FLOAT, L::float64(std::nextafter(double(maximum), infinity)), + L::float32(maximum)}, + {FLOAT, L::float64(std::nextafter(overflow, 0.0)), L::float32(maximum)}, + {FLOAT, L::float64(overflow), L::float32(infinity)}, + {FLOAT, L::float64(std::nextafter(-overflow, 0.0)), + L::float32(-maximum)}, + {FLOAT, L::float64(-overflow), L::float32(-infinity)}, + {FLOAT, L::float64(nan), L::float32(nan)}, + {FLOAT, L::float64(-0.0), L::float32(-0.0F)}, + {DOUBLE, L::float32(1.9F), L::float64(1.89999997615814208984375)}}; + for (const auto& item : cases) { + const TSDataType type = std::get<0>(item); + const L& input = std::get<1>(item); + const L& expected = std::get<2>(item); + SCOPED_TRACE(::testing::Message() + << int(type) << ":" << int(input.type)); + TableSchema schema( + "t", {{"v", type, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + auto filter = A::compare("v", A::CompareOp::EQ, input); + ASSERT_EQ(filter->bind(schema), E_OK); + const L& actual = filter->literal(); + ASSERT_EQ(actual.type, expected.type); + if (actual.type == INT32) { + EXPECT_EQ(actual.int32_value, expected.int32_value); + } else if (actual.type == INT64) { + EXPECT_EQ(actual.int64_value, expected.int64_value); + } else if (actual.type == FLOAT) { + if (std::isnan(expected.float_value)) { + EXPECT_TRUE(std::isnan(actual.float_value)); + } else { + EXPECT_EQ(actual.float_value, expected.float_value); + EXPECT_EQ(std::signbit(actual.float_value), + std::signbit(expected.float_value)); + } + } else { + if (std::isnan(expected.double_value)) { + EXPECT_TRUE(std::isnan(actual.double_value)); + } else { + EXPECT_EQ(actual.double_value, expected.double_value); + EXPECT_EQ(std::signbit(actual.double_value), + std::signbit(expected.double_value)); + } + } + } +} + TEST(SelectionMaskTest, BooleanOperationsAndTrailingBits) { SelectionMask left(70); SelectionMask right(70); From 07388d55cf05002fc74bc0c1e23d5e517be92b53 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 14:26:32 +0800 Subject: [PATCH 12/16] [C++] Reject missing NOT operands before query execution --- cpp/src/reader/filter/aligned_field_filter.cc | 10 ++- cpp/src/reader/filter/aligned_field_filter.h | 2 + .../aligned_field_filter_regression_test.cc | 74 +++++++++++-------- .../filter/aligned_field_filter_test.cc | 26 +++++++ 4 files changed, 80 insertions(+), 32 deletions(-) diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index 0dc5ed60e..d8811d834 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -321,7 +321,11 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::not_filter(Ptr child) { } AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { - if (node == nullptr) return nullptr; + if (node == nullptr) { + // Preserve the invalid operand so query validation cannot mistake + // this predicate for the optional "no FIELD filter" argument. + return Ptr(new AlignedFieldFilter(Kind::NOT, nullptr, nullptr)); + } switch (node->kind_) { case Kind::COMPARISON: { // The literal type does not identify the column type: an integer @@ -346,6 +350,8 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { case Kind::IS_NOT_NULL: return is_null(node->column_); case Kind::NOT: + // Repeated negation must not erase a missing operand either. + if (!node->left_) return node; return std::move(node->left_); case Kind::AND: return or_filter(negate(std::move(node->left_)), @@ -354,7 +360,7 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { return and_filter(negate(std::move(node->left_)), negate(std::move(node->right_))); } - return nullptr; + return node; } std::vector AlignedFieldFilter::referenced_columns() const { diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h index 0ac092c0b..a29f38833 100644 --- a/cpp/src/reader/filter/aligned_field_filter.h +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -78,6 +78,8 @@ class AlignedFieldFilter : public Filter { static Ptr compare(std::string column, CompareOp op, Literal literal); static Ptr is_null(std::string column); static Ptr is_not_null(std::string column); + // Logical operands must be non-null. A missing operand remains an invalid + // non-null tree, rejected with E_INVALID_ARG when bound or queried. static Ptr and_filter(Ptr left, Ptr right); static Ptr or_filter(Ptr left, Ptr right); static Ptr not_filter(Ptr child); diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index d2d7bbcbb..ac66127a5 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -485,37 +485,51 @@ TEST_P(AlignedFieldFilterRegressionTest, ASSERT_EQ(writer.close(), E_OK); } for (int mode : {0, 1, 2}) { - for (bool conjunction : {false, true}) { - for (bool malformed : {false, true}) { - SCOPED_TRACE(::testing::Message() - << populated << ":" << mode << ":" - << conjunction << ":" << malformed); - auto left = A::is_not_null("v"); - auto right = malformed ? A::Ptr() : A::is_null("v"); - auto filter = - conjunction - ? A::and_filter(std::move(left), std::move(right)) - : A::or_filter(std::move(left), std::move(right)); - LocalRandomAccessReadFile file; - ASSERT_EQ(file.open(filename_), E_OK); - TableQueryExecutor executor(&file, std::get<1>(GetParam())); - ResultSet* result = nullptr; - const int ret = - mode == 0 ? executor.query("t", {"v"}, nullptr, nullptr, - filter.release(), result) - : executor.query("t", {"v"}, nullptr, nullptr, - filter.release(), 0, - mode == 2 ? 0 : -1, result); - EXPECT_EQ(ret, malformed ? E_INVALID_ARG : E_OK); - if (malformed) EXPECT_EQ(result, nullptr); - if (result != nullptr) { - if (!malformed) { - bool next = false; - EXPECT_EQ(result->next(next), E_OK); - EXPECT_EQ(next, - populated && mode != 2 && !conjunction); + for (int kind : {0, 1, 2}) { + for (int negations : {0, 1, 2, 3}) { + for (bool malformed : {false, true}) { + SCOPED_TRACE(::testing::Message() + << populated << ":" << mode << ":" << kind + << ":" << negations << ":" << malformed); + auto left = A::is_not_null("v"); + auto right = malformed ? A::Ptr() : A::is_null("v"); + A::Ptr filter; + if (kind == 0) { + filter = A::and_filter(std::move(left), + std::move(right)); + } else if (kind == 1) { + filter = + A::or_filter(std::move(left), std::move(right)); + } else { + filter = A::not_filter(std::move(right)); + } + for (int n = 0; n < negations; ++n) + filter = A::not_filter(std::move(filter)); + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(filename_), E_OK); + TableQueryExecutor executor(&file, + std::get<1>(GetParam())); + ResultSet* result = nullptr; + const int ret = + mode == 0 + ? executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), result) + : executor.query("t", {"v"}, nullptr, nullptr, + filter.release(), 0, + mode == 2 ? 0 : -1, result); + EXPECT_EQ(ret, malformed ? E_INVALID_ARG : E_OK); + if (malformed) EXPECT_EQ(result, nullptr); + if (result != nullptr) { + if (!malformed) { + bool next = false; + EXPECT_EQ(result->next(next), E_OK); + const bool matches = + (kind != 0) != (negations % 2 != 0); + EXPECT_EQ(next, + populated && mode != 2 && matches); + } + executor.destroy_query_data_set(result); } - executor.destroy_query_data_set(result); } } } diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc index 8c1ec67cd..c560535c8 100644 --- a/cpp/test/reader/filter/aligned_field_filter_test.cc +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -33,6 +33,32 @@ namespace storage { namespace { +TEST(AlignedFieldFilterTest, MissingNotOperandsStayInvalidAfterRewriting) { + using namespace common; + using A = AlignedFieldFilter; + TableSchema schema( + "t", {{"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); + for (int shape = 0; shape < 5; ++shape) { + A::Ptr filter; + if (shape > 0) { + auto left = shape % 2 == 0 ? A::Ptr() : A::is_null("v"); + auto right = shape % 2 == 0 ? A::is_null("v") : A::Ptr(); + filter = shape <= 2 + ? A::and_filter(std::move(left), std::move(right)) + : A::or_filter(std::move(left), std::move(right)); + } + for (int depth = 1; depth <= 8; ++depth) { + SCOPED_TRACE(::testing::Message() << shape << ":" << depth); + filter = A::not_filter(std::move(filter)); + ASSERT_NE(filter, nullptr); + EXPECT_FALSE(filter->is_valid()); + EXPECT_EQ(filter->bind(schema), E_INVALID_ARG); + SelectionMask mask; + EXPECT_EQ(filter->evaluate_columns({}, 0, mask), E_INVALID_ARG); + } + } +} + TEST(AlignedFieldFilterTest, NumericBindingMatchesJavaNumberConversions) { using namespace common; using A = AlignedFieldFilter; From 1bf9ca8ab25aefec3c6924e016178e737def17f7 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 15:27:44 +0800 Subject: [PATCH 13/16] [C++] Evaluate FIELD predicates on non-aligned table rows --- cpp/src/file/tsfile_io_reader.cc | 5 +- .../block/single_device_tsblock_reader.cc | 21 +- cpp/src/reader/filter/aligned_field_filter.h | 7 +- cpp/src/reader/table_query_executor.h | 6 + .../aligned_field_filter_regression_test.cc | 186 ++++++++++++++++++ 5 files changed, 204 insertions(+), 21 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 875feb562..b90d7e9e9 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -324,7 +324,10 @@ int TsFileIOReader::alloc_multi_ssi( ssi->destroy(); mem_free(ssi); ssi = nullptr; - return E_NOT_SUPPORT; + // An empty measurement list requests only the shared aligned clock. + // Independent series have no such clock and yield no candidate rows. + // Named requests still fall back to individual column contexts. + return measurement_names.empty() ? E_NO_MORE_DATA : E_NOT_SUPPORT; } // Get time column metadata diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index def0ec5fc..e9e93083a 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -401,19 +401,6 @@ int SingleDeviceTsBlockReader::init_internal(DeviceQueryTask* device_query_task, } } - if (aligned_field_filter_ != nullptr && !all_aligned_ && - !field_column_contexts_.empty()) { - // Missing metadata (or unequal chunk counts) can make an aligned - // device use per-column contexts. Keep the row merge for that case, - // with absent FIELDs exposed as NULL, but still reject non-aligned - // sources for the aligned-only structured filter. - for (const auto* ts_index : time_series_indexs) { - if (ts_index != nullptr && !ts_index->is_aligned()) { - return common::E_NOT_SUPPORT; - } - } - } - if (field_column_contexts_.empty()) { return common::E_OK; } @@ -818,10 +805,10 @@ int SingleDeviceTsBlockReader::satisfy_field_filter(bool& satisfied) { fields.push_back({name, schema.data_type_, value, length}); } if (aligned_field_filter_ != nullptr) { - // Aligned devices missing one of the requested measurements cannot - // use the shared multi-column SSI. Evaluate their merged row through - // the same typed column-view evaluator, including explicit NULL - // views for measurements that have no metadata on this device. + // Non-aligned devices and aligned devices missing a measurement use + // the row merge instead of the shared multi-column SSI. Evaluate the + // merged row through the same typed evaluator, including explicit + // NULL views for missing measurements or timestamps. const uint8_t notnull = 0x80; std::vector strings(fields.size()); std::vector views; diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h index a29f38833..178698a9c 100644 --- a/cpp/src/reader/filter/aligned_field_filter.h +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -34,9 +34,10 @@ namespace storage { class TableSchema; -// Column-aware predicate used by aligned batch readers. It deliberately does -// not implement TableFieldFilter::satisfy_row(); aligned execution evaluates -// it against a whole TsBlock selection. +// Column-aware FIELD predicate. Aligned sources use batch evaluation and page +// pruning when possible; non-aligned sources use the table reader's timestamp +// merge and typed row evaluation. Mixed-device files may use both paths. +// It deliberately does not implement TableFieldFilter::satisfy_row(). class AlignedFieldFilter : public Filter { public: enum class Kind { COMPARISON, IS_NULL, IS_NOT_NULL, AND, OR, NOT }; diff --git a/cpp/src/reader/table_query_executor.h b/cpp/src/reader/table_query_executor.h index 11ccbbd23..e968c3601 100644 --- a/cpp/src/reader/table_query_executor.h +++ b/cpp/src/reader/table_query_executor.h @@ -71,6 +71,12 @@ class TableQueryExecutor { tsfile_io_reader_ = nullptr; } } + // FIELD filters apply to both aligned and non-aligned table devices. + // AlignedFieldFilter uses batch/page pruning where possible and otherwise + // evaluates merged rows. In the non-aligned path, candidate timestamps are + // the union of the projected and filter-referenced FIELD series; values + // absent at a candidate timestamp are NULL. OFFSET/LIMIT count accepted + // rows. A valid empty time range returns an empty result for either layout. int query(const std::string& table_name, const std::vector& columns, Filter* time_filter, Filter* tag_filter, Filter* field_filter, ResultSet*& ret_qds); diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index ac66127a5..9b4540347 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -27,6 +27,7 @@ #include #include "common/config/config.h" +#include "common/record.h" #include "common/tablet.h" #include "file/local_random_access_read_file.h" #include "file/write_file.h" @@ -239,6 +240,62 @@ class AlignedFieldFilterRegressionTest } ASSERT_EQ(writer.close(), E_OK); } + void write_layouts(bool mixed) { + const std::vector columns{ + {"id", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::TAG}, + {"anchor", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"w", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"f", FLOAT, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"flag", BOOLEAN, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"ghost", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}; + WriteFile file; + create(file); + TsFileWriter writer; + ASSERT_EQ(writer.init(&file), E_OK); + writer.set_generate_table_schema(false); + ASSERT_EQ( + writer.register_table(std::make_shared("t", columns)), + E_OK); + // Explicit table schema + ordinary per-device registration writes + // real table metadata with either independent or shared timestamps. + for (int device = 0; device < 3; ++device) { + const std::string name = "t.d" + std::to_string(device); + const bool aligned = mixed && device % 2 == 0; + std::vector measurements; + for (size_t c = 1; c + 1 < columns.size(); ++c) + measurements.push_back(new MeasurementSchema( + columns[c].column_name_, columns[c].data_type_, PLAIN, + UNCOMPRESSED)); + ASSERT_EQ( + aligned ? writer.register_aligned_timeseries(name, measurements) + : writer.register_timeseries(name, measurements), + E_OK); + for (int r = 0; r < 12; ++r) { + const std::string bytes = + r % 2 == 0 ? std::string("a\0b", 3) : "other"; + TsRecord record(device * 20 + r, name); + record.add_point("anchor", int64_t(device * 100 + r)); + if (device != 1 && r % 3 != 0) + record.add_point("v", int64_t(device * 1000 + r)); + if (r % 2 != 0) + record.add_point("w", int64_t(device * 1000 + r)); + if (r % 4 != 0) + record.add_point( + "f", r == 5 ? std::numeric_limits::quiet_NaN() + : float(r)); + if (r % 2 == 0) record.add_point("flag", r % 4 == 0); + if (r % 3 != 1) + record.add_point("s", String(bytes.data(), bytes.size())); + ASSERT_EQ(aligned ? writer.write_record_aligned(record) + : writer.write_record(record), + E_OK); + if (r % 4 == 3) ASSERT_EQ(writer.flush(), E_OK); + } + } + ASSERT_EQ(writer.close(), E_OK); + } ConfigValue saved_; std::string filename_; const std::vector float_values_{ @@ -256,6 +313,135 @@ class AlignedFieldFilterRegressionTest std::numeric_limits::quiet_NaN()}; }; +TEST_P(AlignedFieldFilterRegressionTest, + NonAlignedAndMixedSourcesUseTheRowEvaluator) { + auto make_filter = [](int predicate) -> A::Ptr { + switch (predicate) { + case 0: + return A::compare("v", A::CompareOp::GE, A::Literal::int32(5)); + case 1: + return A::is_null("v"); + case 2: + return A::and_filter(A::is_not_null("v"), + A::compare("w", A::CompareOp::LE, + A::Literal::float64(1006))); + case 3: + return A::or_filter( + A::is_null("v"), + A::compare("f", A::CompareOp::LT, A::Literal::float64(6))); + case 4: + return A::not_filter( + A::compare("f", A::CompareOp::LT, A::Literal::int32(6))); + case 5: + return A::compare("flag", A::CompareOp::EQ, + A::Literal::boolean(true)); + case 6: + return A::compare("s", A::CompareOp::EQ, + A::Literal::string(std::string("a\0b", 3))); + case 7: + return A::is_null("ghost"); + case 8: + return A::compare("w", A::CompareOp::GT, + A::Literal::int64(1000000)); + default: + return A::is_not_null("anchor"); + } + }; + for (bool mixed : {false, true}) { + write_layouts(mixed); + // With no projected/reference series carrying timestamps, an + // independent device has no candidate rows. Aligned devices retain + // their shared timestamps, including all-NULL projections. + Rows missing, null_values; + if (mixed) { + for (int device : {0, 2}) { + for (int r = 0; r < 12; ++r) { + missing.push_back({device * 20 + r, kNull}); + if (r % 3 == 0) + null_values.push_back({device * 20 + r, kNull}); + } + } + } + EXPECT_EQ(scan({"ghost"}, A::is_null("ghost")), missing); + EXPECT_TRUE(scan({"ghost"}, A::is_not_null("ghost")).empty()); + EXPECT_EQ(scan({"v"}, A::is_null("v")), null_values); + for (int window : {0, 1, 2, 3}) { + const int64_t start = window == 1 ? 5 : window == 3 ? 100 : 0; + const int64_t end = window == 1 ? 28 : window == 3 ? 200 : 11; + for (int predicate = 0; predicate < 10; ++predicate) { + Rows expected; + for (int device = 0; device < 3; ++device) { + for (int r = 0; r < 12; ++r) { + const int64_t time = device * 20 + r; + if (window != 0 && (time < start || time > end)) + continue; + const bool has_v = device != 1 && r % 3 != 0; + const bool has_w = r % 2 != 0; + const bool has_f = r % 4 != 0; + const bool less_f = r != 5 && r < 6; + const int64_t v = device * 1000 + r; + bool pass = false; + switch (predicate) { + case 0: + pass = has_v && v >= 5; + break; + case 1: + pass = !has_v; + break; + case 2: + pass = has_v && has_w && v <= 1006; + break; + case 3: + pass = !has_v || (has_f && less_f); + break; + case 4: + pass = has_f && !less_f; + break; + case 5: + pass = r % 4 == 0; + break; + case 6: + pass = r % 3 != 1 && r % 2 == 0; + break; + case 7: + pass = true; + break; + case 8: + pass = false; + break; + default: + pass = true; + break; + } + if (pass) + expected.push_back({time, device * 100 + r, + has_v ? v : kNull, + device * 100 + r}); + } + } + for (int offset : {0, 3}) { + SCOPED_TRACE(::testing::Message() + << mixed << ":" << window << ":" << predicate + << ":" << offset); + const size_t begin = + std::min(expected.size(), size_t(offset)); + const size_t end_row = + offset == 0 ? expected.size() + : std::min(expected.size(), begin + 4); + EXPECT_EQ( + scan({"anchor", "v", "anchor"}, make_filter(predicate), + offset, offset == 0 ? -1 : 4, + window == 0 + ? nullptr + : TimeFilter::between(start, end, false)), + Rows(expected.begin() + begin, + expected.begin() + end_row)); + } + } + } + } +} + TEST_P(AlignedFieldFilterRegressionTest, FloatingStatisticsKeepNaNRowsCorrect) { write_floats(); for (bool is_float : {true, false}) { From 143be35e482ff508bbd7c6ba1161603737e0dd77 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 23:26:57 +0800 Subject: [PATCH 14/16] Harden FIELD reader cleanup and error state Clear borrowed contexts before deletion, make close idempotent, and publish multi-index ownership before fallible lookup. Keep multi-column read failures terminal until reset, validate page and value-view bounds, and reject residual NOT nodes instead of inverting nullable masks. Add real-file fault injection, malformed page and buffer tests, repeated-close and partial-lookup coverage. Validate reset recovery across output blocks and failures after a result prefix. --- cpp/src/file/tsfile_io_reader.cc | 5 +- cpp/src/reader/aligned_chunk_reader.cc | 65 +++- cpp/src/reader/aligned_chunk_reader.h | 15 +- .../block/single_device_tsblock_reader.cc | 17 +- .../block/single_device_tsblock_reader.h | 1 + cpp/src/reader/filter/aligned_field_filter.cc | 59 ++-- cpp/src/reader/filter/aligned_field_filter.h | 2 + cpp/test/reader/chunk_reader_resource_test.cc | 326 +++++++++++++++++- .../filter/aligned_field_filter_test.cc | 40 +++ 9 files changed, 485 insertions(+), 45 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index b90d7e9e9..7c2b1897b 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -349,6 +349,9 @@ int TsFileIOReader::alloc_multi_ssi( } auto* multi_idx = new (multi_buf) MultiAlignedTimeseriesIndex; multi_idx->time_ts_idx_ = time_ts_idx; + // Publish ownership before any later failure so destroy() releases the + // vector backing storage, including a partially resolved name list. + ssi->itimeseries_index_ = multi_idx; // Batch-load all measurement TimeseriesIndex entries in a single pass over // the index tree — cheaper than N separate binary searches + file reads. @@ -385,8 +388,6 @@ int TsFileIOReader::alloc_multi_ssi( multi_idx->value_ts_idxs_.push_back(it->second); } - ssi->itimeseries_index_ = multi_idx; - // Skip global statistic filter for multi — per-chunk filtering still works. if (RET_FAIL(ssi->init_chunk_reader())) { diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index a5c506cc0..0168703a4 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -51,6 +51,8 @@ int AlignedChunkReader::init(RandomAccessReadFile* read_file, String m_name, } void AlignedChunkReader::reset() { + multi_page_error_ = E_OK; + current_page_field_mask_valid_ = false; time_chunk_meta_ = nullptr; value_chunk_meta_ = nullptr; time_chunk_header_.reset(); @@ -1337,6 +1339,9 @@ int AlignedChunkReader::count_non_null_prefix( if (row_limit <= 0 || bitmap.empty()) { return 0; } + if (static_cast(row_limit) > + static_cast(bitmap.size()) * 8U) + return -1; const uint32_t mask_base = 1 << 7; int count = 0; for (int32_t i = 0; i < row_limit; i++) { @@ -1427,6 +1432,11 @@ int AlignedChunkReader::decode_time_page_with(const ChunkPageInfo& page_info, out_times.insert(out_times.end(), batch, batch + actual); } compressor->after_uncompress(uncompressed_buf); + if (IS_SUCC(ret) && + (page_info.row_begin < 0 || page_info.row_end < page_info.row_begin || + static_cast(page_info.row_end) > out_times.size())) { + return E_TSFILE_CORRUPTED; + } return ret; } @@ -1461,8 +1471,11 @@ int AlignedChunkReader::evaluate_page_field_mask(size_t page_idx) { view.notnull_bitmap_size = static_cast(pps.notnull_bitmap.size()); view.row_offset = static_cast(page_info.row_begin); - view.nonnull_start = static_cast( - count_non_null_prefix(pps.notnull_bitmap, page_info.row_begin)); + const int prefix = + count_non_null_prefix(pps.notnull_bitmap, page_info.row_begin); + if (prefix < 0) return E_TSFILE_CORRUPTED; + view.nonnull_start = static_cast(prefix); + view.value_count = static_cast(pps.predecoded_count); if (view.type == common::STRING || view.type == common::TEXT || view.type == common::BLOB) { view.string_values = pps.predecoded_strings.data(); @@ -1703,6 +1716,7 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, auto* col = value_columns_[col_idx]; auto& pps = col->per_page_state[page_idx]; + pps.row_count = 0; pps.notnull_bitmap.clear(); pps.predecoded_values.clear(); pps.predecoded_strings.clear(); @@ -1767,11 +1781,15 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, uint32_t offset = 0; uint32_t data_num = SerializationUtil::read_ui32(uncompressed_buf); offset += sizeof(uint32_t); - uint32_t bitmap_bytes = (data_num + 7) / 8; - if (uncompressed_size - offset < bitmap_bytes) { + uint32_t bitmap_bytes = data_num / 8 + (data_num % 8 != 0); + if (data_num > static_cast(INT32_MAX) || + page_info.row_begin < 0 || page_info.row_end < page_info.row_begin || + data_num < static_cast(page_info.row_end) || + uncompressed_size - offset < bitmap_bytes) { col->compressor->after_uncompress(uncompressed_buf); return E_TSFILE_CORRUPTED; } + pps.row_count = data_num; pps.notnull_bitmap.resize(bitmap_bytes); memcpy(pps.notnull_bitmap.data(), uncompressed_buf + offset, bitmap_bytes); offset += bitmap_bytes; @@ -1787,6 +1805,10 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, static_cast(data_num)); int prefix_nonnull = count_non_null_prefix(pps.notnull_bitmap, page_info.row_begin); + if (nonnull_total < 0 || prefix_nonnull < 0) { + col->compressor->after_uncompress(uncompressed_buf); + return E_TSFILE_CORRUPTED; + } pps.predecoded_read_pos = prefix_nonnull; auto cleanup = [&]() { @@ -2027,9 +2049,43 @@ void AlignedChunkReader::release_page_slot(size_t page_idx) { } } +int AlignedChunkReader::validate_page_slot(size_t page_idx) const { + const auto& page = chunk_pages_[page_idx]; + const auto& times = per_page_times_[page_idx]; + if (page.row_begin < 0 || page.row_end < page.row_begin || + static_cast(page.row_end) > times.size()) { + return E_TSFILE_CORRUPTED; + } + const bool outputs_skipped = + field_filter_ != nullptr && current_page_field_mask_.empty(); + for (size_t c = 0; c < value_columns_.size(); ++c) { + // An empty FIELD mask deliberately leaves output-only columns unread. + if (outputs_skipped && !filter_column_mask_[c]) continue; + const auto& pps = value_columns_[c]->per_page_state[page_idx]; + // A zero-size value page represents an entirely NULL measurement. + if (page.value_compressed_sizes[c] != 0 && + pps.row_count != times.size()) + return E_TSFILE_CORRUPTED; + } + return E_OK; +} + int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, Filter* oneshoot_filter, PageArena& pa, int& row_offset) { + if (multi_page_error_ != E_OK) return multi_page_error_; + const int ret = + get_next_page_multi_impl(ret_tsblock, oneshoot_filter, pa, row_offset); + if (ret != E_OK && ret != E_NO_MORE_DATA && ret != E_OVERFLOW) { + multi_page_error_ = ret; + } + return ret; +} + +int AlignedChunkReader::get_next_page_multi_impl(TsBlock* ret_tsblock, + Filter* oneshoot_filter, + PageArena& pa, + int& row_offset) { int ret = E_OK; Filter* filter = (oneshoot_filter != nullptr ? oneshoot_filter : time_filter_); @@ -2106,6 +2162,7 @@ int AlignedChunkReader::get_next_page_multi(TsBlock* ret_tsblock, return ret; } } + if (RET_FAIL(validate_page_slot(current_page_plan_index_))) return ret; // Lazy decoding may already have produced the mask, including an // empty one that deliberately left output columns undecoded. if (field_filter_ != nullptr && current_page_field_mask_.empty()) { diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index bd19c8263..e0282a672 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -58,6 +58,7 @@ struct ChunkPageInfo { // decode or lazily by the single-thread page-plan path, then consumed by the // scatter loop. struct PageDecodedState { + uint32_t row_count = 0; std::vector notnull_bitmap; std::vector predecoded_values; std::vector predecoded_strings; @@ -127,7 +128,8 @@ class AlignedChunkReader : public IChunkReader { field_filter_ = field_filter; } bool field_filter_applied() const { - return field_filter_ != nullptr && page_plan_built_; + return field_filter_ != nullptr && page_plan_built_ && + multi_page_error_ == common::E_OK; } void reset() override; void destroy() override; @@ -135,7 +137,7 @@ class AlignedChunkReader : public IChunkReader { bool has_more_data() const override { if (multi_value_mode_) { - return has_more_data_multi(); + return multi_page_error_ != common::E_OK || has_more_data_multi(); } return prev_value_page_not_finish() || prev_time_page_not_finish() || (value_chunk_visit_offset_ - @@ -152,6 +154,8 @@ class AlignedChunkReader : public IChunkReader { int load_by_aligned_meta_multi(ChunkMeta* time_meta, const std::vector& value_metas); + // In multi-value mode a hard read/decode error remains terminal until + // reset(). E_OVERFLOW and end-of-stream do not poison the reader. int get_next_page(common::TsBlock* tsblock, Filter* oneshoot_filter, common::PageArena& pa) override; int get_next_page(common::TsBlock* tsblock, Filter* oneshoot_filter, @@ -245,6 +249,12 @@ class AlignedChunkReader : public IChunkReader { int get_next_page_multi(common::TsBlock* ret_tsblock, Filter* oneshoot_filter, common::PageArena& pa, int& row_offset); + // Hard decode/I/O errors are terminal until reset(); partially decoded + // pages must never be reused by a subsequent read. + int get_next_page_multi_impl(common::TsBlock* ret_tsblock, + Filter* oneshoot_filter, common::PageArena& pa, + int& row_offset); + int validate_page_slot(size_t page_idx) const; int get_next_page_multi_serial(common::TsBlock* ret_tsblock, Filter* filter, common::PageArena& pa, int& row_offset); int skip_cur_page_multi(); @@ -327,6 +337,7 @@ class AlignedChunkReader : public IChunkReader { // ── Page-plan state ──────────────────────────────────────────────── std::vector chunk_pages_; std::vector> per_page_times_; + int multi_page_error_ = common::E_OK; bool page_plan_built_ = false; bool page_plan_lazy_ = true; bool current_page_loaded_ = false; diff --git a/cpp/src/reader/block/single_device_tsblock_reader.cc b/cpp/src/reader/block/single_device_tsblock_reader.cc index e9e93083a..619201e63 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.cc +++ b/cpp/src/reader/block/single_device_tsblock_reader.cc @@ -626,7 +626,7 @@ int SingleDeviceTsBlockReader::has_next_aligned(bool& result_has_next) { uint32_t avail = aligned_vec_[0]->available_rows(); if (avail == 0) { for (auto* ctx : aligned_vec_) { - ctx->remove_from(field_column_contexts_); + remove_column_context(ctx); } aligned_vec_.clear(); break; @@ -645,7 +645,7 @@ int SingleDeviceTsBlockReader::has_next_aligned(bool& result_has_next) { } if (batch == 0) { for (auto* ctx : aligned_vec_) { - ctx->remove_from(field_column_contexts_); + remove_column_context(ctx); } aligned_vec_.clear(); break; @@ -819,7 +819,8 @@ int SingleDeviceTsBlockReader::satisfy_field_filter(bool& satisfied) { view.name = field.name; view.type = field.type; view.notnull_bitmap = field.is_null() ? nullptr : ¬null; - view.notnull_bitmap_size = 1; + view.notnull_bitmap_size = field.is_null() ? 0 : 1; + view.value_count = field.is_null() ? 0 : 1; if (field.type == common::STRING || field.type == common::TEXT || field.type == common::BLOB) { strings[i] = common::String(field.data, field.size); @@ -842,12 +843,19 @@ int SingleDeviceTsBlockReader::advance_column( MeasurementColumnContext* column_context) { int ret = column_context->move_iter(); if (ret == common::E_NO_MORE_DATA) { - column_context->remove_from(field_column_contexts_); + remove_column_context(column_context); ret = common::E_OK; } return ret; } +void SingleDeviceTsBlockReader::remove_column_context( + MeasurementColumnContext* column_context) { + // remove_from() deletes the context. Clear borrowed state first. + if (time_only_context_ == column_context) time_only_context_ = nullptr; + column_context->remove_from(field_column_contexts_); +} + void SingleMeasurementColumnContext::remove_from( std::map& column_context_map) { auto iter = column_context_map.find(column_name_); @@ -910,6 +918,7 @@ void SingleDeviceTsBlockReader::close() { for (auto& column_context : field_column_contexts_) { unique_contexts.insert(column_context.second); } + field_column_contexts_.clear(); for (auto* ctx : unique_contexts) { delete ctx; } diff --git a/cpp/src/reader/block/single_device_tsblock_reader.h b/cpp/src/reader/block/single_device_tsblock_reader.h index 291991015..fb592a6c4 100644 --- a/cpp/src/reader/block/single_device_tsblock_reader.h +++ b/cpp/src/reader/block/single_device_tsblock_reader.h @@ -66,6 +66,7 @@ class SingleDeviceTsBlockReader : public TsBlockReader { int fill_ids(); int satisfy_field_filter(bool& satisfied); int advance_column(MeasurementColumnContext* column_context); + void remove_column_context(MeasurementColumnContext* column_context); int32_t compute_dense_row_count( const std::vector& ts_indexes); // Fast path for aligned data: all columns share the same timestamps, diff --git a/cpp/src/reader/filter/aligned_field_filter.cc b/cpp/src/reader/filter/aligned_field_filter.cc index d8811d834..afa6bbb72 100644 --- a/cpp/src/reader/filter/aligned_field_filter.cc +++ b/cpp/src/reader/filter/aligned_field_filter.cc @@ -350,9 +350,9 @@ AlignedFieldFilter::Ptr AlignedFieldFilter::negate(Ptr node) { case Kind::IS_NOT_NULL: return is_null(node->column_); case Kind::NOT: - // Repeated negation must not erase a missing operand either. - if (!node->left_) return node; - return std::move(node->left_); + // Only invalid markers remain after NOT pushdown. Repeated + // negation must preserve them instead of erasing an invalid tree. + return node; case Kind::AND: return or_filter(negate(std::move(node->left_)), negate(std::move(node->right_))); @@ -392,7 +392,8 @@ bool AlignedFieldFilter::is_valid() const { case Kind::IS_NOT_NULL: return true; case Kind::NOT: - return left_ != nullptr && left_->is_valid(); + // Valid NOTs are pushed to leaves; only invalid markers remain. + return false; case Kind::AND: case Kind::OR: return left_ != nullptr && right_ != nullptr && left_->is_valid() && @@ -402,11 +403,11 @@ bool AlignedFieldFilter::is_valid() const { } int AlignedFieldFilter::bind(TableSchema& schema) { - if (kind_ == Kind::AND || kind_ == Kind::OR || kind_ == Kind::NOT) { - if (!left_ || (kind_ != Kind::NOT && !right_)) - return common::E_INVALID_ARG; + if (kind_ == Kind::NOT) return common::E_INVALID_ARG; + if (kind_ == Kind::AND || kind_ == Kind::OR) { + if (!left_ || !right_) return common::E_INVALID_ARG; const int ret = left_->bind(schema); - if (ret != common::E_OK || kind_ == Kind::NOT) return ret; + if (ret != common::E_OK) return ret; return right_->bind(schema); } const common::ColumnSchema column = schema.get_column_schema(column_); @@ -694,14 +695,9 @@ int AlignedFieldFilter::evaluate(common::TsBlock& block, postorder.push_back(node); traversal.pop_back(); } else if (node->kind_ == Kind::NOT) { - if (!node->left_) return common::E_INVALID_ARG; - if (frame.state == 0) { - frame.state = 1; - traversal.push_back({node->left_.get(), 0}); - } else { - postorder.push_back(node); - traversal.pop_back(); - } + // Inverting a one-bit mask would incorrectly select NULL rows. + // Factory-built NOTs have already been pushed to the leaves. + return common::E_INVALID_ARG; } else { if (!node->left_ || !node->right_) return common::E_INVALID_ARG; if (frame.state == 0) { @@ -726,9 +722,6 @@ int AlignedFieldFilter::evaluate(common::TsBlock& block, int ret = node->evaluate_leaf(block, column_index, leaf_mask); if (ret != common::E_OK) return ret; masks.push_back(std::move(leaf_mask)); - } else if (node->kind_ == Kind::NOT) { - if (masks.empty()) return common::E_INVALID_ARG; - masks.back().invert(); } else { if (masks.size() < 2) return common::E_INVALID_ARG; SelectionMask right = std::move(masks.back()); @@ -767,14 +760,9 @@ int AlignedFieldFilter::evaluate_columns(const std::vector& columns, postorder.push_back(node); traversal.pop_back(); } else if (node->kind_ == Kind::NOT) { - if (!node->left_) return common::E_INVALID_ARG; - if (frame.state == 0) { - frame.state = 1; - traversal.push_back({node->left_.get(), 0}); - } else { - postorder.push_back(node); - traversal.pop_back(); - } + // Inverting a one-bit mask would incorrectly select NULL rows. + // Factory-built NOTs have already been pushed to the leaves. + return common::E_INVALID_ARG; } else { if (!node->left_ || !node->right_) return common::E_INVALID_ARG; if (frame.state == 0) { @@ -802,9 +790,6 @@ int AlignedFieldFilter::evaluate_columns(const std::vector& columns, leaf_mask); if (ret != common::E_OK) return ret; masks.push_back(std::move(leaf_mask)); - } else if (node->kind_ == Kind::NOT) { - if (masks.empty()) return common::E_INVALID_ARG; - masks.back().invert(); } else { if (masks.size() < 2) return common::E_INVALID_ARG; SelectionMask right = std::move(masks.back()); @@ -825,12 +810,20 @@ int AlignedFieldFilter::evaluate_leaf(const ColumnView& column, uint32_t row_count, SelectionMask& mask) const { if (column.type == common::INVALID_DATATYPE) return common::E_INVALID_ARG; + if ((column.notnull_bitmap != nullptr && + static_cast(column.row_offset) + row_count > + static_cast(column.notnull_bitmap_size) * 8U) || + column.nonnull_start > column.value_count) { + return common::E_INVALID_ARG; + } uint32_t nonnull_index = column.nonnull_start; for (uint32_t row = 0; row < row_count; ++row) { - const uint32_t source_row = column.row_offset + row; + const uint64_t source_row = + static_cast(column.row_offset) + row; bool is_null = true; if (column.notnull_bitmap != nullptr && - source_row < column.notnull_bitmap_size * 8U) { + source_row < + static_cast(column.notnull_bitmap_size) * 8U) { is_null = (column.notnull_bitmap[source_row / 8U] & (0x80U >> (source_row % 8U))) == 0; } @@ -838,6 +831,8 @@ int AlignedFieldFilter::evaluate_leaf(const ColumnView& column, const char* data = nullptr; uint32_t len = 0; if (!is_null) { + if (nonnull_index >= column.value_count) + return common::E_INVALID_ARG; if (column.type == common::STRING || column.type == common::TEXT || column.type == common::BLOB) { if (column.string_values == nullptr) { diff --git a/cpp/src/reader/filter/aligned_field_filter.h b/cpp/src/reader/filter/aligned_field_filter.h index 178698a9c..659d165b2 100644 --- a/cpp/src/reader/filter/aligned_field_filter.h +++ b/cpp/src/reader/filter/aligned_field_filter.h @@ -53,6 +53,8 @@ class AlignedFieldFilter : public Filter { uint32_t notnull_bitmap_size = 0; uint32_t row_offset = 0; uint32_t nonnull_start = 0; + // Number of accessible non-NULL entries in the value buffer. + uint32_t value_count = 0; }; struct Literal { diff --git a/cpp/test/reader/chunk_reader_resource_test.cc b/cpp/test/reader/chunk_reader_resource_test.cc index e9d4193c0..98fcc5067 100644 --- a/cpp/test/reader/chunk_reader_resource_test.cc +++ b/cpp/test/reader/chunk_reader_resource_test.cc @@ -19,15 +19,28 @@ #include +#include #include #include +#include +#include #include #include #include #include "common/allocator/alloc_base.h" +#include "common/config/config.h" +#include "common/tablet.h" #include "file/local_random_access_read_file.h" +#include "file/tsfile_io_reader.h" +#include "file/write_file.h" #include "reader/aligned_chunk_reader.h" +#include "reader/block/single_device_tsblock_reader.h" +#include "reader/filter/time_operator.h" +#include "writer/tsfile_table_writer.h" +#ifdef ENABLE_THREADS +#include "common/thread_pool.h" +#endif namespace storage { namespace { @@ -50,7 +63,7 @@ class TempTsFile { std::string path_; }; -AlignedChunkReader* allocate_reader(LocalRandomAccessReadFile* read_file) { +AlignedChunkReader* allocate_reader(RandomAccessReadFile* read_file) { void* memory = common::mem_alloc(sizeof(AlignedChunkReader), common::MOD_CHUNK_READER); if (memory == nullptr) { @@ -114,5 +127,316 @@ TEST(ChunkReaderResourceTest, MultiAlignedInitialShortReadReleasesBuffer) { free_reader(reader); } +// A one-shot failure lets a retry reach healthy storage. The reader must still +// return the original hard error rather than reuse partial decoded state. +class PageFailureReadFile : public LocalRandomAccessReadFile { + public: + int64_t target = -1; + uint32_t corrupt_row_count = 0; + bool corrupt = false; + std::atomic fired{false}; + std::atomic reads{0}; + int read(int64_t offset, char* buffer, int32_t size, + int32_t& read_size) override { + ++reads; + if (offset == target && !fired.exchange(true) && !corrupt) { + read_size = 0; + return common::E_FILE_READ_ERR; + } + int ret = + LocalRandomAccessReadFile::read(offset, buffer, size, read_size); + if (ret == common::E_OK && corrupt && offset == target && + read_size >= 4) { + for (unsigned i = 0; i < 4; ++i) + buffer[i] = + static_cast(corrupt_row_count >> (24 - i * 8)); + } + return ret; + } +}; + +class AlignedReaderFailureTest : public ::testing::Test { + protected: + void SetUp() override { + using namespace common; + libtsfile_init(); + saved_ = g_config_value_; + g_config_value_.page_writer_max_point_num_ = 8; + schema_ = std::make_shared( + "t", std::vector{ + {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"w", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, + {"missing", INT64, UNCOMPRESSED, PLAIN, + ColumnCategory::FIELD}}); + path_ = "aligned_reader_failure.tsfile"; + WriteFile file; + int flags = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + flags |= O_BINARY; +#endif + ASSERT_EQ(file.create(path_, flags, 0666), E_OK); + TsFileTableWriter writer(&file, schema_.get()); + Tablet tablet("t", {"v", "w", "s"}, {INT64, INT64, STRING}, + {ColumnCategory::FIELD, ColumnCategory::FIELD, + ColumnCategory::FIELD}, + 40); + for (int r = 0; r < 40; ++r) { + ASSERT_EQ(tablet.add_timestamp(r, r), E_OK); + ASSERT_EQ(tablet.add_value(r, "v", int64_t(r)), E_OK); + ASSERT_EQ(tablet.add_value(r, "w", int64_t(r * 2)), E_OK); + ASSERT_EQ(tablet.add_value(r, "s", "value"), E_OK); + } + ASSERT_EQ(writer.write_table(tablet), E_OK); + ASSERT_EQ(writer.close(), E_OK); + } + void TearDown() override { + std::remove(path_.c_str()); + common::g_config_value_ = saved_; + libtsfile_destroy(); + } + int64_t payload_offset(PageFailureReadFile& file, ChunkMeta* meta, + int page_number = 0) { + std::vector bytes(1024); + int32_t actual = 0; + EXPECT_EQ(file.read(meta->offset_of_chunk_header_, bytes.data(), + bytes.size(), actual), + common::E_OK); + common::ByteStream in; + in.wrap_from(bytes.data(), actual); + ChunkHeader chunk; + EXPECT_EQ(chunk.deserialize_from(in), common::E_OK); + for (int p = 0; p <= page_number; ++p) { + PageHeader page; + EXPECT_EQ( + page.deserialize_from( + in, + (chunk.chunk_type_ & ONLY_ONE_PAGE_CHUNK_HEADER_MARKER) != + ONLY_ONE_PAGE_CHUNK_HEADER_MARKER, + chunk.data_type_), + common::E_OK); + if (p == page_number) + return meta->offset_of_chunk_header_ + in.read_pos(); + in.wrapped_buf_advance_read_pos(page.compressed_size_); + } + return -1; + } + void check_failure(bool parallel, const std::string& target, bool corrupt, + uint32_t data_num = 0, bool time_boundary = false, + int page_number = 0) { + using namespace common; + PageFailureReadFile file; + ASSERT_EQ(file.open(path_), E_OK); + TsFileIOReader io; + ASSERT_EQ(io.init(&file), E_OK); + TsFileMeta* metadata = nullptr; + ASSERT_EQ(io.get_tsfile_meta(metadata), E_OK); + PageArena arena; + arena.init(512, MOD_TSFILE_READER); + auto device = std::make_shared( + std::vector{"t"}); + std::vector indexes(3); + ASSERT_EQ( + io.get_timeseries_indexes(device, {"v", "w", "s"}, indexes, arena), + E_OK); + ChunkMeta* time = nullptr; + std::map values; + for (auto* index : indexes) { + ASSERT_NE(index, nullptr); + time = index->get_time_chunk_meta_list()->begin().get(); + values[index->get_measurement_name().to_std_string()] = + index->get_value_chunk_meta_list()->begin().get(); + } + const auto target_offset = payload_offset( + file, target == "time" ? time : values.at(target), page_number); + auto filter = + AlignedFieldFilter::compare("v", AlignedFieldFilter::CompareOp::GE, + AlignedFieldFilter::Literal::int64(3)); + auto* reader = allocate_reader(&file); + ASSERT_NE(reader, nullptr); + // The production reader uses placement new and explicit destroy(). + std::unique_ptr owned( + reader, free_reader); + reader->set_field_filter(filter.get()); +#ifdef ENABLE_THREADS + common::ThreadPool pool(2); + if (parallel) reader->set_decode_pool(&pool); +#else + (void)parallel; +#endif + const std::vector columns{values.at("v"), values.at("w"), + values.at("s")}; + ASSERT_EQ(reader->load_by_aligned_meta_multi(time, columns), E_OK); + TupleDesc tuple; + tuple.push_back(g_time_column_schema); + for (const auto& name : {"v", "w", "s"}) + tuple.push_back(schema_->get_column_schema(name)); + TsBlock block(&tuple, 2); + ASSERT_EQ(block.init(), E_OK); + TimeBetween range(6, 35, false); + Filter* time_filter = time_boundary ? &range : nullptr; + file.target = target_offset; + file.corrupt = corrupt; + file.corrupt_row_count = data_num; + const int expected = corrupt ? E_TSFILE_CORRUPTED : E_FILE_READ_ERR; + bool saw_error = false; + int prefix_rows = 0; + for (int attempt = 0; attempt < 50; ++attempt) { + block.reset(); + const int ret = reader->get_next_page(&block, time_filter, arena); + if (ret == expected) { + saw_error = true; + break; + } + ASSERT_EQ(ret, E_OK); + prefix_rows += block.get_row_count(); + } + ASSERT_TRUE(saw_error); + ASSERT_TRUE(file.fired); + if (page_number > 0) EXPECT_GT(prefix_rows, 0); + const int reads_after_error = file.reads; + file.target = -1; + for (int retry = 0; retry < 3; ++retry) { + block.reset(); + EXPECT_EQ(reader->get_next_page(&block, time_filter, arena), + expected); + EXPECT_EQ(block.get_row_count(), 0U); + EXPECT_EQ(file.reads, reads_after_error); + } + int offset = 0, limit = -1; + EXPECT_EQ(reader->get_next_page(&block, time_filter, arena, INT64_MIN, + offset, limit), + expected); + EXPECT_EQ(file.reads, reads_after_error); + // Reset is the recovery boundary. The same reader can load healthy + // data again, including continuation through small output blocks. + reader->reset(); + ASSERT_EQ(reader->load_by_aligned_meta_multi(time, columns), E_OK); + int count = 0; + for (int attempt = 0; attempt < 50; ++attempt) { + block.reset(); + const int ret = reader->get_next_page(&block, nullptr, arena); + ASSERT_TRUE(ret == E_OK || ret == E_NO_MORE_DATA) << ret; + RowIterator rows(&block); + while (rows.has_next()) { + const int64_t expected_row = count + 3; + for (uint32_t c = 0; c < 3; ++c) { + uint32_t size = 0; + bool is_null = false; + char* data = rows.read(c, &size, &is_null); + ASSERT_FALSE(is_null); + ASSERT_EQ(size, sizeof(int64_t)); + int64_t value = 0; + std::memcpy(&value, data, sizeof(value)); + EXPECT_EQ(value, c == 2 ? expected_row * 2 : expected_row); + } + uint32_t size = 0; + bool is_null = false; + char* data = rows.read(3, &size, &is_null); + ASSERT_FALSE(is_null); + EXPECT_EQ(std::string(data, size), "value"); + ++count; + rows.next(); + } + if (ret == E_NO_MORE_DATA) break; + } + EXPECT_EQ(count, 37); + } + common::ConfigValue saved_; + std::string path_; + std::shared_ptr schema_; +}; + +TEST_F(AlignedReaderFailureTest, HardErrorsStayTerminalUntilReset) { + for (bool parallel : {false, true}) { + for (const std::string target : {"time", "v", "w", "s"}) { + SCOPED_TRACE(::testing::Message() << parallel << ":" << target); + check_failure(parallel, target, false); + } + // Fail while building a boundary-page plan, before decoding starts. + check_failure(parallel, "time", false, 0, true); + // Output-only failures on a later page happen after rows have already + // been returned, in both eager and lazy decoding. + check_failure(parallel, "w", false, 0, false, 1); + check_failure(parallel, "s", false, 0, false, 1); + } +} + +TEST_F(AlignedReaderFailureTest, RejectsValuePageRowCountMismatch) { + for (bool parallel : {false, true}) { + for (const std::string target : {"v", "w", "s"}) { + for (uint32_t count : {0U, 1U, 7U, UINT32_MAX}) { + SCOPED_TRACE(::testing::Message() + << parallel << ":" << target << ":" << count); + check_failure(parallel, target, true, count); + } + } + // A clipped time range exercises nonzero prefix counting. + check_failure(parallel, "v", true, 1, true); + } +} + +TEST_F(AlignedReaderFailureTest, CloseIsSafeBeforeAndAfterExhaustion) { + using namespace common; + for (bool exhaust : {false, true}) { + for (bool time_only : {false, true}) { + LocalRandomAccessReadFile file; + ASSERT_EQ(file.open(path_), E_OK); + TsFileIOReader io; + ASSERT_EQ(io.init(&file), E_OK); + TsFileMeta* metadata = nullptr; + ASSERT_EQ(io.get_tsfile_meta(metadata), E_OK); + auto mapping = std::make_shared(); + const std::vector names = + time_only ? std::vector{"missing"} + : std::vector{"v", "w", "v"}; + for (size_t c = 0; c < names.size(); ++c) + ASSERT_EQ(mapping->add(names[c], c, *schema_), E_OK); + auto device = std::make_shared( + std::vector{"t"}); + DeviceQueryTask task(device, names, mapping, nullptr, schema_); + SingleDeviceTsBlockReader reader(&task, 7, nullptr, &io, nullptr, + nullptr); + ASSERT_EQ(reader.init(&task, 7, nullptr, nullptr), E_OK); + if (exhaust) { + int rows = 0; + for (int n = 0; n < 50; ++n) { + TsBlock* block = nullptr; + const int ret = reader.next(block); + if (ret == E_NO_MORE_DATA) break; + ASSERT_EQ(ret, E_OK); + ASSERT_NE(block, nullptr); + rows += block->get_row_count(); + } + EXPECT_EQ(rows, 40); + } + reader.close(); + reader.close(); + // The destructor closes a third time. + } + } +} + +TEST_F(AlignedReaderFailureTest, PartiallyResolvedMultiScanReleasesOwnership) { + using namespace common; + TsFileIOReader io; + ASSERT_EQ(io.init(path_), E_OK); + PageArena arena; + arena.init(512, MOD_TSFILE_READER); + auto device = + std::make_shared(std::vector{"t"}); + // Under LSan, each unsuccessful allocation must release the vector built + // for the first two valid names, as well as the SSI and its arena. + for (int attempt = 0; attempt < 32; ++attempt) { + TsFileSeriesScanIterator* ssi = nullptr; + EXPECT_EQ(io.alloc_multi_ssi(device, {"v", "w", "missing"}, ssi, arena), + E_NOT_EXIST); + EXPECT_EQ(ssi, nullptr); + } + TsFileSeriesScanIterator* ssi = nullptr; + ASSERT_EQ(io.alloc_multi_ssi(device, {"v", "w", "s"}, ssi, arena), E_OK); + io.revert_ssi(ssi); +} + } // namespace } // namespace storage diff --git a/cpp/test/reader/filter/aligned_field_filter_test.cc b/cpp/test/reader/filter/aligned_field_filter_test.cc index c560535c8..cb5a20040 100644 --- a/cpp/test/reader/filter/aligned_field_filter_test.cc +++ b/cpp/test/reader/filter/aligned_field_filter_test.cc @@ -59,6 +59,46 @@ TEST(AlignedFieldFilterTest, MissingNotOperandsStayInvalidAfterRewriting) { } } +TEST(AlignedFieldFilterTest, ColumnViewsRejectTruncatedBuffersAndOffsets) { + using namespace common; + using A = AlignedFieldFilter; + const uint8_t bitmap = 0x90; // rows 0 and 3 have values + const int64_t values[] = {4, 9}; + const String strings[] = {String("a", 1), String("b", 1)}; + for (bool text : {false, true}) { + A::ColumnView view; + view.name = "v"; + view.type = text ? STRING : INT64; + view.notnull_bitmap = &bitmap; + view.notnull_bitmap_size = 1; + view.fixed_values = reinterpret_cast(values); + view.string_values = strings; + view.value_count = 2; + auto filter = A::is_not_null("v"); + SelectionMask mask; + ASSERT_EQ(filter->evaluate_columns({view}, 4, mask), E_OK); + EXPECT_EQ(mask.count_ones(), 2U); + view.value_count = 1; + EXPECT_EQ(filter->evaluate_columns({view}, 4, mask), E_INVALID_ARG); + view.value_count = 2; + view.row_offset = 8; + EXPECT_EQ(filter->evaluate_columns({view}, 1, mask), E_INVALID_ARG); + view.row_offset = UINT32_MAX; + EXPECT_EQ(filter->evaluate_columns({view}, 2, mask), E_INVALID_ARG); + view.row_offset = 3; + view.nonnull_start = 1; + ASSERT_EQ(filter->evaluate_columns({view}, 1, mask), E_OK); + EXPECT_TRUE(mask.test(0)); + view.nonnull_start = 2; + EXPECT_EQ(filter->evaluate_columns({view}, 1, mask), E_INVALID_ARG); + view.notnull_bitmap = nullptr; + view.nonnull_start = 0; + view.value_count = 0; + ASSERT_EQ(A::is_null("v")->evaluate_columns({view}, 4, mask), E_OK); + EXPECT_EQ(mask.count_ones(), 4U); + } +} + TEST(AlignedFieldFilterTest, NumericBindingMatchesJavaNumberConversions) { using namespace common; using A = AlignedFieldFilter; From 0fc5e19631402bb86db1d828981e078699b4afcc Mon Sep 17 00:00:00 2001 From: ColinLee Date: Mon, 26 Oct 2026 23:55:47 +0800 Subject: [PATCH 15/16] Isolate reader failure fixtures for parallel CTest Give each test case and invocation a separate TsFile path so CTest workers cannot truncate or remove one another's input. Reproduce the shared-name failure with four parallel workers, then verify 100 repetitions of every case after isolation. --- cpp/test/reader/chunk_reader_resource_test.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cpp/test/reader/chunk_reader_resource_test.cc b/cpp/test/reader/chunk_reader_resource_test.cc index 98fcc5067..0e998fd93 100644 --- a/cpp/test/reader/chunk_reader_resource_test.cc +++ b/cpp/test/reader/chunk_reader_resource_test.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -169,7 +170,17 @@ class AlignedReaderFailureTest : public ::testing::Test { {"s", STRING, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, {"missing", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}}); - path_ = "aligned_reader_failure.tsfile"; + // CTest runs the cases in separate processes sharing one directory. + // Each case needs its own file, including concurrent/repeated runs. + path_ = + "aligned_reader_failure_" + + std::string(::testing::UnitTest::GetInstance() + ->current_test_info() + ->name()) + + "_" + + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()) + + ".tsfile"; WriteFile file; int flags = O_WRONLY | O_CREAT | O_TRUNC; #ifdef _WIN32 From cf4c9aacbaf009915327838569db97129eb4f584 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Tue, 27 Oct 2026 00:28:58 +0800 Subject: [PATCH 16/16] Give parallel time decode tasks independent state Use each strided task's index for its decoder and compressor slot instead of header-inline worker TLS, which can differ between a caller executable and the reader DLL. This prevents concurrent tasks from sharing slot zero and corrupting timestamps. Complete partially initialized pools when retrying after reset. Validate with the same hidden-symbol caller executable: the old code fails in the first iteration; the fix passes 1000 iterations (2000 fault/recovery tests). Full four-worker CTest also passes with no failures. --- cpp/src/reader/aligned_chunk_reader.cc | 47 ++++++++++--------- cpp/src/reader/aligned_chunk_reader.h | 2 +- cpp/test/reader/chunk_reader_resource_test.cc | 2 + 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index 0168703a4..38252744b 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -1894,9 +1894,9 @@ int AlignedChunkReader::decode_value_page_for_slot(uint32_t col_idx, // Multi-thread path: one task per value column, each decoding all non-SKIP // pages of that column serially. Time pages dispatched as worker-bucketed -// strided tasks using per-worker decoder/compressor (filled from -// time_decoder_pool_ / time_compressor_pool_) so they don't contend on the -// shared time_decoder_/time_compressor_. +// strided tasks using a separate decoder/compressor for each task (filled +// from time_decoder_pool_ / time_compressor_pool_) so they do not contend on +// the shared time_decoder_/time_compressor_. // // Single-thread: do NOT pre-decode every page upfront — leave per_page_state // empty so the scatter loop decodes on demand and releases after each page @@ -1912,7 +1912,7 @@ int AlignedChunkReader::decode_all_planned_pages() { #ifdef ENABLE_THREADS if (decode_pool_ != nullptr && value_columns_.size() > 1) { page_plan_lazy_ = false; - // Lazily grow the per-worker time decoder/compressor pool. Both + // Lazily grow the per-task time decoder/compressor pool. Both // factories can return nullptr on OOM/unsupported config; without // checking, the worker task below dereferences null when calling // decode_time_page_with(). @@ -1920,18 +1920,18 @@ int AlignedChunkReader::decode_all_planned_pages() { if (time_decoder_pool_.size() < worker_count) { time_decoder_pool_.resize(worker_count, nullptr); time_compressor_pool_.resize(worker_count, nullptr); - for (size_t w = 0; w < worker_count; w++) { - if (time_decoder_pool_[w] == nullptr) { - time_decoder_pool_[w] = - DecoderFactory::alloc_time_decoder(); - if (time_decoder_pool_[w] == nullptr) return E_OOM; - } - if (time_compressor_pool_[w] == nullptr) { - time_compressor_pool_[w] = - CompressorFactory::alloc_compressor( - time_chunk_header_.compression_type_); - if (time_compressor_pool_[w] == nullptr) return E_OOM; - } + } + // A previous allocation failure can leave partially initialized slots + // even when the vectors already have the requested size. + for (size_t w = 0; w < worker_count; w++) { + if (time_decoder_pool_[w] == nullptr) { + time_decoder_pool_[w] = DecoderFactory::alloc_time_decoder(); + if (time_decoder_pool_[w] == nullptr) return E_OOM; + } + if (time_compressor_pool_[w] == nullptr) { + time_compressor_pool_[w] = CompressorFactory::alloc_compressor( + time_chunk_header_.compression_type_); + if (time_compressor_pool_[w] == nullptr) return E_OOM; } } @@ -1956,12 +1956,15 @@ int AlignedChunkReader::decode_all_planned_pages() { std::vector time_rets(time_task_count, E_OK); for (size_t k = 0; k < time_task_count; k++) { int* tr = &time_rets[k]; - futures.push_back(decode_pool_->submit( - [this, k, tr, time_task_count, worker_count]() { - size_t wid = common::ThreadPool::current_worker_id(); - if (wid >= worker_count) wid = 0; - Decoder* dec = time_decoder_pool_[wid]; - Compressor* comp = time_compressor_pool_[wid]; + futures.push_back( + decode_pool_->submit([this, k, tr, time_task_count]() { + // Each strided task owns a distinct slot. An external + // thread pool can live across a DLL boundary where the + // header-inline worker TLS is a different instance; using + // that ID (or falling back to slot zero) can share mutable + // decoder state between concurrent tasks. + Decoder* dec = time_decoder_pool_[k]; + Compressor* comp = time_compressor_pool_[k]; for (size_t p = k; p < chunk_pages_.size(); p += time_task_count) { int r = decode_time_page_with( diff --git a/cpp/src/reader/aligned_chunk_reader.h b/cpp/src/reader/aligned_chunk_reader.h index e0282a672..54c82799f 100644 --- a/cpp/src/reader/aligned_chunk_reader.h +++ b/cpp/src/reader/aligned_chunk_reader.h @@ -348,7 +348,7 @@ class AlignedChunkReader : public IChunkReader { #ifdef ENABLE_THREADS common::ThreadPool* decode_pool_ = nullptr; // borrowed, not owned - // Per-worker time decoder + compressor pool for parallel time-page decode. + // Per-task time decoder + compressor pool for parallel time-page decode. // Sized to decode_pool_->num_threads() on first use, owned by this reader. std::vector time_decoder_pool_; std::vector time_compressor_pool_; diff --git a/cpp/test/reader/chunk_reader_resource_test.cc b/cpp/test/reader/chunk_reader_resource_test.cc index 0e998fd93..7e0a0af73 100644 --- a/cpp/test/reader/chunk_reader_resource_test.cc +++ b/cpp/test/reader/chunk_reader_resource_test.cc @@ -270,6 +270,8 @@ class AlignedReaderFailureTest : public ::testing::Test { reader, free_reader); reader->set_field_filter(filter.get()); #ifdef ENABLE_THREADS + // This pool lives in the caller executable. Decoder ownership must + // also work when its inline worker TLS is distinct from the DLL's. common::ThreadPool pool(2); if (parallel) reader->set_decode_pool(&pool); #else