forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Jinja): Extra cache keys for calculated columns and metrics using…
… Jinja (apache#30735)
- Loading branch information
1 parent
c03bf80
commit 09d3f60
Showing
2 changed files
with
98 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,13 @@ | |
# specific language governing permissions and limitations | ||
# under the License. | ||
# isort:skip_file | ||
from __future__ import annotations | ||
|
||
import re | ||
from datetime import datetime | ||
from typing import Any, NamedTuple, Optional, Union | ||
from typing import Any, Literal, NamedTuple, Optional, Union | ||
from re import Pattern | ||
from unittest.mock import patch | ||
from unittest.mock import Mock, patch | ||
import pytest | ||
|
||
import numpy as np | ||
|
@@ -913,54 +915,99 @@ def test_extra_cache_keys_in_sql_expression( | |
|
||
@pytest.mark.usefixtures("app_context") | ||
@pytest.mark.parametrize( | ||
"sql_expression,expected_cache_keys,has_extra_cache_keys", | ||
"sql_expression,expected_cache_keys,has_extra_cache_keys,item_type", | ||
[ | ||
("'{{ current_username() }}'", ["abc"], True), | ||
("(user != 'abc')", [], False), | ||
("'{{ current_username() }}'", ["abc"], True, "columns"), | ||
("(user != 'abc')", [], False, "columns"), | ||
("{{ current_user_id() }}", [1], True, "metrics"), | ||
("COUNT(*)", [], False, "metrics"), | ||
], | ||
) | ||
@patch("superset.jinja_context.get_user_id", return_value=1) | ||
@patch("superset.jinja_context.get_username", return_value="abc") | ||
@patch("superset.jinja_context.get_user_email", return_value="[email protected]") | ||
def test_extra_cache_keys_in_columns( | ||
mock_user_email, | ||
mock_username, | ||
mock_user_id, | ||
sql_expression, | ||
expected_cache_keys, | ||
has_extra_cache_keys, | ||
def test_extra_cache_keys_in_adhoc_metrics_and_columns( | ||
mock_username: Mock, | ||
mock_user_id: Mock, | ||
sql_expression: str, | ||
expected_cache_keys: list[str | None], | ||
has_extra_cache_keys: bool, | ||
item_type: Literal["columns", "metrics"], | ||
): | ||
table = SqlaTable( | ||
table_name="test_has_no_extra_cache_keys_table", | ||
sql="SELECT 'abc' as user", | ||
database=get_example_database(), | ||
) | ||
base_query_obj = { | ||
base_query_obj: dict[str, Any] = { | ||
"granularity": None, | ||
"from_dttm": None, | ||
"to_dttm": None, | ||
"groupby": [], | ||
"metrics": [], | ||
"columns": [], | ||
"is_timeseries": False, | ||
"filter": [], | ||
} | ||
|
||
query_obj = dict( | ||
**base_query_obj, | ||
columns=[ | ||
items: dict[str, Any] = { | ||
item_type: [ | ||
{ | ||
"label": None, | ||
"expressionType": "SQL", | ||
"sqlExpression": sql_expression, | ||
} | ||
], | ||
) | ||
} | ||
|
||
query_obj = {**base_query_obj, **items} | ||
|
||
extra_cache_keys = table.get_extra_cache_keys(query_obj) | ||
assert table.has_extra_cache_key_calls(query_obj) == has_extra_cache_keys | ||
assert extra_cache_keys == expected_cache_keys | ||
|
||
|
||
@pytest.mark.usefixtures("app_context") | ||
@patch("superset.jinja_context.get_user_id", return_value=1) | ||
@patch("superset.jinja_context.get_username", return_value="abc") | ||
def test_extra_cache_keys_in_dataset_metrics_and_columns( | ||
mock_username: Mock, | ||
mock_user_id: Mock, | ||
): | ||
table = SqlaTable( | ||
table_name="test_has_no_extra_cache_keys_table", | ||
sql="SELECT 'abc' as user", | ||
database=get_example_database(), | ||
columns=[ | ||
TableColumn(column_name="user", type="VARCHAR(255)"), | ||
TableColumn( | ||
column_name="username", | ||
type="VARCHAR(255)", | ||
expression="{{ current_username() }}", | ||
), | ||
], | ||
metrics=[ | ||
SqlMetric( | ||
metric_name="variable_profit", | ||
expression="SUM(price) * {{ url_param('multiplier') }}", | ||
), | ||
], | ||
) | ||
query_obj: dict[str, Any] = { | ||
"granularity": None, | ||
"from_dttm": None, | ||
"to_dttm": None, | ||
"groupby": [], | ||
"columns": ["username"], | ||
"metrics": ["variable_profit"], | ||
"is_timeseries": False, | ||
"filter": [], | ||
} | ||
|
||
extra_cache_keys = table.get_extra_cache_keys(query_obj) | ||
assert table.has_extra_cache_key_calls(query_obj) is True | ||
assert set(extra_cache_keys) == {"abc", None} | ||
|
||
|
||
@pytest.mark.usefixtures("app_context") | ||
@pytest.mark.parametrize( | ||
"row,dimension,result", | ||
|