You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2025/07/18 03:43:42 UTC
[PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
zhengruifeng opened a new pull request, #51548:
URL: https://github.com/apache/spark/pull/51548
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
Fix the nullability check of Decimal('NaN')
### Why are the changes needed?
It is a long-standing issue that the nullability check of Decimal('NaN') is not correct:
```py
from decimal import Decimal
from pyspark.sql.types import *
data = [Row(dec=Decimal("NaN"))]
schema = StructType([StructField("dec", DecimalType(), False)])
df = spark.createDataFrame(data=data, schema=schema)
In [4]: df.collect()
Out[4]: [Row(dec=None)]
In [5]: df.show()
...
Py4JJavaError: An error occurred while calling o67.showString.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 6 in stage 2.0 failed 1 times, most recent failure: Lost task 6.0 in stage 2.0 (TID 11) (localhost executor driver): java.lang.NullPointerException: Cannot invoke "org.apache.spark.sql.types.Decimal.toPlainString()" because "<local2>" is null
...
```
it should fail on creation, because `Decimal("NaN")` will be treated as `Null` value
### Does this PR introduce _any_ user-facing change?
The creation should eagerly fail
### How was this patch tested?
new tests
### Was this patch authored or co-authored using generative AI tooling?
No
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #51548:
URL: https://github.com/apache/spark/pull/51548#discussion_r2218032236
##########
python/pyspark/sql/types.py:
##########
@@ -2707,7 +2707,9 @@ def new_name(n: str) -> str:
return "field %s in %s" % (n, name)
def verify_nullability(obj: Any) -> bool:
- if obj is None:
+ if obj is None or (isinstance(obj, decimal.Decimal) and obj.is_nan()):
+ # Spark's DecimalType doesn't support NaN,
+ # casting DoubleType NaN to DecimalType will return Null.
Review Comment:
Existing behavior is that:
```
In [10]: data = [Row(v=float("NaN"))]
In [11]: schema = StructType([StructField("v", DoubleType(), False)])
In [12]: df = spark.createDataFrame(data=data, schema=schema)
In [13]: df.select('*', df.v.cast("decimal")).show()
+---+----+
| v| v|
+---+----+
|NaN|NULL|
+---+----+
In [14]: df.select('*', df.v.cast("decimal")).printSchema()
root
|-- v: double (nullable = false)
|-- v: decimal(10,0) (nullable = true)
```
The double->decimal conversion always changes the nullability to true
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #51548: [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')`
URL: https://github.com/apache/spark/pull/51548
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on code in PR #51548:
URL: https://github.com/apache/spark/pull/51548#discussion_r2216209382
##########
python/pyspark/sql/types.py:
##########
@@ -2707,7 +2707,9 @@ def new_name(n: str) -> str:
return "field %s in %s" % (n, name)
def verify_nullability(obj: Any) -> bool:
- if obj is None:
+ if obj is None or (isinstance(obj, decimal.Decimal) and obj.is_nan()):
+ # Spark's DecimalType doesn't support NaN,
+ # casting DoubleType NaN to DecimalType will return Null.
Review Comment:
It seems to describe only `nullable` case. For non-nullable columns, we raise exceptions, don't we?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #51548:
URL: https://github.com/apache/spark/pull/51548#issuecomment-3094967951
thanks, merged to master
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-52862][PYTHON][CONNECT] Fix the nullability check of `Decimal('NaN')` [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #51548:
URL: https://github.com/apache/spark/pull/51548#issuecomment-3089711274
cc @peter-toth
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org