1 | #include <Functions/IFunctionImpl.h> |
---|---|
2 | #include <Functions/FunctionFactory.h> |
3 | #include <DataTypes/DataTypesNumber.h> |
4 | #include <Columns/ColumnsNumber.h> |
5 | #include <atomic> |
6 | |
7 | |
8 | namespace DB |
9 | { |
10 | |
11 | /** Incremental block number among calls of this function. */ |
12 | class FunctionBlockNumber : public IFunction |
13 | { |
14 | private: |
15 | std::atomic<size_t> block_number{0}; |
16 | |
17 | public: |
18 | static constexpr auto name = "blockNumber"; |
19 | static FunctionPtr create(const Context &) |
20 | { |
21 | return std::make_shared<FunctionBlockNumber>(); |
22 | } |
23 | |
24 | /// Get the function name. |
25 | String getName() const override |
26 | { |
27 | return name; |
28 | } |
29 | |
30 | bool isStateful() const override |
31 | { |
32 | return true; |
33 | } |
34 | |
35 | size_t getNumberOfArguments() const override |
36 | { |
37 | return 0; |
38 | } |
39 | |
40 | bool isDeterministic() const override { return false; } |
41 | |
42 | bool isDeterministicInScopeOfQuery() const override |
43 | { |
44 | return false; |
45 | } |
46 | |
47 | DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override |
48 | { |
49 | return std::make_shared<DataTypeUInt64>(); |
50 | } |
51 | |
52 | void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override |
53 | { |
54 | size_t current_block_number = block_number++; |
55 | block.getByPosition(result).column = ColumnUInt64::create(input_rows_count, current_block_number); |
56 | } |
57 | }; |
58 | |
59 | |
60 | void registerFunctionBlockNumber(FunctionFactory & factory) |
61 | { |
62 | factory.registerFunction<FunctionBlockNumber>(); |
63 | } |
64 | |
65 | } |
66 |