2011-08-28 02:22:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Block.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/BlockStreamProfileInfo.h>
|
2019-10-02 21:17:19 +00:00
|
|
|
#include <DataStreams/IBlockStream_fwd.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <DataStreams/SizeLimits.h>
|
2019-10-03 18:27:11 +00:00
|
|
|
#include <DataStreams/ExecutionSpeedLimits.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
#include <IO/Progress.h>
|
2019-03-05 10:12:20 +00:00
|
|
|
#include <Storages/TableStructureLockHolder.h>
|
2019-10-02 21:17:19 +00:00
|
|
|
#include <Common/TypePromotion.h>
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <shared_mutex>
|
2010-03-01 16:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
}
|
2016-05-28 12:22:22 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
class ProcessListElement;
|
2020-03-07 17:37:38 +00:00
|
|
|
class EnabledQuota;
|
2019-01-23 14:48:50 +00:00
|
|
|
class QueryStatus;
|
2019-10-04 17:46:36 +00:00
|
|
|
struct SortColumnDescription;
|
|
|
|
using SortDescription = std::vector<SortColumnDescription>;
|
2016-05-28 12:22:22 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Callback to track the progress of the query.
|
2019-01-23 14:48:50 +00:00
|
|
|
* Used in IBlockInputStream and Context.
|
2017-05-13 22:19:04 +00:00
|
|
|
* The function takes the number of rows in the last block, the number of bytes in the last block.
|
|
|
|
* Note that the callback can be called from different threads.
|
2013-02-16 14:55:14 +00:00
|
|
|
*/
|
2016-05-28 10:35:44 +00:00
|
|
|
using ProgressCallback = std::function<void(const Progress & progress)>;
|
2013-02-16 14:55:14 +00:00
|
|
|
|
2011-08-28 02:22:23 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** The stream interface for reading data by blocks from the database.
|
|
|
|
* Relational operations are supposed to be done also as implementations of this interface.
|
2019-01-23 14:48:50 +00:00
|
|
|
* Watches out at how the source of the blocks works.
|
|
|
|
* Lets you get information for profiling: rows per second, blocks per second, megabytes per second, etc.
|
|
|
|
* Allows you to stop reading data (in nested sources).
|
2010-03-01 16:59:51 +00:00
|
|
|
*/
|
2019-10-02 21:17:19 +00:00
|
|
|
class IBlockInputStream : public TypePromotion<IBlockInputStream>
|
2010-03-01 16:59:51 +00:00
|
|
|
{
|
2019-01-23 14:48:50 +00:00
|
|
|
friend struct BlockStreamProfileInfo;
|
|
|
|
|
2010-03-01 16:59:51 +00:00
|
|
|
public:
|
2019-01-23 14:48:50 +00:00
|
|
|
IBlockInputStream() { info.parent = this; }
|
|
|
|
virtual ~IBlockInputStream() {}
|
|
|
|
|
|
|
|
IBlockInputStream(const IBlockInputStream &) = delete;
|
|
|
|
IBlockInputStream & operator=(const IBlockInputStream &) = delete;
|
|
|
|
|
|
|
|
/// To output the data stream transformation tree (query execution plan).
|
|
|
|
virtual String getName() const = 0;
|
2014-10-25 18:33:52 +00:00
|
|
|
|
2018-01-06 18:10:44 +00:00
|
|
|
/** Get data structure of the stream in a form of "header" block (it is also called "sample block").
|
2018-01-09 00:19:58 +00:00
|
|
|
* Header block contains column names, data types, columns of size 0. Constant columns must have corresponding values.
|
2018-01-06 18:10:44 +00:00
|
|
|
* It is guaranteed that method "read" returns blocks of exactly that structure.
|
|
|
|
*/
|
2018-02-18 03:23:48 +00:00
|
|
|
virtual Block getHeader() const = 0;
|
2018-01-06 18:10:44 +00:00
|
|
|
|
2018-12-05 12:27:21 +00:00
|
|
|
virtual const BlockMissingValues & getMissingValues() const
|
|
|
|
{
|
|
|
|
static const BlockMissingValues none;
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// If this stream generates data in order by some keys, return true.
|
|
|
|
virtual bool isSortedOutput() const { return false; }
|
|
|
|
|
|
|
|
/// In case of isSortedOutput, return corresponding SortDescription
|
2019-10-04 17:46:36 +00:00
|
|
|
virtual const SortDescription & getSortDescription() const;
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
/** Read next block.
|
|
|
|
* If there are no more blocks, return an empty block (for which operator `bool` returns false).
|
|
|
|
* NOTE: Only one thread can read from one instance of IBlockInputStream simultaneously.
|
|
|
|
* This also applies for readPrefix, readSuffix.
|
|
|
|
*/
|
|
|
|
Block read();
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Read something before starting all data or after the end of all data.
|
|
|
|
* In the `readSuffix` function, you can implement a finalization that can lead to an exception.
|
|
|
|
* readPrefix() must be called before the first call to read().
|
|
|
|
* readSuffix() should be called after read() returns an empty block, or after a call to cancel(), but not during read() execution.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2011-09-04 21:23:19 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/** The default implementation calls readPrefixImpl() on itself, and then readPrefix() recursively for all children.
|
|
|
|
* There are cases when you do not want `readPrefix` of children to be called synchronously, in this function,
|
|
|
|
* but you want them to be called, for example, in separate threads (for parallel initialization of children).
|
|
|
|
* Then overload `readPrefix` function.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
virtual void readPrefix();
|
2011-09-04 21:23:19 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/** The default implementation calls recursively readSuffix() on all children, and then readSuffixImpl() on itself.
|
|
|
|
* If this stream calls read() in children in a separate thread, this behavior is usually incorrect:
|
|
|
|
* readSuffix() of the child can not be called at the moment when the same child's read() is executed in another thread.
|
|
|
|
* In this case, you need to override this method so that readSuffix() in children is called, for example, after connecting streams.
|
2018-02-23 10:43:24 +00:00
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
virtual void readSuffix();
|
|
|
|
|
|
|
|
/// Must be called before `read()` and `readPrefix()`.
|
2018-08-31 01:46:39 +00:00
|
|
|
void dumpTree(std::ostream & ostr, size_t indent = 0, size_t multiplier = 1) const;
|
2011-09-04 21:23:19 +00:00
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Check the depth of the pipeline.
|
|
|
|
* If max_depth is specified and the `depth` is greater - throw an exception.
|
2019-01-23 14:48:50 +00:00
|
|
|
* Must be called before `read()` and `readPrefix()`.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2019-01-23 14:48:50 +00:00
|
|
|
size_t checkDepth(size_t max_depth) const { return checkDepthImpl(max_depth, max_depth); }
|
2012-12-25 20:36:35 +00:00
|
|
|
|
2019-03-06 13:48:59 +00:00
|
|
|
/// Do not allow to change the table while the blocks stream and its children are alive.
|
2019-03-07 18:04:47 +00:00
|
|
|
void addTableLock(const TableStructureReadLockHolder & lock) { table_locks.push_back(lock); }
|
2013-05-04 05:20:07 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// Get information about execution speed.
|
|
|
|
const BlockStreamProfileInfo & getProfileInfo() const { return info; }
|
2012-03-09 04:45:27 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/** Get "total" values.
|
|
|
|
* The default implementation takes them from itself or from the first child source in which they are.
|
|
|
|
* The overridden method can perform some calculations. For example, apply an expression to the `totals` of the child source.
|
|
|
|
* There can be no total values - then an empty block is returned.
|
|
|
|
*
|
|
|
|
* Call this method only after all the data has been retrieved with `read`,
|
|
|
|
* otherwise there will be problems if any data at the same time is computed in another thread.
|
|
|
|
*/
|
|
|
|
virtual Block getTotals();
|
|
|
|
|
|
|
|
/// The same for minimums and maximums.
|
2019-08-08 14:46:30 +00:00
|
|
|
virtual Block getExtremes();
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Set the execution progress bar callback.
|
|
|
|
* The callback is passed to all child sources.
|
|
|
|
* By default, it is called for leaf sources, after each block.
|
|
|
|
* (But this can be overridden in the progress() method)
|
|
|
|
* The function takes the number of rows in the last block, the number of bytes in the last block.
|
|
|
|
* Note that the callback can be called from different threads.
|
|
|
|
*/
|
2019-10-04 15:40:05 +00:00
|
|
|
virtual void setProgressCallback(const ProgressCallback & callback);
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** In this method:
|
|
|
|
* - the progress callback is called;
|
|
|
|
* - the status of the query execution in ProcessList is updated;
|
|
|
|
* - checks restrictions and quotas that should be checked not within the same source,
|
|
|
|
* but over the total amount of resources spent in all sources at once (information in the ProcessList).
|
|
|
|
*/
|
|
|
|
virtual void progress(const Progress & value)
|
2018-02-23 10:43:24 +00:00
|
|
|
{
|
2019-01-23 14:48:50 +00:00
|
|
|
/// The data for progress is taken from leaf sources.
|
|
|
|
if (children.empty())
|
|
|
|
progressImpl(value);
|
|
|
|
}
|
2018-04-17 14:26:07 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
void progressImpl(const Progress & value);
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the pointer to the process list item.
|
|
|
|
* It is passed to all child sources.
|
|
|
|
* General information about the resources spent on the request will be written into it.
|
|
|
|
* Based on this information, the quota and some restrictions will be checked.
|
|
|
|
* This information will also be available in the SHOW PROCESSLIST request.
|
|
|
|
*/
|
2019-10-04 15:40:05 +00:00
|
|
|
virtual void setProcessListElement(QueryStatus * elem);
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
/** Set the approximate total number of rows to read.
|
|
|
|
*/
|
2019-10-04 15:40:05 +00:00
|
|
|
virtual void addTotalRowsApprox(size_t value) { total_rows_approx += value; }
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Ask to abort the receipt of data as soon as possible.
|
|
|
|
* By default - just sets the flag is_cancelled and asks that all children be interrupted.
|
|
|
|
* This function can be called several times, including simultaneously from different threads.
|
|
|
|
* Have two modes:
|
|
|
|
* with kill = false only is_cancelled is set - streams will stop silently with returning some processed data.
|
|
|
|
* with kill = true also is_killed set - queries will stop with exception.
|
|
|
|
*/
|
|
|
|
virtual void cancel(bool kill);
|
|
|
|
|
|
|
|
bool isCancelled() const;
|
|
|
|
bool isCancelledOrThrowIfKilled() const;
|
|
|
|
|
|
|
|
/** What limitations and quotas should be checked.
|
2020-03-31 20:12:04 +00:00
|
|
|
* LIMITS_CURRENT - checks amount of data returned by current stream only (BlockStreamProfileInfo is used for check).
|
2019-01-23 14:48:50 +00:00
|
|
|
* Currently it is used in root streams to check max_result_{rows,bytes} limits.
|
|
|
|
* LIMITS_TOTAL - checks total amount of read data from leaf streams (i.e. data read from disk and remote servers).
|
|
|
|
* It is checks max_{rows,bytes}_to_read in progress handler and use info from ProcessListElement::progress_in for this.
|
|
|
|
* Currently this check is performed only in leaf streams.
|
|
|
|
*/
|
|
|
|
enum LimitsMode
|
|
|
|
{
|
|
|
|
LIMITS_CURRENT,
|
|
|
|
LIMITS_TOTAL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// It is a subset of limitations from Limits.
|
|
|
|
struct LocalLimits
|
|
|
|
{
|
|
|
|
LimitsMode mode = LIMITS_CURRENT;
|
|
|
|
|
|
|
|
SizeLimits size_limits;
|
|
|
|
|
2019-10-10 14:16:15 +00:00
|
|
|
ExecutionSpeedLimits speed_limits;
|
2019-01-23 14:48:50 +00:00
|
|
|
|
2019-10-03 18:27:11 +00:00
|
|
|
OverflowMode timeout_overflow_mode = OverflowMode::THROW;
|
2019-01-23 14:48:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Set limitations that checked on each block. */
|
2019-10-04 15:40:05 +00:00
|
|
|
virtual void setLimits(const LocalLimits & limits_)
|
2019-01-23 14:48:50 +00:00
|
|
|
{
|
|
|
|
limits = limits_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LocalLimits & getLimits() const
|
|
|
|
{
|
|
|
|
return limits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Set the quota. If you set a quota on the amount of raw data,
|
|
|
|
* then you should also set mode = LIMITS_TOTAL to LocalLimits with setLimits.
|
|
|
|
*/
|
2020-03-31 20:12:04 +00:00
|
|
|
virtual void setQuota(const std::shared_ptr<const EnabledQuota> & new_quota)
|
2019-01-23 14:48:50 +00:00
|
|
|
{
|
2020-03-31 20:12:04 +00:00
|
|
|
quota = new_quota;
|
2018-02-23 10:43:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
/// Enable calculation of minimums and maximums by the result columns.
|
|
|
|
void enableExtremes() { enabled_extremes = true; }
|
|
|
|
|
2018-02-23 10:43:24 +00:00
|
|
|
protected:
|
2019-03-06 12:25:59 +00:00
|
|
|
/// Order is important: `table_locks` must be destroyed after `children` so that tables from
|
|
|
|
/// which child streams read are protected by the locks during the lifetime of the child streams.
|
2019-03-07 18:04:47 +00:00
|
|
|
std::vector<TableStructureReadLockHolder> table_locks;
|
2019-03-06 12:25:59 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockInputStreams children;
|
2018-04-17 14:26:07 +00:00
|
|
|
std::shared_mutex children_mutex;
|
2014-02-23 02:27:09 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
BlockStreamProfileInfo info;
|
|
|
|
std::atomic<bool> is_cancelled{false};
|
|
|
|
std::atomic<bool> is_killed{false};
|
|
|
|
ProgressCallback progress_callback;
|
|
|
|
QueryStatus * process_list_elem = nullptr;
|
|
|
|
/// According to total_stopwatch in microseconds
|
|
|
|
UInt64 last_profile_events_update_time = 0;
|
|
|
|
|
|
|
|
/// Additional information that can be generated during the work process.
|
|
|
|
|
|
|
|
/// Total values during aggregation.
|
|
|
|
Block totals;
|
|
|
|
/// Minimums and maximums. The first row of the block - minimums, the second - the maximums.
|
|
|
|
Block extremes;
|
|
|
|
|
|
|
|
|
2019-01-25 12:48:59 +00:00
|
|
|
void addChild(const BlockInputStreamPtr & child)
|
2019-01-23 14:48:50 +00:00
|
|
|
{
|
|
|
|
std::unique_lock lock(children_mutex);
|
|
|
|
children.push_back(child);
|
|
|
|
}
|
|
|
|
|
2019-05-14 15:52:03 +00:00
|
|
|
/** Check limits.
|
|
|
|
* But only those that can be checked within each separate stream.
|
|
|
|
*/
|
|
|
|
bool checkTimeLimit();
|
|
|
|
|
2019-12-03 21:12:47 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool read_prefix_is_called = false;
|
|
|
|
bool read_suffix_is_called = false;
|
|
|
|
#endif
|
|
|
|
|
2012-03-09 04:45:27 +00:00
|
|
|
private:
|
2019-01-23 14:48:50 +00:00
|
|
|
bool enabled_extremes = false;
|
|
|
|
|
|
|
|
/// The limit on the number of rows/bytes has been exceeded, and you need to stop execution on the next `read` call, as if the thread has run out.
|
|
|
|
bool limit_exceeded_need_break = false;
|
|
|
|
|
|
|
|
/// Limitations and quotas.
|
|
|
|
|
|
|
|
LocalLimits limits;
|
|
|
|
|
2020-03-07 17:37:38 +00:00
|
|
|
std::shared_ptr<const EnabledQuota> quota; /// If nullptr - the quota is not used.
|
2019-11-04 19:17:27 +00:00
|
|
|
UInt64 prev_elapsed = 0;
|
2019-01-23 14:48:50 +00:00
|
|
|
|
|
|
|
/// The approximate total number of rows to read. For progress bar.
|
|
|
|
size_t total_rows_approx = 0;
|
|
|
|
|
2020-02-17 18:53:59 +00:00
|
|
|
/// Derived classes must implement this function.
|
2019-01-23 14:48:50 +00:00
|
|
|
virtual Block readImpl() = 0;
|
|
|
|
|
|
|
|
/// Here you can do a preliminary initialization.
|
|
|
|
virtual void readPrefixImpl() {}
|
|
|
|
|
|
|
|
/// Here you need to do a finalization, which can lead to an exception.
|
|
|
|
virtual void readSuffixImpl() {}
|
|
|
|
|
|
|
|
void updateExtremes(Block & block);
|
|
|
|
|
2019-05-14 15:52:03 +00:00
|
|
|
/** Check quotas.
|
2019-01-23 14:48:50 +00:00
|
|
|
* But only those that can be checked within each separate stream.
|
|
|
|
*/
|
|
|
|
void checkQuota(Block & block);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t checkDepthImpl(size_t max_depth, size_t level) const;
|
2013-05-04 05:20:07 +00:00
|
|
|
|
2018-02-19 19:18:51 +00:00
|
|
|
/// Get text with names of this source and the entire subtree.
|
2017-04-01 07:20:54 +00:00
|
|
|
String getTreeID() const;
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
template <typename F>
|
|
|
|
void forEachChild(F && f)
|
|
|
|
{
|
|
|
|
/// NOTE: Acquire a read lock, therefore f() should be thread safe
|
|
|
|
std::shared_lock lock(children_mutex);
|
2011-08-28 02:22:23 +00:00
|
|
|
|
2019-08-30 08:09:13 +00:00
|
|
|
// Reduce lock scope and avoid recursive locking since that is undefined for shared_mutex.
|
|
|
|
const auto children_copy = children;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
for (auto & child : children_copy)
|
2019-01-23 14:48:50 +00:00
|
|
|
if (f(*child))
|
|
|
|
return;
|
|
|
|
}
|
2019-02-06 21:40:49 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
};
|
2010-03-01 16:59:51 +00:00
|
|
|
|
2019-01-23 14:48:50 +00:00
|
|
|
}
|