2021-07-09 03:15:41 +00:00
|
|
|
#include <Storages/StorageSnapshot.h>
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <DataTypes/ObjectUtils.h>
|
2021-07-21 14:45:19 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2021-07-20 15:20:21 +00:00
|
|
|
#include <sparsehash/dense_hash_map>
|
|
|
|
#include <sparsehash/dense_hash_set>
|
2021-07-23 16:30:18 +00:00
|
|
|
#include <DataTypes/NestedUtils.h>
|
2021-07-09 03:15:41 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_FOUND_COLUMN_IN_BLOCK;
|
|
|
|
extern const int EMPTY_LIST_OF_COLUMNS_QUERIED;
|
|
|
|
extern const int NO_SUCH_COLUMN_IN_TABLE;
|
|
|
|
extern const int COLUMN_QUERIED_MORE_THAN_ONCE;
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:45:19 +00:00
|
|
|
void StorageSnapshot::init()
|
2021-07-09 03:15:41 +00:00
|
|
|
{
|
2021-07-21 14:45:19 +00:00
|
|
|
for (const auto & [name, type] : storage.getVirtuals())
|
|
|
|
virtual_columns[name] = type;
|
2021-07-20 15:20:21 +00:00
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
|
2021-07-21 14:45:19 +00:00
|
|
|
NamesAndTypesList StorageSnapshot::getColumns(const GetColumnsOptions & options) const
|
2021-07-20 15:20:21 +00:00
|
|
|
{
|
2021-07-21 14:45:19 +00:00
|
|
|
auto all_columns = getMetadataForQuery()->getColumns().get(options);
|
|
|
|
|
|
|
|
if (options.with_extended_objects)
|
2021-07-23 16:30:18 +00:00
|
|
|
extendObjectColumns(all_columns, object_columns, options.with_subcolumns);
|
2021-07-21 14:45:19 +00:00
|
|
|
|
2021-07-09 03:15:41 +00:00
|
|
|
if (options.with_virtuals)
|
|
|
|
{
|
|
|
|
/// Virtual columns must be appended after ordinary,
|
|
|
|
/// because user can override them.
|
|
|
|
auto virtuals = storage.getVirtuals();
|
|
|
|
if (!virtuals.empty())
|
|
|
|
{
|
|
|
|
NameSet column_names;
|
2021-07-21 14:45:19 +00:00
|
|
|
for (const auto & column : all_columns)
|
2021-07-09 03:15:41 +00:00
|
|
|
column_names.insert(column.name);
|
|
|
|
for (auto && column : virtuals)
|
|
|
|
if (!column_names.count(column.name))
|
2021-07-21 14:45:19 +00:00
|
|
|
all_columns.push_back(std::move(column));
|
2021-07-09 03:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:45:19 +00:00
|
|
|
return all_columns;
|
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
|
2021-07-21 14:45:19 +00:00
|
|
|
NamesAndTypesList StorageSnapshot::getColumnsByNames(const GetColumnsOptions & options, const Names & names) const
|
|
|
|
{
|
|
|
|
NamesAndTypesList res;
|
|
|
|
const auto & columns = getMetadataForQuery()->getColumns();
|
|
|
|
for (const auto & name : names)
|
|
|
|
{
|
|
|
|
auto column = columns.tryGetColumn(options, name);
|
|
|
|
if (column && !isObject(column->type))
|
|
|
|
{
|
|
|
|
res.emplace_back(std::move(*column));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.with_extended_objects)
|
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
auto object_column = object_columns.tryGetColumn(options, name);
|
|
|
|
if (object_column)
|
2021-07-21 14:45:19 +00:00
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
res.emplace_back(std::move(*object_column));
|
2021-07-21 14:45:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.with_virtuals)
|
|
|
|
{
|
|
|
|
auto it = virtual_columns.find(name);
|
|
|
|
if (it != virtual_columns.end())
|
|
|
|
{
|
|
|
|
res.emplace_back(name, it->second);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(ErrorCodes::NO_SUCH_COLUMN_IN_TABLE, "There is no column {} in table", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2021-07-09 03:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Block StorageSnapshot::getSampleBlockForColumns(const Names & column_names) const
|
|
|
|
{
|
|
|
|
Block res;
|
|
|
|
|
2021-07-20 15:20:21 +00:00
|
|
|
const auto & columns = getMetadataForQuery()->getColumns();
|
2021-07-09 03:15:41 +00:00
|
|
|
for (const auto & name : column_names)
|
|
|
|
{
|
2021-07-20 15:20:21 +00:00
|
|
|
auto column = columns.tryGetColumnOrSubcolumn(GetColumnsOptions::All, name);
|
2021-07-21 14:45:19 +00:00
|
|
|
if (column && !isObject(column->type))
|
2021-07-20 15:20:21 +00:00
|
|
|
{
|
|
|
|
res.insert({column->type->createColumn(), column->type, column->name});
|
|
|
|
}
|
2021-07-23 16:30:18 +00:00
|
|
|
else if (auto object_column = object_columns.tryGetColumnOrSubcolumn(GetColumnsOptions::All, name))
|
2021-07-21 14:45:19 +00:00
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
res.insert({object_column->type->createColumn(), object_column->type, object_column->name});
|
2021-07-21 14:45:19 +00:00
|
|
|
}
|
2021-07-23 16:30:18 +00:00
|
|
|
else if (auto it = virtual_columns.find(name); it != virtual_columns.end())
|
2021-07-20 15:20:21 +00:00
|
|
|
{
|
2021-07-21 14:45:19 +00:00
|
|
|
/// Virtual columns must be appended after ordinary, because user can
|
|
|
|
/// override them.
|
2021-07-23 16:30:18 +00:00
|
|
|
const auto & type = it->second;
|
2021-07-20 15:20:21 +00:00
|
|
|
res.insert({type->createColumn(), type, name});
|
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
else
|
2021-07-23 16:30:18 +00:00
|
|
|
{
|
2021-07-09 03:15:41 +00:00
|
|
|
throw Exception(ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK,
|
|
|
|
"Column {} not found in table {}", backQuote(name), storage.getStorageID().getNameForLogs());
|
2021-07-23 16:30:18 +00:00
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:30:18 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
using DenseHashSet = google::dense_hash_set<StringRef, StringRefHash>;
|
|
|
|
#else
|
|
|
|
using DenseHashSet = google::sparsehash::dense_hash_set<StringRef, StringRefHash>;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:15:41 +00:00
|
|
|
void StorageSnapshot::check(const Names & column_names) const
|
|
|
|
{
|
2021-07-20 15:20:21 +00:00
|
|
|
const auto & columns = getMetadataForQuery()->getColumns();
|
2021-07-23 16:30:18 +00:00
|
|
|
auto options = GetColumnsOptions(GetColumnsOptions::AllPhysical).withSubcolumns();
|
2021-07-09 03:15:41 +00:00
|
|
|
|
|
|
|
if (column_names.empty())
|
2021-07-20 15:20:21 +00:00
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
auto list_of_columns = listOfColumns(columns.get(options));
|
2021-07-09 03:15:41 +00:00
|
|
|
throw Exception(ErrorCodes::EMPTY_LIST_OF_COLUMNS_QUERIED,
|
2021-07-20 15:20:21 +00:00
|
|
|
"Empty list of columns queried. There are columns: {}", list_of_columns);
|
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
|
2021-07-23 16:30:18 +00:00
|
|
|
DenseHashSet unique_names;
|
|
|
|
unique_names.set_empty_key(StringRef());
|
2021-07-09 03:15:41 +00:00
|
|
|
|
|
|
|
for (const auto & name : column_names)
|
|
|
|
{
|
2021-07-21 14:45:19 +00:00
|
|
|
bool has_column = columns.hasColumnOrSubcolumn(GetColumnsOptions::AllPhysical, name)
|
2021-07-23 16:30:18 +00:00
|
|
|
|| object_columns.hasColumnOrSubcolumn(GetColumnsOptions::AllPhysical, name)
|
|
|
|
|| virtual_columns.count(name);
|
2021-07-20 15:20:21 +00:00
|
|
|
|
|
|
|
if (!has_column)
|
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
auto list_of_columns = listOfColumns(columns.get(options));
|
2021-07-09 03:15:41 +00:00
|
|
|
throw Exception(ErrorCodes::NO_SUCH_COLUMN_IN_TABLE,
|
2021-07-20 15:20:21 +00:00
|
|
|
"There is no column with name {} in table {}. There are columns: {}",
|
|
|
|
backQuote(name), storage.getStorageID().getNameForLogs(), list_of_columns);
|
|
|
|
}
|
2021-07-09 03:15:41 +00:00
|
|
|
|
2021-07-23 16:30:18 +00:00
|
|
|
if (unique_names.count(name))
|
2021-07-09 03:15:41 +00:00
|
|
|
throw Exception(ErrorCodes::COLUMN_QUERIED_MORE_THAN_ONCE, "Column {} queried more than once", name);
|
|
|
|
|
|
|
|
unique_names.insert(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 14:54:53 +00:00
|
|
|
DataTypePtr StorageSnapshot::getConcreteType(const String & column_name) const
|
|
|
|
{
|
2021-07-23 16:30:18 +00:00
|
|
|
auto object_column = object_columns.tryGetColumnOrSubcolumn(GetColumnsOptions::All, column_name);
|
|
|
|
if (object_column)
|
|
|
|
return object_column->type;
|
2021-07-09 14:54:53 +00:00
|
|
|
|
|
|
|
return metadata->getColumns().get(column_name).type;
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:30:18 +00:00
|
|
|
bool StorageSnapshot::isSubcolumnOfObject(const String & name) const
|
|
|
|
{
|
|
|
|
auto split = Nested::splitName(name);
|
|
|
|
return !split.second.empty()
|
|
|
|
&& object_columns.tryGetColumn(GetColumnsOptions::All, split.first);
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:15:41 +00:00
|
|
|
}
|