2019-12-26 18:17:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-05 17:29:40 +00:00
|
|
|
#include <Parsers/IAST_fwd.h>
|
2020-06-16 12:03:27 +00:00
|
|
|
#include <Storages/ColumnDependency.h>
|
2019-12-26 18:17:05 +00:00
|
|
|
#include <Storages/ColumnsDescription.h>
|
|
|
|
#include <Storages/ConstraintsDescription.h>
|
2020-06-05 17:29:40 +00:00
|
|
|
#include <Storages/IndicesDescription.h>
|
|
|
|
#include <Storages/KeyDescription.h>
|
|
|
|
#include <Storages/SelectQueryDescription.h>
|
2020-06-16 12:03:27 +00:00
|
|
|
#include <Storages/TTLDescription.h>
|
2020-06-05 17:29:40 +00:00
|
|
|
|
2020-06-15 16:55:33 +00:00
|
|
|
#include <Common/MultiVersion.h>
|
2019-12-26 18:17:05 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-12-27 15:01:41 +00:00
|
|
|
/// Structure represent table metadata stored in memory.
|
|
|
|
/// Only one storage engine support all fields -- MergeTree.
|
|
|
|
/// Complete table AST can be recreated from this struct.
|
2019-12-26 18:17:05 +00:00
|
|
|
struct StorageInMemoryMetadata
|
|
|
|
{
|
2019-12-27 15:01:41 +00:00
|
|
|
/// Columns of table with their names, types,
|
|
|
|
/// defaults, comments, etc. All table engines have columns.
|
2019-12-26 18:17:05 +00:00
|
|
|
ColumnsDescription columns;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// Table indices. Currently supported for MergeTree only.
|
2020-06-01 12:11:23 +00:00
|
|
|
IndicesDescription secondary_indices;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// Table constraints. Currently supported for MergeTree only.
|
2019-12-26 18:17:05 +00:00
|
|
|
ConstraintsDescription constraints;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// PARTITION BY expression. Currently supported for MergeTree only.
|
2020-06-05 17:29:40 +00:00
|
|
|
KeyDescription partition_key;
|
|
|
|
/// PRIMARY KEY expression. If absent, than equal to order_by_ast.
|
|
|
|
KeyDescription primary_key;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// ORDER BY expression. Required field for all MergeTree tables
|
|
|
|
/// even in old syntax MergeTree(partition_key, order_by, ...)
|
2020-06-05 17:29:40 +00:00
|
|
|
KeyDescription sorting_key;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// SAMPLE BY expression. Supported for MergeTree only.
|
2020-06-05 17:29:40 +00:00
|
|
|
KeyDescription sampling_key;
|
|
|
|
/// Separate ttl expressions for columns
|
|
|
|
TTLColumnsDescription column_ttls_by_name;
|
|
|
|
/// TTL expressions for table (Move and Rows)
|
|
|
|
TTLTableDescription table_ttl;
|
2019-12-27 15:01:41 +00:00
|
|
|
/// SETTINGS expression. Supported for MergeTree, Buffer and Kafka.
|
2020-06-05 17:29:40 +00:00
|
|
|
ASTPtr settings_changes;
|
|
|
|
/// SELECT QUERY. Supported for MaterializedView and View (have to support LiveView).
|
|
|
|
SelectQueryDescription select;
|
2020-02-14 13:17:50 +00:00
|
|
|
|
|
|
|
StorageInMemoryMetadata() = default;
|
2020-06-01 12:11:23 +00:00
|
|
|
StorageInMemoryMetadata(const ColumnsDescription & columns_, const IndicesDescription & secondary_indices_, const ConstraintsDescription & constraints_);
|
2020-06-08 14:18:38 +00:00
|
|
|
|
|
|
|
StorageInMemoryMetadata(const StorageInMemoryMetadata & other);
|
|
|
|
StorageInMemoryMetadata & operator=(const StorageInMemoryMetadata & other);
|
2020-06-15 16:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
void setColumns(ColumnsDescription columns_); /// sets only real columns, possibly overwrites virtual ones.
|
|
|
|
|
|
|
|
void setSecondaryIndices(IndicesDescription secondary_indices_);
|
|
|
|
|
|
|
|
void setConstraints(ConstraintsDescription constraints_);
|
|
|
|
|
2020-06-15 17:17:06 +00:00
|
|
|
/// Set partition key for storage (methods bellow, are just wrappers for this
|
|
|
|
/// struct).
|
|
|
|
void setPartitionKey(const KeyDescription & partition_key_);
|
|
|
|
/// Set sorting key for storage (methods bellow, are just wrappers for this
|
|
|
|
/// struct).
|
|
|
|
void setSortingKey(const KeyDescription & sorting_key_);
|
|
|
|
/// Set primary key for storage (methods bellow, are just wrappers for this
|
|
|
|
/// struct).
|
|
|
|
void setPrimaryKey(const KeyDescription & primary_key_);
|
|
|
|
/// Set sampling key for storage (methods bellow, are just wrappers for this
|
|
|
|
/// struct).
|
|
|
|
void setSamplingKey(const KeyDescription & sampling_key_);
|
2020-06-15 17:50:53 +00:00
|
|
|
|
|
|
|
void setTableTTLs(const TTLTableDescription & table_ttl_);
|
|
|
|
|
|
|
|
void setColumnTTLs(const TTLColumnsDescription & column_ttls_by_name_);
|
2020-06-15 18:08:05 +00:00
|
|
|
|
|
|
|
void setSettingsChanges(const ASTPtr & settings_changes_);
|
|
|
|
|
|
|
|
void setSelectQuery(const SelectQueryDescription & select_);
|
2020-06-16 12:03:27 +00:00
|
|
|
|
|
|
|
const ColumnsDescription & getColumns() const; /// returns combined set of columns
|
|
|
|
const IndicesDescription & getSecondaryIndices() const;
|
|
|
|
/// Has at least one non primary index
|
|
|
|
bool hasSecondaryIndices() const;
|
|
|
|
|
|
|
|
const ConstraintsDescription & getConstraints() const;
|
|
|
|
|
2020-06-17 13:39:26 +00:00
|
|
|
/// Returns true if there is set table TTL, any column TTL or any move TTL.
|
|
|
|
bool hasAnyTTL() const { return hasAnyColumnTTL() || hasAnyTableTTL(); }
|
|
|
|
|
2020-06-16 12:03:27 +00:00
|
|
|
/// Common tables TTLs (for rows and moves).
|
|
|
|
TTLTableDescription getTableTTLs() const;
|
|
|
|
bool hasAnyTableTTL() const;
|
|
|
|
|
|
|
|
/// Separate TTLs for columns.
|
|
|
|
TTLColumnsDescription getColumnTTLs() const;
|
|
|
|
bool hasAnyColumnTTL() const;
|
|
|
|
|
|
|
|
/// Just wrapper for table TTLs, return rows part of table TTLs.
|
|
|
|
TTLDescription getRowsTTL() const;
|
|
|
|
bool hasRowsTTL() const;
|
|
|
|
|
|
|
|
/// Just wrapper for table TTLs, return moves (to disks or volumes) parts of
|
|
|
|
/// table TTL.
|
|
|
|
TTLDescriptions getMoveTTLs() const;
|
|
|
|
bool hasAnyMoveTTL() const;
|
|
|
|
|
|
|
|
/// Returns columns, which will be needed to calculate dependencies (skip
|
|
|
|
/// indices, TTL expressions) if we update @updated_columns set of columns.
|
|
|
|
ColumnDependencies getColumnDependencies(const NameSet & updated_columns) const;
|
2020-06-16 12:48:10 +00:00
|
|
|
|
2020-06-16 12:58:05 +00:00
|
|
|
Block getSampleBlock() const; /// ordinary + materialized.
|
2020-06-16 12:48:10 +00:00
|
|
|
Block getSampleBlockNonMaterialized() const; /// ordinary.
|
2020-06-16 12:58:05 +00:00
|
|
|
Block getSampleBlockWithVirtuals(const NamesAndTypesList & virtuals) const; /// ordinary + materialized + virtuals.
|
2020-06-16 14:25:08 +00:00
|
|
|
Block getSampleBlockForColumns(
|
|
|
|
const Names & column_names, const NamesAndTypesList & virtuals) const; /// ordinary + materialized + aliases + virtuals.
|
2020-06-17 10:34:23 +00:00
|
|
|
|
|
|
|
/// Returns structure with partition key.
|
|
|
|
const KeyDescription & getPartitionKey() const;
|
|
|
|
/// Returns ASTExpressionList of partition key expression for storage or nullptr if there is none.
|
|
|
|
ASTPtr getPartitionKeyAST() const { return partition_key.definition_ast; }
|
|
|
|
/// Storage has user-defined (in CREATE query) partition key.
|
|
|
|
bool isPartitionKeyDefined() const;
|
|
|
|
/// Storage has partition key.
|
|
|
|
bool hasPartitionKey() const;
|
|
|
|
/// Returns column names that need to be read to calculate partition key.
|
|
|
|
Names getColumnsRequiredForPartitionKey() const;
|
2020-06-17 11:05:11 +00:00
|
|
|
|
|
|
|
/// Returns structure with sorting key.
|
|
|
|
const KeyDescription & getSortingKey() const;
|
|
|
|
/// Returns ASTExpressionList of sorting key expression for storage or nullptr if there is none.
|
|
|
|
ASTPtr getSortingKeyAST() const { return sorting_key.definition_ast; }
|
|
|
|
/// Storage has user-defined (in CREATE query) sorting key.
|
|
|
|
bool isSortingKeyDefined() const;
|
|
|
|
/// Storage has sorting key. It means, that it contains at least one column.
|
|
|
|
bool hasSortingKey() const;
|
|
|
|
/// Returns column names that need to be read to calculate sorting key.
|
|
|
|
Names getColumnsRequiredForSortingKey() const;
|
|
|
|
/// Returns columns names in sorting key specified by user in ORDER BY
|
|
|
|
/// expression. For example: 'a', 'x * y', 'toStartOfMonth(date)', etc.
|
|
|
|
Names getSortingKeyColumns() const;
|
|
|
|
|
|
|
|
/// Returns column names that need to be read for FINAL to work.
|
|
|
|
Names getColumnsRequiredForFinal() const { return getColumnsRequiredForSortingKey(); }
|
2020-06-17 12:07:09 +00:00
|
|
|
|
|
|
|
/// Returns structure with sampling key.
|
|
|
|
const KeyDescription & getSamplingKey() const;
|
|
|
|
/// Returns sampling expression AST for storage or nullptr if there is none.
|
|
|
|
ASTPtr getSamplingKeyAST() const { return sampling_key.definition_ast; }
|
|
|
|
/// Storage has user-defined (in CREATE query) sampling key.
|
|
|
|
bool isSamplingKeyDefined() const;
|
|
|
|
/// Storage has sampling key.
|
|
|
|
bool hasSamplingKey() const;
|
|
|
|
/// Returns column names that need to be read to calculate sampling key.
|
|
|
|
Names getColumnsRequiredForSampling() const;
|
2020-06-17 12:39:20 +00:00
|
|
|
|
|
|
|
/// Returns structure with primary key.
|
|
|
|
const KeyDescription & getPrimaryKey() const;
|
|
|
|
/// Returns ASTExpressionList of primary key expression for storage or nullptr if there is none.
|
|
|
|
ASTPtr getPrimaryKeyAST() const { return primary_key.definition_ast; }
|
|
|
|
/// Storage has user-defined (in CREATE query) sorting key.
|
|
|
|
bool isPrimaryKeyDefined() const;
|
|
|
|
/// Storage has primary key (maybe part of some other key). It means, that
|
|
|
|
/// it contains at least one column.
|
|
|
|
bool hasPrimaryKey() const;
|
|
|
|
/// Returns column names that need to be read to calculate primary key.
|
|
|
|
Names getColumnsRequiredForPrimaryKey() const;
|
|
|
|
/// Returns columns names in sorting key specified by. For example: 'a', 'x
|
|
|
|
/// * y', 'toStartOfMonth(date)', etc.
|
|
|
|
Names getPrimaryKeyColumns() const;
|
2020-06-17 13:46:01 +00:00
|
|
|
|
|
|
|
/// Storage settings
|
|
|
|
ASTPtr getSettingsChanges() const;
|
|
|
|
bool hasSettingsChanges() const { return settings_changes != nullptr; }
|
2020-06-17 14:06:22 +00:00
|
|
|
|
|
|
|
/// Select query for *View storages.
|
|
|
|
const SelectQueryDescription & getSelectQuery() const;
|
|
|
|
bool hasSelectQuery() const;
|
2020-06-17 14:32:25 +00:00
|
|
|
|
|
|
|
/// Verify that all the requested names are in the table and are set correctly:
|
|
|
|
/// list of names is not empty and the names do not repeat.
|
|
|
|
void check(const Names & column_names, const NamesAndTypesList & virtuals) const;
|
|
|
|
|
|
|
|
/// Check that all the requested names are in the table and have the correct types.
|
|
|
|
void check(const NamesAndTypesList & columns) const;
|
|
|
|
|
|
|
|
/// Check that all names from the intersection of `names` and `columns` are in the table and have the same types.
|
|
|
|
void check(const NamesAndTypesList & columns, const Names & column_names) const;
|
|
|
|
|
|
|
|
/// Check that the data block contains all the columns of the table with the correct types,
|
|
|
|
/// contains only the columns of the table, and all the columns are different.
|
|
|
|
/// If |need_all| is set, then checks that all the columns of the table are in the block.
|
|
|
|
void check(const Block & block, bool need_all = false) const;
|
2019-12-26 18:17:05 +00:00
|
|
|
};
|
|
|
|
|
2020-06-15 16:55:33 +00:00
|
|
|
using StorageMetadataPtr = std::shared_ptr<StorageInMemoryMetadata>;
|
|
|
|
using MultiVersionStorageMetadataPtr = MultiVersion<StorageInMemoryMetadata>;
|
|
|
|
|
2019-12-26 18:17:05 +00:00
|
|
|
}
|