aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Columns/MaskOperations.cpp
blob: b84268356a7a64dea5db06d25fbcdb010cca387b (plain) (blame)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <Columns/MaskOperations.h>
#include <Columns/ColumnFunction.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnNothing.h>
#include <Columns/ColumnsCommon.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnLowCardinality.h>
#include <algorithm>

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
    extern const int ILLEGAL_COLUMN;
}

template <typename T>
void expandDataByMask(PaddedPODArray<T> & data, const PaddedPODArray<UInt8> & mask, bool inverted)
{
    if (mask.size() < data.size())
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Mask size should be no less than data size.");

    ssize_t from = data.size() - 1;
    ssize_t index = mask.size() - 1;
    data.resize(mask.size());
    while (index >= 0)
    {
        if (!!mask[index] ^ inverted)
        {
            if (from < 0)
                throw Exception(ErrorCodes::LOGICAL_ERROR, "Too many bytes in mask");

            /// Copy only if it makes sense.
            if (index != from)
                data[index] = data[from];
            --from;
        }
        else
            data[index] = T();

        --index;
    }

    if (from != -1)
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Not enough bytes in mask");
}

/// Explicit instantiations - not to place the implementation of the function above in the header file.
#define INSTANTIATE(TYPE) \
template void expandDataByMask<TYPE>(PaddedPODArray<TYPE> &, const PaddedPODArray<UInt8> &, bool);

INSTANTIATE(UInt8)
INSTANTIATE(UInt16)
INSTANTIATE(UInt32)
INSTANTIATE(UInt64)
INSTANTIATE(UInt128)
INSTANTIATE(UInt256)
INSTANTIATE(Int8)
INSTANTIATE(Int16)
INSTANTIATE(Int32)
INSTANTIATE(Int64)
INSTANTIATE(Int128)
INSTANTIATE(Int256)
INSTANTIATE(Float32)
INSTANTIATE(Float64)
INSTANTIATE(Decimal32)
INSTANTIATE(Decimal64)
INSTANTIATE(Decimal128)
INSTANTIATE(Decimal256)
INSTANTIATE(DateTime64)
INSTANTIATE(char *)
INSTANTIATE(UUID)
INSTANTIATE(IPv4)
INSTANTIATE(IPv6)

#undef INSTANTIATE

template <bool inverted, bool column_is_short, typename Container>
size_t extractMaskNumericImpl(
    PaddedPODArray<UInt8> & mask,
    const Container & data,
    UInt8 null_value,
    const PaddedPODArray<UInt8> * null_bytemap,
    PaddedPODArray<UInt8> * nulls)
{
    if constexpr (!column_is_short)
    {
        if (data.size() != mask.size())
            throw Exception(ErrorCodes::LOGICAL_ERROR, "The size of a full data column is not equal to the size of a mask");
    }

    size_t ones_count = 0;
    size_t data_index = 0;

    size_t mask_size = mask.size();
    size_t data_size = data.size();

    size_t i = 0;
    for (; i != mask_size && data_index != data_size; ++i)
    {
        // Change mask only where value is 1.
        if (!mask[i])
            continue;

        UInt8 value;
        size_t index;
        if constexpr (column_is_short)
        {
            index = data_index;
            ++data_index;
        }
        else
            index = i;

        if (null_bytemap && (*null_bytemap)[index])
        {
            value = null_value;
            if (nulls)
                (*nulls)[i] = 1;
        }
        else
            value = static_cast<bool>(data[index]);

        if constexpr (inverted)
            value = !value;

        if (value)
            ++ones_count;

        mask[i] = value;
    }

    if constexpr (column_is_short)
    {
        if (data_index != data_size)
            throw Exception(ErrorCodes::LOGICAL_ERROR, "The size of a short column is not equal to the number of ones in a mask");
    }

    return ones_count;
}

template <bool inverted, typename NumericType>
bool extractMaskNumeric(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    UInt8 null_value,
    const PaddedPODArray<UInt8> * null_bytemap,
    PaddedPODArray<UInt8> * nulls,
    MaskInfo & mask_info)
{
    const auto * numeric_column = checkAndGetColumn<ColumnVector<NumericType>>(column.get());
    if (!numeric_column)
        return false;

    const auto & data = numeric_column->getData();
    size_t ones_count;
    if (column->size() < mask.size())
        ones_count = extractMaskNumericImpl<inverted, true>(mask, data, null_value, null_bytemap, nulls);
    else
        ones_count = extractMaskNumericImpl<inverted, false>(mask, data, null_value, null_bytemap, nulls);

    mask_info.has_ones = ones_count > 0;
    mask_info.has_zeros = ones_count != mask.size();
    return true;
}

template <bool inverted>
MaskInfo extractMaskFromConstOrNull(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    UInt8 null_value,
    PaddedPODArray<UInt8> * nulls = nullptr)
{
    UInt8 value;
    if (column->onlyNull())
    {
        value = null_value;
        if (nulls)
            std::fill(nulls->begin(), nulls->end(), 1);
    }
    else
        value = column->getBool(0);

    if constexpr (inverted)
        value = !value;

    size_t ones_count = 0;
    if (value)
        ones_count = countBytesInFilter(mask);
    else
        std::fill(mask.begin(), mask.end(), 0);

    return {.has_ones = ones_count > 0, .has_zeros = ones_count != mask.size()};
}

template <bool inverted>
MaskInfo extractMaskImpl(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & col,
    UInt8 null_value,
    const PaddedPODArray<UInt8> * null_bytemap,
    PaddedPODArray<UInt8> * nulls = nullptr)
{
    auto column = col->convertToFullColumnIfLowCardinality();

    /// Special implementation for Null and Const columns.
    if (column->onlyNull() || checkAndGetColumn<ColumnConst>(*column))
        return extractMaskFromConstOrNull<inverted>(mask, column, null_value, nulls);

    if (const auto * nullable_column = checkAndGetColumn<ColumnNullable>(*column))
    {
        const PaddedPODArray<UInt8> & null_map = nullable_column->getNullMapData();
        return extractMaskImpl<inverted>(mask, nullable_column->getNestedColumnPtr(), null_value, &null_map, nulls);
    }

    MaskInfo mask_info;

    if (!(extractMaskNumeric<inverted, UInt8>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, UInt16>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, UInt32>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, UInt64>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Int8>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Int16>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Int32>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Int64>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Float32>(mask, column, null_value, null_bytemap, nulls, mask_info)
          || extractMaskNumeric<inverted, Float64>(mask, column, null_value, null_bytemap, nulls, mask_info)))
        throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Cannot convert column {} to mask.", column->getName());

    return mask_info;
}

MaskInfo extractMask(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    UInt8 null_value)
{
    return extractMaskImpl<false>(mask, column, null_value, nullptr);
}

MaskInfo extractInvertedMask(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    UInt8 null_value)
{
    return extractMaskImpl<true>(mask, column, null_value, nullptr);
}

MaskInfo extractMask(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    PaddedPODArray<UInt8> * nulls,
    UInt8 null_value)
{
    return extractMaskImpl<false>(mask, column, null_value, nullptr, nulls);
}

MaskInfo extractInvertedMask(
    PaddedPODArray<UInt8> & mask,
    const ColumnPtr & column,
    PaddedPODArray<UInt8> * nulls,
    UInt8 null_value)
{
    return extractMaskImpl<true>(mask, column, null_value, nullptr, nulls);
}


void inverseMask(PaddedPODArray<UInt8> & mask, MaskInfo & mask_info)
{
    for (auto & byte : mask)
        byte = !byte;
    std::swap(mask_info.has_ones, mask_info.has_zeros);
}

void maskedExecute(ColumnWithTypeAndName & column, const PaddedPODArray<UInt8> & mask, const MaskInfo & mask_info)
{
    const auto * column_function = checkAndGetShortCircuitArgument(column.column);
    if (!column_function)
        return;

    ColumnWithTypeAndName result;
    /// If mask contains only zeros, we can just create
    /// an empty column with the execution result type.
    if (!mask_info.has_ones)
    {
        auto result_type = column_function->getResultType();
        auto empty_column = result_type->createColumn();
        result = {std::move(empty_column), result_type, ""};
    }
    /// Filter column only if mask contains zeros.
    else if (mask_info.has_zeros)
    {
        auto filtered = column_function->filter(mask, -1);
        result = typeid_cast<const ColumnFunction *>(filtered.get())->reduce();
    }
    else
        result = column_function->reduce();

    column = std::move(result);
}

void executeColumnIfNeeded(ColumnWithTypeAndName & column, bool empty)
{
    const auto * column_function = checkAndGetShortCircuitArgument(column.column);
    if (!column_function)
        return;

    if (!empty)
        column = column_function->reduce();
    else
        column.column = column_function->getResultType()->createColumn();
}

int checkShortCircuitArguments(const ColumnsWithTypeAndName & arguments)
{
    int last_short_circuit_argument_index = -1;
    for (size_t i = 0; i != arguments.size(); ++i)
    {
        if (checkAndGetShortCircuitArgument(arguments[i].column))
            last_short_circuit_argument_index = static_cast<int>(i);
    }

    return last_short_circuit_argument_index;
}

void copyMask(const PaddedPODArray<UInt8> & from, PaddedPODArray<UInt8> & to)
{
    if (from.size() != to.size())
        throw Exception(ErrorCodes::LOGICAL_ERROR, "Cannot copy mask, because source and destination have different size");

    if (from.empty())
        return;

    memcpy(to.data(), from.data(), from.size() * sizeof(*from.data()));
}

}