Skip to content

Commit

Permalink
fix: add back custom sql filtering with Query as source (apache#21190)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughhhh authored Aug 27, 2022
1 parent e2ab966 commit c61a507
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isAdhocMetricSimple,
isSavedMetric,
isSimpleAdhocFilter,
isFreeFormAdhocFilter,
JsonValue,
SimpleAdhocFilter,
} from '@superset-ui/core';
Expand Down Expand Up @@ -70,6 +71,7 @@ const isControlValueCompatibleWithDatasource = (
column.column_name === (value as SimpleAdhocFilter).subject,
);
}
if (isFreeFormAdhocFilter(value)) return true;
return false;
};

Expand Down
2 changes: 1 addition & 1 deletion superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]:
#
# e.g.:
#
# class AesGcmEncryptedAdapter( # pylint: disable=too-few-public-methods
# class AesGcmEncryptedAdapter(
# AbstractEncryptedFieldAdapter
# ):
# def create(
Expand Down
2 changes: 1 addition & 1 deletion superset/db_engine_specs/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from superset.models.core import Database

try:
from trino.dbapi import Cursor # pylint: disable=unused-import
from trino.dbapi import Cursor
except ImportError:
pass

Expand Down
31 changes: 18 additions & 13 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ def get_fetch_values_predicate(self) -> List[Any]:
def get_extra_cache_keys(query_obj: Dict[str, Any]) -> List[str]:
raise NotImplementedError()

def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:
raise NotImplementedError()

def _process_sql_expression( # pylint: disable=no-self-use
self,
expression: Optional[str],
Expand Down Expand Up @@ -1291,9 +1294,7 @@ def get_timestamp_expression(
column: Dict[str, Any],
time_grain: Optional[str],
label: Optional[str] = None,
template_processor: Optional[ # pylint: disable=unused-argument
BaseTemplateProcessor
] = None,
template_processor: Optional[BaseTemplateProcessor] = None,
) -> Union[TimestampExpression, Label]:
"""
Return a SQLAlchemy Core element representation of self to be used in a query.
Expand All @@ -1307,6 +1308,11 @@ def get_timestamp_expression(
column_spec = self.db_engine_spec.get_column_spec(column.get("type"))
type_ = column_spec.sqla_type if column_spec else sa.DateTime
col = sa.column(column.get("column_name"), type_=type_)

if template_processor:
expression = template_processor.process_template(column["column_name"])
col = sa.literal_column(expression, type_=type_)

time_expr = self.db_engine_spec.get_timestamp_expr(col, None, time_grain)
return self.make_sqla_column_compatible(time_expr, label)

Expand Down Expand Up @@ -1377,7 +1383,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
applied_template_filters: List[str] = []
template_kwargs["removed_filters"] = removed_filters
template_kwargs["applied_filters"] = applied_template_filters
template_processor = None # self.get_template_processor(**template_kwargs)
template_processor = self.get_template_processor(**template_kwargs)
db_engine_spec = self.db_engine_spec
prequeries: List[str] = []
orderby = orderby or []
Expand Down Expand Up @@ -1487,7 +1493,10 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
table_col = columns_by_name[selected]
if isinstance(table_col, dict):
outer = self.get_timestamp_expression(
table_col, time_grain, selected, template_processor
column=table_col,
time_grain=time_grain,
label=selected,
template_processor=template_processor,
)
else:
outer = table_col.get_timestamp_expression(
Expand Down Expand Up @@ -1550,7 +1559,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
if is_timeseries:
if isinstance(dttm_col, dict):
timestamp = self.get_timestamp_expression(
dttm_col, time_grain, template_processor
dttm_col, time_grain, template_processor=template_processor
)
else:
timestamp = dttm_col.get_timestamp_expression(
Expand Down Expand Up @@ -1639,7 +1648,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
elif col_obj and filter_grain:
if isinstance(col_obj, dict):
sqla_col = self.get_timestamp_expression(
col_obj, time_grain, template_processor
col_obj, time_grain, template_processor=template_processor
)
else:
sqla_col = col_obj.get_timestamp_expression(
Expand Down Expand Up @@ -1770,9 +1779,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
where = extras.get("where")
if where:
try:
where = template_processor.process_template( # type: ignore
f"({where})"
)
where = template_processor.process_template(f"{where}")
except TemplateError as ex:
raise QueryObjectValidationError(
_(
Expand All @@ -1784,9 +1791,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma
having = extras.get("having")
if having:
try:
having = template_processor.process_template( # type: ignore
f"({having})"
)
having = template_processor.process_template(f"{having}")
except TemplateError as ex:
raise QueryObjectValidationError(
_(
Expand Down
4 changes: 4 additions & 0 deletions superset/models/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from sqlalchemy.orm import backref, relationship

from superset import security_manager
from superset.jinja_context import BaseTemplateProcessor, get_template_processor
from superset.models.helpers import (
AuditMixinNullable,
ExploreMixin,
Expand Down Expand Up @@ -126,6 +127,9 @@ class Query(

__table_args__ = (sqla.Index("ti_user_id_changed_on", user_id, changed_on),)

def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:
return get_template_processor(query=self, database=self.database, **kwargs)

def to_dict(self) -> Dict[str, Any]:
return {
"changedOn": self.changed_on,
Expand Down

0 comments on commit c61a507

Please sign in to comment.