From 65e468232ebbda25dbeab7d1d460be9483430679 Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Tue, 27 Aug 2024 15:09:09 +0200 Subject: [PATCH 1/2] Fix normalize case input in cli The Databricks cli currently only supports string input --- src/databricks/labs/lsql/cli.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/databricks/labs/lsql/cli.py b/src/databricks/labs/lsql/cli.py index 29b020bb..592c18d5 100644 --- a/src/databricks/labs/lsql/cli.py +++ b/src/databricks/labs/lsql/cli.py @@ -43,13 +43,14 @@ def create_dashboard( @lsql.command(is_unauthenticated=True) -def fmt(folder: Path = Path.cwd(), normalize_case: bool = True): +def fmt(folder: Path = Path.cwd(), *, normalize_case: str = "true"): """Format SQL files in a folder""" logger.debug("Formatting SQL files ...") folder = Path(folder) + should_normalize_case = normalize_case in STRING_AFFIRMATIVES for sql_file in folder.glob("**/*.sql"): sql = sql_file.read_text() - formatted_sql = QueryTile.format(sql, normalize_case) + formatted_sql = QueryTile.format(sql, should_normalize_case) sql_file.write_text(formatted_sql) logger.debug(f"Formatted {sql_file}") From 4b08983205bf6ecc359a6461009c16987c4cab16 Mon Sep 17 00:00:00 2001 From: Cor Zuurmond Date: Tue, 27 Aug 2024 15:56:25 +0200 Subject: [PATCH 2/2] Make normalize case a key word argument --- src/databricks/labs/lsql/cli.py | 2 +- src/databricks/labs/lsql/dashboards.py | 8 ++++---- tests/unit/test_dashboards.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/databricks/labs/lsql/cli.py b/src/databricks/labs/lsql/cli.py index 592c18d5..0ac30416 100644 --- a/src/databricks/labs/lsql/cli.py +++ b/src/databricks/labs/lsql/cli.py @@ -50,7 +50,7 @@ def fmt(folder: Path = Path.cwd(), *, normalize_case: str = "true"): should_normalize_case = normalize_case in STRING_AFFIRMATIVES for sql_file in folder.glob("**/*.sql"): sql = sql_file.read_text() - formatted_sql = QueryTile.format(sql, should_normalize_case) + formatted_sql = QueryTile.format(sql, normalize_case=should_normalize_case) sql_file.write_text(formatted_sql) logger.debug(f"Formatted {sql_file}") diff --git a/src/databricks/labs/lsql/dashboards.py b/src/databricks/labs/lsql/dashboards.py index 3631af48..fb9ce2c1 100644 --- a/src/databricks/labs/lsql/dashboards.py +++ b/src/databricks/labs/lsql/dashboards.py @@ -418,16 +418,16 @@ def validate(self) -> None: raise ValueError(f"Invalid query content: {self.content}") from e @staticmethod - def format(content: str, normalize_case: bool = True, *, max_text_width: int = 120) -> str: + def format(content: str, *, max_text_width: int = 120, normalize_case: bool = True) -> str: """Format the content Args: content : str The content to format - max_text_width : int + max_text_width : int, optional (default: 120) The maximum text width to wrap at - normalize_case : bool - If the query should be normalized to lower case + normalize_case : bool, optional (default: True) + If the query identifiers should be normalized to lower case """ try: parsed_query = sqlglot.parse(content, dialect=_SQL_DIALECT) diff --git a/tests/unit/test_dashboards.py b/tests/unit/test_dashboards.py index 47994fb1..205729f8 100644 --- a/tests/unit/test_dashboards.py +++ b/tests/unit/test_dashboards.py @@ -843,7 +843,7 @@ def test_query_formats_no_normalize(): FROM system.access.audit AS a LEFT OUTER JOIN inventory.clusters AS c ON a.request_params.clusterId = c.cluster_id AND a.action_name = 'runCommand'""" - assert QueryTile.format(query, False) == query_formatted + assert QueryTile.format(query, normalize_case=False) == query_formatted @pytest.mark.parametrize(