Skip to content

fix: handle metric query returning zero rows gracefully#2609

Merged
m1n0 merged 1 commit intomainfrom
dtl-1709-metric-check-with-query-returning-zero-rows-crashes-with
Mar 2, 2026
Merged

fix: handle metric query returning zero rows gracefully#2609
m1n0 merged 1 commit intomainfrom
dtl-1709-metric-check-with-query-returning-zero-rows-crashes-with

Conversation

@m1n0
Copy link
Contributor

@m1n0 m1n0 commented Feb 27, 2026

When a metric check query returns 0 rows (e.g. a non-aggregate query with a restrictive WHERE clause), MetricQuery.execute() crashed with IndexError at query_result.rows[0][0].

Now checks for empty rows, logs a warning, and returns a Measurement with value=None so threshold evaluation produces a NOT EVALUATED outcome with a clear reason instead of an unhandled crash.

Description

Checklist

  • I added a test to verify the new functionality.
  • I verified this PR does not break soda-extensions.

@m1n0 m1n0 requested review from Niels-b and Copilot February 27, 2026 10:46
@m1n0 m1n0 force-pushed the dtl-1709-metric-check-with-query-returning-zero-rows-crashes-with branch from 4cf77c3 to e1ad7e7 Compare February 27, 2026 10:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a crash in MetricQuery.execute() when a metric check query returns zero rows. Previously, the code would crash with an IndexError when attempting to access query_result.rows[0][0] on an empty result set. The fix adds a check for empty rows and returns a Measurement with value=None instead, allowing the threshold evaluation to produce a NOT_EVALUATED outcome gracefully.

Changes:

  • Added empty row check in MetricQuery.execute() to prevent IndexError when queries return no rows
  • Added warning log when metric queries return empty results for better observability
  • Added comprehensive unit tests covering both empty and non-empty query results

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
soda-core/src/soda_core/contracts/impl/check_types/metric_check.py Added conditional check for empty rows and logging before accessing query result
soda-tests/tests/unit/test_metric_check.py Added unit tests for both empty and non-empty metric query result scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

When a metric check query returns 0 rows (e.g. a non-aggregate query
with a restrictive WHERE clause), MetricQuery.execute() crashed with
IndexError at `query_result.rows[0][0]`.

Now checks for empty rows, logs a warning, and returns a Measurement
with value=None so threshold evaluation marks the check as
NOT_EVALUATED instead of crashing with an unhandled exception.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@m1n0 m1n0 force-pushed the dtl-1709-metric-check-with-query-returning-zero-rows-crashes-with branch from e1ad7e7 to 64f7dbb Compare February 27, 2026 10:52
@m1n0 m1n0 requested a review from Copilot February 27, 2026 10:53
@sonarqubecloud
Copy link

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@Niels-b Niels-b left a comment

Choose a reason for hiding this comment

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

LGTM!
Nice work with the tests for this. I like the idea of testing specific features with this.
But we should be careful not to "over-MagicMock" everything. Integration tests (on a Postgres) will still be required imo.

@m1n0 m1n0 merged commit 12db01d into main Mar 2, 2026
38 of 39 checks passed
@m1n0 m1n0 deleted the dtl-1709-metric-check-with-query-returning-zero-rows-crashes-with branch March 2, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants