Skip to content

Commit 15053c2

Browse files
committed
Add support for column comments
Signed-off-by: Christophe Bornet <[email protected]>
1 parent 62eb1d4 commit 15053c2

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/databricks/sqlalchemy/_ddl.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from sqlalchemy.sql import compiler
2+
from sqlalchemy.sql import compiler, sqltypes
33
import logging
44

55
logger = logging.getLogger(__name__)
@@ -40,16 +40,22 @@ def visit_identity_column(self, identity, **kw):
4040
return text
4141

4242
def get_column_specification(self, column, **kwargs):
43-
"""Currently we override this method only to emit a log message if a user attempts to set
44-
autoincrement=True on a column. See comments in test_suite.py. We may implement implicit
45-
IDENTITY using this feature in the future, similar to the Microsoft SQL Server dialect.
46-
"""
43+
# Emit a log message if a user attempts to set autoincrement=True on a column.
44+
# See comments in test_suite.py. We may implement implicit IDENTITY using this
45+
# feature in the future, similar to the Microsoft SQL Server dialect.
4746
if column is column.table._autoincrement_column or column.autoincrement is True:
4847
logger.warn(
4948
"Databricks dialect ignores SQLAlchemy's autoincrement semantics. Use explicit Identity() instead."
5049
)
5150

52-
return super().get_column_specification(column, **kwargs)
51+
colspec = super().get_column_specification(column, **kwargs)
52+
if column.comment is not None:
53+
literal = self.sql_compiler.render_literal_value(
54+
column.comment, sqltypes.STRINGTYPE
55+
)
56+
colspec += " COMMENT " + literal
57+
58+
return colspec
5359

5460

5561
class DatabricksStatementCompiler(compiler.SQLCompiler):

src/databricks/sqlalchemy/_parse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def parse_column_info_from_tgetcolumnsresponse(thrift_resp_row) -> ReflectedColu
354354
"type": final_col_type,
355355
"nullable": bool(thrift_resp_row.NULLABLE),
356356
"default": thrift_resp_row.COLUMN_DEF,
357+
"comment": thrift_resp_row.REMARKS,
357358
}
358359

359360
# TODO: figure out how to return sqlalchemy.interfaces in a way that mypy respects

src/databricks/sqlalchemy/test_local/e2e/test_basic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ def test_create_table_not_null(db_engine, metadata_obj: MetaData):
188188
metadata_obj.drop_all(db_engine)
189189

190190

191+
def test_column_comment(db_engine, metadata_obj: MetaData):
192+
table_name = "PySQLTest_{}".format(datetime.datetime.utcnow().strftime("%s"))
193+
194+
SampleTable = Table(
195+
table_name,
196+
metadata_obj,
197+
Column("name", String(255), comment="some comment")
198+
)
199+
200+
metadata_obj.create_all(db_engine)
201+
202+
columns = db_engine.dialect.get_columns(
203+
connection=db_engine.connect(), table_name=table_name
204+
)
205+
206+
assert columns[0].get('comment') == "some comment"
207+
208+
metadata_obj.drop_all(db_engine)
209+
210+
191211
def test_bulk_insert_with_core(db_engine, metadata_obj, session):
192212
import random
193213

0 commit comments

Comments
 (0)