-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayRemove.cpp
More file actions
236 lines (198 loc) · 10.4 KB
/
Copy patharrayRemove.cpp
File metadata and controls
236 lines (198 loc) · 10.4 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
#include <Columns/ColumnArray.h>
#include <Columns/ColumnNullable.h>
#include <Common/assert_cast.h>
#include <DataTypes/DataTypesNumber.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeNothing.h>
#include <DataTypes/DataTypeNullable.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/array/arrayRemove.h>
namespace DB
{
namespace
{
ColumnPtr BuildAndExecuteFunction(
const FunctionOverloadResolverPtr & resolver, const ColumnsWithTypeAndName & func_args, size_t expected_column_count)
{
auto func = resolver->build(func_args);
return func->execute(func_args, func->getResultType(), expected_column_count, /* dry_run = */ false);
}
}
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
FunctionPtr FunctionArrayRemove::create(ContextPtr context_)
{
return std::make_shared<FunctionArrayRemove>(context_);
}
FunctionArrayRemove::FunctionArrayRemove(ContextPtr context_)
: is_null_resolver(FunctionFactory::instance().get("isNull", context_))
, not_resolver(FunctionFactory::instance().get("not", context_))
, not_equals_resolver(FunctionFactory::instance().get("notEquals", context_))
, equals_resolver(FunctionFactory::instance().get("equals", context_))
, or_resolver(FunctionFactory::instance().get("or", context_))
, and_resolver(FunctionFactory::instance().get("and", context_))
, if_resolver(FunctionFactory::instance().get("if", context_))
{
}
DataTypePtr FunctionArrayRemove::getReturnTypeImpl(const DataTypes & arguments) const
{
if (arguments.size() != 2)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Number of arguments for function {} doesn't match: passed {}, should be 2",
getName(), arguments.size());
const DataTypeArray * array_type = checkAndGetDataType<DataTypeArray>(arguments[0].get());
if (!array_type)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"First argument for function {} must be an array but it has type {}.",
getName(), arguments[0]->getName());
return arguments[0];
}
ColumnPtr FunctionArrayRemove::executeImpl(
const ColumnsWithTypeAndName & arguments,
const DataTypePtr & return_type,
size_t input_rows_count) const
{
const auto * return_type_array = checkAndGetDataType<DataTypeArray>(return_type.get());
if (!return_type_array)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Return type for function {} must be Array, got {}", getName(), return_type->getName());
if (typeid_cast<const DataTypeNothing *>(return_type_array->getNestedType().get()))
return return_type->createColumnConstWithDefaultValue(input_rows_count);
const auto & arr_arg_column = arguments[0].column->convertToFullColumnIfConst();
const auto * arr_col = checkAndGetColumn<ColumnArray>(arr_arg_column.get());
if (!arr_col)
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
"First argument for function {} must be Array, got {}", getName(), arguments[0].column->getName());
const auto & arr_data_col = arr_col->getDataPtr();
const auto & arr_offsets = arr_col->getOffsets();
size_t arr_elements_count = arr_data_col->size();
const auto & arr_data_type = assert_cast<const DataTypeArray &>(*arguments[0].type).getNestedType();
const auto & elem_type = arguments[1].type;
ColumnPtr filter_col;
if (elem_type->onlyNull())
{
if (!canContainNull(*arr_data_type))
{
return arguments[0].column;
}
// Filter = NOT isNull(arr)
auto is_arr_null_col = BuildAndExecuteFunction(is_null_resolver, {{arr_data_col, arr_data_type, "arr"}}, arr_elements_count);
filter_col = BuildAndExecuteFunction(
not_resolver, {{is_arr_null_col, std::make_shared<DataTypeUInt8>(), "is_null_res"}}, arr_elements_count);
}
else if (!canContainNull(*arr_data_type) && !canContainNull(*elem_type))
{
// Filter = notEquals(arr, replicated_elem_col)
filter_col = BuildAndExecuteFunction(not_equals_resolver,
{{arr_data_col, arr_data_type, "arr"},
{arguments[1].column->replicate(arr_offsets), elem_type, "elem"}}, arr_elements_count);
}
else
{
// Filter = NOT if(
// or(isNull(arr), isNull(elem)), // cond
// and(isNull(arr), isNull(elem)), // then (NULL == NULL)
// equals(arr, elem) // else (Value == Value)
// )
const auto & replicated_elem_col = arguments[1].column->replicate(arr_offsets);
auto is_arr_null_col = BuildAndExecuteFunction(is_null_resolver, {{arr_data_col, arr_data_type, "arr"}}, arr_elements_count);
auto is_elem_null_col = BuildAndExecuteFunction(is_null_resolver, {{replicated_elem_col, elem_type, "elem"}}, arr_elements_count);
auto is_arr_null_arg = ColumnWithTypeAndName{is_arr_null_col, std::make_shared<DataTypeUInt8>(), "is_arr_null"};
auto is_elem_null_arg = ColumnWithTypeAndName{is_elem_null_col, std::make_shared<DataTypeUInt8>(), "is_elem_null"};
// cond: or(isNull(arr), isNull(elem))
auto cond_col = BuildAndExecuteFunction(or_resolver, {is_arr_null_arg, is_elem_null_arg}, arr_elements_count);
// then: and(isNull(arr), isNull(elem))
auto then_col = BuildAndExecuteFunction(and_resolver, {is_arr_null_arg, is_elem_null_arg}, arr_elements_count);
auto equals_result = BuildAndExecuteFunction(
equals_resolver, {{arr_data_col, arr_data_type, "arr"}, {replicated_elem_col, elem_type, "elem"}}, arr_elements_count);
/// equals() on tuples with nullable components may return ColumnConst(Nullable(UInt8)).
/// convertToFullIfWrapped() unwraps ColumnConst so removeNullable can strip the Nullable layer.
auto else_col = removeNullable(equals_result->convertToFullIfWrapped()->convertToFullColumnIfLowCardinality());
/// equals() returns Nullable(Nothing) when comparison is impossible (a Variant operand
/// whose alternatives are all incompatible with the other argument, with
/// `variant_throw_on_type_mismatch` disabled). removeNullable then yields a ColumnNothing
/// declared below as UInt8; substitute a constant-zero filter (nothing equals elem).
/// Any other non-UInt8 result means equals() broke its contract.
if (else_col->getDataType() == TypeIndex::Nothing)
else_col = ColumnUInt8::create(arr_elements_count, UInt8(0));
else if (else_col->getDataType() != TypeIndex::UInt8)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Function equals for arrayRemove returned unexpected type {} instead of UInt8",
else_col->getDataType());
auto cond_arg = ColumnWithTypeAndName{cond_col, std::make_shared<DataTypeUInt8>(), "cond"};
auto then_arg = ColumnWithTypeAndName{then_col, std::make_shared<DataTypeUInt8>(), "then"};
auto else_arg = ColumnWithTypeAndName{else_col, std::make_shared<DataTypeUInt8>(), "else"};
auto equality_check_col = BuildAndExecuteFunction(if_resolver, {cond_arg, then_arg, else_arg}, arr_elements_count);
auto equality_check_arg = ColumnWithTypeAndName{equality_check_col, std::make_shared<DataTypeUInt8>(), "eq_check"};
filter_col = BuildAndExecuteFunction(not_resolver, {equality_check_arg}, arr_elements_count);
}
/// The filter can end up as ColumnConst or ColumnNullable when comparing tuples
/// with NULL components during constant folding. Normalize to a plain ColumnUInt8.
filter_col = filter_col->convertToFullIfWrapped()->convertToFullColumnIfLowCardinality();
if (const auto * nullable_filter = checkAndGetColumn<ColumnNullable>(filter_col.get()))
{
/// NULL in filter means comparison was indeterminate (e.g., tuple with NULL component).
/// Treat as "not equal" → keep the element (filter = 1).
auto result_col = ColumnUInt8::create(nullable_filter->size());
auto & result_data = result_col->getData();
const auto & nested_data = assert_cast<const ColumnUInt8 &>(nullable_filter->getNestedColumn()).getData();
const auto & null_map = nullable_filter->getNullMapData();
for (size_t i = 0; i < null_map.size(); ++i)
result_data[i] = null_map[i] ? 1 : nested_data[i];
filter_col = std::move(result_col);
}
const auto * filter_col_uint8 = checkAndGetColumn<ColumnUInt8>(filter_col.get());
if (!filter_col_uint8)
throw Exception(ErrorCodes::LOGICAL_ERROR,
"Filter column for arrayRemove was not evaluated as ColumnUInt8 but as {}", filter_col->getDataType());
const IColumn::Filter & filter = filter_col_uint8->getData();
ColumnPtr filtered = arr_col->getData().filter(filter, -1);
auto column_offsets = ColumnArray::ColumnOffsets::create(arr_offsets.size());
IColumn::Offsets & out_offsets = column_offsets->getData();
size_t in_pos = 0;
size_t out_pos = 0;
for (size_t i = 0; i < arr_offsets.size(); ++i)
{
size_t src_end = arr_offsets[i];
for (; in_pos < src_end; ++in_pos)
{
if (filter[in_pos])
{
++out_pos;
}
}
out_offsets[i] = out_pos;
}
return ColumnArray::create(filtered, std::move(column_offsets));
}
REGISTER_FUNCTION(ArrayRemove)
{
FunctionDocumentation::Description description = R"(
Removes all elements equal to a given value from an array.
NULLs are treated as equal.
)";
FunctionDocumentation::Syntax syntax = "arrayRemove(arr, elem)";
FunctionDocumentation::Examples examples = {
{"Example 1", "SELECT arrayRemove([1, 2, 2, 3], 2)", "[1,3]"},
{"Example 2", "SELECT arrayRemove(['a', NULL, 'b', NULL], NULL)", "['a','b']"}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns a subset of the source array", {"Array(T)"}};
FunctionDocumentation::IntroducedIn introduced_in = {25, 11};
FunctionDocumentation documentation = {
description, syntax,
{{"arr", "Array(T)"}, {"elem", "T"}},
{},
returned_value,
examples,
introduced_in,
FunctionDocumentation::Category::Array
};
factory.registerFunction<FunctionArrayRemove>(documentation);
factory.registerAlias("array_remove", "arrayRemove", FunctionFactory::Case::Insensitive);
}
}