2011-08-09 15:57:33 +00:00
|
|
|
|
#pragma once
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
2014-09-12 16:05:29 +00:00
|
|
|
|
#include <initializer_list>
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
2016-07-24 21:34:09 +00:00
|
|
|
|
#include <DB/Common/Exception.h>
|
2015-01-03 03:18:49 +00:00
|
|
|
|
#include <DB/Core/BlockInfo.h>
|
2011-11-06 02:29:13 +00:00
|
|
|
|
#include <DB/Core/NamesAndTypes.h>
|
2016-08-04 21:40:20 +00:00
|
|
|
|
#include <DB/Core/ColumnWithTypeAndName.h>
|
2016-02-16 16:39:39 +00:00
|
|
|
|
#include <DB/Core/ColumnsWithTypeAndName.h>
|
2016-07-24 21:34:09 +00:00
|
|
|
|
#include <DB/Core/ColumnNumbers.h>
|
2016-08-04 21:40:20 +00:00
|
|
|
|
#include <DB/Common/Exception.h>
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2017-01-02 20:42:49 +00:00
|
|
|
|
/** Container for set of columns for bunch of rows in memory.
|
|
|
|
|
* This is unit of data processing.
|
|
|
|
|
* Also contains metadata - data types of columns and their names
|
|
|
|
|
* (either original names from a table, or generated names during temporary calculations).
|
|
|
|
|
* Allows to insert, remove columns in arbitary position, to change order of columns.
|
2010-03-01 16:59:51 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2014-09-30 03:08:47 +00:00
|
|
|
|
class Context;
|
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
class Block
|
|
|
|
|
{
|
|
|
|
|
private:
|
2017-01-04 17:25:07 +00:00
|
|
|
|
using Container = ColumnsWithTypeAndName;
|
2016-08-04 21:40:20 +00:00
|
|
|
|
using IndexByName = std::map<String, size_t>;
|
|
|
|
|
|
|
|
|
|
Container data;
|
|
|
|
|
IndexByName index_by_name;
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2015-01-03 03:18:49 +00:00
|
|
|
|
BlockInfo info;
|
|
|
|
|
|
2014-09-12 16:05:29 +00:00
|
|
|
|
Block() = default;
|
2017-01-04 17:25:07 +00:00
|
|
|
|
Block(std::initializer_list<ColumnWithTypeAndName> il);
|
|
|
|
|
Block(const ColumnsWithTypeAndName & data_);
|
2014-06-25 00:17:51 +00:00
|
|
|
|
|
2010-03-05 15:29:17 +00:00
|
|
|
|
/// вставить столбец в заданную позицию
|
2015-07-17 01:27:35 +00:00
|
|
|
|
void insert(size_t position, const ColumnWithTypeAndName & elem);
|
2016-08-04 21:40:20 +00:00
|
|
|
|
void insert(size_t position, ColumnWithTypeAndName && elem);
|
2010-03-05 15:29:17 +00:00
|
|
|
|
/// вставить столбец в конец
|
2015-07-17 01:27:35 +00:00
|
|
|
|
void insert(const ColumnWithTypeAndName & elem);
|
2016-08-04 21:40:20 +00:00
|
|
|
|
void insert(ColumnWithTypeAndName && elem);
|
2011-09-04 05:14:52 +00:00
|
|
|
|
/// вставить столбец в конец, если столбца с таким именем ещё нет
|
2015-07-17 01:27:35 +00:00
|
|
|
|
void insertUnique(const ColumnWithTypeAndName & elem);
|
2016-08-04 21:40:20 +00:00
|
|
|
|
void insertUnique(ColumnWithTypeAndName && elem);
|
2010-03-05 15:29:17 +00:00
|
|
|
|
/// удалить столбец в заданной позиции
|
2010-03-01 16:59:51 +00:00
|
|
|
|
void erase(size_t position);
|
2013-05-24 10:49:19 +00:00
|
|
|
|
/// удалить столбец с заданным именем
|
|
|
|
|
void erase(const String & name);
|
2013-10-28 16:09:42 +00:00
|
|
|
|
/// Добавляет в блок недостающие столбцы со значениями по-умолчанию
|
2014-10-16 16:55:23 +00:00
|
|
|
|
void addDefaults(const NamesAndTypesList & required_columns);
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
2016-08-04 21:40:20 +00:00
|
|
|
|
/// References are invalidated after calling functions above.
|
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
ColumnWithTypeAndName & getByPosition(size_t position) { return data[position]; }
|
|
|
|
|
const ColumnWithTypeAndName & getByPosition(size_t position) const { return data[position]; }
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
ColumnWithTypeAndName & safeGetByPosition(size_t position);
|
|
|
|
|
const ColumnWithTypeAndName & safeGetByPosition(size_t position) const;
|
2013-03-29 01:47:34 +00:00
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
|
ColumnWithTypeAndName & getByName(const std::string & name);
|
|
|
|
|
const ColumnWithTypeAndName & getByName(const std::string & name) const;
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
2011-12-12 06:15:34 +00:00
|
|
|
|
bool has(const std::string & name) const;
|
|
|
|
|
|
2011-08-12 20:39:42 +00:00
|
|
|
|
size_t getPositionByName(const std::string & name) const;
|
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
|
ColumnsWithTypeAndName getColumns() const;
|
2011-11-06 02:29:13 +00:00
|
|
|
|
NamesAndTypesList getColumnsList() const;
|
|
|
|
|
|
2017-01-02 20:42:49 +00:00
|
|
|
|
/// Returns number of rows from first column in block, not equal to nullptr. If no columns, returns 0.
|
2010-03-04 19:20:28 +00:00
|
|
|
|
size_t rows() const;
|
2013-06-08 20:19:29 +00:00
|
|
|
|
|
2016-08-04 21:40:20 +00:00
|
|
|
|
size_t columns() const { return data.size(); }
|
2010-03-04 19:20:28 +00:00
|
|
|
|
|
2017-01-02 20:42:49 +00:00
|
|
|
|
/// Checks that every column in block is not nullptr and has same number of elements.
|
|
|
|
|
void checkNumberOfRows() const;
|
|
|
|
|
|
|
|
|
|
/// Approximate number of bytes in memory - for profiling and limits.
|
2012-05-17 19:15:53 +00:00
|
|
|
|
size_t bytes() const;
|
|
|
|
|
|
2016-08-04 21:40:20 +00:00
|
|
|
|
operator bool() const { return !data.empty(); }
|
|
|
|
|
bool operator!() const { return data.empty(); }
|
2011-08-14 00:49:30 +00:00
|
|
|
|
|
|
|
|
|
/** Получить список имён столбцов через запятую. */
|
|
|
|
|
std::string dumpNames() const;
|
2011-09-19 03:34:23 +00:00
|
|
|
|
|
2013-12-05 12:46:29 +00:00
|
|
|
|
/** Список имен, типов и длин столбцов. Предназначен для отладки. */
|
|
|
|
|
std::string dumpStructure() const;
|
|
|
|
|
|
2011-09-19 03:34:23 +00:00
|
|
|
|
/** Получить такой же блок, но пустой. */
|
|
|
|
|
Block cloneEmpty() const;
|
2014-06-25 00:17:51 +00:00
|
|
|
|
|
2015-02-13 20:37:30 +00:00
|
|
|
|
/** Получить блок со столбцами, переставленными в порядке их имён. */
|
|
|
|
|
Block sortColumns() const;
|
|
|
|
|
|
2013-08-02 14:26:04 +00:00
|
|
|
|
/** Заменяет столбцы смещений внутри вложенных таблиц на один общий для таблицы.
|
|
|
|
|
* Кидает исключение, если эти смещения вдруг оказались неодинаковы.
|
|
|
|
|
*/
|
|
|
|
|
void optimizeNestedArraysOffsets();
|
|
|
|
|
/** Тоже самое, только без замены смещений. */
|
|
|
|
|
void checkNestedArraysOffsets() const;
|
2013-11-17 19:14:17 +00:00
|
|
|
|
|
|
|
|
|
void clear();
|
2016-05-30 20:54:39 +00:00
|
|
|
|
void swap(Block & other) noexcept;
|
2016-07-05 16:23:37 +00:00
|
|
|
|
|
2016-08-07 15:13:40 +00:00
|
|
|
|
/** Some column implementations (ColumnArray) may have shared parts between different columns
|
|
|
|
|
* (common array sizes of elements of nested data structures).
|
|
|
|
|
* Before doing mutating operations on such columns, you must unshare that parts.
|
|
|
|
|
* Also unsharing columns, if whole columns are shared_ptrs pointing to same instances.
|
|
|
|
|
*/
|
|
|
|
|
void unshareColumns();
|
|
|
|
|
|
2016-08-04 21:40:20 +00:00
|
|
|
|
private:
|
|
|
|
|
void eraseImpl(size_t position);
|
2017-01-04 17:25:07 +00:00
|
|
|
|
void initializeIndexByName();
|
2010-03-01 16:59:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-05-28 10:29:17 +00:00
|
|
|
|
using Blocks = std::vector<Block>;
|
|
|
|
|
using BlocksList = std::list<Block>;
|
2012-07-17 20:04:39 +00:00
|
|
|
|
|
2016-12-21 00:18:11 +00:00
|
|
|
|
|
2013-06-21 23:27:26 +00:00
|
|
|
|
/// Сравнить типы столбцов у блоков. Порядок столбцов имеет значение. Имена не имеют значения.
|
|
|
|
|
bool blocksHaveEqualStructure(const Block & lhs, const Block & rhs);
|
|
|
|
|
|
2016-12-21 00:18:11 +00:00
|
|
|
|
/// Calculate difference in structure of blocks and write description into output strings.
|
|
|
|
|
void getBlocksDifference(const Block & lhs, const Block & rhs, std::string & out_lhs_diff, std::string & out_rhs_diff);
|
|
|
|
|
|
2016-12-20 22:22:23 +00:00
|
|
|
|
|
2015-10-12 14:53:16 +00:00
|
|
|
|
/** Дополнительные данные к блокам. Они пока нужны только для запроса
|
|
|
|
|
* DESCRIBE TABLE с Distributed-таблицами.
|
|
|
|
|
*/
|
|
|
|
|
struct BlockExtraInfo
|
|
|
|
|
{
|
|
|
|
|
BlockExtraInfo() {}
|
|
|
|
|
operator bool() const { return is_valid; }
|
|
|
|
|
bool operator!() const { return !is_valid; }
|
|
|
|
|
|
|
|
|
|
std::string host;
|
|
|
|
|
std::string resolved_address;
|
|
|
|
|
std::string user;
|
|
|
|
|
UInt16 port = 0;
|
|
|
|
|
|
|
|
|
|
bool is_valid = false;
|
|
|
|
|
};
|
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
|
}
|