2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <unistd.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Common/FieldVisitors.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2019-07-10 20:47:39 +00:00
|
|
|
#include <common/sleep.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Interpreters/Context_fwd.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-09-08 22:04:39 +00:00
|
|
|
extern const int TOO_SLOW;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
/** sleep(seconds) - the specified number of seconds sleeps each columns.
|
2018-09-08 22:04:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
enum class FunctionSleepVariant
|
|
|
|
{
|
|
|
|
PerBlock,
|
|
|
|
PerRow
|
|
|
|
};
|
|
|
|
|
|
|
|
template <FunctionSleepVariant variant>
|
|
|
|
class FunctionSleep : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = variant == FunctionSleepVariant::PerBlock ? "sleep" : "sleepEachRow";
|
2021-04-10 23:33:54 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionSleep<variant>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the name of the function.
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do not sleep during query analysis.
|
|
|
|
bool isSuitableForConstantFolding() const override
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
WhichDataType which(arguments[0]);
|
|
|
|
|
|
|
|
if (!which.isFloat()
|
|
|
|
&& !which.isNativeUInt())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName() + ", expected Float64",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2020-10-19 15:27:41 +00:00
|
|
|
const IColumn * col = arguments[0].column.get();
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2019-06-27 19:28:52 +00:00
|
|
|
if (!isColumnConst(*col))
|
2018-09-08 22:04:39 +00:00
|
|
|
throw Exception("The argument of function " + getName() + " must be constant.", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
Float64 seconds = applyVisitor(FieldVisitorConvertToNumber<Float64>(), assert_cast<const ColumnConst &>(*col).getField());
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2021-01-21 04:49:35 +00:00
|
|
|
if (seconds < 0 || !std::isfinite(seconds))
|
|
|
|
throw Exception("Cannot sleep infinite or negative amount of time (not implemented)", ErrorCodes::BAD_ARGUMENTS);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
size_t size = col->size();
|
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
/// We do not sleep if the columns is empty.
|
2018-09-08 22:04:39 +00:00
|
|
|
if (size > 0)
|
|
|
|
{
|
2020-06-27 19:05:00 +00:00
|
|
|
/// When sleeping, the query cannot be cancelled. For ability to cancel query, we limit sleep time.
|
2019-01-29 10:42:45 +00:00
|
|
|
if (seconds > 3.0) /// The choice is arbitrary
|
|
|
|
throw Exception("The maximum sleep time is 3 seconds. Requested: " + toString(seconds), ErrorCodes::TOO_SLOW);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2019-05-19 06:52:49 +00:00
|
|
|
UInt64 microseconds = seconds * (variant == FunctionSleepVariant::PerBlock ? 1 : size) * 1e6;
|
2019-07-10 20:47:39 +00:00
|
|
|
sleepForMicroseconds(microseconds);
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
/// convertToFullColumn needed, because otherwise (constant expression case) function will not get called on each columns.
|
2020-10-19 15:27:41 +00:00
|
|
|
return result_type->createColumnConst(size, 0u)->convertToFullColumnIfConst();
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|