2018-09-09 23:36:06 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <Functions/GatherUtils/GatherUtils.h>
|
2018-09-09 23:47:56 +00:00
|
|
|
#include <Functions/GatherUtils/Sources.h>
|
|
|
|
#include <Functions/GatherUtils/Sinks.h>
|
|
|
|
#include <Functions/GatherUtils/Slices.h>
|
2018-09-10 00:51:08 +00:00
|
|
|
#include <Functions/GatherUtils/Algorithms.h>
|
2018-09-09 23:36:06 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
using namespace GatherUtils;
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int ZERO_ARRAY_OR_TUPLE_INDEX;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-09 23:36:06 +00:00
|
|
|
|
2019-04-12 00:34:11 +00:00
|
|
|
/// If 'is_utf8' - measure offset and length in code points instead of bytes.
|
|
|
|
/// UTF8 variant is not available for FixedString arguments.
|
|
|
|
template <bool is_utf8>
|
2018-09-09 23:36:06 +00:00
|
|
|
class FunctionSubstring : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2019-04-12 00:34:11 +00:00
|
|
|
static constexpr auto name = is_utf8 ? "substringUTF8" : "substring";
|
2021-05-28 16:44:59 +00:00
|
|
|
static FunctionPtr create(ContextConstPtr)
|
2018-09-09 23:36:06 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionSubstring>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
size_t number_of_arguments = arguments.size();
|
|
|
|
|
|
|
|
if (number_of_arguments < 2 || number_of_arguments > 3)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(number_of_arguments) + ", should be 2 or 3",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-04-12 00:34:11 +00:00
|
|
|
if ((is_utf8 && !isString(arguments[0])) || !isStringOrFixedString(arguments[0]))
|
2018-09-09 23:36:06 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2019-05-24 12:11:03 +00:00
|
|
|
if (!isNativeNumber(arguments[1]))
|
2018-09-09 23:36:06 +00:00
|
|
|
throw Exception("Illegal type " + arguments[1]->getName()
|
|
|
|
+ " of second argument of function "
|
|
|
|
+ getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2019-05-24 12:11:03 +00:00
|
|
|
if (number_of_arguments == 3 && !isNativeNumber(arguments[2]))
|
2018-09-09 23:36:06 +00:00
|
|
|
throw Exception("Illegal type " + arguments[2]->getName()
|
|
|
|
+ " of second argument of function "
|
|
|
|
+ getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Source>
|
2020-10-19 15:27:41 +00:00
|
|
|
ColumnPtr executeForSource(const ColumnPtr & column_start, const ColumnPtr & column_length,
|
2020-10-14 13:09:11 +00:00
|
|
|
const ColumnConst * column_start_const, const ColumnConst * column_length_const,
|
2020-10-19 15:27:41 +00:00
|
|
|
Int64 start_value, Int64 length_value, Source && source,
|
2020-10-14 13:09:11 +00:00
|
|
|
size_t input_rows_count) const
|
2018-09-09 23:36:06 +00:00
|
|
|
{
|
2019-04-12 00:37:07 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2018-09-09 23:36:06 +00:00
|
|
|
|
|
|
|
if (!column_length)
|
|
|
|
{
|
|
|
|
if (column_start_const)
|
|
|
|
{
|
|
|
|
if (start_value > 0)
|
2021-01-22 20:40:04 +00:00
|
|
|
sliceFromLeftConstantOffsetUnbounded(
|
|
|
|
source, StringSink(*col_res, input_rows_count), static_cast<size_t>(start_value - 1));
|
2018-09-09 23:36:06 +00:00
|
|
|
else if (start_value < 0)
|
2021-01-22 20:40:04 +00:00
|
|
|
sliceFromRightConstantOffsetUnbounded(
|
|
|
|
source, StringSink(*col_res, input_rows_count), -static_cast<size_t>(start_value));
|
2018-09-09 23:36:06 +00:00
|
|
|
else
|
|
|
|
throw Exception("Indices in strings are 1-based", ErrorCodes::ZERO_ARRAY_OR_TUPLE_INDEX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sliceDynamicOffsetUnbounded(source, StringSink(*col_res, input_rows_count), *column_start);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (column_start_const && column_length_const)
|
|
|
|
{
|
|
|
|
if (start_value > 0)
|
2021-01-22 20:40:04 +00:00
|
|
|
sliceFromLeftConstantOffsetBounded(
|
|
|
|
source, StringSink(*col_res, input_rows_count), static_cast<size_t>(start_value - 1), length_value);
|
2018-09-09 23:36:06 +00:00
|
|
|
else if (start_value < 0)
|
2021-01-22 20:40:04 +00:00
|
|
|
sliceFromRightConstantOffsetBounded(
|
|
|
|
source, StringSink(*col_res, input_rows_count), -static_cast<size_t>(start_value), length_value);
|
2018-09-09 23:36:06 +00:00
|
|
|
else
|
|
|
|
throw Exception("Indices in strings are 1-based", ErrorCodes::ZERO_ARRAY_OR_TUPLE_INDEX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sliceDynamicOffsetBounded(source, StringSink(*col_res, input_rows_count), *column_start, *column_length);
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
return col_res;
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2018-09-09 23:36:06 +00:00
|
|
|
{
|
|
|
|
size_t number_of_arguments = arguments.size();
|
|
|
|
|
2020-10-19 15:27:41 +00:00
|
|
|
ColumnPtr column_string = arguments[0].column;
|
|
|
|
ColumnPtr column_start = arguments[1].column;
|
2018-09-09 23:36:06 +00:00
|
|
|
ColumnPtr column_length;
|
|
|
|
|
|
|
|
if (number_of_arguments == 3)
|
2020-10-19 15:27:41 +00:00
|
|
|
column_length = arguments[2].column;
|
2018-09-09 23:36:06 +00:00
|
|
|
|
|
|
|
const ColumnConst * column_start_const = checkAndGetColumn<ColumnConst>(column_start.get());
|
|
|
|
const ColumnConst * column_length_const = nullptr;
|
|
|
|
|
|
|
|
if (number_of_arguments == 3)
|
|
|
|
column_length_const = checkAndGetColumn<ColumnConst>(column_length.get());
|
|
|
|
|
|
|
|
Int64 start_value = 0;
|
|
|
|
Int64 length_value = 0;
|
|
|
|
|
|
|
|
if (column_start_const)
|
|
|
|
start_value = column_start_const->getInt(0);
|
|
|
|
if (column_length_const)
|
|
|
|
length_value = column_length_const->getInt(0);
|
|
|
|
|
2019-04-12 00:34:11 +00:00
|
|
|
if constexpr (is_utf8)
|
|
|
|
{
|
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, UTF8StringSource(*col), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else if (const ColumnConst * col_const = checkAndGetColumnConst<ColumnString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, ConstSource<UTF8StringSource>(*col_const), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else
|
|
|
|
throw Exception(
|
2020-10-19 15:27:41 +00:00
|
|
|
"Illegal column " + arguments[0].column->getName() + " of first argument of function " + getName(),
|
2019-04-12 00:34:11 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2018-09-09 23:36:06 +00:00
|
|
|
else
|
2019-04-12 00:34:11 +00:00
|
|
|
{
|
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, StringSource(*col), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else if (const ColumnFixedString * col_fixed = checkAndGetColumn<ColumnFixedString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, FixedStringSource(*col_fixed), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else if (const ColumnConst * col_const = checkAndGetColumnConst<ColumnString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, ConstSource<StringSource>(*col_const), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else if (const ColumnConst * col_const_fixed = checkAndGetColumnConst<ColumnFixedString>(column_string.get()))
|
2020-10-19 15:27:41 +00:00
|
|
|
return executeForSource(column_start, column_length, column_start_const, column_length_const, start_value,
|
|
|
|
length_value, ConstSource<FixedStringSource>(*col_const_fixed), input_rows_count);
|
2019-04-12 00:34:11 +00:00
|
|
|
else
|
|
|
|
throw Exception(
|
2020-10-19 15:27:41 +00:00
|
|
|
"Illegal column " + arguments[0].column->getName() + " of first argument of function " + getName(),
|
2019-04-12 00:34:11 +00:00
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 23:36:06 +00:00
|
|
|
void registerFunctionSubstring(FunctionFactory & factory)
|
|
|
|
{
|
2019-04-12 00:34:11 +00:00
|
|
|
factory.registerFunction<FunctionSubstring<false>>(FunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerAlias("substr", "substring", FunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerAlias("mid", "substring", FunctionFactory::CaseInsensitive); /// from MySQL dialect
|
|
|
|
|
|
|
|
factory.registerFunction<FunctionSubstring<true>>(FunctionFactory::CaseSensitive);
|
2018-09-09 23:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|