Repository navigation
[SPARK-46654][SQL][PYTHON] Make to_csv explicitly indicate that it does not support some types of data - #44665
panbingkun wants to merge 20 commits into
Conversation
…etween Regular Spark and Spark Connect
|
@panbingkun Let's re-enable the doctest, this test was pass in pyspark-sql task and failed in pyspark-connect task before spark/python/pyspark/sql/functions/builtin.py Lines 15053 to 15058 in 514ecc6 and remove the TODO spark/python/pyspark/sql/functions/builtin.py Lines 15010 to 15013 in 514ecc6 also cc @HyukjinKwon |
Okay, done. |
| (row: InternalRow, ordinal: Int) => | ||
| row.get(ordinal, dt).toString | ||
| (row: InternalRow, ordinal: Int) => { | ||
| val valueUTF8String = ToPrettyString(Literal(row.get(ordinal, dt), dt), |
There was a problem hiding this comment.
Does ToPrettyString print nested fields in the same way as UnivocityGenerator. For instance, does it follow to datetime patterns specified for the CSV datasource?
There was a problem hiding this comment.
Let me think again, maybe we need to rewrite the logic similar to ToPrettyString in UnivocityGenerator based on the characteristics of CSV.
There was a problem hiding this comment.
I changed the approach and enabled UnivocityGenerator to support complex type (ArrayType, MapType, StructType) outputs.
MaxGekk
left a comment
There was a problem hiding this comment.
Can the from_csv function parse the output of to_csv() back? Could you add tests or modify existing tests, please.
HyukjinKwon
left a comment
There was a problem hiding this comment.
- Does CSV source support this? I think it's disallowed
- If we want to support this it should better be able to read it back. Problem is that CSV spec doesn't have this.
Should probably just disallow this case
|
I think the current issue is that the original PySpark can display complex struct types, but PyConnect and Scala code cannot. Ultimately, we should make them consistent. Should we consider removing the current capability of PySpark(Perhaps other scenarios are not supported?)? However, this might be a breaking change. |
For this case, currently the |
I have privately discussed this issue with @LuciferYang , and in response to this situation, we actually have two options:
|
|
I think we should simply disallow this. We could have a legacy conf, but it doesn't much make sense. What does it return for |
to_csv explicitly indicate that it does not support complex types of data
…upport complex types of data
to_csv explicitly indicate that it does not support some types of datato_csv explicitly indicate that it does not support some types of data
|
+1, LGTM. Merging to master. |
### What changes were proposed in this pull request? The pr aims to remove some unused error classes, include: - _LEGACY_ERROR_TEMP_3193: The code in PR(#44961) no longer uses this error class, but the related item in the file `error-classes.json` has not been deleted synchronously. - _LEGACY_ERROR_TEMP_3197: The code in PR(#44961) no longer uses this error class, but the related item in the file `error-classes.json` has not been deleted synchronously. - _LEGACY_ERROR_TEMP_3234: This should be the follow-up work of #44665 - STATE_STORE_MULTIPLE_VALUES_PER_KEY: Introduced by [SPARK-46864](#44883), but not actually used in codebase. - TWS_VALUE_SHOULD_NOT_BE_NULL: Introduced by [SPARK-46864](#44883), but not actually used in codebase. ### Why are the changes needed? Make the code cleaner. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? - Pass GA. - Manually check. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #45509 from panbingkun/remove_outdated_error_classes. Authored-by: panbingkun <panbingkun@baidu.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
|
why do we remove a working feature? what's wrong with |
@cloud-fan It prints "address" of objects, not values. Do you have an example which you worry about? |
|
@panbingkun I forgot to ask you. Does |
|
|
|
So we make a breaking change simply because we don't like the object address in the string? can we fix it? |
Or do we need to add configuration to bring it back? |
Yeah, there is no standard for nested types in |
|
You never know how people will use Spark and I'm not sure why it's necessary to ban this use case. Having inconsistency between scala and python is bad. Can we fix this problem by defining a pretty string format for struct/array/map in the csv writer? |
I believe it is not too bad to ban such output in any case (this is even insecure). @cloud-fan Do you see any use-case for that? Array: val valueSchema = StructType(Seq(StructField("age", LongType), StructField("name", StringType), StructField("scores", ArrayType(LongType))))
val schema = StructType(Seq(StructField("key", LongType), StructField("value", valueSchema)))
val df = spark.createDataFrame(rows, schema)
df.select(to_csv($"value")).show(false)
+--------------------------------------------------------------------------+
|to_csv(value) |
+--------------------------------------------------------------------------+
|2,Alice,org.apache.spark.sql.catalyst.expressions.UnsafeArrayData@e7bb2e54|
+--------------------------------------------------------------------------+Map: +------------------------------------------------------------------------+
|to_csv(value) |
+------------------------------------------------------------------------+
|2,Alice,org.apache.spark.sql.catalyst.expressions.UnsafeMapData@65ab9790|
+------------------------------------------------------------------------+Struct: val rows = new java.util.ArrayList[Row]()
rows.add(Row(1L, Row(2L, "Alice", Row(100L, 200L, null))))
val valueSchema = StructType(Seq(StructField("age", LongType), StructField("name", StringType), StructField("scores", StructType(Seq(StructField("id1", LongType), StructField("id2", LongType), StructField("id3", LongType))))))
val schema = StructType(Seq(StructField("key", LongType), StructField("value", valueSchema)))
val df = spark.createDataFrame(rows, schema)
+---------------------+
|to_csv(value) |
+---------------------+
|2,Alice,"[4,64,c8,0]"|
+---------------------+ |
However, on the
|
|
I'd prefer to fix the inconsistency. Can we use |
|
Use If the final discussion result is to |
|
I don't oppose this resolution, but if |
|
It's ok to not support round-trip. array/struct/map is not CSV standard anyway. I think the principle is to avoid breaking changes if possible. Since |
|
If no objections, I'll revert this tomorrow. We can have a new PR to add pretty string for array/struct/map in |
I will submit a new PR to |
|
@cloud-fan |
…map/binary` as pretty strings ### What changes were proposed in this pull request? The pr aims make `to_csv` - support the output of `array/struct/map/binary` as `pretty strings`. - not support `variant`. ### Why are the changes needed? This PR was generated from follow-up comment suggestions #44665 (comment), <img width="909" alt="image" src="https://github.com/apache/spark/assets/15246973/04dd1497-da42-4b03-b21d-b041ead86f87"> ### Does this PR introduce _any_ user-facing change? Yes. ### How was this patch tested? - Update existed UT. - Pass GA. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #45657 from panbingkun/SPARK-47497. Authored-by: panbingkun <panbingkun@baidu.com> Signed-off-by: Dongjoon Hyun <dhyun@apple.com>


What changes were proposed in this pull request?
The pr aims to make
to_csvexplicitlyindicate that it does not supportsome types of data, includes:Why are the changes needed?
Because the
CSV specificationdoes not have standards for thesetypes, and cannot be read back throughfrom_csv.For this case

Before:
After:

Does this PR introduce any user-facing change?
Yes,
to_csvwill explicitly indicate that it does not support some types of data.How was this patch tested?
Was this patch authored or co-authored using generative AI tooling?
No.