Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ def _calculate_passed_rows_success(self, operator: str, len_rows: int, row_count
passed_rows = (row_count - len_rows) if row_count else 0
failed_rows = len_rows
elif operator == "==":
passed_rows = len_rows
failed_rows = (row_count - len_rows) if row_count else 0
# == counts how far the result is from the expected count as failed (see
# _calculate_equal_failure); a pass is exactly on it, so no row failed.
passed_rows = row_count if row_count else len_rows
failed_rows = 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we count the query matches as failed rows on a passing == check, as we do for <=?

With 10 matches on a 20-row table, == 10 should still succeed and report 10 passed / 10 failed.

else:
passed_rows = len_rows
failed_rows = 0
Expand Down Expand Up @@ -271,16 +273,11 @@ def _calculate_less_than_failure(self, len_rows: int, row_count: int) -> tuple[i
return max(0, passed_rows), max(0, failed_rows)

def _calculate_equal_failure(self, threshold: int, len_rows: int, row_count: int) -> tuple[int, int]:
"""Calculate rows for == operator failure (expected exact count)"""
if row_count:
if len_rows > threshold:
failed_rows = len_rows - threshold
passed_rows = row_count - failed_rows
else:
failed_rows = row_count - len_rows
passed_rows = len_rows
else:
failed_rows = abs(len_rows - threshold)
passed_rows = 0
"""Calculate rows for == operator failure (expected exact count)

The rows the result is off by, above or below the expected count, are the failed rows.
"""
failed_rows = abs(len_rows - threshold)
passed_rows = (row_count - failed_rows) if row_count else 0

return max(0, passed_rows), max(0, failed_rows)
Comment on lines +280 to 283

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Edge Case: == failure can report more failed rows than the table has (>100%)

_calculate_equal_failure now sets failed_rows = abs(len_rows - threshold) without a cap. When the expected count is larger than the table (e.g. == 10 on a 5-row table and the query returns 3 rows), you get failedRows = 7 and passedRows = 0. get_test_case_result_object divides by row_count, so failedRowsPercentage comes out as 140%. Before this change, the fewer-rows branch used row_count - len_rows, which never went above the table size. The fix is to cap failed_rows at row_count when a row count is available.

Cap failed rows at the table row count:

failed_rows = abs(len_rows - threshold)
if row_count:
    failed_rows = min(failed_rows, row_count)
    passed_rows = row_count - failed_rows
else:
    passed_rows = 0

return max(0, passed_rows), max(0, failed_rows)
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def test_none_row_count_does_not_crash(operator, threshold, expected_status):
(">=", 3, ALL_ROWS, TestCaseStatus.Success, 3, 0),
# 0 rows match, 3 total → < 1: Success, passed=3, failed=0
("<", 1, NO_ROWS, TestCaseStatus.Success, 3, 0),
# 0 rows match, 3 total → == 0: Success, passed=0, failed=3
("==", 0, NO_ROWS, TestCaseStatus.Success, 0, 3),
# 0 rows match, 3 total → == 0: Success, passed=3, failed=0
("==", 0, NO_ROWS, TestCaseStatus.Success, 3, 0),
],
)
def test_non_empty_dataset_row_counts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def test_less_than_equal_operator_failure(self, mock_compute_row_count, mock_run
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.compute_row_count"
)
def test_equal_operator_success(self, mock_compute_row_count, mock_run_results, mock_get_runtime_params):
"""Test == operator when test passes"""
"""Test == operator when test passes: the result is exactly on the expected count, so none failed"""
self.mock_test_case.parameterValues = self._create_mock_param_values("==", 10)
mock_run_results.return_value = 10
mock_compute_row_count.return_value = 1000
Expand All @@ -283,8 +283,32 @@ def test_equal_operator_success(self, mock_compute_row_count, mock_run_results,
result = self.validator.run_validation()

self.assertEqual(result.testCaseStatus, TestCaseStatus.Success)
self.assertEqual(result.passedRows, 10)
self.assertEqual(result.failedRows, 990)
self.assertEqual(result.passedRows, 1000)
self.assertEqual(result.failedRows, 0)

@patch(
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.get_runtime_parameters"
)
@patch(
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator._run_results"
)
@patch(
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.compute_row_count"
)
def test_edge_case_zero_threshold_equal_success(
self, mock_compute_row_count, mock_run_results, mock_get_runtime_params
):
"""Test == 0 passing (#38561): a query that returns no row must not fail the whole table"""
self.mock_test_case.parameterValues = self._create_mock_param_values("==", 0)
mock_run_results.return_value = 0
mock_compute_row_count.return_value = 1000
mock_get_runtime_params.return_value = Mock()

result = self.validator.run_validation()

self.assertEqual(result.testCaseStatus, TestCaseStatus.Success)
self.assertEqual(result.passedRows, 1000)
self.assertEqual(result.failedRows, 0)

@patch(
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.get_runtime_parameters"
Expand Down Expand Up @@ -318,7 +342,7 @@ def test_equal_operator_failure_more_rows(self, mock_compute_row_count, mock_run
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.compute_row_count"
)
def test_equal_operator_failure_fewer_rows(self, mock_compute_row_count, mock_run_results, mock_get_runtime_params):
"""Test == operator when test fails with fewer rows than expected"""
"""Test == operator when test fails with fewer rows than expected: the 7 missing rows failed"""
self.mock_test_case.parameterValues = self._create_mock_param_values("==", 10)
mock_run_results.return_value = 3
mock_compute_row_count.return_value = 1000
Expand All @@ -327,8 +351,8 @@ def test_equal_operator_failure_fewer_rows(self, mock_compute_row_count, mock_ru
result = self.validator.run_validation()

self.assertEqual(result.testCaseStatus, TestCaseStatus.Failed)
self.assertEqual(result.passedRows, 3)
self.assertEqual(result.failedRows, 997)
self.assertEqual(result.passedRows, 993)
self.assertEqual(result.failedRows, 7)

@patch(
"metadata.data_quality.validations.table.sqlalchemy.tableCustomSQLQuery.TableCustomSQLQueryValidator.get_runtime_parameters"
Expand Down
Loading