-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-39168][PYTHON] Use all values in a python list when inferring ArrayType schema #36545
Conversation
|
Nice PR description. Yeah, we should probably add a configuration then, please also refer to 2537fe8 for adding a SQL config |
We should probably add a configuration like |
Can one of the admins verify this patch? |
Added this configuration (slightly changed the name) and created a new migration guide page. |
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM otherwise
cc @BryanCutler @viirya @ueshin FYI |
cfc81be
to
7cde786
Compare
@@ -570,10 +570,20 @@ def _inferSchemaFromList( | |||
if not data: | |||
raise ValueError("can not infer schema from empty dataset") | |||
infer_dict_as_struct = self._jconf.inferDictAsStruct() | |||
infer_array_from_first_element = self._jconf.legacyInferArrayTypeFromFirstElement() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need a flag? in the cases where this matters, the schema would be wrong and produce an error; would that be desirable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously it was allowed to have mixed types in a python list, as long as the types could be cast to the type enforced by the schema inferred from the first element:
>>> df = spark.createDataFrame([{"a": ["1", 2]}])
>>> df.show()
+------+
| a|
+------+
|[1, 2]|
+------+
>>> df.schema
StructType(List(StructField(a,ArrayType(StringType,true),true)))
With this change, creating the DataFrame causes an error:
>>> df = spark.createDataFrame([{"a": ["1", 2]}])
...
TypeError: Unable to infer the type of the field a.
Because of this change, I think it makes sense to have the behavior configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, this seems like the wrong behavior. StringType should be inferred for this column, as it is now. I don't see a reason to break this? don't we want to just find the widest applicable type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the argument for casting to the widest applicable type, but I think that should be a separate discussion. Inferring the type of an array I think should be analogous to inferring a type over rows, which raises an error in this case:
>>> spark.createDataFrame([{"a": "1"}, {"a": 2}])
...
TypeError: field a: Can not merge type <class 'pyspark.sql.types.StringType'> and <class 'pyspark.sql.types.LongType'>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see it fixes some cases, by somewhat 'accidentally' correctly inferring a widening type. But it does cause some common cases to start failing, when they 'accidentally' work now (your example in this thread). It feels like the half-measure isn't worth it, as it needs a whole new flag. Is it hard to just implement logic to find the closest type for everything? that code surely already exists in the code base
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be marked as a TODO (at least on the python side). https://github.com/apache/spark/blob/master/python/pyspark/sql/types.py#L1377
Again, I think this should be addressed separately from this PR, but I'm happy to hold off on this PR if you think type-widening logic should be implemented first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not super familiar with this part of the code and I don't object to the change if someone else wants to merge; just seems like it would be relatively straightforward to 'really' fix it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's right that it causes a behaviour change. However, the (previous) string type coercion behaviour in an element of an array is actually a mistake I believe. For example, such type coercion is not supported in regular type inference:
>>> spark.createDataFrame([{"a": "1"}, {"a" :2}])
Traceback (most recent call last):
...
TypeError: field a: Can not merge type <class 'pyspark.sql.types.StringType'> and <class 'pyspark.sql.types.LongType'>
So, what this PR actually does is to match the behaviour with non-nested type inference. The switch was added for users dependent on the previous behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, that is sounding reasonable then to me. I dislike flags, but, if a follow-up made the flag irrelevant, maybe that's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, I think StringType
is the only type that supports coercion currently: https://github.com/apache/spark/blob/master/python/pyspark/sql/types.py#L1596-L1599
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a quick look. The new behavior seems more reasonable, although there is a concern on breaking current behavior. Migration guide is updated and a legacy flag is added. So seems okay to go.
Merged to master. |
Thanks for working on this @physinet, and congrats for being a Apache Spark contributor :-). |
…rArrayTypeFromFirstElement ### What changes were proposed in this pull request? This PR fixes a bug that does not respect `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` in nested arrays, introduced by #36545. ### Why are the changes needed? To have a way to restore the original behaviour. ### Does this PR introduce _any_ user-facing change? Yes, it fixes the regression when `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` is set to `True`. ### How was this patch tested? Unittest added. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #46548 from HyukjinKwon/SPARK-48248. Authored-by: Hyukjin Kwon <gurwls223@apache.org> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
…rArrayTypeFromFirstElement This PR fixes a bug that does not respect `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` in nested arrays, introduced by #36545. To have a way to restore the original behaviour. Yes, it fixes the regression when `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` is set to `True`. Unittest added. No. Closes #46548 from HyukjinKwon/SPARK-48248. Authored-by: Hyukjin Kwon <gurwls223@apache.org> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org> (cherry picked from commit b2140d0) Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
…rArrayTypeFromFirstElement This PR fixes a bug that does not respect `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` in nested arrays, introduced by #36545. To have a way to restore the original behaviour. Yes, it fixes the regression when `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` is set to `True`. Unittest added. No. Closes #46548 from HyukjinKwon/SPARK-48248. Authored-by: Hyukjin Kwon <gurwls223@apache.org> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org> (cherry picked from commit b2140d0) Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
… schema ### What changes were proposed in this pull request? This is similar with #36545. This PR proposes to infer the map types from all pairs instead of the first pair. ### Why are the changes needed? To have the consistent behaivor. e.g., ```python >>> spark.createDataFrame([[1], [2], ["a"], ["c"]]).collect() [Row(_1='1'), Row(_1='2'), Row(_1='a'), Row(_1='c')] ``` ### Does this PR introduce _any_ user-facing change? Yes. See below **Without Spark Connect:** ```python >>> spark.createDataFrame([{"outer": {"payment": 200.5, "name": "A"}}]).collect() [Row(outer={'name': 'A', 'payment': '200.5'})] >>> spark.conf.set("spark.sql.pyspark.legacy.inferMapTypeFromFirstPair.enabled", True) >>> spark.createDataFrame([{"outer": {"payment": 200.5, "name": "A"}}]).collect() [Row(outer={'name': None, 'payment': 200.5})] ``` **With Spark Conenct:** ```python >>> spark.createDataFrame([{"outer": {"payment": 200.5, "name": "A"}}]).collect() [Row(outer={'payment': '200.5', 'name': 'A'})] >>> spark.conf.set("spark.sql.pyspark.legacy.inferMapTypeFromFirstPair.enabled", True) >>> spark.createDataFrame([{"outer": {"payment": 200.5, "name": "A"}}]).collect() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.../spark/python/pyspark/sql/connect/session.py", line 635, in createDataFrame _table = LocalDataToArrowConversion.convert(_data, _schema) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/.../spark/python/pyspark/sql/connect/conversion.py", line 378, in convert return pa.Table.from_arrays(pylist, schema=pa_schema) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pyarrow/table.pxi", line 3974, in pyarrow.lib.Table.from_arrays File "pyarrow/table.pxi", line 1464, in pyarrow.lib._sanitize_arrays File "pyarrow/array.pxi", line 373, in pyarrow.lib.asarray File "pyarrow/array.pxi", line 343, in pyarrow.lib.array File "pyarrow/array.pxi", line 42, in pyarrow.lib._sequence_to_array File "pyarrow/error.pxi", line 154, in pyarrow.lib.pyarrow_internal_check_status File "pyarrow/error.pxi", line 91, in pyarrow.lib.check_status pyarrow.lib.ArrowInvalid: Could not convert 'A' with type str: tried to convert to double ``` ### How was this patch tested? Unittests added ### Was this patch authored or co-authored using generative AI tooling? No. Closes #46547 from HyukjinKwon/infer-map-first. Lead-authored-by: Hyukjin Kwon <gurwls223@apache.org> Co-authored-by: Hyukjin Kwon <gurwls223@gmail.com> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
…rArrayTypeFromFirstElement This PR fixes a bug that does not respect `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` in nested arrays, introduced by apache#36545. To have a way to restore the original behaviour. Yes, it fixes the regression when `spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled` is set to `True`. Unittest added. No. Closes apache#46548 from HyukjinKwon/SPARK-48248. Authored-by: Hyukjin Kwon <gurwls223@apache.org> Signed-off-by: Hyukjin Kwon <gurwls223@apache.org> (cherry picked from commit b2140d0) Signed-off-by: Hyukjin Kwon <gurwls223@apache.org> (cherry picked from commit 1d6724ca8e9e79f666aefd4258cce1482602644f)
What changes were proposed in this pull request?
This PR modifies type inference for python lists to consider all values in the list, not just the first value.
Why are the changes needed?
This enables convenient type inference in the two following cases:
[None, 1]
array<void>
(raisesValueError
)array<bigint>
[{"b": 1}, {"c": 2}]
array<struct<b:bigint>>
array<struct<b:bigint,c:bigint>>
Does this PR introduce any user-facing change?
Possible user-facing changes:
["a", 1]
) may result in a TypeErrorarray<string>
and produced a value["a", "1"]
How was this patch tested?
Added unit tests for various cases.