ClickHouse/src/Functions/getTypeSerializationStreams.cpp

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

88 lines
2.6 KiB
C++
Raw Normal View History

#include <Functions/IFunction.h>
#include <Functions/FunctionFactory.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/Serializations/ISerialization.h>
#include <Core/Field.h>
#include <Columns/ColumnString.h>
#include <Functions/FunctionHelpers.h>
namespace DB
{
namespace
{
/// Enumerate stream paths of data type.
2022-03-25 06:51:59 +00:00
class FunctionGetTypeSerializationStreams : public IFunction
{
public:
2022-03-25 06:51:59 +00:00
static constexpr auto name = "getTypeSerializationStreams";
static FunctionPtr create(ContextPtr)
{
2022-03-25 06:51:59 +00:00
return std::make_shared<FunctionGetTypeSerializationStreams>();
}
String getName() const override
{
return name;
}
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
size_t getNumberOfArguments() const override
{
return 1;
}
2022-03-21 04:49:05 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>());
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
2022-03-21 04:49:05 +00:00
auto type = getType(arguments[0]);
SerializationPtr serialization = type->getDefaultSerialization();
auto col_res = ColumnArray::create(ColumnString::create());
ColumnString & col_res_strings = typeid_cast<ColumnString &>(col_res->getData());
ColumnVectorHelper::Offsets & col_res_offsets = typeid_cast<ColumnArray::Offsets &>(col_res->getOffsets());
serialization->enumerateStreams([&](const ISerialization::SubstreamPath & substream_path)
{
col_res_strings.insert(substream_path.toString());
});
col_res_offsets.push_back(col_res_strings.size());
return ColumnConst::create(std::move(col_res), input_rows_count);
}
2022-03-21 04:49:05 +00:00
private:
2022-03-21 06:19:54 +00:00
static DataTypePtr getType(const ColumnWithTypeAndName & argument)
2022-03-21 04:49:05 +00:00
{
const IColumn * arg_column = argument.column.get();
const ColumnString * arg_string = checkAndGetColumnConstData<ColumnString>(arg_column);
if (!arg_string)
return argument.type;
try
{
DataTypePtr type = DataTypeFactory::instance().get(arg_string->getDataAt(0).toString());
return type;
}
catch (const DB::Exception &)
{
return argument.type;
}
}
};
}
REGISTER_FUNCTION(GetTypeSerializationStreams)
{
2022-03-25 06:51:59 +00:00
factory.registerFunction<FunctionGetTypeSerializationStreams>();
}
}