Fix “Binder Error: RESERVOIR_QUANTILE can only take parameters in the range [0, 1]” in DuckDB

If you’re getting an error that reads “Binder Error: RESERVOIR_QUANTILE can only take parameters in the range [0, 1]” in DuckDB, it appears that you’re passing an invalid second argument to the reservoir_quantile() function.

To fix this issue, make sure the second argument is between 0 and 1.

Example of Error

Suppose we create and populate the following table:

CREATE TABLE sales (
    sale_id INTEGER,
    sale_amount DOUBLE
);

INSERT INTO sales (sale_id, sale_amount)
VALUES
    (1, 100.0),
    (2, 200.0),
    (3, 300.0),
    (4, 400.0),
    (5, 500.0),
    (6, 600.0),
    (7, 700.0),
    (8, 800.0),
    (9, 900.0),
    (10, 1000.0);

Here’s an example of code that produces the error:

SELECT 
    reservoir_quantile(sale_amount, 2)
FROM sales;

Output:

Binder Error: RESERVOIR_QUANTILE can only take parameters in the range [0, 1]

We got an error, because the second argument is greater than 1.

We’ll get the same error if we use a negative value:

SELECT 
    reservoir_quantile(sale_amount, -1)
FROM sales;

Output:

Binder Error: RESERVOIR_QUANTILE can only take parameters in the range [0, 1]

Solution

To fix this issue, we must make sure that the second argument is between 0 and 1:

SELECT 
    reservoir_quantile(sale_amount, 0.5)
FROM sales;

Result:

+--------------------------------------+
| reservoir_quantile(sale_amount, 0.5) |
+--------------------------------------+
| 500.0 |
+--------------------------------------+

This time it ran without error.

The second argument provides the quantile that we want to use for the operation. This quantile is a value between 0 and 1, and so that’s why we get an error when if falls outside that range.

We’ll get a similar error if we pass a value outside that range to the approx_quantile() function.