Skip to content

Commit

Permalink
case insensitive check on partition matching (#888)
Browse files Browse the repository at this point in the history
* case insensitive check on partition matching

* Review change

---------

Co-authored-by: Christophe Oudar <[email protected]>
  • Loading branch information
github-christophe-oudar and Kayrnt authored Aug 22, 2023
1 parent f2525cc commit 2c7220a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230818-214616.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: case insensitive check on partition matching
time: 2023-08-18T21:46:16.828488+02:00
custom:
Author: Kayrnt
Issue: "886"
2 changes: 1 addition & 1 deletion dbt/adapters/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dbt.adapters.bigquery.connections import BigQueryCredentials
from dbt.adapters.bigquery.relation import BigQueryRelation # noqa
from dbt.adapters.bigquery.column import BigQueryColumn # noqa
from dbt.adapters.bigquery.impl import BigQueryAdapter, GrantTarget # noqa
from dbt.adapters.bigquery.impl import BigQueryAdapter, GrantTarget, PartitionConfig # noqa

from dbt.adapters.base import AdapterPlugin
from dbt.include import bigquery
Expand Down
5 changes: 3 additions & 2 deletions dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,13 @@ def _partitions_match(table, conf_partition: Optional[PartitionConfig]) -> bool:
table_field = (
table.time_partitioning.field.lower() if table.time_partitioning.field else None
)

table_granularity = table.partitioning_type
conf_table_field = conf_partition.field
return (
table_field == conf_table_field
table_field == conf_table_field.lower()
or (conf_partition.time_ingestion_partitioning and table_field is not None)
) and table_granularity == conf_partition.granularity
) and table_granularity.lower() == conf_partition.granularity.lower()
elif conf_partition and table.range_partitioning is not None:
dest_part = table.range_partitioning
conf_part = conf_partition.range or {}
Expand Down
31 changes: 30 additions & 1 deletion tests/unit/test_bigquery_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

import dbt.dataclass_schema

from dbt.adapters.bigquery import PartitionConfig
from dbt.adapters.bigquery import BigQueryCredentials
from dbt.adapters.bigquery import BigQueryAdapter
from dbt.adapters.bigquery import BigQueryRelation
from dbt.adapters.bigquery import Plugin as BigQueryPlugin
from google.cloud.bigquery.table import Table
from dbt.adapters.bigquery.connections import BigQueryConnectionManager
from dbt.adapters.bigquery.connections import _sanitize_label, _VALIDATE_LABEL_LENGTH_LIMIT
from dbt.adapters.base.query_headers import MacroQueryStringSetter
Expand Down Expand Up @@ -376,7 +378,10 @@ def test_location_user_agent(self, mock_bq, mock_auth_default):
mock_client.assert_not_called()
connection.handle
mock_client.assert_called_once_with(
"dbt-unit-000000", creds, location="Luna Station", client_info=HasUserAgent()
"dbt-unit-000000",
creds,
location="Luna Station",
client_info=HasUserAgent(),
)


Expand Down Expand Up @@ -1023,6 +1028,30 @@ def test_convert_time_type(self):
for col_idx, expect in enumerate(expected):
assert BigQueryAdapter.convert_time_type(agate_table, col_idx) == expect

# The casing in this case can't be enforced on the API side,
# so we have to validate that we have a case-insensitive comparison
def test_partitions_match(self):
table = Table.from_api_repr(
{
"tableReference": {
"projectId": "test-project",
"datasetId": "test_dataset",
"tableId": "test_table",
},
"timePartitioning": {"type": "DAY", "field": "ts"},
}
)
partition_config = PartitionConfig.parse(
{
"field": "TS",
"data_type": "date",
"granularity": "day",
"time_ingestion_partitioning": False,
"copy_partitions": False,
}
)
assert BigQueryAdapter._partitions_match(table, partition_config) is True


class TestBigQueryGrantAccessTo(BaseTestBigQueryAdapter):
entity = BigQueryRelation.from_dict(
Expand Down

0 comments on commit 2c7220a

Please sign in to comment.