ClickHouse/src/DataTypes/ObjectUtils.h

101 lines
3.4 KiB
C++
Raw Normal View History

2021-04-23 23:56:26 +00:00
#pragma once
#include <Core/Block.h>
#include <Core/NamesAndTypes.h>
#include <Common/FieldVisitors.h>
#include <Storages/ColumnsDescription.h>
2021-12-01 02:58:24 +00:00
#include <DataTypes/DataTypeTuple.h>
#include <DataTypes/Serializations/JSONDataParser.h>
#include <DataTypes/DataTypesNumber.h>
#include <Columns/ColumnObject.h>
2021-04-23 23:56:26 +00:00
namespace DB
{
size_t getNumberOfDimensions(const IDataType & type);
size_t getNumberOfDimensions(const IColumn & column);
DataTypePtr getBaseTypeOfArray(const DataTypePtr & type);
2022-01-27 00:24:34 +00:00
DataTypePtr createArrayOfType(DataTypePtr type, size_t num_dimensions);
2022-01-21 14:55:26 +00:00
Array createEmptyArrayField(size_t num_dimensions);
2021-12-23 12:28:40 +00:00
ColumnPtr getBaseColumnOfArray(const ColumnPtr & column);
ColumnPtr createArrayOfColumn(const ColumnPtr & column, size_t num_dimensions);
2021-04-23 23:56:26 +00:00
DataTypePtr getDataTypeByColumn(const IColumn & column);
void convertObjectsToTuples(NamesAndTypesList & columns_list, Block & block, const NamesAndTypesList & extended_storage_columns);
void checkObjectHasNoAmbiguosPaths(const PathsInData & paths);
DataTypePtr getLeastCommonTypeForObject(const DataTypes & types, bool check_ambiguos_paths = false);
void extendObjectColumns(NamesAndTypesList & columns_list, const ColumnsDescription & object_columns, bool with_subcolumns);
NameSet getNamesOfObjectColumns(const NamesAndTypesList & columns_list);
bool hasObjectColumns(const ColumnsDescription & columns);
void updateObjectColumns(ColumnsDescription & object_columns, const NamesAndTypesList & new_columns);
2021-12-01 02:58:24 +00:00
using DataTypeTuplePtr = std::shared_ptr<DataTypeTuple>;
std::pair<PathsInData, DataTypes> flattenTuple(const DataTypePtr & type);
2022-01-27 00:24:34 +00:00
ColumnPtr flattenTuple(const ColumnPtr & column);
2021-12-01 02:58:24 +00:00
DataTypePtr unflattenTuple(
const PathsInData & paths,
2021-12-01 02:58:24 +00:00
const DataTypes & tuple_types);
2022-01-27 00:24:34 +00:00
std::pair<ColumnPtr, DataTypePtr> unflattenTuple(
const PathsInData & paths,
2021-12-01 02:58:24 +00:00
const DataTypes & tuple_types,
const Columns & tuple_columns);
void replaceMissedSubcolumnsByConstants(
const ColumnsDescription & expected_columns,
const ColumnsDescription & available_columns,
ASTPtr query);
void finalizeObjectColumns(MutableColumns & columns);
2021-04-23 23:56:26 +00:00
template <typename Iterator, typename EntryColumnsGetter>
ColumnsDescription getObjectColumns(
Iterator begin, Iterator end,
const ColumnsDescription & storage_columns,
EntryColumnsGetter && entry_columns_getter)
{
ColumnsDescription res;
if (begin == end)
{
for (const auto & column : storage_columns)
{
if (isObject(column.type))
{
auto tuple_type = std::make_shared<DataTypeTuple>(
DataTypes{std::make_shared<DataTypeUInt8>()},
Names{ColumnObject::COLUMN_NAME_DUMMY});
res.add({column.name, std::move(tuple_type)});
}
}
return res;
}
std::unordered_map<String, DataTypes> types_in_entries;
for (auto it = begin; it != end; ++it)
{
const auto & entry_columns = entry_columns_getter(*it);
for (const auto & column : entry_columns)
{
auto storage_column = storage_columns.tryGetPhysical(column.name);
if (storage_column && isObject(storage_column->type))
types_in_entries[column.name].push_back(column.type);
}
}
for (const auto & [name, types] : types_in_entries)
res.add({String(name), getLeastCommonTypeForObject(types)});
return res;
}
2021-04-23 23:56:26 +00:00
}