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

   ### What changes were proposed in this pull request?
   This PR fix a but by make JDBC dialect decide the decimal precision and 
scale.
   
   **How to reproduce the bug?**
   https://github.com/apache/spark/pull/44397 proposed DS V2 push down 
`PERCENTILE_CONT` and `PERCENTILE_DISC`.
   The bug fired when pushdown the below SQL to H2 JDBC.
   `SELECT "DEPT",PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY "SALARY" ASC 
NULLS FIRST) FROM "test"."employee" WHERE 1=0 GROUP BY "DEPT"`
   
   **The root cause**
   `getQueryOutputSchema` used to get the output schema of query by call 
`JdbcUtils.getSchema`.
   The query for database H2 show below.
   `SELECT "DEPT",PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY "SALARY" ASC 
NULLS FIRST) FROM "test"."employee" WHERE 1=0 GROUP BY "DEPT"`
   We can get the five variables from `ResultSetMetaData`, please refer:
   ```
   columnName = "PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY SALARY NULLS 
FIRST)"
   dataType = 2
   typeName = "NUMERIC"
   fieldSize = 100000
   fieldScale = 50000
   ```
   Then we get the catalyst schema with `JdbcUtils.getCatalystType`, it calls 
`DecimalType.bounded(precision, scale)` actually.
   The `DecimalType.bounded(100000, 50000)` returns `DecimalType(38, 38)`.
   At finally, `makeGetter` throws exception.
   ```
   Caused by: org.apache.spark.SparkArithmeticException: 
[DECIMAL_PRECISION_EXCEEDS_MAX_PRECISION] Decimal precision 42 exceeds max 
precision 38. SQLSTATE: 22003
        at 
org.apache.spark.sql.errors.DataTypeErrors$.decimalPrecisionExceedsMaxPrecisionError(DataTypeErrors.scala:48)
        at org.apache.spark.sql.types.Decimal.set(Decimal.scala:124)
        at org.apache.spark.sql.types.Decimal$.apply(Decimal.scala:577)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$4(JdbcUtils.scala:408)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.nullSafeConvert(JdbcUtils.scala:552)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$3(JdbcUtils.scala:408)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$.$anonfun$makeGetter$3$adapted(JdbcUtils.scala:406)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:358)
        at 
org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils$$anon$1.getNext(JdbcUtils.scala:339)
   ```
   
   ### Why are the changes needed?
   This PR fix the bug that `JdbcUtils` can't get the correct decimal type.
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions 
for the consistent environment, and the instructions could accord to: 
https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this 
patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling 
Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   


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