fix(duckdb): division by zero returns inf, not NULL - #7969
Conversation
DuckDB has followed IEEE 754 since 1.1.0, so the NULLIF that the generator adds for NULL-safe sources was wrongly skipped.
|
Since this concerns transpilation, I think we need to distinguish duckdb's Is this something you'd be willing to work on? |
Here is the shape I have in mind — tell me if it matches yours before I write it. Three-state semantics. Replace the boolean with a
For Instance setting. One decision I would rather leave to you. |
georgesittas
left a comment
There was a problem hiding this comment.
I'll just get this in for now, I don't think it's worth dealing with the general case, because it will add overhead to division which is a pretty common operation. Getting rid of the nullif when targeting unsafe dialects is an improvement on its own because it results in runtime failures vs silently incorrect null values.
DuckDB has followed IEEE 754 since 1.1.0, so the NULLIF that the generator adds for NULL-safe sources was wrongly skipped.
DuckDB has followed IEEE 754 since 1.1.0, so the NULLIF that the generator adds for NULL-safe sources was wrongly skipped.
DuckDB is declared
SAFE_DIVISION = True, which the docstring defines as "whether division by zero throws an error (False) or returnsNULL(True)". Since DuckDB 1.1.0 neither is true: float division follows IEEE 754, so1 / 0isinf,-1 / 0is-infand0 / 0isnan.NULLonly comes back withieee_floating_point_ops = false, which is not the default. Checked against DuckDB 1.5.5:The effect is in
div_sql, which wraps the divisor inNULLIF(b, 0)when the source dialect is NULL-safe and the target is not. DuckDB was declared safe, so the wrap was skipped anda / bread as MySQL stayeda / bin DuckDB, which evaluates toinfwhere MySQL givesNULL. In the other direction sqlglot emulated aNULLthat DuckDB never produces.Dropping the flag changes exactly one entry of the existing
test_safe_divmatrix, the DuckDB target. Generating from DuckDB no longer wraps the divisor, since there is no NULL to emulate.I realise that matrix entry was written deliberately, and that neither value of a two-state flag really describes
inf. If you would rather model the third state explicitly, or keep the current behaviour for compatibility, say so and I will close this.