Skip to content
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-48248][PYTHON] Fix nested array to respect legacy conf of inferArrayTypeFromFirstElement #46548

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/pyspark/sql/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,13 @@ def test_collated_string(self):
StringType("UTF8_BINARY_LCASE"),
)

def test_infer_array_element_type_with_struct(self):
# SPARK-48248: Nested array to respect legacy conf of inferArrayTypeFromFirstElement
with self.sql_conf(
{"spark.sql.pyspark.legacy.inferArrayTypeFromFirstElement.enabled": True}
):
self.assertEqual([[1, None]], self.spark.createDataFrame([[[[1, "a"]]]]).first()[0])


class DataTypeTests(unittest.TestCase):
# regression test for SPARK-6055
Expand Down
18 changes: 16 additions & 2 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,13 +1951,27 @@ def _infer_type(
if len(obj) > 0:
if infer_array_from_first_element:
return ArrayType(
_infer_type(obj[0], infer_dict_as_struct, prefer_timestamp_ntz), True
_infer_type(
obj[0],
infer_dict_as_struct,
infer_array_from_first_element,
prefer_timestamp_ntz,
),
True,
)
else:
return ArrayType(
reduce(
_merge_type,
(_infer_type(v, infer_dict_as_struct, prefer_timestamp_ntz) for v in obj),
(
_infer_type(
v,
infer_dict_as_struct,
infer_array_from_first_element,
prefer_timestamp_ntz,
)
for v in obj
),
),
True,
)
Expand Down