ClickHouse/src/Functions/formatString.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

144 lines
4.5 KiB
C++
Raw Normal View History

2019-05-18 12:29:10 +00:00
#include <Columns/ColumnFixedString.h>
2019-05-18 13:21:48 +00:00
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
2019-05-18 11:30:36 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2019-05-18 11:30:36 +00:00
#include <IO/WriteHelpers.h>
2021-10-02 07:13:14 +00:00
#include <base/range.h>
2019-05-18 11:30:36 +00:00
#include <memory>
#include <string>
#include <vector>
#include "formatString.h"
2019-05-18 11:30:36 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2020-09-07 18:00:37 +00:00
namespace
{
template <typename Name>
2019-05-18 11:30:36 +00:00
class FormatFunction : public IFunction
{
public:
static constexpr auto name = Name::name;
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr) { return std::make_shared<FormatFunction>(); }
2019-05-18 11:30:36 +00:00
String getName() const override { return name; }
bool isVariadic() const override { return true; }
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
2019-05-18 11:30:36 +00:00
size_t getNumberOfArguments() const override { return 0; }
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0}; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
2022-03-28 04:58:22 +00:00
if (arguments.size() < 2)
2019-05-18 13:21:48 +00:00
throw Exception(
2022-03-28 04:58:22 +00:00
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
"Number of arguments for function {} doesn't match: passed {}, should be at least 2",
getName(),
arguments.size());
2019-05-18 11:30:36 +00:00
2021-06-15 19:55:21 +00:00
for (const auto arg_idx : collections::range(0, arguments.size()))
2019-05-18 11:30:36 +00:00
{
2020-04-22 08:31:10 +00:00
const auto * arg = arguments[arg_idx].get();
2019-05-18 11:30:36 +00:00
if (!isStringOrFixedString(arg))
2019-05-18 13:21:48 +00:00
throw Exception(
2022-03-28 04:58:22 +00:00
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Illegal type {} of argument {} of function {}",
arg->getName(),
arg_idx + 1,
getName());
2019-05-18 11:30:36 +00:00
}
return std::make_shared<DataTypeString>();
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
2019-05-18 11:30:36 +00:00
{
2020-10-17 21:41:50 +00:00
const ColumnPtr & c0 = arguments[0].column;
2019-05-18 11:30:36 +00:00
const ColumnConst * c0_const_string = typeid_cast<const ColumnConst *>(&*c0);
if (!c0_const_string)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "First argument of function {} must be constant string", getName());
2019-05-18 11:30:36 +00:00
String pattern = c0_const_string->getValue<String>();
auto col_res = ColumnString::create();
2019-05-18 11:30:36 +00:00
std::vector<const ColumnString::Chars *> data(arguments.size() - 1);
std::vector<const ColumnString::Offsets *> offsets(arguments.size() - 1);
std::vector<size_t> fixed_string_sizes(arguments.size() - 1);
2022-03-28 04:58:22 +00:00
std::vector<std::optional<String>> constant_strings(arguments.size() - 1);
2019-05-18 15:09:52 +00:00
2019-05-18 12:52:33 +00:00
bool has_column_string = false;
bool has_column_fixed_string = false;
2019-05-18 11:30:36 +00:00
for (size_t i = 1; i < arguments.size(); ++i)
{
2020-10-17 21:41:50 +00:00
const ColumnPtr & column = arguments[i].column;
2019-05-18 11:30:36 +00:00
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
{
2019-05-18 12:56:26 +00:00
has_column_string = true;
data[i - 1] = &col->getChars();
offsets[i - 1] = &col->getOffsets();
2019-05-18 11:30:36 +00:00
}
2019-05-18 12:29:10 +00:00
else if (const ColumnFixedString * fixed_col = checkAndGetColumn<ColumnFixedString>(column.get()))
{
2019-05-18 12:56:26 +00:00
has_column_fixed_string = true;
data[i - 1] = &fixed_col->getChars();
fixed_string_sizes[i - 1] = fixed_col->getN();
2019-05-18 12:29:10 +00:00
}
2019-05-18 15:09:52 +00:00
else if (const ColumnConst * const_col = checkAndGetColumnConstStringOrFixedString(column.get()))
{
constant_strings[i - 1] = const_col->getValue<String>();
}
2019-05-18 11:30:36 +00:00
else
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}",
column->getName(), getName());
2019-05-18 11:30:36 +00:00
}
2019-05-18 13:21:48 +00:00
FormatImpl::formatExecute(
has_column_string,
has_column_fixed_string,
std::move(pattern),
data,
offsets,
fixed_string_sizes,
constant_strings,
col_res->getChars(),
col_res->getOffsets(),
input_rows_count);
2020-10-17 21:41:50 +00:00
return col_res;
2019-05-18 11:30:36 +00:00
}
};
struct NameFormat
{
static constexpr auto name = "format";
};
using FunctionFormat = FormatFunction<NameFormat>;
2019-05-18 11:30:36 +00:00
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(Format)
2019-05-18 11:30:36 +00:00
{
factory.registerFunction<FunctionFormat>();
}
}