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 119817f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 40 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
2 changes: 1 addition & 1 deletion tests/integration_tests/datasource_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def test_get_samples_with_multiple_filters(
assert "2000-01-02" in rv.json["result"]["query"]
assert "2000-01-04" in rv.json["result"]["query"]
assert "col3 = 1.2" in rv.json["result"]["query"]
assert "col4 IS NULL" in rv.json["result"]["query"]
assert "col4 is null" in rv.json["result"]["query"]
assert "col2 = 'c'" in rv.json["result"]["query"]


Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/query_context_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_query_response_type(self):
sql_text = get_sql_text(payload)

assert "SELECT" in sql_text
assert re.search(r'NOT [`"\[]?num[`"\]]? IS NULL', sql_text)
assert re.search(r'[`"\[]?num[`"\]]? IS NOT NULL', sql_text)
assert re.search(
r"""NOT \([\s\n]*[`"\[]?name[`"\]]? IS NULL[\s\n]* """
r"""OR [`"\[]?name[`"\]]? IN \('"abc"'\)[\s\n]*\)""",
Expand Down Expand Up @@ -1161,11 +1161,11 @@ def test_time_offset_with_temporal_range_filter(app_context, physical_dataset):
OFFSET 0
"""
assert (
re.search(r"WHERE\n col6 >= .*2002-01-01", sqls[0])
re.search(r"WHERE col6 >= .*2002-01-01", sqls[0])
and re.search(r"AND col6 < .*2003-01-01", sqls[0])
) is not None
assert (
re.search(r"WHERE\n col6 >= .*2001-10-01", sqls[1])
re.search(r"WHERE col6 >= .*2001-10-01", sqls[1])
and re.search(r"AND col6 < .*2002-10-01", sqls[1])
) is not None

Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/sqla_models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def test_none_operand_in_filter(login_as_admin, physical_dataset):
{
"operator": FilterOperator.NOT_EQUALS.value,
"count": 0,
"sql_should_contain": "NOT COL4 IS NULL",
"sql_should_contain": "COL4 IS NOT NULL",
},
]
for expected in expected_results:
Expand Down
39 changes: 10 additions & 29 deletions tests/unit_tests/jinja_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,48 +470,29 @@ def test_dataset_macro(mocker: MockerFixture) -> None:
return_value=[],
)

space = " "

assert (
dataset_macro(1)
== """(
SELECT
ds AS ds,
num_boys AS num_boys,
revenue AS revenue,
expenses AS expenses,
revenue - expenses AS profit
== f"""(
SELECT ds AS ds, num_boys AS num_boys, revenue AS revenue, expenses AS expenses, revenue-expenses AS profit{space}
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
== f"""(
SELECT ds AS ds, num_boys AS num_boys, revenue AS revenue, expenses AS expenses, revenue-expenses AS profit, COUNT(*) AS cnt{space}
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
== f"""(
SELECT ds AS ds, COUNT(*) AS cnt{space}
FROM my_schema.old_dataset GROUP BY ds
) AS dataset_1"""
)

Expand Down

0 comments on commit 119817f

Please sign in to comment.