ClickHouse/src/Functions/sleep.h

164 lines
5.7 KiB
C++
Raw Normal View History

2020-10-10 18:37:02 +00:00
#pragma once
#include <unistd.h>
2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
#include <Functions/FunctionHelpers.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypesNumber.h>
2021-06-14 04:13:35 +00:00
#include <Common/FieldVisitorConvertToNumber.h>
#include <Common/ProfileEvents.h>
#include <Common/assert_cast.h>
2021-10-02 07:13:14 +00:00
#include <base/sleep.h>
#include <IO/WriteHelpers.h>
#include <Interpreters/Context.h>
#include <Interpreters/ProcessList.h>
namespace ProfileEvents
{
extern const Event SleepFunctionCalls;
extern const Event SleepFunctionMicroseconds;
2023-12-11 14:55:34 +00:00
extern const Event SleepFunctionElapsedMicroseconds;
}
namespace DB
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
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.
*/
enum class FunctionSleepVariant
{
PerBlock,
PerRow
};
template <FunctionSleepVariant variant>
class FunctionSleep : public IFunction
{
private:
UInt64 max_microseconds;
QueryStatusPtr query_status;
public:
static constexpr auto name = variant == FunctionSleepVariant::PerBlock ? "sleep" : "sleepEachRow";
static FunctionPtr create(ContextPtr context)
{
return std::make_shared<FunctionSleep<variant>>(
context->getSettingsRef().function_sleep_max_microseconds_per_block,
context->getProcessListElementSafe());
}
FunctionSleep(UInt64 max_microseconds_, QueryStatusPtr query_status_)
: max_microseconds(std::min(max_microseconds_, static_cast<UInt64>(std::numeric_limits<UInt32>::max())))
, query_status(query_status_)
{
}
/// 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; }
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
{
WhichDataType which(arguments[0]);
if (!which.isFloat()
&& !which.isNativeUInt())
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}, expected Float64",
arguments[0]->getName(), getName());
return std::make_shared<DataTypeUInt8>();
}
ColumnPtr executeImplDryRun(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
{
return execute(arguments, result_type, true);
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t /*input_rows_count*/) const override
{
return execute(arguments, result_type, false);
}
ColumnPtr execute(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, bool dry_run) const
{
2020-10-19 15:27:41 +00:00
const IColumn * col = arguments[0].column.get();
if (!isColumnConst(*col))
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "The argument of function {} must be constant.", getName());
Float64 seconds = applyVisitor(FieldVisitorConvertToNumber<Float64>(), assert_cast<const ColumnConst &>(*col).getField());
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)");
size_t size = col->size();
2020-10-14 14:04:50 +00:00
/// We do not sleep if the columns is empty.
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)
throw Exception(ErrorCodes::TOO_SLOW, "The maximum sleep time is {} microseconds. Requested: {} microseconds",
max_microseconds, microseconds);
if (!dry_run)
{
UInt64 count = (variant == FunctionSleepVariant::PerBlock ? 1 : size);
2023-11-13 10:56:49 +00:00
microseconds *= count;
2023-04-29 17:08:52 +00:00
if (max_microseconds && microseconds > max_microseconds)
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-12-11 14:55:34 +00:00
UInt64 elapsed = 0;
while (elapsed < microseconds)
{
2023-12-11 14:55:34 +00:00
UInt64 sleep_time = microseconds - elapsed;
if (query_status)
2023-12-11 14:55:34 +00:00
sleep_time = std::min(sleep_time, /* 1 second */ static_cast<size_t>(1000000));
2023-12-11 14:55:34 +00:00
sleepForMicroseconds(sleep_time);
elapsed += sleep_time;
if (query_status && !query_status->checkTimeLimit())
break;
}
ProfileEvents::increment(ProfileEvents::SleepFunctionCalls, count);
ProfileEvents::increment(ProfileEvents::SleepFunctionMicroseconds, microseconds);
2023-12-11 14:55:34 +00:00
ProfileEvents::increment(ProfileEvents::SleepFunctionElapsedMicroseconds, elapsed);
}
}
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();
}
};
}