If you’re getting an error in DuckDB that includes “list dimensions must be equal“, it appears that you’re performing a list operation that requires the lists to be the same dimension, but the lists are of different dimensions.
To fix this error, be sure to use lists of equal dimensions when performing the operation.
Example of Error
Here’s an example of code that produces the error:
SELECT [10, 25, 31] <=> [9, 30, 27, 2];
Output:
Invalid Input Error:
<=>: list dimensions must be equal, got left length '3' and right length '4'
The error occurred because the list on the left contains three elements while the list on the right contains four.
We get the same error when we pass the same lists to the list_cosine_distance()
function:
SELECT list_cosine_distance([10, 25, 31], [9, 30, 27, 2]);
Output:
Invalid Input Error:
list_cosine_distance: list dimensions must be equal, got left length '3' and right length '4'
And we can get the same error when performing a different operation, for example when using list_distance()
function:
SELECT list_distance([10, 25, 31], [10, 24, 37, 5]);
Output:
Invalid Input Error:
list_distance: list dimensions must be equal, got left length '3' and right length '4'
Or its alias, the <->
operator:
SELECT [10, 25, 31] <-> [10, 24, 37, 5];
Output:
Invalid Input Error:
<->: list dimensions must be equal, got left length '3' and right length '4'
In all of the above cases, we got the error because we tried to perform the operation on lists of different sizes.
Solution
To fix all of the above errors, we need to provide lists of the same size. For example:
SELECT [10, 25, 31, 0] <=> [9, 30, 27, 2];
Output:
0.013495958811329034
In this case, both lists contain four elements and so we don’t get an error. We can apply the same solution to the other examples listed above.
So if you’re encountering the same error, check the size of the lists.