Repository: duckdb/duckdb
File: extension/core_functions/scalar/debug/sleep.cpp (C++)

Code before:
			continue;
		}
		// Sleep for the specified number of milliseconds (clamp negative values to 0)
		int64_t sleep_ms = milliseconds[idx];
		if (sleep_ms < 0) {
			sleep_ms = 0;
		}
		std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
		FlatVector::SetNull(result, i, true);
	}
}

static unique_ptr<FunctionData> SleepBind(ClientContext &context, ScalarFunction &bound_function,
                                          vector<unique_ptr<Expression>> &arguments) {
	// Validate that we have exactly one argument
	D_ASSERT(arguments.size() == 1);
	
	// Return type is always SQLNULL
	bound_function.return_type = LogicalType::SQLNULL;
	
	return nullptr;
}

static unique_ptr<Expression> BindSleepFunctionExpression(FunctionBindExpressionInput &input) {
	if (input.children[0]->IsFoldable()) {
		Value value = ExpressionExecutor::EvaluateScalar(input.context, *input.children[0]);
		if (!value.IsNull()) {
			int64_t sleep_ms = value.GetValue<int64_t>();
			if (sleep_ms < 0) {
				sleep_ms = 0;
			}
			std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
		}
		return make_uniq<BoundConstantExpression>(Value(LogicalType::SQLNULL));
	}
	// If not constant, return nullptr to use normal function execution
	return nullptr;
}

ScalarFunction SleepFun::GetFunction() {
	auto sleep_fun = ScalarFunction("sleep",
	                                {LogicalType::BIGINT},
	                                LogicalType::SQLNULL,
	                                SleepFunction,
	                                SleepBind);
	sleep_fun.stability = FunctionStability::VOLATILE;
	sleep_fun.bind_expression = BindSleepFunctionExpression;
	return sleep_fun;
}

} 

Review comment (suggestion):
I don't think the constant optimization is necessary. In Postgres, for example, the sleep function just runs once for every invocation:

```sql
\timing on
select pg_sleep(1) from generate_series(0,9);
 pg_sleep 
----------
 
 
 
 
 
 
 
 
 
 
(10 rows)

Time: 10011.742 ms (00:10.012)
```

If you want to only sleep once it's easy to only invoke the function once (`select sleep(...)`).

Code after:
#include "core_functions/scalar/debug_functions.hpp"

#include "duckdb/common/vector_operations/generic_executor.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include <thread>
#include <chrono>

namespace duckdb {

struct NullResultType {
	using STRUCT_STATE = PrimitiveTypeState;

	static void AssignResult(Vector &result, idx_t i, NullResultType) {
		FlatVector::SetNull(result, i, true);
	}
};

static void SleepFunction(DataChunk &input, ExpressionState &state, Vector &result) {
	input.Flatten();
	GenericExecutor::ExecuteUnary<PrimitiveType<int64_t>, NullResultType>(
	    input.data[0], result, input.size(), [](PrimitiveType<int64_t> input) {
		    // Sleep for the specified number of milliseconds (clamp negative values to 0)
		    int64_t sleep_ms = input.val;
		    if (sleep_ms < 0) {
			    sleep_ms = 0;
		    }
		    std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
		    return NullResultType();
	    });
}

ScalarFunction SleepMsFun::GetFunction() {
	auto sleep_fun = ScalarFunction({LogicalType::BIGINT}, LogicalType::SQLNULL, SleepFunction, nullptr);
	sleep_fun.stability = FunctionStability::VOLATILE;
	return sleep_fun;
}

} // namespace duckdb