SkastVnT commented on code in PR #66895:
URL: https://github.com/apache/airflow/pull/66895#discussion_r3266359122


##########
providers/databricks/src/airflow/providers/databricks/hooks/databricks_sql.py:
##########
@@ -71,6 +74,35 @@ def _cancel():
     return timer, timeout_event
 
 
+def _format_query_tag_value(value: str) -> str:
+    """
+    Escape special characters and truncate a single query tag value.
+
+    Databricks ``QUERY_TAGS`` uses ``key:value`` pairs delimited by commas, so
+    backslash, comma and colon inside *values* must be escaped.  Values are 
also
+    capped at 128 characters before escaping to keep the overall tag string
+    within reasonable bounds.
+    """
+    raw = str(value)
+    if len(raw) > 128:
+        log.warning(
+            "Query tag value truncated to 128 characters (original length %d): 
%r", len(raw), raw[:128]
+        )
+    value = raw[:128]
+    return value.replace("\\", "\\\\").replace(",", "\\,").replace(":", "\\:")
+
+
+def _format_query_tags(tags: dict[str, str | None]) -> str:
+    """
+    Serialize a query-tags dict to the ``key:value,key:value`` string expected 
by ``QUERY_TAGS``.
+
+    Entries whose value is ``None`` are omitted.
+    """
+    return ",".join(
+        f"{key}:{_format_query_tag_value(value)}" for key, value in 
tags.items() if value is not None

Review Comment:
   Correct. Keys are expected to be strings; only values can be None. The None 
filtering is only for values so optional tags can be omitted cleanly.



##########
providers/databricks/src/airflow/providers/databricks/hooks/databricks_sql.py:
##########
@@ -71,6 +74,35 @@ def _cancel():
     return timer, timeout_event
 
 
+def _format_query_tag_value(value: str) -> str:
+    """
+    Escape special characters and truncate a single query tag value.
+
+    Databricks ``QUERY_TAGS`` uses ``key:value`` pairs delimited by commas, so

Review Comment:
   Thanks! The 128 limit follows the Databricks **QUERY_TAGS** key/value limit. 
I added the comment to make that contract explicit: we truncate the raw source 
value to 128 characters before escaping it for the **QUERY_TAGS** string format.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to