Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion datacontract/engines/checks/physical_type_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/bigquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 */}

Expand Down
5 changes: 5 additions & 0 deletions tests/test_physical_type_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading