You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "itholic (via GitHub)" <gi...@apache.org> on 2023/07/04 02:03:42 UTC

[GitHub] [spark] itholic commented on a diff in pull request #41606: [WIP] [SPARK-44061] Add assertDFEqual util function

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


##########
python/pyspark/testing/utils.py:
##########
@@ -209,3 +234,70 @@ def check_error(
         self.assertEqual(
             expected, actual, f"Expected message parameters was '{expected}', got '{actual}'"
         )
+
+
+def assertSchemaEqual(
+    s1: Optional[Union[AtomicType, StructType, str, List[str], Tuple[str, ...]]],
+    s2: Optional[Union[AtomicType, StructType, str, List[str], Tuple[str, ...]]],
+):
+    if s1 != s2:
+        msg = "Schemas are different"
+        raise AssertionError(msg)
+
+
+def assertDataFrameEqual(
+    df: DataFrame, expected: Union[DataFrame, List[Row]], 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(r1: Row, r2: Row):
+        if r1 is None and r2 is None:
+            return True
+        elif r1 is None or r2 is None:
+            return False
+
+        d1 = r1.asDict()
+        d2 = r2.asDict()
+
+        for key in d1.keys() & d2.keys():
+            if isinstance(d1[key], float) and isinstance(d2[key], float):
+                if abs(d1[key] - d2[key]) > 1e-5:
+                    return False
+            else:
+                if d1[key] != d2[key]:
+                    return False
+        return True
+
+    def assert_rows_equal(rows1: Row, rows2: Row):
+        zipped = list(zip_longest(rows1, rows2))
+        rows_equal = True
+        error_table = PrettyTable(["df", "expected"])
+
+        for r1, r2 in zipped:
+            if compare(r1, r2):
+                error_table.add_row([blue(r1), blue(r2)])
+            else:
+                rows_equal = False
+                error_table.add_row([red(r1), red(r2)])

Review Comment:
   I think maybe we can just raise `PySparkAssertionError` here, and also show the each rows ??
   
   e.g.
   ```suggestion
           for r1, r2 in zipped:
               if compare(r1, r2):
                   error_table.add_row([blue(r1), blue(r2)])
               else:
                   raise PySparkAssertionError(
                   error_class="ROWS_NOT_EQUAL",
                   message_parameters={"row1": r1, "row2": r2},
               )
   ```
   
   The error class may can be like:
   
   ```json
     "ROWS_NOT_EQUAL" : {
       "message" : [
         "Row <r1> and <r2> are not equal."
       ]
     }
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org