forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtrim.cpp
More file actions
231 lines (198 loc) · 8.08 KB
/
Copy pathtrim.cpp
File metadata and controls
231 lines (198 loc) · 8.08 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
#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include "Columns/IColumn.h"
#include "Functions/IFunction.h"
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <base/find_symbols.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
namespace
{
template <typename Mode>
class FunctionTrim : public IFunction
{
public:
static constexpr auto name = Mode::name;
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionTrim<Mode>>(); }
String getName() const override { return name; }
bool isVariadic() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
bool useDefaultImplementationForConstants() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
FunctionArgumentDescriptors mandatory_args{
{"input_string", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isStringOrFixedString), nullptr, "String or FixedString"}
};
FunctionArgumentDescriptors optional_args{
{"trim_character", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isString), isColumnConst, "const String"}
};
validateFunctionArguments(*this, arguments, mandatory_args, optional_args);
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
std::optional<SearchSymbols> custom_trim_characters;
if (arguments.size() == 2 && input_rows_count > 0)
{
if (const ColumnString * col_trim_characters = checkAndGetColumn<ColumnString>(arguments[1].column.get()))
{
const String & trim_characters_string = col_trim_characters->getDataAt(0).toString();
custom_trim_characters = std::make_optional<SearchSymbols>(trim_characters_string);
}
else if (const ColumnConst * col_trim_characters_const = checkAndGetColumnConst<ColumnString>(arguments[1].column.get()))
{
const String & trim_characters_string = col_trim_characters_const->getDataAt(0).toString();
custom_trim_characters = std::make_optional<SearchSymbols>(trim_characters_string);
}
}
ColumnPtr col_input_full;
col_input_full = arguments[0].column->convertToFullColumnIfConst();
auto col_res = ColumnString::create();
if (const ColumnString * col_input_string = checkAndGetColumn<ColumnString>(col_input_full.get()))
{
vector(
col_input_string->getChars(), col_input_string->getOffsets(),
custom_trim_characters,
col_res->getChars(), col_res->getOffsets(),
input_rows_count);
}
else if (const ColumnFixedString * col_input_fixed_string = checkAndGetColumn<ColumnFixedString>(col_input_full.get()))
{
vectorFixed(
col_input_fixed_string->getChars(), col_input_fixed_string->getN(),
custom_trim_characters,
col_res->getChars(),
col_res->getOffsets(),
input_rows_count);
}
else
{
throw Exception(
ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", arguments[0].column->getName(), getName());
}
return col_res;
}
static void vector(
const ColumnString::Chars & input_data,
const ColumnString::Offsets & input_offsets,
const std::optional<SearchSymbols> & custom_trim_characters,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets,
size_t input_rows_count)
{
res_offsets.resize_exact(input_rows_count);
res_data.reserve_exact(input_data.size());
size_t prev_offset = 0;
size_t res_offset = 0;
const UInt8 * start;
size_t length;
for (size_t i = 0; i < input_rows_count; ++i)
{
execute(reinterpret_cast<const UInt8 *>(&input_data[prev_offset]), input_offsets[i] - prev_offset - 1, custom_trim_characters, start, length);
res_data.resize(res_data.size() + length + 1);
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], start, length);
res_offset += length + 1;
res_data[res_offset - 1] = '\0';
res_offsets[i] = res_offset;
prev_offset = input_offsets[i];
}
}
static void vectorFixed(
const ColumnString::Chars & input_data,
size_t n,
const std::optional<SearchSymbols> & custom_trim_characters,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets,
size_t input_rows_count)
{
res_offsets.resize_exact(input_rows_count);
res_data.reserve_exact(input_data.size());
size_t prev_offset = 0;
size_t res_offset = 0;
const UInt8 * start;
size_t length;
for (size_t i = 0; i < input_rows_count; ++i)
{
execute(reinterpret_cast<const UInt8 *>(&input_data[prev_offset]), n, custom_trim_characters, start, length);
res_data.resize(res_data.size() + length + 1);
memcpySmallAllowReadWriteOverflow15(&res_data[res_offset], start, length);
res_offset += length + 1;
res_data[res_offset - 1] = '\0';
res_offsets[i] = res_offset;
prev_offset += n;
}
}
static void execute(const UInt8 * data, size_t size, const std::optional<SearchSymbols> & custom_trim_characters, const UInt8 *& res_data, size_t & res_size)
{
const char * char_begin = reinterpret_cast<const char *>(data);
const char * char_end = char_begin + size;
if constexpr (Mode::trim_left)
{
const char * found = nullptr;
if (!custom_trim_characters)
found = find_first_not_symbols<' '>(char_begin, char_end);
else
{
std::string_view input(char_begin, char_end);
found = find_first_not_symbols(input, *custom_trim_characters);
}
size_t num_chars = found - char_begin;
char_begin += num_chars;
}
if constexpr (Mode::trim_right)
{
const char * found = nullptr;
if (!custom_trim_characters)
found = find_last_not_symbols_or_null<' '>(char_begin, char_end);
else
{
std::string_view input(char_begin, char_end);
found = find_last_not_symbols_or_null(input, *custom_trim_characters);
}
if (found)
char_end = found + 1;
else
char_end = char_begin;
}
res_data = reinterpret_cast<const UInt8 *>(char_begin);
res_size = char_end - char_begin;
}
};
struct TrimModeLeft
{
static constexpr auto name = "trimLeft";
static constexpr bool trim_left = true;
static constexpr bool trim_right = false;
};
struct TrimModeRight
{
static constexpr auto name = "trimRight";
static constexpr bool trim_left = false;
static constexpr bool trim_right = true;
};
struct TrimModeBoth
{
static constexpr auto name = "trimBoth";
static constexpr bool trim_left = true;
static constexpr bool trim_right = true;
};
using FunctionTrimLeft = FunctionTrim<TrimModeLeft>;
using FunctionTrimRight = FunctionTrim<TrimModeRight>;
using FunctionTrimBoth = FunctionTrim<TrimModeBoth>;
}
REGISTER_FUNCTION(Trim)
{
factory.registerFunction<FunctionTrimLeft>();
factory.registerFunction<FunctionTrimRight>();
factory.registerFunction<FunctionTrimBoth>();
factory.registerAlias("ltrim", FunctionTrimLeft::name);
factory.registerAlias("rtrim", FunctionTrimRight::name);
factory.registerAlias("trim", FunctionTrimBoth::name);
}
}