Fix “Macro function generate_subscripts(arr, dim) requires 2 positional arguments” in DuckDB

If you’re getting an error that reads something like “Macro function generate_subscripts(arr, dim) requires 2 positional arguments” in DuckDB, it appears that you’re calling the generate_subscripts() function with the wrong number of arguments.

DuckDB’s generate_subscripts() function accepts two arguments; the array as the first argument, and the dimension as the second argument.

To fix this issue, be sure to provide both arguments when calling this function.

Example of Error

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

SELECT generate_subscripts(
    array_value('Sun', 'Sky', 'Sea')
    );

Result:

Binder Error: Macro function generate_subscripts(arr, dim) requires 2 positional arguments, but a single positional argument was provided.
LINE 1: SELECT generate_subscripts(
^

In this case I passed only one argument (the array). I forgot to include the second argument, which resulted in the error.

I get the same error if I pass the dimension but not the array:

SELECT generate_subscripts( 
    1
    );

Result:

Binder Error: Macro function generate_subscripts(arr, dim) requires 2 positional arguments, but a single positional argument was provided.
LINE 1: SELECT generate_subscripts(
^

Solution

To fix this issue, we need to specify both the array and the dimension:

SELECT generate_subscripts(
    array_value('Sun', 'Sky', 'Sea'), 
    1
    );

Result:

+----------------------------------------------------------+
| generate_subscripts(array_value('Sun', 'Sky', 'Sea'), 1) |
+----------------------------------------------------------+
| 1 |
| 2 |
| 3 |
+----------------------------------------------------------+

In DuckDB, arrays start at 1, and so that’s why I passed 1 as the second argument. If I passed 0, this would have caused another error, due to it being out of range. The same would’ve happened if I’d passed 2 or more (due to this being a one-dimensional array).