zhaoyongjie commented on a change in pull request #17287:
URL: https://github.com/apache/superset/pull/17287#discussion_r743362503
##########
File path: superset/connectors/base/models.py
##########
@@ -317,11 +317,23 @@ def data_for_slices(self, slices: List[Slice]) ->
Dict[str, Any]:
if "column" in filter_config
)
- column_names.update(
- column
- for column_param in COLUMN_FORM_DATA_PARAMS
- for column in utils.get_iterable(form_data.get(column_param)
or [])
- )
+ # legacy charts don't have query_context charts
+ if slc.query_context:
+ query_context = slc.get_query_context()
+ column_names.update(
+ [
+ column
+ for query in query_context.get("queries", [])
+ for column in query.get("columns", [])
+ ]
+ or []
+ )
+ else:
+ column_names.update(
+ column
+ for column_param in COLUMN_FORM_DATA_PARAMS
+ for column in
utils.get_iterable(form_data.get(column_param) or [])
+ )
Review comment:
Thanks for the explanation. I understood.
##########
File path: superset/models/slice.py
##########
@@ -247,6 +247,17 @@ def form_data(self) -> Dict[str, Any]:
update_time_range(form_data)
return form_data
+ def get_query_context(self) -> Dict[str, Any]:
+ query_context: Dict[str, Any] = {}
+ if not self.query_context:
+ return query_context
+ try:
+ query_context = json.loads(self.query_context)
+ except json.decoder.JSONDecodeError as ex:
+ logger.error("Malformed json in slice's query context",
exc_info=True)
+ logger.exception(ex)
+ return query_context
+
Review comment:
We can make this function more generic. like this:
```Python
def get_query_context(self) -> QueryContext:
if self.query_context:
try:
return self.query_context(**json.loads(self.query_context))
except json.decoder.JSONDecodeError as ex:
logger.error("Malformed json in slice's query context",
exc_info=True)
logger.exception(ex)
return QueryContext(
datasource={"type": self.datasource_type, "id":
self.datasource_id},
queries=[self.viz.query_obj()],)
```
##########
File path: superset/models/slice.py
##########
@@ -247,6 +247,17 @@ def form_data(self) -> Dict[str, Any]:
update_time_range(form_data)
return form_data
+ def get_query_context(self) -> Dict[str, Any]:
+ query_context: Dict[str, Any] = {}
+ if not self.query_context:
+ return query_context
+ try:
+ query_context = json.loads(self.query_context)
+ except json.decoder.JSONDecodeError as ex:
+ logger.error("Malformed json in slice's query context",
exc_info=True)
+ logger.exception(ex)
+ return query_context
+
Review comment:
Nice! It makes sense return `None` in legacy chart.
##########
File path: superset/connectors/base/models.py
##########
@@ -317,11 +317,23 @@ def data_for_slices(self, slices: List[Slice]) ->
Dict[str, Any]:
if "column" in filter_config
)
- column_names.update(
- column
- for column_param in COLUMN_FORM_DATA_PARAMS
- for column in utils.get_iterable(form_data.get(column_param)
or [])
- )
+ # legacy charts don't have query_context charts
+ if slc.query_context:
+ query_context = slc.get_query_context()
+ column_names.update(
+ [
+ column
+ for query in query_context.get("queries", [])
+ for column in query.get("columns", [])
+ ]
+ or []
+ )
+ else:
+ column_names.update(
+ column
+ for column_param in COLUMN_FORM_DATA_PARAMS
+ for column in
utils.get_iterable(form_data.get(column_param) or [])
+ )
Review comment:
Thanks for the explanation. I understood.
##########
File path: superset/models/slice.py
##########
@@ -247,6 +247,17 @@ def form_data(self) -> Dict[str, Any]:
update_time_range(form_data)
return form_data
+ def get_query_context(self) -> Dict[str, Any]:
+ query_context: Dict[str, Any] = {}
+ if not self.query_context:
+ return query_context
+ try:
+ query_context = json.loads(self.query_context)
+ except json.decoder.JSONDecodeError as ex:
+ logger.error("Malformed json in slice's query context",
exc_info=True)
+ logger.exception(ex)
+ return query_context
+
Review comment:
We can make this function more generic. like this:
```Python
def get_query_context(self) -> QueryContext:
if self.query_context:
try:
return self.query_context(**json.loads(self.query_context))
except json.decoder.JSONDecodeError as ex:
logger.error("Malformed json in slice's query context",
exc_info=True)
logger.exception(ex)
return QueryContext(
datasource={"type": self.datasource_type, "id":
self.datasource_id},
queries=[self.viz.query_obj()],)
```
##########
File path: superset/models/slice.py
##########
@@ -247,6 +247,17 @@ def form_data(self) -> Dict[str, Any]:
update_time_range(form_data)
return form_data
+ def get_query_context(self) -> Dict[str, Any]:
+ query_context: Dict[str, Any] = {}
+ if not self.query_context:
+ return query_context
+ try:
+ query_context = json.loads(self.query_context)
+ except json.decoder.JSONDecodeError as ex:
+ logger.error("Malformed json in slice's query context",
exc_info=True)
+ logger.exception(ex)
+ return query_context
+
Review comment:
Nice! It makes sense return `None` in legacy chart.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]