alekjarmov opened a new pull request, #60767:
URL: https://github.com/apache/spark/pull/60767

   ### What changes were proposed in this pull request?
   
   `MsSqlServerSQLBuilder.visitCast` refuses a cast to STRING from FLOAT, 
DOUBLE or BOOLEAN, so `compileExpression` returns `None` and Spark evaluates 
the cast instead of pushing it to SQL Server. Casts to STRING from integral 
types, DECIMAL and other types are still pushed down, and other dialects are 
unchanged.
   
   ### Why are the changes needed?
   
   Every atomic-to-string cast is an up-cast, so filters, aggregates and join 
conditions that contain one are pushed in both ANSI and non-ANSI mode, as 
`CAST(x AS NVARCHAR(MAX))`. That includes implicit casts such as `d LIKE '1.%'` 
on a DOUBLE column. SQL Server converts FLOAT and REAL to strings with at most 
6 significant digits and BIT as `1`/`0`, so the pushed expression compares 
different strings than Spark does and the query silently returns wrong rows:
   
   | Value | Spark `CAST(x AS STRING)` | SQL Server `CAST(x AS NVARCHAR(MAX))` |
   |-|-|-|
   | DOUBLE `100.0` | `100.0` | `100` |
   | DOUBLE `123.456789` | `123.456789` | `123.457` |
   | DOUBLE `1234567.0` | `1234567.0` | `1.23457e+006` |
   | REAL `123.4567` | `123.4567` | `123.457` |
   | BOOLEAN `true` | `true` | `1` |
   
   For example, `SELECT id FROM t WHERE CAST(d AS STRING) = '100.0'` returns no 
rows for a row with `d = 100.0`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Queries against SQL Server that cast a FLOAT, DOUBLE or BOOLEAN column 
to STRING in a filter, aggregate or join condition now return the same rows as 
Spark's own evaluation. Previously they could return wrong rows. Those casts 
are no longer pushed down.
   
   ### How was this patch tested?
   
   - New `JDBCSuite` test: the MsSqlServer dialect compiles no cast to STRING 
from FLOAT, DOUBLE or BOOLEAN, still compiles it from INT, BIGINT and 
DECIMAL(10,2), and the generic dialect still compiles it from DOUBLE.
   - New `MsSqlServerIntegrationSuite` (v2) test on the SQL Server Docker 
image, with join pushdown on. Rows `(id, d DOUBLE, r FLOAT, b BOOLEAN, s 
STRING)`: `(1, 1.5, 1.5, true, '1.5')`, `(2, 100.0, 100.0, false, '100.0')`, 
`(3, 123.456789, 123.4567, true, '123.456789')`, `(4, 1234567.0, 1234567.0, 
false, '100')`. For each query it checks that no remote query contains the cast 
and that the rows match Spark:
   
   | Query | Expected | Before this change |
   |-|-|-|
   | `WHERE CAST(d AS STRING) = '100.0'` | 2 | no rows |
   | `WHERE CAST(d AS STRING) = '123.456789'` | 3 | no rows |
   | `WHERE startswith(CAST(d AS STRING), '1234567')` | 4 | no rows |
   | `WHERE CAST(d AS STRING) = s` | 1, 2, 3 | 1 |
   | `WHERE concat('v', CAST(d AS STRING)) = 'v100.0'` | 2 | no rows |
   | `WHERE d LIKE '1.%'` | 1 | 1, 4 |
   | `MAX(CAST(d AS STRING)), MIN(CAST(d AS STRING))` | `1234567.0`, `1.5` | 
`123.457`, `1.23457e+006` |
   | `x JOIN y ON CAST(x.d AS STRING) = y.s` | (1, 1), (2, 2), (3, 3) | (1, 1), 
(2, 4) |
   | `WHERE CAST(r AS STRING) = '123.4567'` | 3 | no rows |
   | `WHERE CAST(b AS STRING) = 'true'` | 1, 3 | no rows |
   | `WHERE CAST(b AS STRING) = 'false'` | 2, 4 | no rows |
   | `WHERE CAST(b AS STRING) IN ('true', 'false')` | 1, 2, 3, 4 | no rows |
   
   ```
   build/sbt 'sql/testOnly org.apache.spark.sql.jdbc.JDBCSuite -- -z 
"SPARK-62136"'
   ENABLE_DOCKER_INTEGRATION_TESTS=1 build/sbt -Pdocker-integration-tests \
     'docker-integration-tests/testOnly 
org.apache.spark.sql.jdbc.v2.MsSqlServerIntegrationSuite -- -z "SPARK-62136"'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Code, claude-opus-5-5)
   
   This pull request and its description were written by Isaac.
   


-- 
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