-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathFunctionsStringSearch.h
More file actions
474 lines (413 loc) · 18.6 KB
/
FunctionsStringSearch.h
File metadata and controls
474 lines (413 loc) · 18.6 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Modified from: https://github.com/ClickHouse/ClickHouse/blob/30fcaeb2a3fff1bf894aae9c776bed7fd83f783f/dbms/src/Functions/FunctionsStringSearch.h
//
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <Columns/IColumn.h>
#include <Common/Exception.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/UTF8Helpers.h>
#include <Common/typeid_cast.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <Functions/StringUtil.h>
#include <common/defines.h>
#include <cstring>
#include <string_view>
namespace DB
{
using Chars_t = ColumnString::Chars_t;
using Offsets = ColumnString::Offsets;
class IlikeLowerHelper
{
public:
static void convertCollatorToBin(TiDB::TiDBCollatorPtr & collator)
{
if (collator == nullptr)
return;
switch (collator->getCollatorType())
{
case TiDB::ITiDBCollator::CollatorType::UTF8_GENERAL_CI:
case TiDB::ITiDBCollator::CollatorType::UTF8_UNICODE_CI:
collator = TiDB::ITiDBCollator::getCollator(TiDB::ITiDBCollator::UTF8_BIN);
break;
case TiDB::ITiDBCollator::CollatorType::UTF8MB4_GENERAL_CI:
case TiDB::ITiDBCollator::CollatorType::UTF8MB4_UNICODE_CI:
collator = TiDB::ITiDBCollator::getCollator(TiDB::ITiDBCollator::UTF8MB4_BIN);
break;
case TiDB::ITiDBCollator::CollatorType::UTF8MB4_0900_AI_CI:
collator = TiDB::ITiDBCollator::getCollator(TiDB::ITiDBCollator::UTF8MB4_0900_BIN);
break;
case TiDB::ITiDBCollator::CollatorType::LATIN1_SWEDISH_CI:
collator = TiDB::ITiDBCollator::getCollator(TiDB::ITiDBCollator::LATIN1_BIN);
break;
default:
break;
}
}
// Only lower 'A', 'B', 'C'... 'Z', excluding the escape char
static void lowerAlphaASCII(Block & block, const ColumnNumbers & arguments)
{
MutableColumnPtr column_expr = block.getByPosition(arguments[0]).column->assumeMutable();
MutableColumnPtr column_pat = block.getByPosition(arguments[1]).column->assumeMutable();
const ColumnPtr & column_escape = block.getByPosition(arguments[2]).column;
auto * col_haystack_const = typeid_cast<ColumnConst *>(&*column_expr);
auto * col_needle_const = typeid_cast<ColumnConst *>(&*column_pat);
if (col_haystack_const != nullptr)
lowerColumnConst(col_haystack_const, nullptr);
else
lowerColumnString(column_expr, nullptr);
if (col_needle_const != nullptr)
lowerColumnConst(col_needle_const, column_escape);
else
lowerColumnString(column_pat, column_escape);
}
private:
static void lowerStrings(Chars_t & chars)
{
size_t size = chars.size();
for (size_t i = 0; i < size; ++i)
{
if (isUpperAlphaASCII(chars[i]))
{
chars[i] = toLowerIfAlphaASCII(chars[i]);
}
else
{
size_t utf8_len = UTF8::seqLength(chars[i]);
i += utf8_len - 1;
}
}
}
// When escape_char is a lower char, we need to convert it to the capital char
// Because: when lowering "ABC" with escape 'a', after lower, "ABC" -> "abc",
// then 'a' will be an escape char and it is not expected.
// Morever, when escape char is uppered we need to tell it to the caller.
static void lowerStringsExcludeEscapeChar(Chars_t & chars, char escape_char)
{
if (!isAlphaASCII(escape_char))
{
lowerStrings(chars);
return;
}
size_t size = chars.size();
bool escaped = false;
char actual_escape_char = isLowerAplhaASCII(escape_char) ? toUpperIfAlphaASCII(escape_char) : escape_char;
for (size_t i = 0; i < size; ++i)
{
char char_to_lower = chars[i];
if (isUpperAlphaASCII(char_to_lower))
{
// Do not lower the escape char, however when a char is equal to
// an escape char and it's after an escape char, we still lower it
// For example: "AA" (escape 'A'), -> "Aa"
if (char_to_lower != escape_char || escaped)
{
chars[i] = toLowerIfAlphaASCII(char_to_lower);
}
else
{
escaped = true;
continue;
}
}
else
{
if ((chars[i] == static_cast<unsigned char>(escape_char)) && !escaped)
{
escaped = true;
// It should be `chars[i] = toUpperIfAlphaASCII(chars[i])`,
// but 'actual_escape_char' is always equal to 'toUpperIfAlphaASCII(str[i])'
chars[i] = actual_escape_char;
continue;
}
size_t utf8_len = UTF8::seqLength(char_to_lower);
i += utf8_len - 1;
}
escaped = false;
}
}
static void lowerColumnConst(ColumnConst * lowered_col_const, const ColumnPtr & column_escape)
{
auto * col_data = typeid_cast<ColumnString *>(&lowered_col_const->getDataColumn());
RUNTIME_ASSERT(col_data != nullptr, "Invalid column type, should be ColumnString");
lowerColumnStringImpl(col_data, column_escape);
}
static void lowerColumnString(MutableColumnPtr & col, const ColumnPtr & column_escape)
{
auto * col_vector = typeid_cast<ColumnString *>(&*col);
RUNTIME_ASSERT(col_vector != nullptr, "Invalid column type, should be ColumnString");
lowerColumnStringImpl(col_vector, column_escape);
}
static void lowerColumnStringImpl(ColumnString * lowered_col_data, const ColumnPtr & column_escape)
{
if (column_escape == nullptr)
{
lowerStrings(lowered_col_data->getChars());
return;
}
const auto * col_escape_const = typeid_cast<const ColumnConst *>(&*column_escape);
RUNTIME_CHECK_MSG(col_escape_const != nullptr, "escape char column should be constant");
char escape_char = static_cast<Int32>(col_escape_const->getValue<Int32>());
lowerStringsExcludeEscapeChar(lowered_col_data->getChars(), escape_char);
}
};
/** Search and replace functions in strings:
*
* position(haystack, needle) - the normal search for a substring in a string, returns the position (in bytes) of the found substring starting with 1, or 0 if no substring is found.
* positionUTF8(haystack, needle) - the same, but the position is calculated at code points, provided that the string is encoded in UTF-8.
* positionCaseInsensitive(haystack, needle)
* positionCaseInsensitiveUTF8(haystack, needle)
*
* like(haystack, pattern) - search by the regular expression LIKE; Returns 0 or 1. Case-insensitive, but only for Latin.
* notLike(haystack, pattern)
*
* match(haystack, pattern) - search by regular expression re2; Returns 0 or 1.
*
* Applies regexp re2 and pulls:
* - the first subpattern, if the regexp has a subpattern;
* - the zero subpattern (the match part, otherwise);
* - if not match - an empty string.
* extract(haystack, pattern)
*
* replaceOne(haystack, pattern, replacement) - replacing the pattern with the specified rules, only the first occurrence.
* replaceAll(haystack, pattern, replacement) - replacing the pattern with the specified rules, all occurrences.
*
* replaceRegexpOne(haystack, pattern, replacement) - replaces the pattern with the specified regexp, only the first occurrence.
* replaceRegexpAll(haystack, pattern, replacement) - replaces the pattern with the specified type, all occurrences.
*
* Warning! At this point, the arguments needle, pattern, n, replacement must be constants.
*/
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
static const UInt8 CH_ESCAPE_CHAR = '\\';
struct NameIlike3Args
{
static constexpr auto name = "ilike3Args";
};
template <typename Impl, typename Name>
class FunctionsStringSearch : public IFunction
{
static_assert(!(Impl::need_customized_escape_char && Impl::support_match_type));
public:
static constexpr auto name = Name::name;
static FunctionPtr create(const Context &) { return std::make_shared<FunctionsStringSearch>(); }
String getName() const override { return name; }
void setCollator(const TiDB::TiDBCollatorPtr & collator_) override { collator = collator_; }
size_t getNumberOfArguments() const override { return 0; }
bool isVariadic() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {3}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!arguments[0]->isString())
throw Exception(
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!arguments[1]->isString())
throw Exception(
"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if constexpr (Impl::need_customized_escape_char)
{
if (!arguments[2]->isInteger())
throw Exception(
"Illegal type " + arguments[2]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
else if constexpr (Impl::support_match_type)
{
if (arguments.size() > 2 && !arguments[2]->isString())
throw Exception(
"Illegal type " + arguments[2]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
size_t max_arguments_number = Impl::need_customized_escape_char || Impl::support_match_type ? 3 : 2;
if (arguments.size() > max_arguments_number)
throw Exception(
"Too many arguments, only " + std::to_string(max_arguments_number)
+ " argument is supported for function " + getName());
return std::make_shared<DataTypeNumber<typename Impl::ResultType>>();
}
void executeImpl(Block & result_block, const ColumnNumbers & arguments, size_t result) const override
{
auto block = result_block;
if constexpr (name == std::string_view(NameIlike3Args::name))
{
block.getByPosition(arguments[0]).column
= (*std::move(result_block.getByPosition(arguments[0]).column)).mutate();
block.getByPosition(arguments[1]).column
= (*std::move(result_block.getByPosition(arguments[1]).column)).mutate();
IlikeLowerHelper::lowerAlphaASCII(block, arguments);
IlikeLowerHelper::convertCollatorToBin(collator);
}
using ResultType = typename Impl::ResultType;
const ColumnPtr & column_haystack = block.getByPosition(arguments[0]).column;
const ColumnPtr & column_needle = block.getByPosition(arguments[1]).column;
const auto * col_haystack_const = typeid_cast<const ColumnConst *>(&*column_haystack);
const auto * col_needle_const = typeid_cast<const ColumnConst *>(&*column_needle);
UInt8 escape_char = CH_ESCAPE_CHAR;
String match_type;
if constexpr (Impl::need_customized_escape_char)
{
const auto * col_escape_const
= typeid_cast<const ColumnConst *>(&*block.getByPosition(arguments[2]).column);
bool valid_args = true;
if (col_escape_const == nullptr)
{
valid_args = false;
}
else
{
auto c = col_escape_const->getValue<Int32>();
if (c < 0 || c > 255)
{
// todo maybe use more strict constraint
valid_args = false;
}
else
{
escape_char = static_cast<UInt8>(c);
if constexpr (name == std::string_view(NameIlike3Args::name))
if (isLowerAplhaASCII(escape_char))
escape_char = toUpperIfAlphaASCII(escape_char);
}
}
if (!valid_args)
{
throw Exception("3rd arguments of function " + getName() + " must be constants and between 0 and 255.");
}
}
else if constexpr (Impl::support_match_type)
{
if (arguments.size() > 2)
{
const auto * col_match_type_const
= typeid_cast<const ColumnConst *>(&*block.getByPosition(arguments[2]).column);
if (col_match_type_const == nullptr)
throw Exception("Match type argument of function " + getName() + " must be constant");
match_type = col_match_type_const->getValue<String>();
}
}
if (col_haystack_const && col_needle_const)
{
ResultType res{};
auto needle_string = col_needle_const->getValue<String>();
Impl::constantConstant(
col_haystack_const->getValue<String>(),
needle_string,
escape_char,
match_type,
collator,
res);
result_block.getByPosition(result).column
= result_block.getByPosition(result).type->createColumnConst(col_haystack_const->size(), toField(res));
return;
}
auto col_res = ColumnVector<ResultType>::create();
typename ColumnVector<ResultType>::Container & vec_res = col_res->getData();
vec_res.resize(column_haystack->size());
const auto * col_haystack_vector = checkAndGetColumn<ColumnString>(&*column_haystack);
const auto * col_needle_vector = checkAndGetColumn<ColumnString>(&*column_needle);
if (col_haystack_vector && col_needle_vector)
Impl::vectorVector(
col_haystack_vector->getChars(),
col_haystack_vector->getOffsets(),
col_needle_vector->getChars(),
col_needle_vector->getOffsets(),
escape_char,
match_type,
collator,
vec_res);
else if (col_haystack_vector && col_needle_const)
{
auto needle_string = col_needle_const->getValue<String>();
Impl::vectorConstant(
col_haystack_vector->getChars(),
col_haystack_vector->getOffsets(),
needle_string,
escape_char,
match_type,
collator,
vec_res);
}
else if (col_haystack_const && col_needle_vector)
{
auto haystack = col_haystack_const->getValue<String>();
const ColumnString::Chars_t & needle_chars = col_needle_vector->getChars();
const IColumn::Offsets & needle_offsets = col_needle_vector->getOffsets();
Impl::constantVector(haystack, needle_chars, needle_offsets, escape_char, match_type, collator, vec_res);
}
else
throw Exception(
"Illegal columns " + block.getByPosition(arguments[0]).column->getName() + " and "
+ block.getByPosition(arguments[1]).column->getName() + " of arguments of function " + getName(),
ErrorCodes::ILLEGAL_COLUMN);
result_block.getByPosition(result).column = std::move(col_res);
}
private:
mutable TiDB::TiDBCollatorPtr collator = nullptr;
};
template <typename Impl, typename Name>
class FunctionsStringSearchToString : public IFunction
{
public:
static constexpr auto name = Name::name;
static FunctionPtr create(const Context &) { return std::make_shared<FunctionsStringSearchToString>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 2; }
bool useDefaultImplementationForConstants() const override { return true; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
if (!arguments[0]->isString())
throw Exception(
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if (!arguments[1]->isString())
throw Exception(
"Illegal type " + arguments[1]->getName() + " of argument of function " + getName(),
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) const override
{
const ColumnPtr column = block.getByPosition(arguments[0]).column;
const ColumnPtr column_needle = block.getByPosition(arguments[1]).column;
const auto * col_needle = typeid_cast<const ColumnConst *>(&*column_needle);
if (!col_needle)
throw Exception(
"Second argument of function " + getName() + " must be constant string.",
ErrorCodes::ILLEGAL_COLUMN);
if (const auto * col = checkAndGetColumn<ColumnString>(column.get()))
{
auto col_res = ColumnString::create();
ColumnString::Chars_t & vec_res = col_res->getChars();
ColumnString::Offsets & offsets_res = col_res->getOffsets();
Impl::vector(col->getChars(), col->getOffsets(), col_needle->getValue<String>(), vec_res, offsets_res);
block.getByPosition(result).column = std::move(col_res);
}
else
throw Exception(
"Illegal column " + block.getByPosition(arguments[0]).column->getName() + " of argument of function "
+ getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
};
} // namespace DB