ClickHouse/src/Functions/toJSONString.cpp

62 lines
2.0 KiB
C++
Raw Normal View History

2021-06-10 09:23:20 +00:00
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#include <Formats/FormatFactory.h>
2021-06-10 09:23:20 +00:00
#include <IO/WriteBufferFromVector.h>
#include <IO/WriteHelpers.h>
namespace DB
{
namespace
{
class FunctionToJSONString : public IFunction
{
public:
static constexpr auto name = "toJSONString";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionToJSONString>(context); }
2021-07-05 10:29:36 +00:00
explicit FunctionToJSONString(ContextPtr context) : format_settings(getFormatSettings(context)) {}
2021-06-10 09:23:20 +00:00
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 1; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName &) const override { return std::make_shared<DataTypeString>(); }
bool useDefaultImplementationForConstants() const override { return true; }
ColumnPtr
executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & /*result_type*/, size_t input_rows_count) const override
{
auto res = ColumnString::create();
ColumnString::Chars & data_to = res->getChars();
ColumnString::Offsets & offsets_to = res->getOffsets();
offsets_to.resize(input_rows_count);
2021-06-10 11:14:20 +00:00
auto serializer = arguments[0].type->getDefaultSerialization();
2021-06-10 09:23:20 +00:00
WriteBufferFromVector<ColumnString::Chars> json(data_to);
for (size_t i = 0; i < input_rows_count; ++i)
{
serializer->serializeTextJSON(*arguments[0].column, i, json, format_settings);
2021-06-10 09:23:20 +00:00
writeChar(0, json);
offsets_to[i] = json.count();
}
json.finalize();
return res;
}
private:
/// Affects only subset of part of settings related to json.
2021-07-05 10:29:36 +00:00
const FormatSettings format_settings;
2021-06-10 09:23:20 +00:00
};
}
void registerFunctionToJSONString(FunctionFactory & factory)
{
factory.registerFunction<FunctionToJSONString>();
}
}