ClickHouse
145 строк · 4.9 Кб
1#include <Columns/ColumnString.h>2#include <DataTypes/DataTypeString.h>3#include <DataTypes/DataTypeEnum.h>4#include <Functions/FunctionFactory.h>5#include <Functions/IFunction.h>6#include <Interpreters/parseColumnsListForTableFunction.h>7#include <Interpreters/Context.h>8#include <IO/WriteBufferFromVector.h>9#include <Formats/StructureToCapnProtoSchema.h>10#include <Formats/StructureToProtobufSchema.h>11
12#include <Common/randomSeed.h>13
14
15namespace DB16{
17
18namespace ErrorCodes19{
20extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;21extern const int ILLEGAL_TYPE_OF_ARGUMENT;22}
23
24template <class Impl>25class FunctionStructureToFormatSchema : public IFunction26{
27public:28
29static constexpr auto name = Impl::name;30explicit FunctionStructureToFormatSchema(ContextPtr context_) : context(std::move(context_))31{32}33
34static FunctionPtr create(ContextPtr ctx)35{36return std::make_shared<FunctionStructureToFormatSchema>(std::move(ctx));37}38
39String getName() const override { return name; }40
41size_t getNumberOfArguments() const override { return 0; }42bool isVariadic() const override { return true; }43
44bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }45ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 1}; }46bool useDefaultImplementationForConstants() const override { return false; }47bool useDefaultImplementationForNulls() const override { return false; }48
49DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override50{51if (arguments.empty() || arguments.size() > 2)52throw Exception(53ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,54"Number of arguments for function {} doesn't match: passed {}, expected 1 or 2",55getName(), arguments.size());56
57if (!isString(arguments[0]))58{59throw Exception(60ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,61"Illegal type {} of the first argument of function {}, expected constant string",62arguments[0]->getName(),63getName());64}65
66if (arguments.size() > 1 && !isString(arguments[1]))67{68throw Exception(69ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,70"Illegal type {} of the second argument of function {}, expected constant string",71arguments[1]->getName(),72getName());73}74
75return std::make_shared<DataTypeString>();76}77
78ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override79{80if (arguments.empty() || arguments.size() > 2)81throw Exception(82ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,83"Number of arguments for function {} doesn't match: passed {}, expected 1 or 2",84getName(), arguments.size());85
86String structure = arguments[0].column->getDataAt(0).toString();87String message_name = arguments.size() == 2 ? arguments[1].column->getDataAt(0).toString() : "Message";88auto columns_list = parseColumnsListFromString(structure, context);89auto col_res = ColumnString::create();90auto & data = assert_cast<ColumnString &>(*col_res).getChars();91WriteBufferFromVector buf(data);92Impl::writeSchema(buf, message_name, columns_list.getAll());93buf.finalize();94auto & offsets = assert_cast<ColumnString &>(*col_res).getOffsets();95offsets.push_back(data.size());96return ColumnConst::create(std::move(col_res), input_rows_count);97}98
99private:100ContextPtr context;101};102
103
104REGISTER_FUNCTION(StructureToCapnProtoSchema)105{
106factory.registerFunction<FunctionStructureToFormatSchema<StructureToCapnProtoSchema>>(FunctionDocumentation107{108.description=R"(109Function that converts ClickHouse table structure to CapnProto format schema
110)",111.examples{112{"random", "SELECT structureToCapnProtoSchema('s String, x UInt32', 'MessageName') format TSVRaw", "struct MessageName\n"113"{\n"
114" s @0 : Data;\n"
115" x @1 : UInt32;\n"
116"}"},117},118.categories{"Other"}119},120FunctionFactory::CaseSensitive);121}
122
123
124REGISTER_FUNCTION(StructureToProtobufSchema)125{
126factory.registerFunction<FunctionStructureToFormatSchema<StructureToProtobufSchema>>(FunctionDocumentation127{128.description=R"(129Function that converts ClickHouse table structure to Protobuf format schema
130)",131.examples{132{"random", "SELECT structureToCapnProtoSchema('s String, x UInt32', 'MessageName') format TSVRaw", "syntax = \"proto3\";\n"133"\n"
134"message MessageName\n"
135"{\n"
136" bytes s = 1;\n"
137" uint32 x = 2;\n"
138"}"},139},140.categories{"Other"}141},142FunctionFactory::CaseSensitive);143}
144
145}
146