ueshin commented on code in PR #41606:
URL: https://github.com/apache/spark/pull/41606#discussion_r1253709341


##########
python/pyspark/testing/utils.py:
##########
@@ -209,3 +234,79 @@ def check_error(
         self.assertEqual(
             expected, actual, f"Expected message parameters was '{expected}', 
got '{actual}'"
         )
+
+
+def assertDataFrameEqual(df: DataFrame, expected: DataFrame, ignore_row_order: 
bool = True):
+    if df is None and expected is None:
+        return True
+    elif df is None or expected is None:
+        return False
+
+    def compare_rows(r1: Row, r2: Row):
+        def compare_vals(val1, val2):
+            if isinstance(val1, list) and isinstance(val2, list):
+                return all([compare_vals(x, y) for x, y in list(zip(val1, 
val2))])
+            elif isinstance(val1, dict) and isinstance(val2, dict):
+                return all([compare_vals(x, y) for x, y in 
list(zip(val1.values(), val2.values()))])

Review Comment:
   The keys should also be tested?
   Also, `val1.values()` doesn't guarantee the order of the values. We should 
iterate by their keys:
   
   ```py
   return val1.keys() == val2.keys() and all([compare_vals(val1[k], val2[k]) 
for k in val1.keys()])
   ```
   



##########
python/pyspark/testing/utils.py:
##########
@@ -209,3 +234,79 @@ def check_error(
         self.assertEqual(
             expected, actual, f"Expected message parameters was '{expected}', 
got '{actual}'"
         )
+
+
+def assertDataFrameEqual(df: DataFrame, expected: DataFrame, ignore_row_order: 
bool = True):
+    if df is None and expected is None:
+        return True
+    elif df is None or expected is None:
+        return False
+
+    def compare_rows(r1: Row, r2: Row):
+        def compare_vals(val1, val2):
+            if isinstance(val1, list) and isinstance(val2, list):
+                return all([compare_vals(x, y) for x, y in list(zip(val1, 
val2))])
+            elif isinstance(val1, dict) and isinstance(val2, dict):
+                return all([compare_vals(x, y) for x, y in 
list(zip(val1.values(), val2.values()))])
+            elif isinstance(val1, float) and isinstance(val2, float):
+                if abs(val1 - val2) > 1e-5:
+                    return False
+            else:
+                if val1 != val2:
+                    return False
+            return True
+
+        if r1 is None and r2 is None:
+            return True
+        elif r1 is None or r2 is None:
+            return False
+
+        zipped = zip(r1, r2)
+
+        result = True
+
+        for val1, val2 in zipped:
+            result &= compare_vals(val1, val2)
+
+        return result

Review Comment:
   I guess we can simplify here to:
   
   ```py
   return compare_vals(r1, r2)
   ```
   
   when `compare_vals` covers all the complex types properly.



##########
python/pyspark/testing/utils.py:
##########
@@ -209,3 +234,79 @@ def check_error(
         self.assertEqual(
             expected, actual, f"Expected message parameters was '{expected}', 
got '{actual}'"
         )
+
+
+def assertDataFrameEqual(df: DataFrame, expected: DataFrame, ignore_row_order: 
bool = True):
+    if df is None and expected is None:
+        return True
+    elif df is None or expected is None:
+        return False
+
+    def compare_rows(r1: Row, r2: Row):
+        def compare_vals(val1, val2):
+            if isinstance(val1, list) and isinstance(val2, list):
+                return all([compare_vals(x, y) for x, y in list(zip(val1, 
val2))])
+            elif isinstance(val1, dict) and isinstance(val2, dict):
+                return all([compare_vals(x, y) for x, y in 
list(zip(val1.values(), val2.values()))])
+            elif isinstance(val1, float) and isinstance(val2, float):

Review Comment:
   What about nested struct type cases?



##########
python/pyspark/testing/utils.py:
##########
@@ -209,3 +234,79 @@ def check_error(
         self.assertEqual(
             expected, actual, f"Expected message parameters was '{expected}', 
got '{actual}'"
         )
+
+
+def assertDataFrameEqual(df: DataFrame, expected: DataFrame, ignore_row_order: 
bool = True):
+    if df is None and expected is None:
+        return True
+    elif df is None or expected is None:
+        return False
+
+    def compare_rows(r1: Row, r2: Row):
+        def compare_vals(val1, val2):
+            if isinstance(val1, list) and isinstance(val2, list):
+                return all([compare_vals(x, y) for x, y in list(zip(val1, 
val2))])

Review Comment:
   The lengths of the lists should also be tested?
   
   ```py
   return len(val1) == len(val2) and all( ... )
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to