-
Notifications
You must be signed in to change notification settings - Fork 153
Add VerificationResult.rowLevelResultsAsDataFrame support #262
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
Open
billpratt
wants to merge
3
commits into
awslabs:master
Choose a base branch
from
billpratt:row-level-results
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import unittest | ||
|
|
||
| import pandas as pd | ||
| from pyspark.sql import Row | ||
| from pyspark.sql.types import BooleanType | ||
|
|
||
| from pydeequ.checks import Check, CheckLevel | ||
| from pydeequ.verification import VerificationResult, VerificationSuite | ||
| from tests.conftest import setup_pyspark | ||
|
|
||
|
|
||
| class TestRowLevelResults(unittest.TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.spark = setup_pyspark().appName("test-row-level-results-local").getOrCreate() | ||
| cls.sc = cls.spark.sparkContext | ||
| cls.df = cls.sc.parallelize( | ||
| [ | ||
| Row(a="foo", b=1, c=5), | ||
| Row(a="bar", b=2, c=6), | ||
| Row(a="baz", b=3, c=None), | ||
| ] | ||
| ).toDF() | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| # Must shutdown callback for tests to stop | ||
| # TODO Document this call to users or encapsulate in PyDeequSession | ||
| cls.spark.sparkContext._gateway.shutdown_callback_server() | ||
| cls.spark.stop() | ||
|
|
||
| def test_row_level_results_with_completeness(self): | ||
| """Test that isComplete produces a Boolean column with correct per-row values.""" | ||
| check = Check(self.spark, CheckLevel.Error, "completeness_check") | ||
| check = check.isComplete("c") | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
|
billpratt marked this conversation as resolved.
|
||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame(self.spark, result, self.df) | ||
|
|
||
| # Should have same row count as original DataFrame | ||
| self.assertEqual(row_level_df.count(), self.df.count()) | ||
|
|
||
| # Should have original columns (a, b, c) plus one Boolean column for the check | ||
| self.assertIn("completeness_check", row_level_df.columns) | ||
| self.assertTrue(isinstance(row_level_df.schema["completeness_check"].dataType, BooleanType)) | ||
|
|
||
|
billpratt marked this conversation as resolved.
|
||
| # Order by b to ensure deterministic row ordering | ||
| # b=1: c=5 (complete), b=2: c=6 (complete), b=3: c=None (incomplete) | ||
| results = row_level_df.orderBy("b").select("completeness_check").collect() | ||
| values = [row["completeness_check"] for row in results] | ||
|
billpratt marked this conversation as resolved.
|
||
| self.assertEqual(values, [True, True, False]) | ||
|
|
||
| def test_row_level_results_with_contained_in(self): | ||
| """Test that isContainedIn produces correct row-level results.""" | ||
| check = Check(self.spark, CheckLevel.Error, "contained_check") | ||
| check = check.isContainedIn("a", ["foo", "bar"]) | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame(self.spark, result, self.df) | ||
|
|
||
| self.assertIn("contained_check", row_level_df.columns) | ||
|
|
||
|
billpratt marked this conversation as resolved.
|
||
| # Order by a to ensure deterministic row ordering | ||
| # a="bar" (contained), a="baz" (not contained), a="foo" (contained) | ||
| results = row_level_df.orderBy("a").select("contained_check").collect() | ||
| values = [row["contained_check"] for row in results] | ||
| self.assertEqual(values, [True, False, True]) | ||
|
|
||
| def test_row_level_results_multiple_constraints_anded(self): | ||
| """Test that multiple constraints in one Check are ANDed into a single column.""" | ||
| check = Check(self.spark, CheckLevel.Error, "multi_check") | ||
| check = check.isComplete("a").isComplete("c") | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame(self.spark, result, self.df) | ||
|
|
||
| self.assertIn("multi_check", row_level_df.columns) | ||
|
billpratt marked this conversation as resolved.
|
||
|
|
||
| # Order by b to ensure deterministic row ordering | ||
| # b=1: a,c complete -> True, b=2: a,c complete -> True, b=3: c=None -> False | ||
| results = row_level_df.orderBy("b").select("multi_check").collect() | ||
| values = [row["multi_check"] for row in results] | ||
| self.assertEqual(values, [True, True, False]) | ||
|
|
||
|
billpratt marked this conversation as resolved.
|
||
| def test_row_level_results_aggregate_only_check(self): | ||
| """Test that aggregate-only checks (hasSize) don't add columns.""" | ||
| check = Check(self.spark, CheckLevel.Warning, "size_check") | ||
| check = check.hasSize(lambda x: x >= 3) | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
|
billpratt marked this conversation as resolved.
|
||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame(self.spark, result, self.df) | ||
|
|
||
| # hasSize is aggregate-only, so no new column should be added | ||
| self.assertEqual(sorted(row_level_df.columns), sorted(self.df.columns)) | ||
|
|
||
| def test_row_level_results_preserves_original_columns(self): | ||
| """Test that the original DataFrame columns are preserved.""" | ||
| check = Check(self.spark, CheckLevel.Error, "preserve_check") | ||
| check = check.isComplete("c") | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame(self.spark, result, self.df) | ||
|
|
||
| for col in self.df.columns: | ||
| self.assertIn(col, row_level_df.columns) | ||
|
|
||
| # Verify original data is unchanged (ordered for deterministic comparison) | ||
| original_values = self.df.orderBy("b").select("a", "b").collect() | ||
| result_values = row_level_df.orderBy("b").select("a", "b").collect() | ||
| self.assertEqual(original_values, result_values) | ||
|
|
||
| def test_row_level_results_as_pandas(self): | ||
| """Test the pandas=True option returns a Pandas DataFrame.""" | ||
| check = Check(self.spark, CheckLevel.Error, "pandas_check") | ||
| check = check.isComplete("c") | ||
|
|
||
| result = VerificationSuite(self.spark).onData(self.df).addCheck(check).run() | ||
| row_level_df = VerificationResult.rowLevelResultsAsDataFrame( | ||
| self.spark, result, self.df, pandas=True | ||
| ) | ||
|
|
||
| self.assertIsInstance(row_level_df, pd.DataFrame) | ||
| self.assertIn("pandas_check", row_level_df.columns) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.