1 | #pragma once |
2 | |
3 | #include <ext/shared_ptr_helper.h> |
4 | #include <optional> |
5 | #include <Storages/IStorage.h> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | class Context; |
12 | |
13 | |
14 | /** Implements a table engine for the system table "numbers". |
15 | * The table contains the only column number UInt64. |
16 | * From this table, you can read all natural numbers, starting from 0 (to 2^64 - 1, and then again). |
17 | * |
18 | * You could also specify a limit (how many numbers to give). |
19 | * If multithreaded is specified, numbers will be generated in several streams |
20 | * (and result could be out of order). If both multithreaded and limit are specified, |
21 | * the table could give you not exactly 1..limit range, but some arbitrary 'limit' numbers. |
22 | * |
23 | * In multithreaded case, if even_distributed is False, implementation with atomic is used, |
24 | * and result is always in [0 ... limit - 1] range. |
25 | */ |
26 | class StorageSystemNumbers : public ext::shared_ptr_helper<StorageSystemNumbers>, public IStorage |
27 | { |
28 | friend struct ext::shared_ptr_helper<StorageSystemNumbers>; |
29 | public: |
30 | std::string getName() const override { return "SystemNumbers" ; } |
31 | std::string getTableName() const override { return name; } |
32 | std::string getDatabaseName() const override { return "system" ; } |
33 | |
34 | BlockInputStreams read( |
35 | const Names & column_names, |
36 | const SelectQueryInfo & query_info, |
37 | const Context & context, |
38 | QueryProcessingStage::Enum processed_stage, |
39 | size_t max_block_size, |
40 | unsigned num_streams) override; |
41 | |
42 | private: |
43 | const std::string name; |
44 | bool multithreaded; |
45 | bool even_distribution; |
46 | std::optional<UInt64> limit; |
47 | UInt64 offset; |
48 | |
49 | protected: |
50 | /// If even_distribution is true, numbers are distributed evenly between streams. |
51 | /// Otherwise, streams concurrently increment atomic. |
52 | StorageSystemNumbers(const std::string & name_, bool multithreaded_, std::optional<UInt64> limit_ = std::nullopt, UInt64 offset_ = 0, bool even_distribution_ = true); |
53 | }; |
54 | |
55 | } |
56 | |