From ec733d84cc31fd89dbd35cf7b198f1dfea589c02 Mon Sep 17 00:00:00 2001 From: Roy K Smith <5792632+royroyroys@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:28:59 +1100 Subject: [PATCH] Keep BigQuery DATETIME and TIMESTAMP distinct in physical type checks Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NoJ39WkN8uwNGtLBZUiMAC --- CHANGELOG.md | 1 + datacontract/engines/checks/physical_type_match.py | 4 +++- docs/docs/reference/bigquery.md | 2 +- tests/test_physical_type_match.py | 5 +++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3802fefef..3929ef63a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The CLI no longer fails with `UnicodeEncodeError` when its output is piped on Windows - `datacontract import postgres` and `import sql` no longer map bit strings of more than one bit (`bit(8)`, `bit varying`) to `boolean` - `datacontract lint` accepts the ids and references the current ODCS v3.1.0 schema allows, such as ids with `:` and shorthand references with `-` (#1859) +- `datacontract test` no longer treats BigQuery `DATETIME` and `TIMESTAMP` columns as the same physical type - `datacontract lint` accepts quality rules that combine several operators, such as `mustBeGreaterThan` with `mustBeLessThan`, and `test`, `breaking`, `dbt sync` and `export sodacl` apply every one of them; `export excel` warns about the ones it cannot hold (#1859) ## [1.2.11] - 2026-11-17 diff --git a/datacontract/engines/checks/physical_type_match.py b/datacontract/engines/checks/physical_type_match.py index a56f3995a..c47e90e5d 100644 --- a/datacontract/engines/checks/physical_type_match.py +++ b/datacontract/engines/checks/physical_type_match.py @@ -38,6 +38,8 @@ def _timestamp_family() -> set: time zone`` column: they are the same base type and the distinction is not modelled at the logical level many contracts are written at (in particular, DCS contracts store the logical ``timestamp`` keyword as the physicalType). + Not on BigQuery, where they are two types: ``DATETIME`` (parsed as TIMESTAMP) + is a civil date and time, ``TIMESTAMP`` (parsed as TIMESTAMPTZ) an instant. """ names = ("TIMESTAMP", "TIMESTAMPTZ", "TIMESTAMPLTZ", "TIMESTAMPNTZ", "TIMESTAMP_S", "TIMESTAMP_MS", "TIMESTAMP_NS") return {getattr(exp.DataType.Type, n) for n in names if hasattr(exp.DataType.Type, n)} @@ -218,7 +220,7 @@ def _base_compatible(exp_dt: exp.DataType, act_dt: exp.DataType, dialect) -> boo both_numeric = {exp_dt.this, act_dt.this} <= exp.DataType.NUMERIC_TYPES return ( exp_dt.this == act_dt.this - or {exp_dt.this, act_dt.this} <= _TIMESTAMP_FAMILY + or ({exp_dt.this, act_dt.this} <= _TIMESTAMP_FAMILY and _dialect_name(dialect) != "bigquery") or (both_numeric and _base_sql(exp_dt, dialect) == _base_sql(act_dt, dialect)) or (_is_snowflake(dialect) and any({exp_dt.this, act_dt.this} <= family for family in _SNOWFLAKE_FAMILIES)) or (_dialect_name(dialect) in _TRINO_DIALECTS and {exp_dt.this, act_dt.this} <= _TRINO_TEXT_FAMILY) diff --git a/docs/docs/reference/bigquery.md b/docs/docs/reference/bigquery.md index 79ca8a7aa..3c927a6ec 100644 --- a/docs/docs/reference/bigquery.md +++ b/docs/docs/reference/bigquery.md @@ -89,7 +89,7 @@ Fields with mode `REQUIRED` become `required: true`. Tables, external tables, an ### Testing -BigQuery supports **native type introspection**: the declared `physicalType` is checked against the actual column type from the BigQuery API. Timezone variants of timestamps are interchangeable; parameters (precision, length) are only enforced when the contract declares them. If the `physicalType` cannot be interpreted as a BigQuery type, the check falls back to the logical type category comparison. +BigQuery supports **native type introspection**: the declared `physicalType` is checked against the actual column type from the BigQuery API. `DATETIME` and `TIMESTAMP` are distinct types and do not match each other; parameters (precision, length) are only enforced when the contract declares them. If the `physicalType` cannot be interpreted as a BigQuery type, the check falls back to the logical type category comparison. {/* AUTOGENERATED TYPE MAPPING: do not edit by hand; regenerate with update_reference_types.py */} diff --git a/tests/test_physical_type_match.py b/tests/test_physical_type_match.py index 3632408fa..d4ea62a3e 100644 --- a/tests/test_physical_type_match.py +++ b/tests/test_physical_type_match.py @@ -96,6 +96,11 @@ def test_bigquery_legacy_type_names_match_googlesql_names(): assert physical_type_matches("BYTEINT", "INT64", "bigquery")[0] is True +def test_bigquery_datetime_and_timestamp_stay_distinct(): + assert physical_type_matches("DATETIME", "TIMESTAMP", "bigquery")[0] is False + assert physical_type_matches("TIMESTAMP", "DATETIME", "bigquery")[0] is False + + def test_integer_widths_stay_distinct_outside_bigquery(): # Aliasing is the dialect's call, not ours: INTEGER is a narrower type than # BIGINT in Postgres, so declaring one against the other fails.