Skip to content

Commit 0cc14fc

Browse files
authored
Feat: enable duckdb>=0.10 comment registration (#2660)
1 parent 77a7c36 commit 0cc14fc

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

docs/concepts/models/overview.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,20 @@ In the latter method, separate commands are required for every comment. This may
126126

127127
This table lists each engine's support for `TABLE` and `VIEW` object comments:
128128

129-
| Engine | `TABLE` comments | `VIEW` comments |
130-
| ------------ | ---------------- | --------------- |
131-
| BigQuery | Y | Y |
132-
| Databricks | Y | Y |
133-
| DuckDB | N | N |
134-
| MySQL | Y | Y |
135-
| MSSQL | N | N |
136-
| Postgres | Y | Y |
137-
| GCP Postgres | Y | Y |
138-
| Redshift | Y | N |
139-
| Snowflake | Y | Y |
140-
| Spark | Y | Y |
141-
| Trino | Y | Y |
129+
| Engine | `TABLE` comments | `VIEW` comments |
130+
| ------------- | ---------------- | --------------- |
131+
| BigQuery | Y | Y |
132+
| Databricks | Y | Y |
133+
| DuckDB <=0.9 | N | N |
134+
| DuckDB >=0.10 | Y | Y |
135+
| MySQL | Y | Y |
136+
| MSSQL | N | N |
137+
| Postgres | Y | Y |
138+
| GCP Postgres | Y | Y |
139+
| Redshift | Y | N |
140+
| Snowflake | Y | Y |
141+
| Spark | Y | Y |
142+
| Trino | Y | Y |
142143

143144

144145
## Model properties

sqlmesh/core/engine_adapter/duckdb.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import typing as t
4-
4+
from duckdb import __version__ as duckdb_version
55
from sqlglot import exp
66

77
from sqlmesh.core.engine_adapter.mixins import (
@@ -17,6 +17,7 @@
1717
SourceQuery,
1818
set_catalog,
1919
)
20+
from sqlmesh.utils import major_minor
2021

2122
if t.TYPE_CHECKING:
2223
from sqlmesh.core._typing import SchemaName, TableName
@@ -28,8 +29,13 @@ class DuckDBEngineAdapter(LogicalMergeMixin, GetCurrentCatalogFromFunctionMixin)
2829
DIALECT = "duckdb"
2930
SUPPORTS_TRANSACTIONS = False
3031
CATALOG_SUPPORT = CatalogSupport.FULL_SUPPORT
31-
COMMENT_CREATION_TABLE = CommentCreationTable.UNSUPPORTED
32-
COMMENT_CREATION_VIEW = CommentCreationView.UNSUPPORTED
32+
33+
# TODO: remove once we stop supporting DuckDB 0.9
34+
COMMENT_CREATION_TABLE, COMMENT_CREATION_VIEW = (
35+
(CommentCreationTable.UNSUPPORTED, CommentCreationView.UNSUPPORTED)
36+
if major_minor(duckdb_version) < (0, 10)
37+
else (CommentCreationTable.COMMENT_COMMAND_ONLY, CommentCreationView.COMMENT_COMMAND_ONLY)
38+
)
3339

3440
def set_current_catalog(self, catalog: str) -> None:
3541
"""Sets the catalog name of the current connection."""

tests/core/engine_adapter/test_integration.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ def get_table_comment(
269269
schema_name = '{schema_name}'
270270
AND table_name = '{table_name}'
271271
"""
272+
elif self.dialect == "duckdb":
273+
kind = "table" if table_kind == "BASE TABLE" else "view"
274+
query = f"""
275+
SELECT
276+
{kind}_name,
277+
comment
278+
FROM duckdb_{kind}s()
279+
WHERE
280+
schema_name = '{schema_name}'
281+
AND {kind}_name = '{table_name}'
282+
"""
272283

273284
result = self.engine_adapter.fetchall(query)
274285

@@ -353,6 +364,16 @@ def get_column_comments(
353364
elif self.dialect == "trino":
354365
query = f"SHOW COLUMNS FROM {schema_name}.{table_name}"
355366
comment_index = 3
367+
elif self.dialect == "duckdb":
368+
query = f"""
369+
SELECT
370+
column_name,
371+
comment
372+
FROM duckdb_columns()
373+
WHERE
374+
schema_name = '{schema_name}'
375+
AND table_name = '{table_name}'
376+
"""
356377

357378
result = self.engine_adapter.fetchall(query)
358379

0 commit comments

Comments
 (0)