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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/UTF8Helpers.h>
#include <Common/HashTable/HashMap.h>
#include <numeric>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int BAD_ARGUMENTS;
}
struct TranslateImpl
{
using Map = std::array<UInt8, 128>;
static void fillMapWithValues(
Map & map,
const std::string & map_from,
const std::string & map_to)
{
if (map_from.size() != map_to.size())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second and third arguments must be the same length");
std::iota(map.begin(), map.end(), 0);
for (size_t i = 0; i < map_from.size(); ++i)
{
if (!isASCII(map_from[i]) || !isASCII(map_to[i]))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second and third arguments must be ASCII strings");
map[map_from[i]] = map_to[i];
}
}
static void vector(
const ColumnString::Chars & data,
const ColumnString::Offsets & offsets,
const std::string & map_from,
const std::string & map_to,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets)
{
Map map;
fillMapWithValues(map, map_from, map_to);
res_data.resize(data.size());
res_offsets.assign(offsets);
UInt8 * dst = res_data.data();
for (UInt64 i = 0; i < offsets.size(); ++i)
{
const UInt8 * src = data.data() + offsets[i - 1];
const UInt8 * src_end = data.data() + offsets[i] - 1;
while (src < src_end)
{
if (*src <= ascii_upper_bound)
*dst = map[*src];
else
*dst = *src;
++src;
++dst;
}
/// Technically '\0' can be mapped into other character,
/// so we need to process '\0' delimiter separately
*dst++ = 0;
}
}
static void vectorFixed(
const ColumnString::Chars & data,
size_t /*n*/,
const std::string & map_from,
const std::string & map_to,
ColumnString::Chars & res_data)
{
std::array<UInt8, 128> map;
fillMapWithValues(map, map_from, map_to);
res_data.resize(data.size());
const UInt8 * src = data.data();
const UInt8 * src_end = data.data() + data.size();
UInt8 * dst = res_data.data();
while (src < src_end)
{
if (*src <= ascii_upper_bound)
*dst = map[*src];
else
*dst = *src;
++src;
++dst;
}
}
private:
static constexpr auto ascii_upper_bound = '\x7f';
};
struct TranslateUTF8Impl
{
using MapASCII = std::array<UInt32, 128>;
using MapUTF8 = HashMap<UInt32, UInt32, HashCRC32<UInt32>>;
static void fillMapWithValues(
MapASCII & map_ascii,
MapUTF8 & map,
const std::string & map_from,
const std::string & map_to)
{
auto map_from_size = UTF8::countCodePoints(reinterpret_cast<const UInt8 *>(map_from.data()), map_from.size());
auto map_to_size = UTF8::countCodePoints(reinterpret_cast<const UInt8 *>(map_to.data()), map_to.size());
if (map_from_size != map_to_size)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second and third arguments must be the same length");
std::iota(map_ascii.begin(), map_ascii.end(), 0);
const UInt8 * map_from_ptr = reinterpret_cast<const UInt8 *>(map_from.data());
const UInt8 * map_from_end = map_from_ptr + map_from.size();
const UInt8 * map_to_ptr = reinterpret_cast<const UInt8 *>(map_to.data());
const UInt8 * map_to_end = map_to_ptr + map_to.size();
while (map_from_ptr < map_from_end && map_to_ptr < map_to_end)
{
size_t len_from = UTF8::seqLength(*map_from_ptr);
size_t len_to = UTF8::seqLength(*map_to_ptr);
std::optional<UInt32> res_from, res_to;
if (map_from_ptr + len_from <= map_from_end)
res_from = UTF8::convertUTF8ToCodePoint(map_from_ptr, len_from);
if (map_to_ptr + len_to <= map_to_end)
res_to = UTF8::convertUTF8ToCodePoint(map_to_ptr, len_to);
if (!res_from)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second argument must be a valid UTF-8 string");
if (!res_to)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Third argument must be a valid UTF-8 string");
if (*map_from_ptr <= ascii_upper_bound)
map_ascii[*map_from_ptr] = *res_to;
else
map[*res_from] = *res_to;
map_from_ptr += len_from;
map_to_ptr += len_to;
}
}
static void vector(
const ColumnString::Chars & data,
const ColumnString::Offsets & offsets,
const std::string & map_from,
const std::string & map_to,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets)
{
MapASCII map_ascii;
MapUTF8 map;
fillMapWithValues(map_ascii, map, map_from, map_to);
res_data.resize(data.size());
res_offsets.resize(offsets.size());
UInt8 * dst = res_data.data();
UInt64 data_size = 0;
for (UInt64 i = 0; i < offsets.size(); ++i)
{
const UInt8 * src = data.data() + offsets[i - 1];
const UInt8 * src_end = data.data() + offsets[i] - 1;
while (src < src_end)
{
/// Maximum length of UTF-8 sequence is 4 bytes + 1 zero byte
if (data_size + 5 > res_data.size())
{
res_data.resize(data_size * 2 + 5);
dst = res_data.data() + data_size;
}
if (*src <= ascii_upper_bound)
{
size_t dst_len = UTF8::convertCodePointToUTF8(map_ascii[*src], dst, 4);
assert(0 < dst_len && dst_len <= 4);
src += 1;
dst += dst_len;
data_size += dst_len;
continue;
}
size_t src_len = UTF8::seqLength(*src);
assert(0 < src_len && src_len <= 4);
if (src + src_len <= src_end)
{
auto src_code_point = UTF8::convertUTF8ToCodePoint(src, src_len);
if (src_code_point)
{
auto * it = map.find(*src_code_point);
if (it != map.end())
{
size_t dst_len = UTF8::convertCodePointToUTF8(it->getMapped(), dst, 4);
assert(0 < dst_len && dst_len <= 4);
src += src_len;
dst += dst_len;
data_size += dst_len;
continue;
}
}
}
else
{
src_len = src_end - src;
}
memcpy(dst, src, src_len);
dst += src_len;
src += src_len;
data_size += src_len;
}
/// Technically '\0' can be mapped into other character,
/// so we need to process '\0' delimiter separately
*dst++ = 0;
++data_size;
res_offsets[i] = data_size;
}
res_data.resize(data_size);
}
[[noreturn]] static void vectorFixed(
const ColumnString::Chars & /*data*/,
size_t /*n*/,
const std::string & /*map_from*/,
const std::string & /*map_to*/,
ColumnString::Chars & /*res_data*/)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Function translateUTF8 does not support FixedString argument");
}
private:
static constexpr auto ascii_upper_bound = '\x7f';
};
template <typename Impl, typename Name>
class FunctionTranslate : public IFunction
{
public:
static constexpr auto name = Name::name;
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionTranslate>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 3; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!isStringOrFixedString(arguments[0]))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of first argument of function {}",
arguments[0]->getName(), getName());
if (!isStringOrFixedString(arguments[1]))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of second argument of function {}",
arguments[1]->getName(), getName());
if (!isStringOrFixedString(arguments[2]))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of third argument of function {}",
arguments[2]->getName(), getName());
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
{
const ColumnPtr column_src = arguments[0].column;
const ColumnPtr column_map_from = arguments[1].column;
const ColumnPtr column_map_to = arguments[2].column;
if (!isColumnConst(*column_map_from) || !isColumnConst(*column_map_to))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "2nd and 3rd arguments of function {} must be constants.", getName());
const IColumn * c1 = arguments[1].column.get();
const IColumn * c2 = arguments[2].column.get();
const ColumnConst * c1_const = typeid_cast<const ColumnConst *>(c1);
const ColumnConst * c2_const = typeid_cast<const ColumnConst *>(c2);
String map_from = c1_const->getValue<String>();
String map_to = c2_const->getValue<String>();
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vector(col->getChars(), col->getOffsets(), map_from, map_to, col_res->getChars(), col_res->getOffsets());
return col_res;
}
else if (const ColumnFixedString * col_fixed = checkAndGetColumn<ColumnFixedString>(column_src.get()))
{
auto col_res = ColumnFixedString::create(col_fixed->getN());
Impl::vectorFixed(col_fixed->getChars(), col_fixed->getN(), map_from, map_to, col_res->getChars());
return col_res;
}
else
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}",
arguments[0].column->getName(), getName());
}
};
namespace
{
struct NameTranslate
{
static constexpr auto name = "translate";
};
struct NameTranslateUTF8
{
static constexpr auto name = "translateUTF8";
};
using FunctionTranslateASCII = FunctionTranslate<TranslateImpl, NameTranslate>;
using FunctionTranslateUTF8 = FunctionTranslate<TranslateUTF8Impl, NameTranslateUTF8>;
}
REGISTER_FUNCTION(Translate)
{
factory.registerFunction<FunctionTranslateASCII>();
factory.registerFunction<FunctionTranslateUTF8>();
}
}
|