You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conversion error with Python float value conversion to UNION containing float #115
When registering a Python UDF that returns a union of float/varchar/int, calling the function in duckdb will raise a Conversion Error if the result contains a float value.
res = Value::BIGINT(value).DefaultCastAs(target_type);
break;
Possible fix: this seems to fix it, provided the original return types are disambiguated with return_type=DuckDBPyType("MAP(VARCHAR, UNION(int_val BIGINT, float_val DOUBLE, str_val VARCHAR))[]"):
changed the title [-]Conversion error with float in Python UDF[/-][+]Conversion error with Python `float` value conversion to `UNION` containing float[/+]on Oct 9, 2025
Yes, I've just tested both solutions and I can confirm that the alternate solution does work.
But the first one still raise an error, so as @Tishj mentioned by changing the title of the issue, it's only when the float variable is in an Union that it doesn't work.
The first one requires a change/build of the source code to change the default case. If you only tried with the updated return type without rebuilding from source, it wouldn't work.
Fixed in #332. We now handle casts for unions as well. The hardest part is to disambiguate the union if it contains the same data type. I don't see a reliable way of doing so. We now just pick the first compatible type by first checking for an exact match, and then a type we can cast to.
Activity
paultiq commentedon Oct 9, 2025
The cause seems to be that PythonValueConversion doesn't have the DefaultCastAs branch that the other datatypes have:
Consider HandleDouble:
duckdb-python/src/duckdb_py/native/python_conversion.cpp
Lines 478 to 479 in 3e79791
vs HandleBigint:
duckdb-python/src/duckdb_py/native/python_conversion.cpp
Lines 503 to 505 in 3e79791
Possible fix: this seems to fix it, provided the original return types are disambiguated with
return_type=DuckDBPyType("MAP(VARCHAR, UNION(int_val BIGINT, float_val DOUBLE, str_val VARCHAR))[]"):Alternate Solution
If the types are constant for each mapping, where a's are ints and b's are floats, could use a struct instead:
[-]Conversion error with float in Python UDF[/-][+]Conversion error with Python `float` value conversion to `UNION` containing float[/+]Hunknown commentedon Oct 10, 2025
Hi @paultiq
Yes, I've just tested both solutions and I can confirm that the alternate solution does work.
But the first one still raise an error, so as @Tishj mentioned by changing the title of the issue, it's only when the float variable is in an Union that it doesn't work.
paultiq commentedon Oct 10, 2025
The first one requires a change/build of the source code to change the default case. If you only tried with the updated return type without rebuilding from source, it wouldn't work.
evertlammerts commentedon Feb 19, 2026
Fixed in #332. We now handle casts for unions as well. The hardest part is to disambiguate the union if it contains the same data type. I don't see a reliable way of doing so. We now just pick the first compatible type by first checking for an exact match, and then a type we can cast to.
Outputs:
Hunknown commentedon Feb 19, 2026
Awesome, thank you very much!
Fix numeric conversion logic (#332)