From 19c1f44dbf55a9ee9a51f03cfc411e7d0a6301b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6dl?= Date: Tue, 27 Oct 2026 10:58:05 +0100 Subject: [PATCH 1/4] Resolve table names without a dataset in BigQuery SQL quality rules against the server's dataset --- CHANGELOG.md | 3 +++ datacontract/engines/ibis/connections/connect.py | 5 ++++- tests/test_connect_bigquery.py | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80d285a98..eb11e8b29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - `datacontract test` matches custom quality check IDs regardless of case +### Fixed +- `datacontract test` on BigQuery: SQL quality rules and custom quality checks can name the table without its dataset again + ## [1.2.8] - 2026-10-26 ### Added diff --git a/datacontract/engines/ibis/connections/connect.py b/datacontract/engines/ibis/connections/connect.py index a4350e161..617133cb5 100644 --- a/datacontract/engines/ibis/connections/connect.py +++ b/datacontract/engines/ibis/connections/connect.py @@ -375,7 +375,10 @@ def _connect_bigquery(ibis, server: Server, config: Config): ) if credentials: kwargs["credentials"] = credentials - return ibis.bigquery.connect(**kwargs) + con = ibis.bigquery.connect(**kwargs) + # BigQuery requires a dataset on every table name, but the SQL of quality rules may name the table bare. + con.client.default_query_job_config.default_dataset = f"{con.data_project}.{con.dataset}" + return con _BIGQUERY_SCOPES = ["https://www.googleapis.com/auth/bigquery"] diff --git a/tests/test_connect_bigquery.py b/tests/test_connect_bigquery.py index 3c9c275c4..700290e42 100644 --- a/tests/test_connect_bigquery.py +++ b/tests/test_connect_bigquery.py @@ -109,6 +109,7 @@ def test_billing_project_keeps_the_contract_project_as_the_data_project(env): assert con.data_project == "my-project" assert con.billing_project == "my-billing-project" assert con.dataset == "my_dataset" + assert str(con.client.default_query_job_config.default_dataset) == "my-project.my_dataset" assert client.call_args.kwargs["credentials"] is impersonated.return_value From 3406bd1761564f9674b6e73d0f3d3e58d1de6a90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6dl?= Date: Tue, 27 Oct 2026 10:58:06 +0100 Subject: [PATCH 2/4] Name the custom quality check and its arguments when the database rejects its query --- datacontract/engines/checks/check_spec.py | 1 + datacontract/engines/checks/create_checks.py | 1 + datacontract/engines/checks/custom_quality_check.py | 6 +++++- datacontract/engines/ibis/ibis_check_execute.py | 11 +++++++++-- tests/test_custom_quality_checks.py | 9 +++++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/datacontract/engines/checks/check_spec.py b/datacontract/engines/checks/check_spec.py index 2b0617aaf..d583fdec5 100644 --- a/datacontract/engines/checks/check_spec.py +++ b/datacontract/engines/checks/check_spec.py @@ -166,6 +166,7 @@ class CheckSpec: query: Optional[str] = None # CUSTOM_SQL (placeholders already substituted) dialect: Optional[str] = None # CUSTOM_SQL input SQL dialect diagnostics: Optional[dict] = None # CUSTOM_SQL: reported alongside the measured value + custom_quality_check: Optional[str] = None # CUSTOM_SQL: the check's ID and arguments, as between(min=1, max=100) warnings: Optional[List[str]] = None # for the run's log seconds: Optional[int] = None # FRESHNESS / RETENTION threshold in seconds diff --git a/datacontract/engines/checks/create_checks.py b/datacontract/engines/checks/create_checks.py index c3ff2b097..f63032740 100644 --- a/datacontract/engines/checks/create_checks.py +++ b/datacontract/engines/checks/create_checks.py @@ -1001,6 +1001,7 @@ def _quality_rule_checks( severity=rule.severity, dimension=rule.dimension, diagnostics=instance.diagnostics, + custom_quality_check=instance.call, warnings=instance.warnings, ) ] diff --git a/datacontract/engines/checks/custom_quality_check.py b/datacontract/engines/checks/custom_quality_check.py index 92898a208..918bce440 100644 --- a/datacontract/engines/checks/custom_quality_check.py +++ b/datacontract/engines/checks/custom_quality_check.py @@ -299,6 +299,8 @@ class Instance: # The rule with the check's defaults filled in: expected result and dimension. quality: DataQuality name: str + # The check's ID with the rule's arguments, as between(min=1, max=100) + call: str diagnostics: dict warnings: list[str] @@ -531,6 +533,7 @@ def value_text(value) -> str: for item in (value if isinstance(value, list) else [value]) ] arguments[argument] = (False, numbers if isinstance(value, list) else numbers[0]) + call = f"{check.id}({', '.join(f'{name}={value_text(value)}' for name, (_, value) in arguments.items())})" if quality.description: name = quality.description elif check.description: @@ -550,7 +553,7 @@ def fill(match: re.Match) -> str: name = reference_pattern(TABLE_PLACEHOLDERS + COLUMN_PLACEHOLDERS).sub(fill, check.description) else: - name = f"{check.id}({', '.join(f'{name}={value_text(value)}' for name, (_, value) in arguments.items())})" + name = call for argument, (identifier, value) in arguments.items(): if identifier and (not isinstance(value, str) or not value): @@ -612,6 +615,7 @@ def fill(match: re.Match) -> str: arguments=arguments, quality=quality.model_copy(update=updates), name=name, + call=call, diagnostics={ "custom_quality_check": check.id, "query": query_dialect, diff --git a/datacontract/engines/ibis/ibis_check_execute.py b/datacontract/engines/ibis/ibis_check_execute.py index b92354eb0..95b5e95b5 100644 --- a/datacontract/engines/ibis/ibis_check_execute.py +++ b/datacontract/engines/ibis/ibis_check_execute.py @@ -363,8 +363,15 @@ def _invalid(c, _t=t, _dtype=dtype, _spec=spec, _flag=unconstrained): except _ColumnNotFound as e: set_result(run, spec.key, ResultEnum.failed, str(e) or f"Column '{spec.field}' not found") except Exception as e: - logger.warning("Check '%s' errored: %s", spec.key, e) - set_result(run, spec.key, ResultEnum.failed, f"Error evaluating check: {e}") + # BigQuery's str() repeats the message after the failed job's details + detail = e.message if isinstance(getattr(e, "message", None), str) else str(e) + logger.warning("Check '%s' errored: %s", spec.key, detail) + reason = ( + f"The custom quality check {spec.custom_quality_check} could not run: {detail}" + if spec.custom_quality_check + else f"Error evaluating check: {detail}" + ) + set_result(run, spec.key, ResultEnum.failed, reason) if agg_exprs: _run_aggregation(run, t, agg_exprs) diff --git a/tests/test_custom_quality_checks.py b/tests/test_custom_quality_checks.py index cdbb013d4..46a2dadb0 100644 --- a/tests/test_custom_quality_checks.py +++ b/tests/test_custom_quality_checks.py @@ -467,6 +467,15 @@ def test_a_value_in_a_string_that_is_not_plain_makes_the_check_invalid(tmp_path) _unexecuted(spec, "error", "${arguments.x} in queries.postgres is inside a string written as E'...'.") +def test_a_query_the_database_rejects_names_the_check_and_its_arguments(tmp_path): + checks = _check_file(tmp_path, "below", " min:\n", "SELECT COUNT(*) FROM ${table} WHERE email < ${arguments.min}") + run = _test(_contract(schema_quality=_schema_rule(_rule("below", "min: 0"))), checks) + + check = next(c for c in run.checks if c.type == "model_quality_custom") + assert check.result == ResultEnum.failed + assert check.reason.startswith("The custom quality check below(min=0) could not run: Binder Error:") + + def test_a_negative_number_after_a_minus_does_not_start_a_comment(tmp_path): checks = _check_file( tmp_path, "check", " x:\n", "SELECT COUNT(*) FROM ${table} WHERE ${column} < 5 -${arguments.x} AND 1 = 1" From a1c69f217d5d714c0c28050971d36170ab126115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6dl?= Date: Tue, 27 Oct 2026 11:22:37 +0100 Subject: [PATCH 3/4] Link the BigQuery changelog entry to #1770 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb11e8b29..65a015fc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `datacontract test` matches custom quality check IDs regardless of case ### Fixed -- `datacontract test` on BigQuery: SQL quality rules and custom quality checks can name the table without its dataset again +- `datacontract test` on BigQuery: SQL quality rules and custom quality checks can name the table without its dataset again (#1770) ## [1.2.8] - 2026-10-26 From c6d50cc80a6e2d931b731967a7a5efe822453dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakob=20Sch=C3=B6dl?= Date: Tue, 27 Oct 2026 12:27:21 +0100 Subject: [PATCH 4/4] Show a BigQuery error's message once when a check errors --- .../engines/ibis/ibis_check_execute.py | 7 +++++-- tests/test_custom_quality_checks.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/datacontract/engines/ibis/ibis_check_execute.py b/datacontract/engines/ibis/ibis_check_execute.py index 95b5e95b5..427e80d89 100644 --- a/datacontract/engines/ibis/ibis_check_execute.py +++ b/datacontract/engines/ibis/ibis_check_execute.py @@ -363,8 +363,11 @@ def _invalid(c, _t=t, _dtype=dtype, _spec=spec, _flag=unconstrained): except _ColumnNotFound as e: set_result(run, spec.key, ResultEnum.failed, str(e) or f"Column '{spec.field}' not found") except Exception as e: - # BigQuery's str() repeats the message after the failed job's details - detail = e.message if isinstance(getattr(e, "message", None), str) else str(e) + # BigQuery spells its message out twice; its first error holds it once + try: + detail = e.errors[0]["message"] + except (AttributeError, LookupError, TypeError): + detail = str(e) logger.warning("Check '%s' errored: %s", spec.key, detail) reason = ( f"The custom quality check {spec.custom_quality_check} could not run: {detail}" diff --git a/tests/test_custom_quality_checks.py b/tests/test_custom_quality_checks.py index 46a2dadb0..939b7a98a 100644 --- a/tests/test_custom_quality_checks.py +++ b/tests/test_custom_quality_checks.py @@ -5,12 +5,14 @@ import pytest import responses +from google.api_core.exceptions import BadRequest from open_data_contract_standard.model import Server from datacontract.config import Config from datacontract.data_contract import DataContract from datacontract.engines.checks.create_checks import create_checks from datacontract.engines.checks.custom_quality_check import CustomQualityChecks +from datacontract.engines.ibis import ibis_check_execute from datacontract.model.run import ResultEnum CHECKS = "./fixtures/custom-quality-checks/checks" @@ -476,6 +478,24 @@ def test_a_query_the_database_rejects_names_the_check_and_its_arguments(tmp_path assert check.reason.startswith("The custom quality check below(min=0) could not run: Binder Error:") +def test_a_bigquery_error_shows_its_message_once(tmp_path, monkeypatch): + message = "Unrecognized name: nope at [1:38]" + error = BadRequest( + f"{message}; reason: invalidQuery, location: query, message: {message}\n\nLocation: US\nJob ID: 1234\n", + errors=[{"reason": "invalidQuery", "location": "query", "message": message}], + ) + + def reject(con, query, dialect): + raise error + + monkeypatch.setattr(ibis_check_execute, "_run_scalar", reject) + checks = _check_file(tmp_path, "below", " min:\n", "SELECT COUNT(*) FROM ${table} WHERE nope < ${arguments.min}") + run = _test(_contract(schema_quality=_schema_rule(_rule("below", "min: 0"))), checks) + + check = next(c for c in run.checks if c.type == "model_quality_custom") + assert check.reason == f"The custom quality check below(min=0) could not run: {message}" + + def test_a_negative_number_after_a_minus_does_not_start_a_comment(tmp_path): checks = _check_file( tmp_path, "check", " x:\n", "SELECT COUNT(*) FROM ${table} WHERE ${column} < 5 -${arguments.x} AND 1 = 1"