ClickHouse/src/DataTypes/DataTypeArray.cpp

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

114 lines
3.3 KiB
C++
Raw Normal View History

2021-03-09 14:10:28 +00:00
#include <Columns/ColumnArray.h>
#include <Formats/FormatSettings.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/Serializations/SerializationArray.h>
#include <Parsers/IAST.h>
#include <Common/typeid_cast.h>
#include <Common/assert_cast.h>
#include <Core/NamesAndTypes.h>
2023-07-26 14:57:32 +00:00
#include <Columns/ColumnConst.h>
2021-03-09 14:10:28 +00:00
#include <IO/WriteHelpers.h>
#include <IO/Operators.h>
2021-03-09 14:10:28 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
2023-07-26 14:57:32 +00:00
using FieldType = Array;
2021-03-09 14:10:28 +00:00
DataTypeArray::DataTypeArray(const DataTypePtr & nested_)
: nested{nested_}
{
}
MutableColumnPtr DataTypeArray::createColumn() const
{
return ColumnArray::create(nested->createColumn(), ColumnArray::ColumnOffsets::create());
}
Field DataTypeArray::getDefault() const
{
return Array();
}
bool DataTypeArray::equals(const IDataType & rhs) const
{
return typeid(rhs) == typeid(*this) && nested->equals(*static_cast<const DataTypeArray &>(rhs).nested);
}
SerializationPtr DataTypeArray::doGetDefaultSerialization() const
{
return std::make_shared<SerializationArray>(nested->getDefaultSerialization());
}
size_t DataTypeArray::getNumberOfDimensions() const
{
const DataTypeArray * nested_array = typeid_cast<const DataTypeArray *>(nested.get());
if (!nested_array)
return 1;
return 1 + nested_array->getNumberOfDimensions(); /// Every modern C++ compiler optimizes tail recursion.
}
String DataTypeArray::doGetPrettyName(size_t indent) const
{
WriteBufferFromOwnString s;
2023-09-28 12:33:08 +00:00
s << "Array(" << nested->getPrettyName(indent) << ')';
return s.str();
}
void DataTypeArray::forEachChild(const ChildCallback & callback) const
{
callback(*nested);
nested->forEachChild(callback);
}
2021-03-09 14:10:28 +00:00
2024-04-26 11:01:32 +00:00
std::unique_ptr<ISerialization::SubstreamData> DataTypeArray::getDynamicSubcolumnData(std::string_view subcolumn_name, const DB::IDataType::SubstreamData & data, bool throw_if_null) const
{
auto nested_type = assert_cast<const DataTypeArray &>(*data.type).nested;
auto nested_data = std::make_unique<ISerialization::SubstreamData>(nested_type->getDefaultSerialization());
nested_data->type = nested_type;
nested_data->column = data.column ? assert_cast<const ColumnArray &>(*data.column).getDataPtr() : nullptr;
auto nested_subcolumn_data = nested_type->getSubcolumnData(subcolumn_name, *nested_data, throw_if_null);
if (!nested_subcolumn_data)
return nullptr;
auto creator = SerializationArray::SubcolumnCreator(data.column ? assert_cast<const ColumnArray &>(*data.column).getOffsetsPtr() : nullptr);
auto res = std::make_unique<ISerialization::SubstreamData>();
res->serialization = creator.create(nested_subcolumn_data->serialization);
res->type = creator.create(nested_subcolumn_data->type);
if (data.column)
res->column = creator.create(nested_subcolumn_data->column);
return res;
}
2021-03-09 14:10:28 +00:00
static DataTypePtr create(const ASTPtr & arguments)
{
if (!arguments || arguments->children.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Array data type family must have exactly one argument - type of elements");
2021-03-09 14:10:28 +00:00
return std::make_shared<DataTypeArray>(DataTypeFactory::instance().get(arguments->children[0]));
}
void registerDataTypeArray(DataTypeFactory & factory)
{
factory.registerDataType("Array", create);
}
}