Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
"databricks-labs-blueprint[yaml]~=0.12.0",
"databricks-sdk>=0.38.0,<1.0.0",
"pip~=26.0", # Temporary, blueprint doesn't declare this but it's needed.
"sqlglot>=22.3.1,<26.30.0"
"sqlglot>=28.0.0,<30.12.0",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion src/databricks/labs/lsql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class StatementExecutionExt:
megabytes or gigabytes of data serialized in Apache Arrow format, and low result fetching latency, should use
the stateful Databricks SQL Connector for Python."""

def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
def __init__( # pylint: disable=too-many-positional-arguments
self,
ws: WorkspaceClient,
disposition: Disposition | None = None,
Expand Down
17 changes: 10 additions & 7 deletions src/databricks/labs/lsql/dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ class TileMetadata:
"""The height of the tile."""

id: str = ""
"""The unique id for the tile.
"""The unique id for the tile.

If not given, the stem of the path is used. Needs to adhere to :func:_is_valid_resource_name.
"""

Expand Down Expand Up @@ -551,9 +551,9 @@ def validate(self) -> None:
class QueryTile(Tile):
"""A tile based on a sql query."""

query_transformer: Callable[[sqlglot.Expression], sqlglot.Expression] | None = None
query_transformer: Callable[[sqlglot.exp.Expression], sqlglot.exp.Expression] | None = None
"""A sqlglot transformer to apply to the query before rendering the tile.

Useful for templating SQL queries, like a dynamic catalor or database name.
"""

Expand Down Expand Up @@ -613,13 +613,16 @@ def format(content: str, *, max_text_width: int = 120, normalize_case: bool = Tr
formatted_query = re.sub(r"\${(\w+)}", r"$\1", formatted_query)
return formatted_query + ("\n" if has_eol else "")

def _get_abstract_syntax_tree(self) -> sqlglot.Expression | None:
def _get_abstract_syntax_tree(self) -> sqlglot.exp.Expression | None:
"""Convert the contents to an abstract syntax tree."""
try:
return sqlglot.parse_one(self.content, dialect=_SQL_DIALECT)
parsed_query = sqlglot.parse_one(self.content, dialect=_SQL_DIALECT)
except sqlglot.ParseError as e:
logger.warning(f"Parsing {self.content}: {e}")
return None
if isinstance(parsed_query, sqlglot.exp.Expression):
return parsed_query
return None

def _find_fields(self) -> list[Field]:
"""Find the fields in a query.
Expand Down Expand Up @@ -665,7 +668,7 @@ def replace_database(
https://sqlglot.com/sqlglot/transforms.html
"""

def replace_catalog_and_database_in_query(node: sqlglot.Expression) -> sqlglot.Expression:
def replace_catalog_and_database_in_query(node: sqlglot.exp.Expression) -> sqlglot.exp.Expression:
if isinstance(node, sqlglot.exp.Table):
if (
node.args.get("catalog") is not None
Expand Down
17 changes: 13 additions & 4 deletions tests/unit/test_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from databricks.labs.lsql.backends import MockBackend
from databricks.labs.lsql.core import Row
from databricks.labs.lsql.dashboards import (
_SQL_DIALECT,
BaseHandler,
DashboardMetadata,
Dashboards,
Expand Down Expand Up @@ -831,7 +832,9 @@ def test_query_tile_keeps_original_query(tmp_path):
"""
SELECT
a
FROM server.database.table, remote_server.other_database.table
FROM server.database.table
JOIN remote_server.other_database.table
ON TRUE
""".strip(),
),
(
Expand Down Expand Up @@ -958,7 +961,9 @@ def test_query_tile_creates_database_with_database_overwrite(

datasets = dashboard.datasets
assert len(datasets) == 1
assert datasets[0].query == sqlglot.parse_one(query_transformed).sql(pretty=True)
assert datasets[0].query == sqlglot.parse_one(query_transformed, dialect=_SQL_DIALECT).sql(
dialect=_SQL_DIALECT, pretty=True
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1006,7 +1011,9 @@ def test_query_tile_creates_database_with_catalog_overwrite(

datasets = dashboard.datasets
assert len(datasets) == 1
assert datasets[0].query == sqlglot.parse_one(query_transformed).sql(pretty=True)
assert datasets[0].query == sqlglot.parse_one(query_transformed, dialect=_SQL_DIALECT).sql(
dialect=_SQL_DIALECT, pretty=True
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1055,7 +1062,9 @@ def test_query_tile_creates_database_with_database_and_catalog_overwrite(

datasets = dashboard.datasets
assert len(datasets) == 1
assert datasets[0].query == sqlglot.parse_one(query_transformed).sql(pretty=True)
assert datasets[0].query == sqlglot.parse_one(query_transformed, dialect=_SQL_DIALECT).sql(
dialect=_SQL_DIALECT, pretty=True
)


@pytest.mark.parametrize("width", [5, 8, 13])
Expand Down
Loading
Loading