Skip to content

Add support for column comments #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 27 additions & 6 deletions src/databricks/sqlalchemy/_ddl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from sqlalchemy.sql import compiler
from sqlalchemy.sql import compiler, sqltypes
import logging

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -39,17 +39,38 @@ def visit_identity_column(self, identity, **kw):
)
return text

def visit_set_column_comment(self, create, **kw):
return "ALTER TABLE %s ALTER COLUMN %s COMMENT %s" % (
self.preparer.format_table(create.element.table),
self.preparer.format_column(create.element),
self.sql_compiler.render_literal_value(
create.element.comment, sqltypes.String()
),
)

def visit_drop_column_comment(self, create, **kw):
return "ALTER TABLE %s ALTER COLUMN %s COMMENT ''" % (
self.preparer.format_table(create.element.table),
self.preparer.format_column(create.element),
)

def get_column_specification(self, column, **kwargs):
"""Currently we override this method only to emit a log message if a user attempts to set
autoincrement=True on a column. See comments in test_suite.py. We may implement implicit
IDENTITY using this feature in the future, similar to the Microsoft SQL Server dialect.
"""
# Emit a log message if a user attempts to set autoincrement=True on a column.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this as a docstring, since regular comments aren't surfaced in a code editor

# See comments in test_suite.py. We may implement implicit IDENTITY using this
# feature in the future, similar to the Microsoft SQL Server dialect.
if column is column.table._autoincrement_column or column.autoincrement is True:
logger.warn(
"Databricks dialect ignores SQLAlchemy's autoincrement semantics. Use explicit Identity() instead."
)

return super().get_column_specification(column, **kwargs)
colspec = super().get_column_specification(column, **kwargs)
if column.comment is not None:
literal = self.sql_compiler.render_literal_value(
column.comment, sqltypes.STRINGTYPE
)
colspec += " COMMENT " + literal

return colspec


class DatabricksStatementCompiler(compiler.SQLCompiler):
Expand Down
1 change: 1 addition & 0 deletions src/databricks/sqlalchemy/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def parse_column_info_from_tgetcolumnsresponse(thrift_resp_row) -> ReflectedColu
"type": final_col_type,
"nullable": bool(thrift_resp_row.NULLABLE),
"default": thrift_resp_row.COLUMN_DEF,
"comment": thrift_resp_row.REMARKS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to pass the ComponentReflectionTest we need to update this line to

"comment": thrift_resp_row.REMARKS or None

}

# TODO: figure out how to return sqlalchemy.interfaces in a way that mypy respects
Expand Down
36 changes: 36 additions & 0 deletions src/databricks/sqlalchemy/test_local/e2e/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sqlalchemy.engine import Engine
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
from sqlalchemy.schema import DropColumnComment, SetColumnComment
from sqlalchemy.types import BOOLEAN, DECIMAL, Date, DateTime, Integer, String

try:
Expand Down Expand Up @@ -188,6 +189,41 @@ def test_create_table_not_null(db_engine, metadata_obj: MetaData):
metadata_obj.drop_all(db_engine)


def test_column_comment(db_engine, metadata_obj: MetaData):
table_name = "PySQLTest_{}".format(datetime.datetime.utcnow().strftime("%s"))

column = Column("name", String(255), comment="some comment")
SampleTable = Table(table_name, metadata_obj, column)

metadata_obj.create_all(db_engine)
connection = db_engine.connect()

columns = db_engine.dialect.get_columns(
connection=connection, table_name=table_name
)

assert columns[0].get("comment") == "some comment"

column.comment = "other comment"
connection.execute(SetColumnComment(column))

columns = db_engine.dialect.get_columns(
connection=connection, table_name=table_name
)

assert columns[0].get("comment") == "other comment"

connection.execute(DropColumnComment(column))

columns = db_engine.dialect.get_columns(
connection=connection, table_name=table_name
)

assert columns[0].get("comment") == ""

metadata_obj.drop_all(db_engine)


def test_bulk_insert_with_core(db_engine, metadata_obj, session):
import random

Expand Down