Simplify right(expr, length) function to be just an alias to substring(expr, -length)

This commit is contained in:
Ivan Blinkov 2018-12-14 14:42:49 +03:00
parent ebc0a165f4
commit 2279058f0d
2 changed files with 120 additions and 27 deletions

View File

@ -0,0 +1,117 @@
#include <Common/config.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/GatherUtils/Algorithms.h>
#include <IO/WriteHelpers.h>
#include <re2/re2.h>
#include <re2/stringpiece.h>
#if USE_RE2_ST
#include <re2_st/re2.h> // Y_IGNORE
#else
#define re2_st re2
#endif
namespace DB
{
using namespace GatherUtils;
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
class FunctionRegexpQuoteMeta : public IFunction
{
public:
static constexpr auto name = "regexpQuoteMeta";
static FunctionPtr create(const Context &)
{
return std::make_shared<FunctionRegexpQuoteMeta>();
}
String getName() const override
{
return name;
}
size_t getNumberOfArguments() const override
{
return 1;
}
bool useDefaultImplementationForConstants() const override
{
return true;
}
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (!WhichDataType(arguments[0].type).isString())
throw Exception(
"Illegal type " + arguments[0].type->getName() + " of 1 argument of function " + getName() + ". Must be String.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return std::make_shared<DataTypeString>();
}
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
{
const ColumnPtr column_string = block.getByPosition(arguments[0]).column;
const ColumnString * input = checkAndGetColumn<ColumnString>(column_string.get());
if (!input)
throw Exception(
"Illegal column " + block.getByPosition(arguments[0]).column->getName() + " of first argument of function " + getName(),
ErrorCodes::ILLEGAL_COLUMN);
auto dst_column = ColumnString::create();
auto & dst_data = dst_column->getChars();
auto & dst_offsets = dst_column->getOffsets();
dst_data.resize(input->getChars().size() * input->size());
dst_offsets.resize(input_rows_count);
const ColumnString::Offsets & src_offsets = input->getOffsets();
auto source = reinterpret_cast<const char *>(input->getChars().data());
auto dst = reinterpret_cast<char *>(dst_data.data());
auto dst_pos = dst;
size_t src_offset_prev = 0;
for (size_t row = 0; row < input_rows_count; ++row)
{
size_t srclen = src_offsets[row] - src_offset_prev - 1;
/// suboptimal, but uses original implementation from re2
re2_st::StringPiece unquoted(source, srclen);
const auto & quoted = re2_st::RE2::QuoteMeta(unquoted);
std::memcpy(dst_pos, quoted.data(), quoted.size());
source += srclen + 1;
dst_pos += quoted.size() + 1;
dst_offsets[row] = dst_pos - dst;
src_offset_prev = src_offsets[row];
}
dst_data.resize(dst_pos - dst);
block.getByPosition(result).column = std::move(dst_column);
}
};
void registerFunctionRegexpQuoteMeta(FunctionFactory & factory)
{
factory.registerFunction<FunctionRegexpQuoteMeta>();
}
}

View File

@ -590,7 +590,7 @@ bool ParserLeftExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expect
bool ParserRightExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
{
/// Rewrites RIGHT(expr, length) to substring(expr, greatest((length(expr) + 1) - length, 1))
/// Rewrites RIGHT(expr, length) to substring(expr, -length)
ASTPtr expr_node;
ASTPtr length_node;
@ -614,35 +614,11 @@ bool ParserRightExpression::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
return false;
++pos;
auto length_expr_list_args = std::make_shared<ASTExpressionList>();
length_expr_list_args->children = {expr_node};
auto length_func_node = std::make_shared<ASTFunction>();
length_func_node->name = "length";
length_func_node->arguments = std::move(length_expr_list_args);
length_func_node->children.push_back(length_func_node->arguments);
auto plus_expr_list_args = std::make_shared<ASTExpressionList>();
plus_expr_list_args->children = {length_func_node, std::make_shared<ASTLiteral>(1)};
auto plus_node = std::make_shared<ASTFunction>();
plus_node->name = "plus";
plus_node->arguments = std::move(plus_expr_list_args);
plus_node->children.push_back(plus_node->arguments);
auto minus_expr_list_args = std::make_shared<ASTExpressionList>();
minus_expr_list_args->children = {plus_node, length_node};
auto minus_node = std::make_shared<ASTFunction>();
minus_node->name = "minus";
minus_node->arguments = std::move(minus_expr_list_args);
minus_node->children.push_back(minus_node->arguments);
auto start_expr_list_args = std::make_shared<ASTExpressionList>();
start_expr_list_args->children = {minus_node, std::make_shared<ASTLiteral>(1)};
start_expr_list_args->children = {length_node};
auto start_node = std::make_shared<ASTFunction>();
start_node->name = "greatest";
start_node->name = "negate";
start_node->arguments = std::move(start_expr_list_args);
start_node->children.push_back(start_node->arguments);