Skip to content

Commit 9a6f5d9

Browse files
committed
[PYTHON] Compare Row lengths in assertDataFrameEqual
1 parent 3033357 commit 9a6f5d9

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

python/pyspark/sql/tests/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,32 @@ def test_assert_schema_equal_with_timestamp_nanos_types(self):
18841884
assertSchemaEqual(s1, StructType([StructField("ts", TimestampNTZNanosType(7), True)]))
18851885

18861886

1887+
class RowComparisonTests(unittest.TestCase):
1888+
def test_different_row_lengths(self):
1889+
pairs = [(Row(), Row(x=1)), (Row(x=1), Row(x=1, y=2))]
1890+
for left, right in pairs:
1891+
for actual, expected in [(left, right), (right, left)]:
1892+
for wrap in [
1893+
lambda row: row,
1894+
lambda row: Row(nested=row),
1895+
lambda row: Row(items=[row]),
1896+
lambda row: Row(mapping={"key": row}),
1897+
]:
1898+
for ordered in [False, True]:
1899+
with self.subTest(actual=actual, expected=expected, ordered=ordered):
1900+
with self.assertRaises(PySparkAssertionError) as error:
1901+
assertDataFrameEqual(
1902+
[wrap(actual)], [wrap(expected)], checkRowOrder=ordered
1903+
)
1904+
self.assertEqual(error.exception.getCondition(), "DIFFERENT_ROWS")
1905+
1906+
def test_equal_row_lengths(self):
1907+
for row in [Row(), Row(x=1), Row(nested=Row(x=1, y=2))]:
1908+
assertDataFrameEqual([row], [row])
1909+
with self.assertRaises(PySparkAssertionError):
1910+
assertDataFrameEqual([Row(x=1)], [Row(x=2)])
1911+
1912+
18871913
class UtilsTests(UtilsTestsMixin, ReusedSQLTestCase):
18881914
pass
18891915

python/pyspark/testing/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,9 @@ def compare_vals(val1, val2):
11131113
compare_vals(x, y) for x, y in zip(val1, val2)
11141114
)
11151115
elif isinstance(val1, Row) and isinstance(val2, Row):
1116-
return all(compare_vals(x, y) for x, y in zip(val1, val2))
1116+
return len(val1) == len(val2) and all(
1117+
compare_vals(x, y) for x, y in zip(val1, val2)
1118+
)
11171119
elif isinstance(val1, dict) and isinstance(val2, dict):
11181120
return (
11191121
len(val1) == len(val2)

0 commit comments

Comments
 (0)