11import re
2- from sqlalchemy .sql import compiler
2+ from sqlalchemy .sql import compiler , sqltypes , ColumnElement
33
44
55class DatabricksIdentifierPreparer (compiler .IdentifierPreparer ):
@@ -15,3 +15,52 @@ def __init__(self, dialect):
1515class DatabricksDDLCompiler (compiler .DDLCompiler ):
1616 def post_create_table (self , table ):
1717 return " USING DELTA"
18+ def visit_set_column_comment (self , create , ** kw ):
19+ """
20+ Example syntax for adding column comment:
21+ "ALTER TABLE schema.table_name CHANGE COLUMN COLUMN_NAME COMMENT 'Comment to be added to column';"
22+
23+ """
24+ return """ALTER TABLE {0} CHANGE COLUMN {1} COMMENT {2}""" .format (
25+ 'testing.alembic_test1' ,
26+ self ._format_table_from_column (
27+ create , use_schema = True
28+ ),
29+ self .preparer .format_column (
30+ create .element , use_table = False
31+ ),
32+ self .sql_compiler .render_literal_value (
33+ create .element .comment , sqltypes .String ()
34+ ),
35+ )
36+
37+ def visit_drop_column_comment (self , drop , ** kw ):
38+ """
39+ Example syntax for dropping column comment:
40+ "ALTER TABLE schema.table_name CHANGE COLUMN COLUMN_NAME COMMENT '';"
41+
42+ Note: There is no syntactical 'DROP' statement in this case, the comment must be replaced with an empty string
43+ """
44+ return "ALTER TABLE {0} CHANGE COLUMN {1} COMMENT '';" .format (
45+ self ._format_table_from_column (
46+ drop , use_schema = True
47+ ),
48+ self .preparer .format_column (
49+ drop .element , use_table = False
50+ )
51+ )
52+
53+ def _format_table_from_column (self , column_object , use_schema = False ):
54+ """
55+ Prepare a quoted table name from the column object (including schema if specified)
56+ """
57+ schema_table_column = self .preparer .format_column (
58+ column_object .element , use_table = True , use_schema = True
59+ )
60+
61+ name = schema_table_column .split ("." )[1 ]
62+
63+ if use_schema :
64+ name = schema_table_column .split ("." )[0 ] + '.' + name
65+
66+ return name
0 commit comments