diff --git a/plugins/bigquery/dbt/adapters/bigquery/connections.py b/plugins/bigquery/dbt/adapters/bigquery/connections.py index 69ae2178532..acbecfa84c2 100644 --- a/plugins/bigquery/dbt/adapters/bigquery/connections.py +++ b/plugins/bigquery/dbt/adapters/bigquery/connections.py @@ -596,7 +596,7 @@ def _is_retryable(error): def _sanitize_label(value: str, max_length: int = 63) -> str: """Return a legal value for a BigQuery label.""" - value = value.lower() + value = value.strip().lower() value = _SANITIZE_LABEL_PATTERN.sub("_", value) - value = value[: max_length - 1] + value = value[: max_length] return value diff --git a/test/unit/test_bigquery_adapter.py b/test/unit/test_bigquery_adapter.py index 7cc71c6e459..5f317376191 100644 --- a/test/unit/test_bigquery_adapter.py +++ b/test/unit/test_bigquery_adapter.py @@ -16,12 +16,14 @@ from dbt.adapters.bigquery import BigQueryRelation from dbt.adapters.bigquery import Plugin as BigQueryPlugin from dbt.adapters.bigquery.connections import BigQueryConnectionManager +from dbt.adapters.bigquery.connections import _sanitize_label from dbt.adapters.base.query_headers import MacroQueryStringSetter from dbt.clients import agate_helper import dbt.exceptions from dbt.logger import GLOBAL_LOGGER as logger # noqa from dbt.context.providers import RuntimeConfigObject +import pytest import google.cloud.bigquery from .utils import config_from_parts_or_dicts, inject_adapter, TestAdapterConversions @@ -939,3 +941,16 @@ def test_convert_time_type(self): expected = ['time', 'time', 'time'] for col_idx, expect in enumerate(expected): assert BigQueryAdapter.convert_time_type(agate_table, col_idx) == expect + + +@pytest.mark.parametrize( + ["input", "output"], + [ + ("a" * 64, "a" * 63), + ("ABC", "abc"), + ("a c", "a_c"), + ("a ", "a"), + ], +) +def test_sanitize_label(input, output): + assert _sanitize_label(input) == output