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