2020-05-07 00:55:54 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/Regexps.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-05-07 23:31:15 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-05-07 00:55:54 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-05-07 00:55:54 +00:00
|
|
|
|
|
|
|
/** Match all groups of given input string with given re, return array of arrays of matches.
|
|
|
|
*
|
|
|
|
* SELECT extractGroups('hello abc=111 world', '("[^"]+"|\\w+)=("[^"]+"|\\w+)')
|
|
|
|
* should produce:
|
|
|
|
* ['abc', '111']
|
|
|
|
*/
|
|
|
|
class FunctionExtractGroups : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "extractGroups";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionExtractGroups>(); }
|
2020-05-07 00:55:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-05-07 00:55:54 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return false; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
FunctionArgumentDescriptors args{
|
2021-09-30 11:35:24 +00:00
|
|
|
{"haystack", &isStringOrFixedString<IDataType>, nullptr, "const String or const FixedString"},
|
|
|
|
{"needle", &isStringOrFixedString<IDataType>, isColumnConst, "const String or const FixedString"},
|
2020-05-07 00:55:54 +00:00
|
|
|
};
|
|
|
|
validateFunctionArgumentTypes(*this, arguments, args);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2020-05-07 00:55:54 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
const ColumnPtr column_haystack = arguments[0].column;
|
|
|
|
const ColumnPtr column_needle = arguments[1].column;
|
2020-05-07 00:55:54 +00:00
|
|
|
|
|
|
|
const auto needle = typeid_cast<const ColumnConst &>(*column_needle).getValue<String>();
|
|
|
|
|
|
|
|
if (needle.empty())
|
2020-06-09 09:47:59 +00:00
|
|
|
throw Exception(getName() + " length of 'needle' argument must be greater than 0.", ErrorCodes::BAD_ARGUMENTS);
|
2020-05-07 00:55:54 +00:00
|
|
|
|
2020-06-25 16:57:30 +00:00
|
|
|
auto regexp = Regexps::get<false, false>(needle);
|
2020-05-07 01:25:06 +00:00
|
|
|
const auto & re2 = regexp->getRE2();
|
2020-05-07 23:31:15 +00:00
|
|
|
|
|
|
|
if (!re2)
|
2021-04-04 09:33:06 +00:00
|
|
|
throw Exception("There are no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
|
2020-05-07 23:31:15 +00:00
|
|
|
|
2020-05-07 01:25:06 +00:00
|
|
|
const size_t groups_count = re2->NumberOfCapturingGroups();
|
2020-05-07 00:55:54 +00:00
|
|
|
|
2020-05-07 23:31:15 +00:00
|
|
|
if (!groups_count)
|
2021-04-04 09:33:06 +00:00
|
|
|
throw Exception("There are no groups in regexp: " + needle, ErrorCodes::BAD_ARGUMENTS);
|
2020-05-07 23:31:15 +00:00
|
|
|
|
2020-05-07 00:55:54 +00:00
|
|
|
// Including 0-group, which is the whole regexp.
|
2020-05-07 01:25:06 +00:00
|
|
|
PODArrayWithStackMemory<re2_st::StringPiece, 128> matched_groups(groups_count + 1);
|
2020-05-07 00:55:54 +00:00
|
|
|
|
|
|
|
ColumnArray::ColumnOffsets::MutablePtr offsets_col = ColumnArray::ColumnOffsets::create();
|
|
|
|
ColumnString::MutablePtr data_col = ColumnString::create();
|
|
|
|
|
|
|
|
auto & offsets_data = offsets_col->getData();
|
|
|
|
|
|
|
|
offsets_data.resize(input_rows_count);
|
|
|
|
ColumnArray::Offset current_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
2020-05-07 01:25:06 +00:00
|
|
|
StringRef current_row = column_haystack->getDataAt(i);
|
2020-05-07 00:55:54 +00:00
|
|
|
|
2020-05-07 01:25:06 +00:00
|
|
|
if (re2->Match(re2_st::StringPiece(current_row.data, current_row.size),
|
|
|
|
0, current_row.size, re2_st::RE2::UNANCHORED, matched_groups.data(), matched_groups.size()))
|
2020-05-07 00:55:54 +00:00
|
|
|
{
|
|
|
|
// 1 is to exclude group #0 which is whole re match.
|
|
|
|
for (size_t group = 1; group <= groups_count; ++group)
|
2020-05-07 01:25:06 +00:00
|
|
|
data_col->insertData(matched_groups[group].data(), matched_groups[group].size());
|
2020-05-07 00:55:54 +00:00
|
|
|
|
|
|
|
current_offset += groups_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets_data[i] = current_offset;
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:41:50 +00:00
|
|
|
return ColumnArray::create(std::move(data_col), std::move(offsets_col));
|
2020-05-07 00:55:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 00:55:54 +00:00
|
|
|
void registerFunctionExtractGroups(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionExtractGroups>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|