ClickHouse/dbms/src/Storages/IStorage.cpp

113 lines
3.2 KiB
C++
Raw Normal View History

#include <set>
#include <DB/Storages/IStorage.h>
namespace DB
{
2011-11-01 17:12:11 +00:00
static std::string listOfColumns(const NamesAndTypesMap & available_columns)
{
std::stringstream s;
2011-11-01 17:12:11 +00:00
for (NamesAndTypesMap::const_iterator it = available_columns.begin(); it != available_columns.end(); ++it)
{
if (it != available_columns.begin())
s << ", ";
s << it->first;
}
return s.str();
}
2011-11-01 17:12:11 +00:00
NamesAndTypesMap IStorage::getColumnsMap() const
{
NamesAndTypesMap res;
const NamesAndTypesList & names_and_types = getColumnsList();
for (NamesAndTypesList::const_iterator it = names_and_types.begin(); it != names_and_types.end(); ++it)
res.insert(*it);
return res;
}
const DataTypePtr IStorage::getDataTypeByName(const String & column_name) const
{
const NamesAndTypesList & names_and_types = getColumnsList();
for (NamesAndTypesList::const_iterator it = names_and_types.begin(); it != names_and_types.end(); ++it)
if (it->first == column_name)
return it->second;
throw Exception("There is no column " + column_name + " in table " + getTableName(), ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
}
2011-10-31 06:37:12 +00:00
Block IStorage::getSampleBlock() const
{
Block res;
2011-11-01 17:12:11 +00:00
const NamesAndTypesList & names_and_types = getColumnsList();
2011-10-31 06:37:12 +00:00
2011-11-01 17:12:11 +00:00
for (NamesAndTypesList::const_iterator it = names_and_types.begin(); it != names_and_types.end(); ++it)
2011-10-31 06:37:12 +00:00
{
ColumnWithNameAndType col;
col.name = it->first;
col.type = it->second;
col.column = col.type->createColumn();
res.insert(col);
}
return res;
}
void IStorage::check(const Names & column_names) const
{
2011-11-01 17:12:11 +00:00
const NamesAndTypesMap & available_columns = getColumnsMap();
if (column_names.empty())
throw Exception("Empty list of columns queried for table " + getTableName()
+ ". There are columns: " + listOfColumns(available_columns),
ErrorCodes::EMPTY_LIST_OF_COLUMNS_QUERIED);
typedef std::set<std::string> UniqueStrings;
UniqueStrings unique_names;
for (Names::const_iterator it = column_names.begin(); it != column_names.end(); ++it)
{
if (available_columns.end() == available_columns.find(*it))
throw Exception("There is no column with name " + *it + " in table " + getTableName()
+ ". There are columns: " + listOfColumns(available_columns),
ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
if (unique_names.end() != unique_names.find(*it))
throw Exception("Column " + *it + " queried more than once in table " + getTableName(),
ErrorCodes::COLUMN_QUERIED_MORE_THAN_ONCE);
unique_names.insert(*it);
}
}
void IStorage::check(const Block & block) const
{
2011-11-01 17:12:11 +00:00
const NamesAndTypesMap & available_columns = getColumnsMap();
for (size_t i = 0; i < block.columns(); ++i)
{
const ColumnWithNameAndType & column = block.getByPosition(i);
2011-11-01 17:12:11 +00:00
NamesAndTypesMap::const_iterator it = available_columns.find(column.name);
if (available_columns.end() == it)
throw Exception("There is no column with name " + column.name + " in table " + getTableName()
+ ". There are columns: " + listOfColumns(available_columns),
ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
if (column.type->getName() != it->second->getName())
throw Exception("Type mismatch for column " + column.name + " in table " + getTableName()
+ ". Column has type " + it->second->getName() + ", got type " + column.type->getName(),
ErrorCodes::TYPE_MISMATCH);
}
}
}