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>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2010-03-12 20:44:25 +00:00
|
|
|
|
|
|
|
Block::Block(const Block & other)
|
|
|
|
{
|
|
|
|
*this = other;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block & Block::operator= (const Block & other)
|
|
|
|
{
|
|
|
|
data = other.data;
|
|
|
|
rebuildIndexByPosition();
|
2011-08-21 03:41:37 +00:00
|
|
|
index_by_name.clear();
|
2010-03-12 20:44:25 +00:00
|
|
|
|
|
|
|
for (IndexByName_t::const_iterator it = other.index_by_name.begin(); it != other.index_by_name.end(); ++it)
|
|
|
|
{
|
|
|
|
Container_t::iterator value = data.begin();
|
|
|
|
std::advance(value, std::distance(const_cast<Block&>(other).data.begin(), it->second));
|
|
|
|
index_by_name[it->first] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
void Block::rebuildIndexByPosition()
|
|
|
|
{
|
2010-03-04 19:20:28 +00:00
|
|
|
index_by_position.resize(data.size());
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Block::insert(size_t position, const ColumnWithNameAndType & elem)
|
|
|
|
{
|
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::insert(), max position = "
|
|
|
|
+ Poco::NumberFormatter::format(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
|
2010-03-05 15:29:17 +00:00
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
Container_t::iterator it = data.insert(index_by_position[position], elem);
|
2010-03-01 16:59:51 +00:00
|
|
|
rebuildIndexByPosition();
|
|
|
|
index_by_name[elem.name] = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-05 15:29:17 +00:00
|
|
|
void Block::insert(const ColumnWithNameAndType & elem)
|
|
|
|
{
|
|
|
|
Container_t::iterator it = data.insert(data.end(), elem);
|
|
|
|
rebuildIndexByPosition();
|
2011-09-04 05:14:52 +00:00
|
|
|
index_by_name[elem.name] = it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Block::insertUnique(const ColumnWithNameAndType & elem)
|
|
|
|
{
|
|
|
|
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 = "
|
|
|
|
+ Poco::NumberFormatter::format(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
|
2010-03-05 15:29:17 +00:00
|
|
|
|
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);
|
2010-03-01 16:59:51 +00:00
|
|
|
rebuildIndexByPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
ColumnWithNameAndType & 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())
|
|
|
|
throw Exception("Position out of bound in Block::getByPosition(), max position = "
|
|
|
|
+ Poco::NumberFormatter::format(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
return *index_by_position[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
const ColumnWithNameAndType & 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())
|
|
|
|
throw Exception("Position out of bound in Block::getByPosition(), max position = "
|
|
|
|
+ Poco::NumberFormatter::format(index_by_position.size()), ErrorCodes::POSITION_OUT_OF_BOUND);
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
return *index_by_position[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
ColumnWithNameAndType & 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
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
const ColumnWithNameAndType & 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-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 (Container_t::const_iterator it = data.begin(); it != data.end(); ++it)
|
|
|
|
{
|
2010-03-12 18:25:35 +00:00
|
|
|
size_t size = it->column->size();
|
2010-03-04 19:20:28 +00:00
|
|
|
|
|
|
|
if (size == 0)
|
2011-08-14 00:49:30 +00:00
|
|
|
throw Exception("Empty column " + it->name + " in block.", ErrorCodes::EMPTY_COLUMN_IN_BLOCK);
|
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: "
|
|
|
|
+ data.begin()->name + ": " + Poco::NumberFormatter::format(res)
|
|
|
|
+ ", " + it->name + ": " + Poco::NumberFormatter::format(size)
|
|
|
|
, 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
|
|
|
|
|
|
|
size_t Block::columns() const
|
|
|
|
{
|
|
|
|
return data.size();
|
2010-03-01 16:59:51 +00:00
|
|
|
}
|
|
|
|
|
2011-08-14 00:49:30 +00:00
|
|
|
|
|
|
|
std::string Block::dumpNames() const
|
|
|
|
{
|
|
|
|
std::stringstream res;
|
|
|
|
for (Container_t::const_iterator it = data.begin(); it != data.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != data.begin())
|
|
|
|
res << ", ";
|
|
|
|
res << it->name;
|
|
|
|
}
|
|
|
|
return res.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
}
|