toJSONString

This commit is contained in:
Amos Bird 2021-06-10 17:23:20 +08:00
parent 0e9ce8c23a
commit 94bfe539ac
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
4 changed files with 68 additions and 0 deletions

View File

@ -40,6 +40,7 @@ void registerFunctionsGeo(FunctionFactory &);
void registerFunctionsIntrospection(FunctionFactory &); void registerFunctionsIntrospection(FunctionFactory &);
void registerFunctionsNull(FunctionFactory &); void registerFunctionsNull(FunctionFactory &);
void registerFunctionsJSON(FunctionFactory &); void registerFunctionsJSON(FunctionFactory &);
void registerFunctionToJSONString(FunctionFactory &);
void registerFunctionsConsistentHashing(FunctionFactory & factory); void registerFunctionsConsistentHashing(FunctionFactory & factory);
void registerFunctionsUnixTimestamp64(FunctionFactory & factory); void registerFunctionsUnixTimestamp64(FunctionFactory & factory);
void registerFunctionBitHammingDistance(FunctionFactory & factory); void registerFunctionBitHammingDistance(FunctionFactory & factory);
@ -98,6 +99,7 @@ void registerFunctions()
registerFunctionsGeo(factory); registerFunctionsGeo(factory);
registerFunctionsNull(factory); registerFunctionsNull(factory);
registerFunctionsJSON(factory); registerFunctionsJSON(factory);
registerFunctionToJSONString(factory);
registerFunctionsIntrospection(factory); registerFunctionsIntrospection(factory);
registerFunctionsConsistentHashing(factory); registerFunctionsConsistentHashing(factory);
registerFunctionsUnixTimestamp64(factory); registerFunctionsUnixTimestamp64(factory);

View File

@ -0,0 +1,53 @@
#include <Columns/ColumnString.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/IFunction.h>
#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) { return std::make_shared<FunctionToJSONString>(); }
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);
WriteBufferFromVector<ColumnString::Chars> json(data_to);
for (size_t i = 0; i < input_rows_count; ++i)
{
arguments[0].type->getDefaultSerialization()->serializeTextJSON(*arguments[0].column, i, json, FormatSettings());
writeChar(0, json);
offsets_to[i] = json.count();
}
json.finalize();
return res;
}
};
}
void registerFunctionToJSONString(FunctionFactory & factory)
{
factory.registerFunction<FunctionToJSONString>();
}
}

View File

@ -0,0 +1,3 @@
[] 2947817982 "&" -69802.9769 "w" -1.9158530982937093e25 ["2003-05-15","1988-03-19 07:13:49","2090-04-14 03:58:26.029","91943d2e-480d-66b5-ee4c-1b5bb8eb7256"] "½O" []
[-115] 481807067 ",{MM" -170235.0663 "o" 3.3808659558052087e155 ["2055-01-12","2070-08-09 03:49:21","2068-11-30 09:36:49.672","20b0e7b5-ad0e-177b-3054-c779b2a8ebe0"] "I\\u001C" ["e57178f9-4d10-2fa1-7c2d-53c5a65c3463"]
{"1234":"5678"}

View File

@ -0,0 +1,10 @@
drop table if exists t;
create table t engine Memory as select * from generateRandom('a Array(Int8), b UInt32, c Nullable(String), d Decimal32(4), e Nullable(Enum16(\'h\' = 1, \'w\' = 5 , \'o\' = -200)), f Float64, g Tuple(Date, DateTime, DateTime64, UUID), h FixedString(2), i Array(Nullable(UUID))', 10, 5, 3) limit 2;
select * apply toJSONString from t;
drop table t;
set allow_experimental_map_type = 1;
select toJSONString(map('1234', '5678'));