Using TRY_CAST() to Handle Errors When Converting Between Data Types in DuckDB

Encountering errors while converting between data types can be frustrating when working with SQL databases like DuckDB. But it usually means that something’s wrong. In most cases these errors occur because we’re trying to perform an impossible conversion, like from a number to a date or something.

But sometimes errors can get in the way, especially when we’re trying to convert a bunch of values. Sometimes it would be better for the system to return NULL for such failed conversions than to return an error and mess up the whole operation. Fortunately, we can do this.

Continue reading

Fix “No function matches the given name and argument types ‘list_concat…” When Using array_push_front() or array_push_back() in DuckDB

If you’re getting a binder error that reads something like “No function matches the given name and argument types ‘list_concat(STRING_LITERAL, VARCHAR[][])’. You might need to add explicit type casts.” in DuckDB when using either the array_push_front() or array_push_back() functions, it could be due to a slight syntax error.

Continue reading

Fix “No function matches the given name and argument types” When Using GENERATE_SUBSCRIPTS() in DuckDB

If you’re getting an error that reads something like “No function matches the given name and argument types” when using the generate_subscripts() function in DuckDB, it could be that your first argument is not an array. Or it could be that your second argument is not compatible with the INT64 type.

Continue reading

Fix “Out of Range Error” When Using GENERATE_SUBSCRIPTS() in DuckDB

If you’re getting an “Out of Range Error” when using the generate_subscripts() function in DuckDB, it could be that you’re specifying a non-existent dimension for the array.

DuckDB’s generate_subscripts() function accepts the array as the first argument, and the dimension as the second argument. The second argument must correspond to an actual dimension present in the array.

To fix this issue, be sure to specify a dimension that actually exists in the array.

Continue reading

Fix ‘Binder Error: ‘list_has_all’ cannot compare lists of different types’ in DuckDB

If you’re getting an error that reads “Binder Error: ‘list_has_all’ cannot compare lists of different types” in DuckDB, it appears that you’re passing two different types of lists to the list_has_all() function.

DuckDB’s list_has_all() function accepts two lists as arguments. These need to be of the same type.

To fix this issue, make sure that both lists are of the same type.

Continue reading

Fix “Sample method … cannot be used with a discrete sample count” When Using the SAMPLE Clause in DuckDB

If you’re getting an error that reads something like “Sample method System cannot be used with a discrete sample count” when using the SAMPLE clause in DuckDB, it looks like you’re specifying an invalid sampling method for the context with which you’re using the SAMPLE clause. Perhaps you’re using system or bernoulli, when you should be using reservoir.

Continue reading

Fix ‘Binder Error: Could not find key “…” in struct’ When Using DuckDB’s ARRAY_EXTRACT() Function

If you’re getting an error that reads something like “Binder Error: Could not find key “…” in struct” when using the array_extract() function in DuckDB, it could be that you’re specifying a non-existent key.

When using DuckDB’s array_extract() function to extract a value from a struct, we must provide a key that actually exists in the struct. Otherwise we’ll end up with an error like the above one.

To fix this issue, be sure to specify a key that actually exists in the struct.

Continue reading

Fix “Could not convert string ‘…’ to INT64” When Using the GENERATE_SUBSCRIPTS() Function in DuckDB

If you’re getting an error that reads something like “Could not convert string ‘…’ to INT64” when using the generate_subscripts() function in DuckDB, it appears that your second argument is a string, when it should be an integer.

DuckDB’s generate_subscripts() function accepts two arguments; the array as the first argument, and the dimension as the second argument. The second argument must be INT64 (or be able to be implicitly converted to that type). Passing the wrong data type as the second argument can cause the above error to occur.

To fix this issue, make sure that the second argument is compatible with INT64.

Continue reading