2020-10-10 18:37:02 +00:00
|
|
|
#pragma once
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <unistd.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorConvertToNumber.h>
|
2021-07-14 16:44:09 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/sleep.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2023-04-29 17:04:20 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2021-07-14 16:44:09 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event SleepFunctionCalls;
|
|
|
|
extern const Event SleepFunctionMicroseconds;
|
|
|
|
}
|
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
|
|
|
|
{
|
2023-04-29 17:04:20 +00:00
|
|
|
private:
|
|
|
|
UInt64 max_microseconds;
|
2018-09-08 22:04:39 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = variant == FunctionSleepVariant::PerBlock ? "sleep" : "sleepEachRow";
|
2023-04-29 17:04:20 +00:00
|
|
|
static FunctionPtr create(ContextPtr context)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionSleep<variant>>(context->getSettingsRef().function_sleep_max_microseconds_per_block);
|
|
|
|
}
|
|
|
|
|
2023-11-10 17:23:56 +00:00
|
|
|
FunctionSleep(UInt64 max_microseconds_)
|
|
|
|
: max_microseconds(std::min(max_microseconds_, static_cast<UInt64>(std::numeric_limits<UInt32>::max())))
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
WhichDataType which(arguments[0]);
|
|
|
|
|
|
|
|
if (!which.isFloat()
|
|
|
|
&& !which.isNativeUInt())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}, expected Float64",
|
|
|
|
arguments[0]->getName(), getName());
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
2022-07-18 10:52:27 +00:00
|
|
|
ColumnPtr executeImplDryRun(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
|
|
|
{
|
|
|
|
return execute(arguments, result_type, true);
|
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
|
2022-07-18 10:52:27 +00:00
|
|
|
{
|
|
|
|
return execute(arguments, result_type, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, bool dry_run) const
|
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))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "The argument of function {} must be constant.", getName());
|
2018-09-08 22:04:39 +00:00
|
|
|
|
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
|
|
|
|
2023-11-10 17:23:56 +00:00
|
|
|
if (seconds < 0 || !std::isfinite(seconds) || seconds > static_cast<Float64>(std::numeric_limits<UInt32>::max()))
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Cannot sleep infinite, very large or negative amount of time (not implemented)");
|
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.
|
2023-11-13 10:56:49 +00:00
|
|
|
UInt64 microseconds = static_cast<UInt64>(seconds * 1e6);
|
|
|
|
if (max_microseconds && microseconds > max_microseconds)
|
2023-11-10 17:20:17 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_SLOW, "The maximum sleep time is {} microseconds. Requested: {} microseconds",
|
|
|
|
max_microseconds, microseconds);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2022-07-18 10:52:27 +00:00
|
|
|
if (!dry_run)
|
|
|
|
{
|
|
|
|
UInt64 count = (variant == FunctionSleepVariant::PerBlock ? 1 : size);
|
2023-11-13 10:56:49 +00:00
|
|
|
microseconds *= count;
|
2023-04-29 17:04:20 +00:00
|
|
|
|
2023-04-29 17:08:52 +00:00
|
|
|
if (max_microseconds && microseconds > max_microseconds)
|
2023-04-29 17:04:20 +00:00
|
|
|
throw Exception(ErrorCodes::TOO_SLOW,
|
2023-04-29 17:48:33 +00:00
|
|
|
"The maximum sleep time is {} microseconds. Requested: {} microseconds per block (of size {})",
|
|
|
|
max_microseconds, microseconds, size);
|
2023-04-29 17:04:20 +00:00
|
|
|
|
2022-07-18 10:52:27 +00:00
|
|
|
sleepForMicroseconds(microseconds);
|
|
|
|
ProfileEvents::increment(ProfileEvents::SleepFunctionCalls, count);
|
|
|
|
ProfileEvents::increment(ProfileEvents::SleepFunctionMicroseconds, 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|