diff --git a/docs/integrations/engines/bigquery.md b/docs/integrations/engines/bigquery.md index 4ea4d1d222..66878b2a8d 100644 --- a/docs/integrations/engines/bigquery.md +++ b/docs/integrations/engines/bigquery.md @@ -164,6 +164,7 @@ pip install "sqlmesh[bigquery]" | `job_retries` | The number of times to retry the underlying job if it fails. (Default: `1`) | int | N | | `priority` | The priority of the underlying job. (Default: `INTERACTIVE`) | string | N | | `maximum_bytes_billed` | The maximum number of bytes to be billed for the underlying job. | int | N | +| `max_table_comment_length` | Maximum length of table and view descriptions in characters. Defaults to 1,024. Set a value from 1 to 16,384. | int | N | ## Authentication Methods - [oauth](https://google-auth.readthedocs.io/en/master/reference/google.auth.html#google.auth.default) (default) diff --git a/sqlmesh/core/config/connection.py b/sqlmesh/core/config/connection.py index c23c468841..9a237162cb 100644 --- a/sqlmesh/core/config/connection.py +++ b/sqlmesh/core/config/connection.py @@ -1122,6 +1122,7 @@ class BigQueryConnectionConfig(ConnectionConfig): priority: t.Optional[BigQueryPriority] = None maximum_bytes_billed: t.Optional[int] = None reservation: t.Optional[str] = None + max_table_comment_length: t.Optional[int] = Field(default=None, ge=1, le=16_384) concurrent_tasks: int = 1 register_comments: bool = True @@ -1232,6 +1233,7 @@ def _extra_engine_config(self) -> t.Dict[str, t.Any]: "priority", "maximum_bytes_billed", "reservation", + "max_table_comment_length", } } diff --git a/sqlmesh/core/engine_adapter/bigquery.py b/sqlmesh/core/engine_adapter/bigquery.py index e87a768f2d..ae26e9c5af 100644 --- a/sqlmesh/core/engine_adapter/bigquery.py +++ b/sqlmesh/core/engine_adapter/bigquery.py @@ -1049,6 +1049,12 @@ def _build_view_properties_exp( return exp.Properties(expressions=properties) return None + def _truncate_table_comment(self, comment: str) -> str: + limit = self._extra_config.get("max_table_comment_length") + return self._truncate_comment( + comment, self.MAX_TABLE_COMMENT_LENGTH if limit is None else limit + ) + def _build_create_comment_table_exp( self, table: exp.Table, table_comment: str, table_kind: str ) -> exp.Comment | str: diff --git a/tests/core/engine_adapter/test_bigquery.py b/tests/core/engine_adapter/test_bigquery.py index 783bd48f91..f3206cfc30 100644 --- a/tests/core/engine_adapter/test_bigquery.py +++ b/tests/core/engine_adapter/test_bigquery.py @@ -5,12 +5,14 @@ import pandas as pd # noqa: TID253 import pytest from google.cloud import bigquery +from pydantic import ValidationError from pytest_mock.plugin import MockerFixture from sqlglot import expressions as exp from sqlglot import parse_one from sqlglot.helper import ensure_list import sqlmesh.core.dialect as d +from sqlmesh.core.config import BigQueryConnectionConfig from sqlmesh.core.engine_adapter import BigQueryEngineAdapter from sqlmesh.core.engine_adapter.bigquery import select_partitions_expr from sqlmesh.core.engine_adapter.mixins import ( @@ -626,10 +628,20 @@ def test_create_table_table_options(make_mocked_engine_adapter: t.Callable, mock ] -def test_comments(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture): - adapter = make_mocked_engine_adapter(BigQueryEngineAdapter) +@pytest.mark.parametrize("max_table_comment_length", [None, 16_384]) +def test_comments( + make_mocked_engine_adapter: t.Callable, + mocker: MockerFixture, + max_table_comment_length: t.Optional[int], +): + config = ( + BigQueryConnectionConfig() + if max_table_comment_length is None + else BigQueryConnectionConfig(max_table_comment_length=max_table_comment_length) + ) + adapter = make_mocked_engine_adapter(BigQueryEngineAdapter, **config._extra_engine_config) - allowed_table_comment_length = BigQueryEngineAdapter.MAX_TABLE_COMMENT_LENGTH + allowed_table_comment_length = max_table_comment_length or 1_024 truncated_table_comment = "a" * allowed_table_comment_length long_table_comment = truncated_table_comment + "b" @@ -708,6 +720,12 @@ def test_comments(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture) ] +@pytest.mark.parametrize("max_table_comment_length", [0, 16_385]) +def test_invalid_max_table_comment_length(max_table_comment_length: int): + with pytest.raises(ValidationError, match="max_table_comment_length"): + BigQueryConnectionConfig(max_table_comment_length=max_table_comment_length) + + def test_nested_comments(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture): adapter = make_mocked_engine_adapter(BigQueryEngineAdapter)