-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Expand file tree
/
Copy pathNullableUtils.cpp
More file actions
245 lines (204 loc) · 9.84 KB
/
Copy pathNullableUtils.cpp
File metadata and controls
245 lines (204 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <Columns/ColumnDynamic.h>
#include <Columns/ColumnLowCardinality.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnTuple.h>
#include <Columns/ColumnVariant.h>
#include <Columns/ColumnsCommon.h>
#include <DataTypes/DataTypeLowCardinality.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/NullableUtils.h>
#include <DataTypes/Serializations/SerializationNullable.h>
#include <DataTypes/Serializations/SerializationNullableWithParentNullMap.h>
#include <Core/Settings.h>
#include <Interpreters/Context.h>
#include <Common/assert_cast.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
namespace Setting
{
extern const SettingsBool allow_nullable_tuple_in_extracted_subcolumns;
}
static bool isNullableTupleInExtractedSubcolumnsEnabledByGlobalSetting()
{
auto context = Context::getGlobalContextInstance();
return context && context->getSettingsRef()[Setting::allow_nullable_tuple_in_extracted_subcolumns];
}
static bool canExtractedSubcolumnsBeInsideNullable(const ColumnPtr & column)
{
if (checkAndGetColumn<ColumnTuple>(column.get()))
return isNullableTupleInExtractedSubcolumnsEnabledByGlobalSetting();
return column->canBeInsideNullable();
}
bool canExtractedSubcolumnsBeInsideNullable(const DataTypePtr & type)
{
if (isTuple(type))
return isNullableTupleInExtractedSubcolumnsEnabledByGlobalSetting();
return type->canBeInsideNullable();
}
bool canExtractedSubcolumnsBeInsideNullableOrLowCardinalityNullable(const DataTypePtr & type)
{
return canExtractedSubcolumnsBeInsideNullable(removeLowCardinality(type));
}
DataTypePtr makeExtractedSubcolumnsNullableOrLowCardinalityNullableSafe(const DataTypePtr & type)
{
if (!canExtractedSubcolumnsBeInsideNullableOrLowCardinalityNullable(type))
return type;
return makeNullableOrLowCardinalityNullableSafe(type);
}
ColumnPtr extractNestedColumnsAndNullMap(ColumnRawPtrs & key_columns, ConstNullMapPtr & null_map)
{
ColumnPtr null_map_holder;
auto addNullMap = [&](const ColumnNullable * column_nullable)
{
if (!null_map_holder)
{
/// First nullable column: just take its null map as the base
null_map_holder = column_nullable->getNullMapColumnPtr();
}
else
{
/// Subsequent nullable columns: OR their null maps into the accumulated one
MutableColumnPtr mutable_null_map_holder = IColumn::mutate(std::move(null_map_holder));
PaddedPODArray<UInt8> & mutable_null_map = assert_cast<ColumnUInt8 &>(*mutable_null_map_holder).getData();
const PaddedPODArray<UInt8> & other_null_map = column_nullable->getNullMapData();
for (size_t i = 0, size = mutable_null_map.size(); i < size; ++i)
mutable_null_map[i] |= other_null_map[i];
null_map_holder = std::move(mutable_null_map_holder);
}
};
for (auto & column : key_columns)
{
if (const auto * column_nullable = checkAndGetColumn<ColumnNullable>(&*column))
{
/// Top-level Nullable(...) always contributes to the combined null map
addNullMap(column_nullable);
const IColumn * nested_column = &column_nullable->getNestedColumn();
column = nested_column;
/// Special case: Nullable(Tuple(...))
/// If the nested column is a tuple, also fold in null maps of nullable tuple elements
if (const auto * tuple = checkAndGetColumn<ColumnTuple>(nested_column))
{
const auto & tuple_columns = tuple->getColumns();
for (const auto & element : tuple_columns)
{
if (const auto * elem_nullable = checkAndGetColumn<ColumnNullable>(element.get()))
{
addNullMap(elem_nullable);
}
}
}
}
}
null_map = null_map_holder ? &assert_cast<const ColumnUInt8 &>(*null_map_holder).getData() : nullptr;
return null_map_holder;
}
void applyParentNullMapToExtractedSubcolumn(
IColumn & column, const NullMap & parent_null_map, size_t column_offset, size_t parent_null_map_offset)
{
chassert(column_offset <= column.size());
const size_t length = column.size() - column_offset;
chassert(parent_null_map_offset + length <= parent_null_map.size());
/// A non-nullable `LowCardinality(T)` read from disk has no NULL placeholder in its dictionary, so
/// promote it to `LowCardinality(Nullable(T))` in place. The extracted subcolumn's type is
/// `LowCardinality(Nullable(T))` (see `create(DataTypePtr)`), so this keeps the (type, column) pair
/// consistent even for ranges that contain no parent NULLs (handled before the early-out below).
if (auto * low_cardinality_to_promote = typeid_cast<ColumnLowCardinality *>(&column);
low_cardinality_to_promote && !low_cardinality_to_promote->nestedIsNullable())
low_cardinality_to_promote->convertDictionaryToNullableInplace();
/// When no row of the range is NULL in the parent, the subcolumn already holds the correct values and
/// nothing needs to be marked NULL.
if (memoryIsZero(parent_null_map.data(), parent_null_map_offset, parent_null_map_offset + length))
return;
/// Build a keep-mask that covers only the applied range: zero for the rows that are NULL in the parent
/// and one elsewhere.
IColumn::Filter keep_mask(length);
for (size_t i = 0; i < length; ++i)
keep_mask[i] = !parent_null_map[parent_null_map_offset + i];
if (auto * nullable = typeid_cast<ColumnNullable *>(&column))
{
nullable->applyNegatedNullMap(keep_mask, column_offset);
return;
}
if (auto * variant = typeid_cast<ColumnVariant *>(&column))
{
variant->applyNegatedNullMap(keep_mask, column_offset);
return;
}
if (auto * dynamic = typeid_cast<ColumnDynamic *>(&column))
{
dynamic->applyNegatedNullMap(keep_mask, column_offset);
return;
}
if (auto * low_cardinality = typeid_cast<ColumnLowCardinality *>(&column))
{
low_cardinality->applyNegatedNullMap(keep_mask, column_offset);
return;
}
throw Exception(
ErrorCodes::LOGICAL_ERROR, "Cannot apply the parent null map to subcolumn {} that cannot represent NULL values", column.getName());
}
DataTypePtr NullableSubcolumnCreator::create(const DataTypePtr & prev) const
{
/// Wrap into `Nullable(...)` when possible, or `LowCardinality(Nullable(T))` for a non-nullable
/// `LowCardinality(T)` element (which cannot be wrapped in plain `Nullable`) so it can still
/// represent the outer column's NULLs. Returns `prev` unchanged for types that can do neither.
return makeExtractedSubcolumnsNullableOrLowCardinalityNullableSafe(prev);
}
SerializationPtr NullableSubcolumnCreator::create(const SerializationPtr & prev_serialization, const DataTypePtr & prev_type) const
{
if (prev_type && !canExtractedSubcolumnsBeInsideNullable(prev_type))
{
/// The extracted subcolumn cannot be wrapped into Nullable, but some types can represent NULL
/// themselves: Nullable (possibly inside LowCardinality), Dynamic and Variant. For them return a
/// serialization that also reads the outer null map and marks the corresponding rows as NULL in
/// the subcolumn's own null representation.
///
/// A non-nullable `LowCardinality(T)` cannot represent NULL as-is, but its extracted subcolumn's
/// type is promoted to `LowCardinality(Nullable(T))` (see `create(DataTypePtr)`). Wrap the original
/// (non-nullable) serialization so the on-disk stream layout is unchanged; the wrapper reads the
/// `LowCardinality(T)` column and `applyParentNullMapToExtractedSubcolumn` promotes it to
/// `LowCardinality(Nullable(T))` before folding in the outer null map, keeping the read
/// (type, column) pair consistent with the in-memory one. `prev_type` is passed to the wrapper in
/// that case, because the promoted result column is not the on-disk representation to deserialize
/// into.
if (canContainNull(*prev_type))
return SerializationNullableWithParentNullMap::create(prev_serialization);
if (prev_type->lowCardinality())
return SerializationNullableWithParentNullMap::create(prev_serialization, prev_type);
return prev_serialization;
}
return SerializationNullable::create(prev_serialization);
}
ColumnPtr NullableSubcolumnCreator::create(const ColumnPtr & prev) const
{
if (canExtractedSubcolumnsBeInsideNullable(prev))
return ColumnNullable::create(prev, null_map);
if (null_map)
{
const auto & outer_null_map_data = assert_cast<const ColumnUInt8 &>(*null_map).getData();
/// The extracted subcolumn cannot be wrapped into Nullable, but if it can already represent NULL
/// itself, mark rows that are NULL in the outer column as NULL in it.
if (canContainNull(*prev))
{
auto mutable_column = IColumn::mutate(prev);
applyParentNullMapToExtractedSubcolumn(*mutable_column, outer_null_map_data, 0, 0);
return mutable_column;
}
/// A non-nullable `LowCardinality(T)` cannot represent NULL as-is, but it can once promoted to
/// `LowCardinality(Nullable(T))` -- which is what the type `create(DataTypePtr)` returns. Promote the
/// column the same way with `cloneNullable()` before folding in the outer null map, so the extracted
/// (type, column) pair stays consistent.
if (const auto * prev_lc = checkAndGetColumn<ColumnLowCardinality>(prev.get()))
{
auto mutable_column = prev_lc->cloneNullable();
applyParentNullMapToExtractedSubcolumn(*mutable_column, outer_null_map_data, 0, 0);
return mutable_column;
}
}
return prev;
}
}