diff --git a/src/databricks/labs/lsql/dashboards.py b/src/databricks/labs/lsql/dashboards.py index fb9ce2c1..ad29f8e0 100644 --- a/src/databricks/labs/lsql/dashboards.py +++ b/src/databricks/labs/lsql/dashboards.py @@ -429,6 +429,7 @@ def format(content: str, *, max_text_width: int = 120, normalize_case: bool = Tr normalize_case : bool, optional (default: True) If the query identifiers should be normalized to lower case """ + has_eol = content.endswith("\n") try: parsed_query = sqlglot.parse(content, dialect=_SQL_DIALECT) except sqlglot.ParseError: @@ -453,7 +454,7 @@ def format(content: str, *, max_text_width: int = 120, normalize_case: bool = Tr if "$" in content: # replace ${x} with $x, because we use it in UCX view definitions for now formatted_query = re.sub(r"\${(\w+)}", r"$\1", formatted_query) - return formatted_query + return formatted_query + ("\n" if has_eol else "") def _get_abstract_syntax_tree(self) -> sqlglot.Expression | None: try: diff --git a/tests/unit/test_dashboards.py b/tests/unit/test_dashboards.py index 205729f8..1b825fa4 100644 --- a/tests/unit/test_dashboards.py +++ b/tests/unit/test_dashboards.py @@ -829,7 +829,12 @@ def test_query_tile_keeps_original_query(tmp_path): ], ) def test_query_formats(query, query_formatted): - assert QueryTile.format(query) == query_formatted + assert QueryTile.format(query).strip() == query_formatted.strip() + + +def test_query_format_preserves_eol(): + assert not QueryTile.format("SELECT x, y FROM a, b").endswith("\n") + assert QueryTile.format("SELECT x, y FROM a, b\n").endswith("\n") def test_query_formats_no_normalize():