Skip to content

Commit a4423dc

Browse files
authored
[opt](column) reuse nullable column metadata during function execution (apache#66031)
The default nullable function path independently inspected the same null maps when checking for all-NULL arguments, unnesting nullable inputs, and wrapping function results. This caused redundant full null-map scans and made type-level nullable checks difficult to distinguish from runtime NULL-value checks. This change introduces `NullableColumnInfo` to collect the nested column, typed null-map column, constness, and non-NULL count once per input column. The information is reused across nullable unnesting, CAST handling, and result wrapping. It also renames the type-level helper to `has_nullable_argument_type` and preserves the existing copy-on-write behavior by copying nested data only when NULL payloads must be replaced with defaults.
1 parent 3b44e00 commit a4423dc

11 files changed

Lines changed: 331 additions & 60 deletions

File tree

‎be/src/core/block/column_with_type_and_name.cpp‎

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
#include "core/column/column.h"
3131
#include "core/column/column_const.h"
3232
#include "core/column/column_nothing.h"
33+
#include "core/column/column_nullable.h"
3334
#include "core/data_type/data_type.h"
3435
#include "core/data_type/data_type_nullable.h"
3536
#include "core/types.h"
36-
#include "util/simd/bits.h"
3737

3838
namespace doris {
3939

@@ -105,41 +105,58 @@ void ColumnWithTypeAndName::to_pb_column_meta(PColumnMeta* col_meta) const {
105105
type->to_pb_column_meta(col_meta);
106106
}
107107

108+
const ColumnNullable& ColumnWithTypeAndName::get_nullable_column() const {
109+
DCHECK(type->is_nullable());
110+
DCHECK(column);
111+
const auto& [physical_column, _] = unpack_if_const(column);
112+
return assert_cast<const ColumnNullable&, TypeCheckOnRelease::DISABLE>(*physical_column);
113+
}
114+
115+
const ColumnUInt8::Ptr& ColumnWithTypeAndName::get_nullable_null_map_column() const {
116+
return get_nullable_column().get_null_map_column_ptr();
117+
}
118+
119+
NullableColumnInfo ColumnWithTypeAndName::get_nullable_column_info() const {
120+
DCHECK(type->is_nullable());
121+
DCHECK(column);
122+
123+
const auto [has_null, only_null] = get_nullable_column().get_null_map_state();
124+
return {.has_null = has_null,
125+
.only_null = only_null,
126+
.is_const = is_column_const(*column),
127+
.is_nullable = true};
128+
}
129+
108130
ColumnWithTypeAndName ColumnWithTypeAndName::unnest_nullable(
109-
bool replace_null_data_to_default) const {
110-
if (type->is_nullable()) {
111-
auto nested_type =
112-
assert_cast<const DataTypeNullable*, TypeCheckOnRelease::DISABLE>(type.get())
113-
->get_nested_type();
114-
ColumnPtr nested_column = column;
115-
if (column) {
116-
// A column_ptr is needed here to ensure that the column in convert_to_full_column_if_const is not released.
117-
auto [column_ptr, is_const] = unpack_if_const(column);
118-
const auto* source_column =
119-
assert_cast<const ColumnNullable*, TypeCheckOnRelease::DISABLE>(
120-
column_ptr.get());
121-
if (is_const) {
122-
nested_column =
123-
ColumnConst::create(source_column->get_nested_column_ptr(), column->size());
124-
} else {
125-
nested_column = source_column->get_nested_column_ptr();
126-
}
127-
128-
if (replace_null_data_to_default) {
129-
const auto& null_map = source_column->get_null_map_data();
130-
// only need to mutate nested column, avoid to copy nullmap
131-
auto mutable_nested_col = (*std::move(nested_column)).mutate();
132-
if (simd::contain_one(null_map.data(), null_map.size())) {
133-
mutable_nested_col->replace_column_null_data(null_map.data());
134-
}
135-
136-
return {std::move(mutable_nested_col), nested_type, ""};
137-
}
138-
}
139-
return {nested_column, nested_type, ""};
140-
} else {
131+
const NullableColumnInfo& info, bool replace_null_data_to_default) const {
132+
if (!type->is_nullable()) {
141133
return {column, type, ""};
142134
}
135+
DCHECK(info.is_nullable);
136+
137+
const auto& nullable_column = get_nullable_column();
138+
const auto get_nested_column = [&]() -> ColumnPtr {
139+
const auto& nested_column = nullable_column.get_nested_column_ptr();
140+
if (info.is_const) {
141+
return ColumnConst::create(nested_column, column->size());
142+
}
143+
return nested_column;
144+
};
145+
146+
auto nested_type = assert_cast<const DataTypeNullable*, TypeCheckOnRelease::DISABLE>(type.get())
147+
->get_nested_type();
148+
if (replace_null_data_to_default && info.has_null) {
149+
if (column->try_replace_null_payload_with_default_without_cow()) {
150+
return {get_nested_column(), nested_type, ""};
151+
}
152+
153+
// Only copy the nested column because the original nullable column must remain unchanged.
154+
const auto nested_column = get_nested_column();
155+
auto mutable_nested_col = nested_column->clone_resized(nested_column->size());
156+
mutable_nested_col->replace_column_null_data(nullable_column.get_null_map_data().data());
157+
return {std::move(mutable_nested_col), nested_type, ""};
158+
}
159+
return {get_nested_column(), nested_type, ""};
143160
}
144161

145162
Status ColumnWithTypeAndName::check_type_and_column_match() const {

‎be/src/core/block/column_with_type_and_name.h‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,29 @@
2626
#include <memory>
2727
#include <string>
2828
#include <utility>
29+
#include <vector>
2930

31+
#include "core/column/column_vector.h"
3032
#include "core/data_type/data_type.h"
3133
#include "core/data_type_serde/data_type_serde.h"
3234
#include "core/types.h"
3335

3436
namespace doris {
37+
class ColumnNullable;
3538
class PColumnMeta;
3639
} // namespace doris
3740

3841
namespace doris {
3942

43+
struct NullableColumnInfo {
44+
bool has_null = false;
45+
bool only_null = false;
46+
bool is_const = false;
47+
bool is_nullable = false;
48+
};
49+
50+
using NullableColumnInfos = std::vector<NullableColumnInfo>;
51+
4052
// class WriteBuffer;
4153

4254
/** Column data along with its data type and name.
@@ -69,9 +81,15 @@ struct ColumnWithTypeAndName {
6981

7082
void to_pb_column_meta(PColumnMeta* col_meta) const;
7183

72-
ColumnWithTypeAndName unnest_nullable(bool replace_null_data_to_default = false) const;
84+
const ColumnUInt8::Ptr& get_nullable_null_map_column() const;
85+
NullableColumnInfo get_nullable_column_info() const;
86+
ColumnWithTypeAndName unnest_nullable(const NullableColumnInfo& info,
87+
bool replace_null_data_to_default) const;
7388

7489
Status check_type_and_column_match() const;
90+
91+
private:
92+
const ColumnNullable& get_nullable_column() const;
7593
};
7694

7795
} // namespace doris

‎be/src/core/column/column.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,17 @@ class IColumn : public COW<IColumn> {
764764
// column_vector and column_decimal override this method to return true
765765
virtual bool support_replace_column_null_data() const { return false; }
766766

767+
/**
768+
* Try to replace the payload of NULL rows with the nested column's default value without
769+
* going through COW. Implementations must return false without modifying data unless the
770+
* complete column ownership chain is exclusive. This is only safe because payloads of rows
771+
* that are already NULL are not observable through the nullable column. In particular, a
772+
* shared nested column may belong to another nullable column with a different null map.
773+
*
774+
* This bypasses the normal COW mutation path. Do not use it for general column mutation.
775+
*/
776+
virtual bool try_replace_null_payload_with_default_without_cow() const { return false; }
777+
767778
// For float/double types, replace -0.0 with 0.0, set NaN to quiet NaN,
768779
// used to ensure data hash equality for -0.0 and +0.0, e.g. aggregate and join
769780
virtual void replace_float_special_values() {}

‎be/src/core/column/column_const.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ class ColumnConst final : public COWHelper<IColumn, ColumnConst> {
310310
return data->support_replace_column_null_data();
311311
}
312312

313+
bool try_replace_null_payload_with_default_without_cow() const override {
314+
if (!IColumn::is_exclusive()) {
315+
return false;
316+
}
317+
return data->try_replace_null_payload_with_default_without_cow();
318+
}
319+
313320
void finalize() override { data->finalize(); }
314321

315322
void erase(size_t start, size_t length) override {

‎be/src/core/column/column_nullable.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,29 @@ bool ColumnNullable::only_null() const {
676676
return !simd::contain_zero(get_null_map_data().data(), size());
677677
}
678678

679+
ColumnNullable::NullMapState ColumnNullable::get_null_map_state() const {
680+
const auto& null_map = get_null_map_data();
681+
if (null_map.empty()) {
682+
return {.has_null = false, .only_null = true};
683+
}
684+
685+
if (null_map[0]) {
686+
return {.has_null = true,
687+
.only_null = !simd::contain_zero(null_map.data() + 1, null_map.size() - 1)};
688+
}
689+
return {.has_null = simd::contain_one(null_map.data() + 1, null_map.size() - 1),
690+
.only_null = false};
691+
}
692+
693+
bool ColumnNullable::try_replace_null_payload_with_default_without_cow() const {
694+
if (!is_exclusive()) {
695+
return false;
696+
}
697+
698+
const_cast<IColumn&>(get_nested_column()).replace_column_null_data(get_null_map_data().data());
699+
return true;
700+
}
701+
679702
bool ColumnNullable::has_null(size_t begin, size_t end) const {
680703
return simd::contain_one(get_null_map_data().data() + begin, end - begin);
681704
}

‎be/src/core/column/column_nullable.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class ColumnNullable final : public COWHelper<IColumn, ColumnNullable> {
6262
ColumnNullable(const ColumnNullable&) = default;
6363

6464
public:
65+
struct NullMapState {
66+
bool has_null;
67+
bool only_null;
68+
};
69+
6570
/** Create a column from immutable/shared subcolumns without cloning them.
6671
* Call IColumn::mutate before modifying the returned column tree.
6772
*/
@@ -275,7 +280,10 @@ class ColumnNullable final : public COWHelper<IColumn, ColumnNullable> {
275280
get_null_map_column().is_exclusive();
276281
}
277282

283+
bool try_replace_null_payload_with_default_without_cow() const override;
284+
278285
bool only_null() const override;
286+
NullMapState get_null_map_state() const;
279287

280288
// used in schema change
281289
void change_nested_column(ColumnPtr& other) { ((ColumnPtr&)_nested_column) = other; }

‎be/src/exprs/function/cast/function_cast.cpp‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,37 @@ WrapperType prepare_remove_nullable(FunctionContext* context, const DataTypePtr&
195195
bool replace_null_data_to_default = need_replace_null_data_to_default(
196196
context, from_type_not_nullable, to_type_not_nullable);
197197

198+
NullableColumnInfo source_info;
199+
if (block.get_by_position(arguments[0]).type->is_nullable()) {
200+
source_info = block.get_by_position(arguments[0]).get_nullable_column_info();
201+
}
198202
auto nested_result_index = block.columns();
199-
block.insert(block.get_by_position(result).unnest_nullable());
203+
const auto& result_column = block.get_by_position(result);
204+
block.insert({nullptr, to_type_not_nullable, result_column.name});
200205
auto nested_source_index = block.columns();
201-
block.insert(block.get_by_position(arguments[0])
202-
.unnest_nullable(replace_null_data_to_default));
206+
if (source_info.is_nullable) {
207+
block.insert(block.get_by_position(arguments[0])
208+
.unnest_nullable(source_info, replace_null_data_to_default));
209+
} else {
210+
block.insert(block.get_by_position(arguments[0]));
211+
}
203212

204-
const auto& arg_col = block.get_by_position(arguments[0]);
205213
const NullMap::value_type* arg_null_map = nullptr;
206-
if (const auto* nullable = check_and_get_column<ColumnNullable>(*arg_col.column)) {
207-
arg_null_map = nullable->get_null_map_data().data();
214+
if (source_info.is_nullable) {
215+
arg_null_map = block.get_by_position(arguments[0])
216+
.get_nullable_null_map_column()
217+
->get_data()
218+
.data();
208219
}
209220
RETURN_IF_ERROR(prepare_impl(context, from_type_not_nullable, to_type_not_nullable)(
210221
context, block, {nested_source_index}, nested_result_index, input_rows_count,
211222
arg_null_map));
212223

224+
NullableColumnInfos nullable_column_infos(block.columns());
225+
nullable_column_infos[arguments[0]] = std::move(source_info);
213226
block.get_by_position(result).column =
214227
wrap_in_nullable(block.get_by_position(nested_result_index).column, block,
215-
arguments, input_rows_count);
228+
arguments, nullable_column_infos, input_rows_count);
216229

217230
block.erase(nested_source_index);
218231
block.erase(nested_result_index);

‎be/src/exprs/function/function.cpp‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
namespace doris {
4545
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
46+
const NullableColumnInfos& nullable_column_infos,
4647
size_t input_rows_count) {
4748
ColumnPtr result_null_map_column;
4849
/// If result is already nullable.
@@ -55,13 +56,13 @@ ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const Colum
5556
}
5657

5758
for (const auto& arg : args) {
58-
const ColumnWithTypeAndName& elem = block.get_by_position(arg);
59-
if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
59+
const auto& info = nullable_column_infos[arg];
60+
if (!info.is_nullable || info.is_const) {
6061
continue;
6162
}
6263

63-
if (auto nullable = cast_to_column<ColumnNullable>(elem.column); nullable->has_null()) {
64-
const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
64+
if (info.has_null) {
65+
const auto& null_map_column = block.get_by_position(arg).get_nullable_null_map_column();
6566
if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
6667
result_null_map_column = null_map_column;
6768
continue;
@@ -73,8 +74,7 @@ ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const Colum
7374

7475
NullMap& result_null_map =
7576
assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
76-
const NullMap& src_null_map =
77-
assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
77+
const NullMap& src_null_map = null_map_column->get_data();
7878

7979
VectorizedUtils::update_null_map(result_null_map, src_null_map);
8080
}
@@ -99,6 +99,18 @@ ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const Colum
9999
return ColumnNullable::create(src_not_nullable, result_null_map_column);
100100
}
101101

102+
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
103+
size_t input_rows_count) {
104+
NullableColumnInfos nullable_column_infos(block.columns());
105+
for (const auto arg : args) {
106+
const auto& column = block.get_by_position(arg);
107+
if (column.type->is_nullable()) {
108+
nullable_column_infos[arg] = column.get_nullable_column_info();
109+
}
110+
}
111+
return wrap_in_nullable(src, block, args, nullable_column_infos, input_rows_count);
112+
}
113+
102114
bool have_null_column(const Block& block, const ColumnNumbers& args) {
103115
return std::ranges::any_of(args, [&block](const auto& elem) {
104116
return block.get_by_position(elem).type->is_nullable();
@@ -195,16 +207,25 @@ Status PreparedFunctionImpl::default_implementation_for_nulls(
195207
return Status::OK();
196208
}
197209

198-
if (std::ranges::any_of(args, [&block](const auto& elem) {
199-
return block.get_by_position(elem).column->only_null();
200-
})) {
201-
block.get_by_position(result).column =
202-
block.get_by_position(result).type->create_column_const(input_rows_count, Field());
203-
*executed = true;
204-
return Status::OK();
205-
}
206-
207210
if (have_null_column(block, args)) {
211+
NullableColumnInfos nullable_column_infos(block.columns());
212+
for (const auto arg : args) {
213+
const auto& argument = block.get_by_position(arg);
214+
if (!argument.type->is_nullable()) {
215+
continue;
216+
}
217+
218+
auto info = argument.get_nullable_column_info();
219+
if (info.only_null) {
220+
auto& result_column = block.get_by_position(result);
221+
result_column.column =
222+
result_column.type->create_column_const(input_rows_count, Field());
223+
*executed = true;
224+
return Status::OK();
225+
}
226+
nullable_column_infos[arg] = info;
227+
}
228+
208229
bool need_to_default = need_replace_null_data_to_default();
209230
// extract nested column from nulls
210231
ColumnNumbers new_args;
@@ -213,7 +234,8 @@ Status PreparedFunctionImpl::default_implementation_for_nulls(
213234
for (int i = 0; i < args.size(); ++i) {
214235
uint32_t arg = args[i];
215236
new_args.push_back(i);
216-
new_block.insert(block.get_by_position(arg).unnest_nullable(need_to_default));
237+
new_block.insert(block.get_by_position(arg).unnest_nullable(nullable_column_infos[arg],
238+
need_to_default));
217239
}
218240
new_block.insert(block.get_by_position(result));
219241
int new_result = new_block.columns() - 1;
@@ -222,8 +244,9 @@ Status PreparedFunctionImpl::default_implementation_for_nulls(
222244
// After run with nested, wrap them in null. Before this, block.get_by_position(result).type
223245
// is not compatible with get_by_position(result).column
224246

225-
block.get_by_position(result).column = wrap_in_nullable(
226-
new_block.get_by_position(new_result).column, block, args, input_rows_count);
247+
block.get_by_position(result).column =
248+
wrap_in_nullable(new_block.get_by_position(new_result).column, block, args,
249+
nullable_column_infos, input_rows_count);
227250

228251
*executed = true;
229252
return Status::OK();

0 commit comments

Comments
 (0)