aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/array/arrayFirstLast.cpp
blob: 055c2a0f79a2851c6e88fa303da6f7e5d1a47401 (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
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnNullable.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>

#include "FunctionArrayMapped.h"


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

enum class ArrayFirstLastStrategy : uint8_t
{
    First,
    Last
};

enum class ArrayFirstLastElementNotExistsStrategy : uint8_t
{
    Default,
    Null
};

template <ArrayFirstLastStrategy strategy, ArrayFirstLastElementNotExistsStrategy element_not_exists_strategy>
struct ArrayFirstLastImpl
{
    static bool needBoolean() { return false; }
    static bool needExpression() { return true; }
    static bool needOneArray() { return false; }

    static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & array_element)
    {
        if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
            return makeNullable(array_element);

        return array_element;
    }

    static ColumnPtr createNullableColumn(MutableColumnPtr && column, ColumnUInt8::MutablePtr && null_map)
    {
        if (auto * nullable_column = typeid_cast<ColumnNullable *>(column.get()))
        {
            nullable_column->applyNullMap(*null_map);
            return std::move(column);
        }
        return ColumnNullable::create(std::move(column), std::move(null_map));
    }

    static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
    {
        const auto * column_filter = typeid_cast<const ColumnUInt8 *>(&*mapped);

        if (!column_filter)
        {
            const auto * column_filter_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);

            if (!column_filter_const)
                throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Unexpected type of filter column");

            if (column_filter_const->getValue<UInt8>())
            {
                const auto & offsets = array.getOffsets();
                const auto & data = array.getData();
                auto out = data.cloneEmpty();
                out->reserve(data.size());

                size_t offsets_size = offsets.size();

                ColumnUInt8::MutablePtr col_null_map_to;
                ColumnUInt8::Container * vec_null_map_to = nullptr;

                if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
                {
                    col_null_map_to = ColumnUInt8::create(offsets_size, false);
                    vec_null_map_to = &col_null_map_to->getData();
                }

                for (size_t offset_index = 0; offset_index < offsets_size; ++offset_index)
                {
                    size_t start_offset = offsets[offset_index - 1];
                    size_t end_offset = offsets[offset_index];

                    if (end_offset > start_offset)
                    {
                        if constexpr (strategy == ArrayFirstLastStrategy::First)
                            out->insert(data[start_offset]);
                        else
                            out->insert(data[end_offset - 1]);
                    }
                    else
                    {
                        out->insertDefault();

                        if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
                            (*vec_null_map_to)[offset_index] = true;
                    }
                }

                if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
                    return createNullableColumn(std::move(out), std::move(col_null_map_to));

                return out;
            }
            else
            {
                auto out = array.getData().cloneEmpty();
                out->insertManyDefaults(array.size());

                if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
                {
                    auto col_null_map_to = ColumnUInt8::create(out->size(), true);
                    return createNullableColumn(std::move(out), std::move(col_null_map_to));
                }

                return out;
            }
        }

        const auto & filter = column_filter->getData();
        const auto & offsets = array.getOffsets();
        const auto & data = array.getData();
        auto out = data.cloneEmpty();
        out->reserve(data.size());

        size_t offsets_size = offsets.size();

        ColumnUInt8::MutablePtr col_null_map_to;
        ColumnUInt8::Container * vec_null_map_to = nullptr;

        if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
        {
            col_null_map_to = ColumnUInt8::create(offsets_size, false);
            vec_null_map_to = &col_null_map_to->getData();
        }

        for (size_t offset_index = 0; offset_index < offsets_size; ++offset_index)
        {
            size_t start_offset = offsets[offset_index - 1];
            size_t end_offset = offsets[offset_index];

            bool exists = false;

            if constexpr (strategy == ArrayFirstLastStrategy::First)
            {
                for (; start_offset != end_offset; ++start_offset)
                {
                    if (filter[start_offset])
                    {
                        out->insert(data[start_offset]);
                        exists = true;
                        break;
                    }
                }
            }
            else
            {
                for (; end_offset != start_offset; --end_offset)
                {
                    if (filter[end_offset - 1])
                    {
                        out->insert(data[end_offset - 1]);
                        exists = true;
                        break;
                    }
                }
            }

            if (!exists)
            {
                out->insertDefault();

                if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
                    (*vec_null_map_to)[offset_index] = true;
            }
        }

        if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
            return createNullableColumn(std::move(out), std::move(col_null_map_to));

        return out;
    }
};

struct NameArrayFirst { static constexpr auto name = "arrayFirst"; };
using ArrayFirstImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::First, ArrayFirstLastElementNotExistsStrategy::Default>;
using FunctionArrayFirst = FunctionArrayMapped<ArrayFirstImpl, NameArrayFirst>;

struct NameArrayFirstOrNull { static constexpr auto name = "arrayFirstOrNull"; };
using ArrayFirstOrNullImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::First, ArrayFirstLastElementNotExistsStrategy::Null>;
using FunctionArrayFirstOrNull = FunctionArrayMapped<ArrayFirstOrNullImpl, NameArrayFirstOrNull>;

struct NameArrayLast { static constexpr auto name = "arrayLast"; };
using ArrayLastImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::Last, ArrayFirstLastElementNotExistsStrategy::Default>;
using FunctionArrayLast = FunctionArrayMapped<ArrayLastImpl, NameArrayLast>;

struct NameArrayLastOrNull { static constexpr auto name = "arrayLastOrNull"; };
using ArrayLastOrNullImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::Last, ArrayFirstLastElementNotExistsStrategy::Null>;
using FunctionArrayLastOrNull = FunctionArrayMapped<ArrayLastOrNullImpl, NameArrayLastOrNull>;

REGISTER_FUNCTION(ArrayFirst)
{
    factory.registerFunction<FunctionArrayFirst>();
    factory.registerFunction<FunctionArrayFirstOrNull>();
    factory.registerFunction<FunctionArrayLast>();
    factory.registerFunction<FunctionArrayLastOrNull>();
}

}