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 docs/integrations/engines/bigquery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions sqlmesh/core/config/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1232,6 +1233,7 @@ def _extra_engine_config(self) -> t.Dict[str, t.Any]:
"priority",
"maximum_bytes_billed",
"reservation",
"max_table_comment_length",
}
}

Expand Down
6 changes: 6 additions & 0 deletions sqlmesh/core/engine_adapter/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 21 additions & 3 deletions tests/core/engine_adapter/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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)

Expand Down