Repository: dbt-athena/dbt-athena
Pull request #485 (2023-11-02 10:50:27)

fix: persist_docs too long comment
# Description

<!--- Add a little description on what you plan to tackle with this PR -->
<!--- Add resolve #issue-number in case you resolve an open issue -->

## Models used to test - Optional
<!--- Add here the models that you use to test the changes -->

## Checklist

- [ ] You followed [contributing section](https://github.com/dbt-athena/dbt-athena#contributing)
- [ ] You kept your Pull Request small and focused on a single feature or bug fix.
- [ ] You added unit testing when necessary
- [ ] You added functional testing when necessary


nicor88: @Jrmyy shall we merge this one? 

Patch:
diff --git a/dbt/adapters/athena/impl.py b/dbt/adapters/athena/impl.py
index 063afd8..390ca11 100755
--- a/dbt/adapters/athena/impl.py
+++ b/dbt/adapters/athena/impl.py
@@ -48,6 +48,7 @@ from dbt.adapters.athena.s3 import S3DataNaming
 from dbt.adapters.athena.utils import (
     AthenaCatalogType,
     clean_sql_comment,
+    ellipsis_comment,
     get_catalog_id,
     get_catalog_type,
     get_chunks,
@@ -958,7 +959,7 @@ class AthenaAdapter(SQLAdapter):
         # Update table description
         if persist_relation_docs:
             # Prepare dbt description
-            clean_table_description = clean_sql_comment(model["description"])
+            clean_table_description = ellipsis_comment(clean_sql_comment(model["description"]))
             # Get current description from Glue
             glue_table_description = table.get("Description", "")
             # Get current description parameter from Glue
@@ -1004,7 +1005,7 @@ class AthenaAdapter(SQLAdapter):
                 if col_name in model["columns"]:
                     col_comment = model["columns"][col_name]["description"]
                     # Prepare column description from dbt
-                    clean_col_comment = clean_sql_comment(col_comment)
+                    clean_col_comment = ellipsis_comment(clean_sql_comment(col_comment))
                     # Get current column comment from Glue
                     glue_col_comment = col_obj.get("Comment", "")
                     # Update column description if it's different
diff --git a/dbt/adapters/athena/utils.py b/dbt/adapters/athena/utils.py
index 0922f6d..39ec755 100644
--- a/dbt/adapters/athena/utils.py
+++ b/dbt/adapters/athena/utils.py
@@ -56,3 +56,8 @@ def get_chunks(lst: List[T], n: int) -> Generator[List[T], None, None]:
     """Yield successive n-sized chunks from lst."""
     for i in range(0, len(lst), n):
         yield lst[i : i + n]
+
+
+def ellipsis_comment(s: str, max_len: int = 255) -> str:
+    """Ellipsis string if it exceeds max length"""
+    return f"{s[:(max_len - 3)]}..." if len(s) > max_len else s


Test patch:
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 543558e..69a2dc9 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -1,5 +1,8 @@
+import pytest
+
 from dbt.adapters.athena.utils import (
     clean_sql_comment,
+    ellipsis_comment,
     get_chunks,
     is_valid_table_parameter_key,
     stringify_table_parameter_value,
@@ -56,3 +59,14 @@ def test_get_chunks_more_elements_than_chunk():
     chunks = list(get_chunks([1, 2, 3], 4))
     assert chunks[0] == [1, 2, 3]
     assert len(chunks) == 1
+
+
+@pytest.mark.parametrize(
+    ("max_len", "expected"),
+    (
+        pytest.param(12, "abc def ghi", id="ok string"),
+        pytest.param(6, "abc...", id="ellipsis"),
+    ),
+)
+def test_ellipsis_comment(max_len, expected):
+    assert expected == ellipsis_comment("abc def ghi", max_len=max_len)