| 1 | #pragma once |
| 2 | |
| 3 | #include <Core/Types.h> |
| 4 | #include <DataStreams/IBlockStream_fwd.h> |
| 5 | #include <Common/Stopwatch.h> |
| 6 | |
| 7 | #include <vector> |
| 8 | |
| 9 | namespace DB |
| 10 | { |
| 11 | |
| 12 | class Block; |
| 13 | class ReadBuffer; |
| 14 | class WriteBuffer; |
| 15 | |
| 16 | /// Information for profiling. See IBlockInputStream.h |
| 17 | struct BlockStreamProfileInfo |
| 18 | { |
| 19 | /// Info about stream object this profile info refers to. |
| 20 | IBlockInputStream * parent = nullptr; |
| 21 | |
| 22 | bool started = false; |
| 23 | Stopwatch total_stopwatch {CLOCK_MONOTONIC_COARSE}; /// Time with waiting time |
| 24 | |
| 25 | size_t rows = 0; |
| 26 | size_t blocks = 0; |
| 27 | size_t bytes = 0; |
| 28 | |
| 29 | using BlockStreamProfileInfos = std::vector<const BlockStreamProfileInfo *>; |
| 30 | |
| 31 | /// Collect BlockStreamProfileInfo for the nearest sources in the tree named `name`. Example; collect all info for PartialSorting streams. |
| 32 | void collectInfosForStreamsWithName(const char * name, BlockStreamProfileInfos & res) const; |
| 33 | |
| 34 | /** Get the number of rows if there were no LIMIT. |
| 35 | * If there is no LIMIT, 0 is returned. |
| 36 | * If the query does not contain ORDER BY, the number can be underestimated - return the number of rows in blocks that were read before LIMIT reached. |
| 37 | * If the query contains an ORDER BY, then returns the exact number of rows as if LIMIT is removed from query. |
| 38 | */ |
| 39 | size_t getRowsBeforeLimit() const; |
| 40 | bool hasAppliedLimit() const; |
| 41 | |
| 42 | void update(Block & block); |
| 43 | |
| 44 | /// Binary serialization and deserialization of main fields. |
| 45 | /// Writes only main fields i.e. fields that required by internal transmission protocol. |
| 46 | void read(ReadBuffer & in); |
| 47 | void write(WriteBuffer & out) const; |
| 48 | |
| 49 | /// Sets main fields from other object (see methods above). |
| 50 | /// If skip_block_size_info if true, then rows, bytes and block fields are ignored. |
| 51 | void setFrom(const BlockStreamProfileInfo & rhs, bool skip_block_size_info); |
| 52 | |
| 53 | /// Only for Processors. |
| 54 | void setRowsBeforeLimit(size_t rows_before_limit_) |
| 55 | { |
| 56 | applied_limit = true; |
| 57 | rows_before_limit = rows_before_limit_; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | void calculateRowsBeforeLimit() const; |
| 62 | |
| 63 | /// For these fields we make accessors, because they must be calculated beforehand. |
| 64 | mutable bool applied_limit = false; /// Whether LIMIT was applied |
| 65 | mutable size_t rows_before_limit = 0; |
| 66 | mutable bool calculated_rows_before_limit = false; /// Whether the field rows_before_limit was calculated |
| 67 | }; |
| 68 | |
| 69 | } |
| 70 | |