adriangb opened a new pull request, #27478:
URL: https://github.com/apache/datafusion/pull/27478

   ## Which issue does this PR close?
   
   - Closes #27477.
   
   ## Rationale for this change
   
   `plan_to_sql` and `expr_to_sql` render a non-finite `Float16`, `Float32`, or 
`Float64` literal as a bare number token (`inf`, `-inf`, `NaN`). The SQL parser 
reads that token as an identifier, so the unparsed SQL fails to plan with 
`Schema error: No field named inf.` Constant folding turns `'inf'::double`, 
`'NaN'::double`, and `1e999` into such literals, so ordinary queries hit this.
   
   ## What changes are included in this PR?
   
   In `scalar_to_sql`, a non-finite float now renders as a cast of a string 
literal:
   
   | Value | Float64 | Float32 / Float16 |
   | --- | --- | --- |
   | `+inf` | `CAST('Infinity' AS DOUBLE)` | `CAST('Infinity' AS FLOAT)` |
   | `-inf` | `CAST('-Infinity' AS DOUBLE)` | `CAST('-Infinity' AS FLOAT)` |
   | `NaN` | `CAST('NaN' AS DOUBLE)` | `CAST('NaN' AS FLOAT)` |
   
   The target type comes from `arrow_dtype_to_ast_dtype`, so dialects keep 
their own float type names. `Float16` uses the `Float32` SQL type, because 
`arrow_dtype_to_ast_dtype` has no SQL type for `Float16`. The sign stays inside 
the string, so `'-Infinity'` folds back to one `Float64(-inf)` literal and not 
to a negation of a cast. Finite values render exactly as before.
   
   ## What is the testing strategy for this PR?
   
   - `test_float_scalar_to_expr` in `unparser/expr.rs` gains cases for 
infinity, negative infinity, and NaN for each of `Float64`, `Float32`, and 
`Float16`.
   - `roundtrip_non_finite_float_literals` in `tests/cases/plan_to_sql.rs` 
plans queries such as `SELECT * FROM t WHERE x < 'inf'::double` and `x NOT IN 
('NaN'::double, 'Infinity'::double, '-Infinity'::double)`, folds the literal 
casts (as constant folding does), unparses, re-plans, folds again, and asserts 
the two plans are equal. It adds a table `t` with a `Float64` column `x` to the 
mock context provider.
   
   Before the fix:
   
   ```
   ---- unparser::expr::tests::test_float_scalar_to_expr stdout ----
   assertion `left == right` failed
     left: "inf"
    right: "CAST('Infinity' AS DOUBLE)"
   
   ---- cases::plan_to_sql::roundtrip_non_finite_float_literals stdout ----
   assertion `left == right` failed
     left: "SELECT t.x FROM t WHERE (t.x < inf)"
    right: "SELECT t.x FROM t WHERE (t.x < CAST('Infinity' AS DOUBLE))"
   ```
   
   After the fix, both pass, and `cargo test -p datafusion-sql`, `cargo fmt 
--all -- --check`, and `cargo clippy -p datafusion-sql --all-targets -- -D 
warnings` pass.
   
   ## Are there any user-facing changes?
   
   The SQL text for a non-finite float literal changes from an invalid bare 
token to a cast that parses and plans. There is no API change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to