This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 4b2a9740c91 [refactor](be) Encapsulate column COW reference counts
(#70856)
4b2a9740c91 is described below
commit 4b2a9740c9192e0308c8755c2c9c90a411d658c8
Author: Mryange <[email protected]>
AuthorDate: Tue Nov 24 17:28:22 2026 +0800
[refactor](be) Encapsulate column COW reference counts (#70856)
### Background
Doris columns use intrusive reference counting for copy-on-write. A
composite column can have a single owner while its child columns are
shared.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Refactor
Problem Summary:
The public COW reference count lets callers use the outer object's count
to decide whether to reuse a column. Make the count private and route
reuse decisions through `is_exclusive()`, which can also check child
ownership. The column dump retains the exact count for diagnostics.
### Details
- **Moves vs. logic changes:** Move the accessor into COW's private
section. Migrate Boolean predicate reuse checks and update ownership
assertions in existing tests.
- **Behavior change:** None intended. The Boolean predicate reuse path
handles non-nullable UInt8 columns. COW mutation retains its shallow
ownership check and detaches shared children recursively.
- **Compatibility:** Affects the internal C++ API. SQL, storage formats,
and the column dump output retain their existing contracts.
- **Performance impact:** The scalar ownership predicate uses the same
atomic count load. Runtime performance was not measured.
- **Rollback:** A plain revert restores the public accessor.
### Testing
The BE ASAN build passed with `sh build.sh --be -j48` on the submitted
patch. The updated BE unit tests were not run.
### Release note
None
### Check List (For Author)
- Behavior changed: No
- Does this need documentation: No
- Pre-submission status: Partially verified: the updated BE unit tests
were not run, and clang-tidy could not resolve `stddef.h`.
### AI Disclosure
- Tool: Codex (GPT-6)
- Generated: Code, test updates, and PR description
- Human review: The author reviewed the code changes.
---
be/src/core/column/column.h | 2 +-
be/src/core/cow.h | 10 +++++--
be/src/exprs/vcompound_pred.h | 4 +--
be/src/exprs/vexpr_context.cpp | 5 ++--
be/test/core/block/column_test.cpp | 6 ++--
.../core/column/check_and_get_column_ptr_test.cpp | 32 ++++++++++------------
be/test/core/column/column_array_test.cpp | 2 +-
be/test/core/column/common_column_test.h | 7 ++---
be/test/exec/sink/tablet_column_validator_test.cpp | 8 +++---
9 files changed, 39 insertions(+), 37 deletions(-)
diff --git a/be/src/core/column/column.h b/be/src/core/column/column.h
index 9a1afcfb41f..77917b6d066 100644
--- a/be/src/core/column/column.h
+++ b/be/src/core/column/column.h
@@ -631,7 +631,7 @@ public:
// true if column has null element [begin, end)
virtual bool has_null(size_t begin, size_t end) const { return false; }
- virtual bool is_exclusive() const { return use_count() == 1; }
+ virtual bool is_exclusive() const { return is_exclusive_self(); }
/// Clear data of column, just like vector clear
virtual void clear() = 0;
diff --git a/be/src/core/cow.h b/be/src/core/cow.h
index 89c6228223d..9b22da181fc 100644
--- a/be/src/core/cow.h
+++ b/be/src/core/cow.h
@@ -30,6 +30,8 @@
namespace doris {
+struct ColumnWithTypeAndName;
+
/** Copy-on-write shared ptr.
* Allows to work with shared immutable objects and sometimes unshare and
mutate you own unique copy.
*
@@ -90,9 +92,15 @@ namespace doris {
*/
template <typename Derived>
class COW {
+ friend struct ColumnWithTypeAndName;
+
std::atomic_uint ref_counter;
+ unsigned int use_count() const { return ref_counter.load(); }
+
protected:
+ bool is_exclusive_self() const { return use_count() == 1; }
+
COW() : ref_counter(0) {}
COW(COW const&) : ref_counter(0) {}
@@ -243,8 +251,6 @@ protected:
public:
using MutablePtr = mutable_ptr<Derived>;
- unsigned int use_count() const { return ref_counter.load(); }
-
protected:
template <typename T>
class immutable_ptr : public intrusive_ptr<const T> {
diff --git a/be/src/exprs/vcompound_pred.h b/be/src/exprs/vcompound_pred.h
index 793d912d3e3..263f46d79fb 100644
--- a/be/src/exprs/vcompound_pred.h
+++ b/be/src/exprs/vcompound_pred.h
@@ -475,11 +475,11 @@ public:
MutableColumnPtr mutable_result_column;
uint8_t* __restrict result_data_column = nullptr;
const uint8_t* __restrict other_data_column = rhs_data_column;
- if (lhs_column->use_count() == 1) {
+ if (lhs_column->is_exclusive()) {
mutable_result_column = IColumn::mutate(std::move(lhs_column));
result_data_column =
assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
- } else if (rhs_column->use_count() == 1) {
+ } else if (rhs_column->is_exclusive()) {
mutable_result_column = IColumn::mutate(std::move(rhs_column));
result_data_column =
assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data();
diff --git a/be/src/exprs/vexpr_context.cpp b/be/src/exprs/vexpr_context.cpp
index fcda9757238..32b15201e15 100644
--- a/be/src/exprs/vexpr_context.cpp
+++ b/be/src/exprs/vexpr_context.cpp
@@ -451,9 +451,8 @@ Status
VExprContext::execute_conjuncts_and_filter_block(const VExprContextSPtrs&
// do_projection: for some query(e.g. in
MultiCastDataStreamerSourceOperator::get_block()),
// output_vexpr_ctxs will output the same column more than once, and if the
output_block
-// is mem-reused later, it will trigger DCHECK_EQ(d.column->use_count(), 1)
failure when
-// doing Block::clear_column_data, set do_projection to true to copy the
column data to
-// avoid this problem.
+// is mem-reused later, it may share columns that Block::clear_column_data
needs to detach.
+// Set do_projection to true to reuse or copy the column data during
projection.
Status VExprContext::get_output_block_after_execute_exprs(
const VExprContextSPtrs& output_vexpr_ctxs, const Block& input_block,
Block* output_block,
bool do_projection) {
diff --git a/be/test/core/block/column_test.cpp
b/be/test/core/block/column_test.cpp
index 662f74f6f02..e27a284926d 100644
--- a/be/test/core/block/column_test.cpp
+++ b/be/test/core/block/column_test.cpp
@@ -132,7 +132,7 @@ TEST_F(ColumnTest, ShrinkColumnString) {
shrunk_col = shrunk_col->shrink(10);
EXPECT_EQ(shrunk_col->size(), 10);
- EXPECT_EQ(shrunk_col->use_count(), 1);
+ EXPECT_TRUE(shrunk_col->is_exclusive());
EXPECT_EQ(shrunk_col->get_data_at(0), StringRef("aaa"));
EXPECT_EQ(shrunk_col->get_data_at(1), StringRef("bb"));
// column string scale in will not clear
@@ -146,7 +146,7 @@ TEST_F(ColumnTest, ShrinkColumnInt64) {
shrunk_col = col_int->shrink(10);
EXPECT_EQ(shrunk_col->size(), 10);
- EXPECT_EQ(shrunk_col->use_count(), 1);
+ EXPECT_TRUE(shrunk_col->is_exclusive());
EXPECT_EQ(static_cast<const
ColumnInt64*>(shrunk_col.get())->get_element(0), 1);
EXPECT_EQ(static_cast<const
ColumnInt64*>(shrunk_col.get())->get_element(1), 2);
// column vector scale out will not empty init
@@ -162,7 +162,7 @@ TEST_F(ColumnTest, ShrinkColumnDecimal64) {
shrunk_col = col_dcm->shrink(10);
EXPECT_EQ(shrunk_col->size(), 10);
- EXPECT_EQ(shrunk_col->use_count(), 1);
+ EXPECT_TRUE(shrunk_col->is_exclusive());
EXPECT_EQ(static_cast<const
ColumnDecimal64*>(shrunk_col.get())->get_element(0),
Decimal64(1.23));
EXPECT_EQ(static_cast<const
ColumnDecimal64*>(shrunk_col.get())->get_element(1),
diff --git a/be/test/core/column/check_and_get_column_ptr_test.cpp
b/be/test/core/column/check_and_get_column_ptr_test.cpp
index 6318183461b..43ff97aea1c 100644
--- a/be/test/core/column/check_and_get_column_ptr_test.cpp
+++ b/be/test/core/column/check_and_get_column_ptr_test.cpp
@@ -55,25 +55,25 @@ TEST(CheckAndGetColumnPtrTest, checktest) {
{
ColumnPtr column_ptr = ColumnHelper::create_column<DataTypeInt32>({1,
2, 3});
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
auto column_i32 = check_and_get_column_ptr<ColumnInt32>(column_ptr);
EXPECT_TRUE(column_i32);
- EXPECT_EQ(column_ptr->use_count(), 2);
+ EXPECT_FALSE(column_ptr->is_exclusive());
- EXPECT_EQ(column_i32->use_count(), 2);
+ EXPECT_FALSE(column_i32->is_exclusive());
}
{
ColumnPtr column_ptr = ColumnHelper::create_column<DataTypeInt32>({1,
2, 3});
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
auto column_i64 = check_and_get_column_ptr<ColumnInt64>(column_ptr);
EXPECT_FALSE(column_i64);
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
}
}
@@ -90,7 +90,6 @@ TEST(CheckAndGetColumnPtrTest, errorTest) {
const auto* source_column =
check_and_get_column<ColumnNullable>(nested_column.get());
nested_column = source_column->get_nested_column_ptr();
// The source_column is now a dangling pointer.
- // std::cout<<source_column->use_count()<<std::endl;
}
{
@@ -105,7 +104,6 @@ TEST(CheckAndGetColumnPtrTest, errorTest) {
const auto& source_column = assert_cast<const
ColumnNullable&>(*nested_column);
nested_column = source_column.get_nested_column_ptr();
// The source_column is now a dangling pointer.
- // std::cout<<source_column.use_count()<<std::endl;
}
{
@@ -119,40 +117,40 @@ TEST(CheckAndGetColumnPtrTest, errorTest) {
nested_column = nested_column->convert_to_full_column_if_const();
const auto source_column =
check_and_get_column_ptr<ColumnNullable>(nested_column);
nested_column = source_column->get_nested_column_ptr();
- EXPECT_EQ(source_column->use_count(), 1);
+ EXPECT_TRUE(source_column->IColumn::is_exclusive());
}
}
TEST(CheckAndGetColumnPtrTest, destructstest) {
ColumnPtr column_ptr = ColumnHelper::create_column<DataTypeInt32>({1, 2,
3});
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
{
auto column_i32 = check_and_get_column_ptr<ColumnInt32>(column_ptr);
EXPECT_TRUE(column_i32);
- EXPECT_EQ(column_ptr->use_count(), 2);
+ EXPECT_FALSE(column_ptr->is_exclusive());
- EXPECT_EQ(column_i32->use_count(), 2);
+ EXPECT_FALSE(column_i32->is_exclusive());
}
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
}
TEST(CheckAndGetColumnPtrTest, cast_to_column_immut) {
{
ColumnPtr column_ptr = ColumnHelper::create_column<DataTypeInt32>({1,
2, 3});
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
ColumnInt32::Ptr column_i32 = cast_to_column<ColumnInt32>(column_ptr);
EXPECT_TRUE(column_i32);
- EXPECT_EQ(column_ptr->use_count(), 2);
+ EXPECT_FALSE(column_ptr->is_exclusive());
- EXPECT_EQ(column_i32->use_count(), 2);
+ EXPECT_FALSE(column_i32->is_exclusive());
}
}
@@ -160,12 +158,12 @@ TEST(CheckAndGetColumnPtrTest, cast_to_column_mut) {
{
MutableColumnPtr column_ptr = ColumnInt32::create();
- EXPECT_EQ(column_ptr->use_count(), 1);
+ EXPECT_TRUE(column_ptr->is_exclusive());
ColumnInt32::MutablePtr column_i32 =
cast_to_column<ColumnInt32>(std::move(column_ptr));
EXPECT_TRUE(column_i32);
- EXPECT_EQ(column_i32->use_count(), 1);
+ EXPECT_TRUE(column_i32->is_exclusive());
}
}
diff --git a/be/test/core/column/column_array_test.cpp
b/be/test/core/column/column_array_test.cpp
index 147fd06e2aa..6a1aec7d0cc 100644
--- a/be/test/core/column/column_array_test.cpp
+++ b/be/test/core/column/column_array_test.cpp
@@ -626,7 +626,7 @@ TEST_F(ColumnArrayTest, CreateArrayTest) {
// The constructor enforces this via check_const_only_in_top_level(),
preventing COW-related issues:
// - ColumnConst is immutable and meant for repeated values across rows
// - ColumnArray requires mutable nested data for operations like
insert/filter/clear
- // - Wrapping shared ColumnConst in ColumnArray violates use_count()
assumptions in clear_column_data()
+ // - Wrapping shared ColumnConst in ColumnArray violates exclusive
ownership assumptions in clear_column_data()
for (auto& array_column : array_columns) {
const auto* column = check_and_get_column<ColumnArray>(
remove_nullable(array_column->assert_mutable()).get());
diff --git a/be/test/core/column/common_column_test.h
b/be/test/core/column/common_column_test.h
index 6320dd52aa3..0deaa953bc2 100644
--- a/be/test/core/column/common_column_test.h
+++ b/be/test/core/column/common_column_test.h
@@ -1418,7 +1418,7 @@ public:
//virtual Ptr
//shrink (size_t length) const final shrink many has different ptr for
the column, because of
- // here has some improvement according the origin column which use_count
is 1, we can just return the origin column to avoid the copy
+ // shrink can reuse the original outer column when it is exclusively owned
// but we should make sure the column data is absolutely the same as
expand or cut with the data
static void assert_shrink_callback(MutableColumns& load_cols,
DataTypeSerDeSPtrs serders) {
// Create an empty column to verify `cut` functionality
@@ -1439,13 +1439,12 @@ public:
LOG(INFO) << "now we are in shrink column : " <<
load_cols[i]->get_name()
<< " with check length: " << insert_size
<< " for column size : " << source_column->size();
- size_t cnt = source_column->use_count();
+ bool reuses_outer_column =
source_column->IColumn::is_exclusive();
ptr = source_column->shrink(insert_size);
- LOG(INFO) << "use_count : " << source_column->use_count();
// check size
EXPECT_EQ(ptr->size(), insert_size);
// check ptr is not the same
- if (cnt == 1) {
+ if (reuses_outer_column) {
// just return the origin column to avoid the copy
EXPECT_EQ(ptr.get(), source_column.get());
} else {
diff --git a/be/test/exec/sink/tablet_column_validator_test.cpp
b/be/test/exec/sink/tablet_column_validator_test.cpp
index b6b954a8a5b..5d6bf6fda5f 100644
--- a/be/test/exec/sink/tablet_column_validator_test.cpp
+++ b/be/test/exec/sink/tablet_column_validator_test.cpp
@@ -162,7 +162,7 @@ TEST_F(TabletColumnValidatorTest,
UniqueScalarKeepsObjectBufferAndReferenceCount
auto before = snapshot(*column);
column = TEST_TRY(validate(column, std::make_shared<DataTypeInt32>()));
expect_same_storage(before, *column);
- EXPECT_EQ(column->use_count(), 1);
+ EXPECT_TRUE(column->is_exclusive());
}
TEST_F(TabletColumnValidatorTest, NullableArrayStructReusesLeafStorage) {
@@ -206,7 +206,7 @@ TEST_F(TabletColumnValidatorTest,
StructReusesSharedReadOnlyFields) {
EXPECT_EQ(&tuple.get_column(0), shared.get());
EXPECT_EQ(assert_cast<const
ColumnInt32&>(tuple.get_column(0)).get_data().data(), shared_data);
EXPECT_EQ(assert_cast<const
ColumnInt32&>(tuple.get_column(0)).get_data()[0], 7);
- EXPECT_EQ(shared->use_count(), 2);
+ EXPECT_FALSE(shared->is_exclusive());
expect_same_storage(second_before, tuple.get_column(1));
expect_same_storage(third_before, tuple.get_column(2));
}
@@ -467,7 +467,7 @@ TEST_F(TabletColumnValidatorTest,
NestedReplacementBuildsShellsAndPreservesSourc
EXPECT_EQ(array.get_offsets().data(), offsets_address);
EXPECT_EQ(checked_nullable.get_null_map_data().data(), nulls_address);
EXPECT_EQ(checked_nullable.get_nested_column().get_data_at(0).to_string(),
"ab");
- EXPECT_EQ(checked_nullable.get_nested_column().use_count(), 1);
+ EXPECT_TRUE(checked_nullable.get_nested_column().is_exclusive());
expect_same_storage(sibling_before, checked_tuple.get_column(1));
EXPECT_EQ(filter_map, (std::vector<char> {0}));
EXPECT_EQ(nullable_before.get_nested_column().get_data_at(0).to_string(),
"abcd");
@@ -480,7 +480,7 @@ TEST_F(TabletColumnValidatorTest,
SharedReadOnlyLeavesKeepStorage) {
auto result = TEST_TRY(validate(original, type));
EXPECT_EQ(result.get(), original.get());
expect_same_storage(before, *result);
- EXPECT_EQ(original->use_count(), 2);
+ EXPECT_FALSE(original->is_exclusive());
};
check(ints({7, 8}), std::make_shared<DataTypeInt32>());
check(strings({"valid"}), std::make_shared<DataTypeJsonb>());
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]