ClickHouse/dbms/src/Core/Block.cpp

445 lines
12 KiB
C++
Raw Normal View History

2010-03-12 20:44:25 +00:00
#include <iterator>
2010-03-04 19:20:28 +00:00
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
2010-03-01 16:59:51 +00:00
#include <DB/Core/Block.h>
#include <DB/Storages/ColumnDefault.h>
#include <DB/Columns/ColumnArray.h>
#include <DB/DataTypes/DataTypeNested.h>
#include <DB/DataTypes/DataTypeArray.h>
#include <DB/Parsers/ASTExpressionList.h>
2015-02-10 21:10:58 +00:00
#include <memory>
#include <DB/Parsers/formatAST.h>
2010-03-01 16:59:51 +00:00
namespace DB
{
2010-03-12 20:44:25 +00:00
Block::Block(const Block & other)
{
*this = other;
}
2014-10-16 16:55:23 +00:00
void Block::addDefaults(const NamesAndTypesList & required_columns)
{
/// Для недостающих столбцов из вложенной структуры нужно создавать не столбец пустых массивов, а столбец массивов правильных длин.
/// Сначала запомним столбцы смещений для всех массивов в блоке.
std::map<String, ColumnPtr> offset_columns;
for (const auto & elem : data)
{
if (const ColumnArray * array = typeid_cast<const ColumnArray *>(&*elem.column))
{
String offsets_name = DataTypeNested::extractNestedTableName(elem.name);
auto & offsets_column = offset_columns[offsets_name];
/// Если почему-то есть разные столбцы смещений для одной вложенной структуры, то берём непустой.
if (!offsets_column || offsets_column->empty())
offsets_column = array->getOffsetsColumn();
}
}
for (const auto & requested_column : required_columns)
{
if (has(requested_column.name))
continue;
ColumnWithTypeAndName column_to_add;
column_to_add.name = requested_column.name;
column_to_add.type = requested_column.type;
String offsets_name = DataTypeNested::extractNestedTableName(column_to_add.name);
if (offset_columns.count(offsets_name))
{
ColumnPtr offsets_column = offset_columns[offsets_name];
DataTypePtr nested_type = typeid_cast<DataTypeArray &>(*column_to_add.type).getNestedType();
size_t nested_rows = offsets_column->empty() ? 0
: typeid_cast<ColumnUInt64 &>(*offsets_column).getData().back();
ColumnPtr nested_column = dynamic_cast<IColumnConst &>(
*nested_type->createConstColumn(
nested_rows, nested_type->getDefault())).convertToFullColumn();
column_to_add.column = new ColumnArray(nested_column, offsets_column);
}
else
{
/** Нужно превратить константный столбец в полноценный, так как в части блоков (из других кусков),
* он может быть полноценным (а то интерпретатор может посчитать, что он константный везде).
*/
column_to_add.column = dynamic_cast<IColumnConst &>(
*column_to_add.type->createConstColumn(
rowsInFirstColumn(), column_to_add.type->getDefault())).convertToFullColumn();
}
insert(column_to_add);
}
}
2010-03-12 20:44:25 +00:00
Block & Block::operator= (const Block & other)
{
info = other.info;
2010-03-12 20:44:25 +00:00
data = other.data;
2010-03-04 19:20:28 +00:00
index_by_position.resize(data.size());
index_by_name.clear();
2010-03-01 16:59:51 +00:00
size_t pos = 0;
2010-03-04 19:20:28 +00:00
for (Container_t::iterator it = data.begin(); it != data.end(); ++it, ++pos)
{
2010-03-01 16:59:51 +00:00
index_by_position[pos] = it;
index_by_name[it->name] = it;
}
2010-03-01 16:59:51 +00:00
return *this;
}
2010-03-01 16:59:51 +00:00
void Block::insert(size_t position, const ColumnWithTypeAndName & elem)
2010-03-01 16:59:51 +00:00
{
if (position > index_by_position.size())
2011-08-14 00:49:30 +00:00
throw Exception("Position out of bound in Block::insert(), max position = "
2013-06-21 20:34:19 +00:00
+ toString(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
if (position == index_by_position.size())
{
insert(elem);
return;
}
2010-03-04 19:20:28 +00:00
Container_t::iterator it = data.insert(index_by_position[position], elem);
index_by_name[elem.name] = it;
index_by_position.resize(index_by_position.size() + 1);
for (size_t i = index_by_position.size() - 1; i > position; --i)
index_by_position[i] = index_by_position[i - 1];
index_by_position[position] = it;
2010-03-01 16:59:51 +00:00
}
void Block::insert(const ColumnWithTypeAndName & elem)
2010-03-05 15:29:17 +00:00
{
Container_t::iterator it = data.insert(data.end(), elem);
2011-09-04 05:14:52 +00:00
index_by_name[elem.name] = it;
index_by_position.push_back(it);
2011-09-04 05:14:52 +00:00
}
void Block::insertUnique(const ColumnWithTypeAndName & elem)
2011-09-04 05:14:52 +00:00
{
if (index_by_name.end() == index_by_name.find(elem.name))
insert(elem);
2010-03-05 15:29:17 +00:00
}
2010-03-01 16:59:51 +00:00
void Block::erase(size_t position)
{
2010-03-05 15:29:17 +00:00
if (position >= index_by_position.size())
2011-08-14 00:49:30 +00:00
throw Exception("Position out of bound in Block::erase(), max position = "
2013-06-21 20:34:19 +00:00
+ toString(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
2010-03-01 16:59:51 +00:00
Container_t::iterator it = index_by_position[position];
index_by_name.erase(index_by_name.find(it->name));
2010-03-04 19:20:28 +00:00
data.erase(it);
for (size_t i = position, size = index_by_position.size() - 1; i < size; ++i)
index_by_position[i] = index_by_position[i + 1];
index_by_position.resize(index_by_position.size() - 1);
2010-03-01 16:59:51 +00:00
}
void Block::erase(const String & name)
{
IndexByName_t::iterator index_it = index_by_name.find(name);
if (index_it == index_by_name.end())
throw Exception("No such name in Block::erase(): '"
+ name + "'", ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
Container_t::iterator it = index_it->second;
index_by_name.erase(index_it);
size_t position = std::distance(data.begin(), it);
data.erase(it);
for (size_t i = position, size = index_by_position.size() - 1; i < size; ++i)
index_by_position[i] = index_by_position[i + 1];
index_by_position.resize(index_by_position.size() - 1);
}
ColumnWithTypeAndName & Block::getByPosition(size_t position)
2010-03-01 16:59:51 +00:00
{
2011-08-21 03:41:37 +00:00
if (position >= index_by_position.size())
2013-06-21 20:34:19 +00:00
throw Exception("Position " + toString(position)
2011-09-05 00:32:22 +00:00
+ " is out of bound in Block::getByPosition(), max position = "
2013-06-21 20:34:19 +00:00
+ toString(index_by_position.size() - 1)
2011-09-05 00:32:22 +00:00
+ ", there are columns: " + dumpNames(), ErrorCodes::POSITION_OUT_OF_BOUND);
2010-03-01 16:59:51 +00:00
return *index_by_position[position];
}
const ColumnWithTypeAndName & Block::getByPosition(size_t position) const
2010-03-01 16:59:51 +00:00
{
2011-08-21 03:41:37 +00:00
if (position >= index_by_position.size())
2013-06-21 20:34:19 +00:00
throw Exception("Position " + toString(position)
2011-09-05 00:32:22 +00:00
+ " is out of bound in Block::getByPosition(), max position = "
2013-06-21 20:34:19 +00:00
+ toString(index_by_position.size() - 1)
2011-09-05 00:32:22 +00:00
+ ", there are columns: " + dumpNames(), ErrorCodes::POSITION_OUT_OF_BOUND);
2010-03-01 16:59:51 +00:00
return *index_by_position[position];
}
ColumnWithTypeAndName & Block::getByName(const std::string & name)
2010-03-01 16:59:51 +00:00
{
2011-08-21 03:41:37 +00:00
IndexByName_t::const_iterator it = index_by_name.find(name);
if (index_by_name.end() == it)
throw Exception("Not found column " + name + " in block. There are only columns: " + dumpNames()
, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
return *it->second;
2010-03-01 16:59:51 +00:00
}
const ColumnWithTypeAndName & Block::getByName(const std::string & name) const
2010-03-01 16:59:51 +00:00
{
2010-03-04 19:20:28 +00:00
IndexByName_t::const_iterator it = index_by_name.find(name);
if (index_by_name.end() == it)
2011-08-14 00:49:30 +00:00
throw Exception("Not found column " + name + " in block. There are only columns: " + dumpNames()
, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
2010-03-04 19:20:28 +00:00
return *it->second;
}
2011-12-12 06:15:34 +00:00
bool Block::has(const std::string & name) const
{
return index_by_name.end() != index_by_name.find(name);
}
2011-08-12 20:39:42 +00:00
size_t Block::getPositionByName(const std::string & name) const
{
IndexByName_t::const_iterator it = index_by_name.find(name);
if (index_by_name.end() == it)
2011-08-14 00:49:30 +00:00
throw Exception("Not found column " + name + " in block. There are only columns: " + dumpNames()
, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
2011-08-12 20:39:42 +00:00
return std::distance(const_cast<Container_t &>(data).begin(), it->second);
}
2010-03-04 19:20:28 +00:00
size_t Block::rows() const
{
size_t res = 0;
for (const auto & elem : data)
2010-03-04 19:20:28 +00:00
{
size_t size = elem.column->size();
2010-03-04 19:20:28 +00:00
2010-03-18 20:32:35 +00:00
if (res != 0 && size != res)
2011-08-14 00:49:30 +00:00
throw Exception("Sizes of columns doesn't match: "
2013-06-21 20:34:19 +00:00
+ data.begin()->name + ": " + toString(res)
+ ", " + elem.name + ": " + toString(size)
2011-08-14 00:49:30 +00:00
, ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
2010-03-04 19:20:28 +00:00
res = size;
}
return res;
2010-03-01 16:59:51 +00:00
}
2010-03-04 19:20:28 +00:00
2013-06-08 20:19:29 +00:00
size_t Block::rowsInFirstColumn() const
{
if (data.empty())
2013-06-08 20:19:29 +00:00
return 0;
for (const auto & elem : data)
{
if (!elem.column.isNull())
return elem.column->size();
}
2013-06-08 20:19:29 +00:00
return 0;
2013-06-08 20:19:29 +00:00
}
2012-05-17 19:15:53 +00:00
size_t Block::bytes() const
{
size_t res = 0;
for (const auto & elem : data)
res += elem.column->byteSize();
2012-05-17 19:15:53 +00:00
return res;
}
2011-08-14 00:49:30 +00:00
std::string Block::dumpNames() const
{
std::stringstream res;
for (auto it = data.begin(); it != data.end(); ++it)
2011-08-14 00:49:30 +00:00
{
if (it != data.begin())
res << ", ";
res << it->name;
}
return res.str();
}
std::string Block::dumpStructure() const
{
std::stringstream res;
for (auto it = data.begin(); it != data.end(); ++it)
{
if (it != data.begin())
res << ", ";
res << it->name << ' ' << it->type->getName();
if (it->column)
res << ' ' << it->column->getName() << ' ' << it->column->size();
else
res << "nullptr";
}
return res.str();
}
2011-09-19 03:34:23 +00:00
Block Block::cloneEmpty() const
{
Block res;
for (const auto & elem : data)
res.insert(elem.cloneEmpty());
2011-09-19 03:34:23 +00:00
return res;
}
Block Block::sortColumns() const
{
Block sorted_block;
for (const auto & name : index_by_name)
sorted_block.insert(*name.second);
return sorted_block;
}
ColumnsWithTypeAndName Block::getColumns() const
{
return ColumnsWithTypeAndName(data.begin(), data.end());
}
2011-11-06 02:29:13 +00:00
NamesAndTypesList Block::getColumnsList() const
{
NamesAndTypesList res;
for (const auto & elem : data)
res.push_back(NameAndTypePair(elem.name, elem.type));
2011-11-06 02:29:13 +00:00
return res;
}
void Block::checkNestedArraysOffsets() const
{
/// Указатели на столбцы-массивы, для проверки равенства столбцов смещений во вложенных структурах данных
typedef std::map<String, const ColumnArray *> ArrayColumns;
ArrayColumns array_columns;
for (const auto & elem : data)
{
if (const ColumnArray * column_array = typeid_cast<const ColumnArray *>(&*elem.column))
{
String name = DataTypeNested::extractNestedTableName(elem.name);
ArrayColumns::const_iterator it = array_columns.find(name);
if (array_columns.end() == it)
array_columns[name] = column_array;
else
{
if (!it->second->hasEqualOffsets(*column_array))
throw Exception("Sizes of nested arrays do not match", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH);
}
}
}
}
void Block::optimizeNestedArraysOffsets()
{
/// Указатели на столбцы-массивы, для проверки равенства столбцов смещений во вложенных структурах данных
typedef std::map<String, ColumnArray *> ArrayColumns;
ArrayColumns array_columns;
for (auto & elem : data)
{
if (ColumnArray * column_array = typeid_cast<ColumnArray *>(&*elem.column))
{
String name = DataTypeNested::extractNestedTableName(elem.name);
ArrayColumns::const_iterator it = array_columns.find(name);
if (array_columns.end() == it)
array_columns[name] = column_array;
else
{
if (!it->second->hasEqualOffsets(*column_array))
throw Exception("Sizes of nested arrays do not match", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH);
/// делаем так, чтобы столбцы смещений массивов внутри одной вложенной таблицы указывали в одно место
column_array->getOffsetsColumn() = it->second->getOffsetsColumn();
}
}
}
}
bool blocksHaveEqualStructure(const Block & lhs, const Block & rhs)
{
size_t columns = lhs.columns();
if (rhs.columns() != columns)
return false;
for (size_t i = 0; i < columns; ++i)
{
const IDataType & lhs_type = *lhs.getByPosition(i).type;
const IDataType & rhs_type = *rhs.getByPosition(i).type;
if (lhs_type.getName() != rhs_type.getName())
return false;
}
return true;
}
void Block::clear()
{
info = BlockInfo();
data.clear();
index_by_name.clear();
index_by_position.clear();
}
void Block::swap(Block & other)
{
std::swap(info, other.info);
data.swap(other.data);
index_by_name.swap(other.index_by_name);
index_by_position.swap(other.index_by_position);
}
2010-03-04 19:20:28 +00:00
}