1#include <Storages/System/StorageSystemDetachedParts.h>
2
3#include <DataTypes/DataTypeString.h>
4#include <DataTypes/DataTypesNumber.h>
5#include <DataTypes/DataTypeNullable.h>
6#include <DataStreams/OneBlockInputStream.h>
7#include <ext/shared_ptr_helper.h>
8#include <Storages/IStorage.h>
9#include <Storages/System/StorageSystemPartsBase.h>
10
11namespace DB
12{
13
14/**
15 * Implements system table 'detached_parts' which allows to get information
16 * about detached data parts for tables of MergeTree family.
17 * We don't use StorageSystemPartsBase, because it introduces virtual _state
18 * column and column aliases which we don't need.
19 */
20class StorageSystemDetachedParts :
21 public ext::shared_ptr_helper<StorageSystemDetachedParts>,
22 public IStorage
23{
24 friend struct ext::shared_ptr_helper<StorageSystemDetachedParts>;
25public:
26 std::string getName() const override { return "SystemDetachedParts"; }
27 std::string getTableName() const override { return "detached_parts"; }
28 std::string getDatabaseName() const override { return "system"; }
29
30protected:
31 explicit StorageSystemDetachedParts()
32 {
33 setColumns(ColumnsDescription{{
34 {"database", std::make_shared<DataTypeString>()},
35 {"table", std::make_shared<DataTypeString>()},
36 {"partition_id", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>())},
37 {"name", std::make_shared<DataTypeString>()},
38 {"disk", std::make_shared<DataTypeString>()},
39 {"reason", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeString>())},
40 {"min_block_number", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeInt64>())},
41 {"max_block_number", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeInt64>())},
42 {"level", std::make_shared<DataTypeNullable>(std::make_shared<DataTypeUInt32>())}
43 }});
44 }
45
46 BlockInputStreams read(
47 const Names & /* column_names */,
48 const SelectQueryInfo & query_info,
49 const Context & context,
50 QueryProcessingStage::Enum /*processed_stage*/,
51 const size_t /*max_block_size*/,
52 const unsigned /*num_streams*/) override
53 {
54 StoragesInfoStream stream(query_info, context);
55
56 /// Create the result.
57 Block block = getSampleBlock();
58 MutableColumns new_columns = block.cloneEmptyColumns();
59
60 while (StoragesInfo info = stream.next())
61 {
62 const auto parts = info.data->getDetachedParts();
63 for (auto & p : parts)
64 {
65 size_t i = 0;
66 new_columns[i++]->insert(info.database);
67 new_columns[i++]->insert(info.table);
68 new_columns[i++]->insert(p.valid_name ? p.partition_id : Field());
69 new_columns[i++]->insert(p.dir_name);
70 new_columns[i++]->insert(p.disk);
71 new_columns[i++]->insert(p.valid_name ? p.prefix : Field());
72 new_columns[i++]->insert(p.valid_name ? p.min_block : Field());
73 new_columns[i++]->insert(p.valid_name ? p.max_block : Field());
74 new_columns[i++]->insert(p.valid_name ? p.level : Field());
75 }
76 }
77
78 return BlockInputStreams(1, std::make_shared<OneBlockInputStream>(
79 block.cloneWithColumns(std::move(new_columns))));
80 }
81};
82
83StoragePtr
84createDetachedPartsTable()
85{
86 return StorageSystemDetachedParts::create();
87}
88
89}
90