Skip to content

Commit

Permalink
fix: don't reformat generated queries
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Sep 20, 2024
1 parent 038ef32 commit bdb60ad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 32 deletions.
7 changes: 1 addition & 6 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@
ColumnNotFoundException,
QueryClauseValidationException,
QueryObjectValidationError,
SupersetParseError,
SupersetSecurityException,
)
from superset.extensions import feature_flag_manager
from superset.jinja_context import BaseTemplateProcessor
from superset.sql.parse import SQLScript, SQLStatement
from superset.sql.parse import SQLScript
from superset.sql_parse import (
has_table_query,
insert_rls_in_predicate,
Expand Down Expand Up @@ -870,10 +869,6 @@ def get_query_str_extended(
sqlaq = self.get_sqla_query(**query_obj)
sql = self.database.compile_sqla_query(sqlaq.sqla_query)
sql = self._apply_cte(sql, sqlaq.cte)
try:
sql = SQLStatement(sql, engine=self.db_engine_spec.engine).format()
except SupersetParseError:
logger.warning("Unable to parse SQL to format it, passing it as-is")

if mutate:
sql = self.database.mutate_sql_based_on_config(sql)
Expand Down
31 changes: 5 additions & 26 deletions tests/unit_tests/jinja_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,45 +473,24 @@ def test_dataset_macro(mocker: MockerFixture) -> None:
assert (
dataset_macro(1)
== """(
SELECT
ds AS ds,
num_boys AS num_boys,
revenue AS revenue,
expenses AS expenses,
revenue - expenses AS profit
SELECT ds AS ds, num_boys AS num_boys, revenue AS revenue, expenses AS expenses, revenue-expenses AS profit
FROM my_schema.old_dataset
) AS dataset_1"""
)

assert (
dataset_macro(1, include_metrics=True)
== """(
SELECT
ds AS ds,
num_boys AS num_boys,
revenue AS revenue,
expenses AS expenses,
revenue - expenses AS profit,
COUNT(*) AS cnt
FROM my_schema.old_dataset
GROUP BY
ds,
num_boys,
revenue,
expenses,
revenue - expenses
SELECT ds AS ds, num_boys AS num_boys, revenue AS revenue, expenses AS expenses, revenue-expenses AS profit, COUNT(*) AS cnt
FROM my_schema.old_dataset GROUP BY ds, num_boys, revenue, expenses, revenue-expenses
) AS dataset_1"""
)

assert (
dataset_macro(1, include_metrics=True, columns=["ds"])
== """(
SELECT
ds AS ds,
COUNT(*) AS cnt
FROM my_schema.old_dataset
GROUP BY
ds
SELECT ds AS ds, COUNT(*) AS cnt
FROM my_schema.old_dataset GROUP BY ds
) AS dataset_1"""
)

Expand Down

0 comments on commit bdb60ad

Please sign in to comment.