| 1 | #pragma once | 
|---|
| 2 |  | 
|---|
| 3 | #include <DataStreams/IBlockInputStream.h> | 
|---|
| 4 | #include <Common/SharedBlockRowRef.h> | 
|---|
| 5 | #include <Core/SortDescription.h> | 
|---|
| 6 |  | 
|---|
| 7 |  | 
|---|
| 8 | namespace DB | 
|---|
| 9 | { | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | /** Implements the LIMIT relational operation. | 
|---|
| 13 | */ | 
|---|
| 14 | class LimitBlockInputStream : public IBlockInputStream | 
|---|
| 15 | { | 
|---|
| 16 | public: | 
|---|
| 17 | /** If always_read_till_end = false (by default), then after reading enough data, | 
|---|
| 18 | *  returns an empty block, and this causes the query to be canceled. | 
|---|
| 19 | * If always_read_till_end = true - reads all the data to the end, but ignores them. This is necessary in rare cases: | 
|---|
| 20 | *  when otherwise, due to the cancellation of the request, we would not have received the data for GROUP BY WITH TOTALS from the remote server. | 
|---|
| 21 | * If use_limit_as_total_rows_approx = true, then addTotalRowsApprox is called to use the limit in progress & stats | 
|---|
| 22 | * with_ties = true, when query has WITH TIES modifier. If so, description should be provided | 
|---|
| 23 | * description lets us know which row we should check for equality | 
|---|
| 24 | */ | 
|---|
| 25 | LimitBlockInputStream( | 
|---|
| 26 | const BlockInputStreamPtr & input, UInt64 limit_, UInt64 offset_, | 
|---|
| 27 | bool always_read_till_end_ = false, bool use_limit_as_total_rows_approx = false, | 
|---|
| 28 | bool with_ties_ = false, const SortDescription & description_ = {}); | 
|---|
| 29 |  | 
|---|
| 30 | String getName() const override { return "Limit"; } | 
|---|
| 31 |  | 
|---|
| 32 | Block () const override { return children.at(0)->getHeader(); } | 
|---|
| 33 |  | 
|---|
| 34 | protected: | 
|---|
| 35 | Block readImpl() override; | 
|---|
| 36 |  | 
|---|
| 37 | private: | 
|---|
| 38 | UInt64 limit; | 
|---|
| 39 | UInt64 offset; | 
|---|
| 40 | UInt64 pos = 0; | 
|---|
| 41 | bool always_read_till_end; | 
|---|
| 42 | bool with_ties; | 
|---|
| 43 | const SortDescription description; | 
|---|
| 44 | SharedBlockRowRef ties_row_ref; | 
|---|
| 45 | }; | 
|---|
| 46 |  | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|