dosubot[bot] commented on issue #29908:
URL: https://github.com/apache/superset/issues/29908#issuecomment-2278687109
<!-- Answer -->
To pass the line `actual_results_backend =
results_backend._get_current_object()` in your Superset setup, you need to
modify the relevant parts of your code where `results_backend` is being used.
Here’s a step-by-step guide:
1. **Define `actual_results_backend` in your configuration**: Ensure that
`actual_results_backend` is defined in your Superset configuration file (e.g.,
`superset_config.py`).
2. **Modify the `ResultsBackendManager` class**: Update the
`ResultsBackendManager` class to use `actual_results_backend`.
Here is the modified code:
```python
class ResultsBackendManager:
def __init__(self) -> None:
self._results_backend = None
self._use_msgpack = False
self._actual_results_backend = None
def init_app(self, app: Flask) -> None:
self._results_backend = app.config["RESULTS_BACKEND"]
self._use_msgpack = app.config["RESULTS_BACKEND_USE_MSGPACK"]
self._actual_results_backend =
app.config.get("ACTUAL_RESULTS_BACKEND")
@property
def results_backend(self) -> Optional[BaseCache]:
return self._results_backend
@property
def should_use_msgpack(self) -> bool:
return self._use_msgpack
@property
def actual_results_backend(self) -> Optional[BaseCache]:
return self._actual_results_backend
```
3. **Use `actual_results_backend` in ORM operations**: Wherever you need to
use `actual_results_backend` in your ORM operations, you can access it via the
`results_backend_manager` instance.
Example usage in your ORM operations:
```python
from superset.extensions import results_backend_manager
def some_orm_operation():
backend = results_backend_manager.actual_results_backend
if backend:
# Perform operations using actual_results_backend
pass
```
In the context of `superset/commands/sql_lab/results.py`, you can integrate
`actual_results_backend` in the `SqlExecutionResultsCommand` class to store
results using the actual results backend:
```python
class SqlExecutionResultsCommand(BaseCommand):
_key: str
_rows: int | None
_blob: Any
_query: Query
def __init__(
self,
key: str,
rows: int | None = None,
) -> None:
self._key = key
self._rows = rows
def validate(self) -> None:
if not results_backend_manager.actual_results_backend:
raise SupersetErrorException(
SupersetError(
message=__("Actual results backend is not configured."),
error_type=SupersetErrorType.RESULTS_BACKEND_NOT_CONFIGURED_ERROR,
level=ErrorLevel.ERROR,
)
)
read_from_results_backend_start = now_as_float()
self._blob =
results_backend_manager.actual_results_backend.get(self._key)
stats_logger.timing(
"sqllab.query.results_backend_read",
now_as_float() - read_from_results_backend_start,
)
if not self._blob:
raise SupersetErrorException(
SupersetError(
message=__(
"Data could not be retrieved from the actual results
backend. You "
"need to re-run the original query."
),
error_type=SupersetErrorType.RESULTS_BACKEND_ERROR,
level=ErrorLevel.ERROR,
),
status=410,
)
self._query = (
db.session.query(Query).filter_by(results_key=self._key).one_or_none()
)
if self._query is None:
raise SupersetErrorException(
SupersetError(
message=__(
"The query associated with these results could not
be found. "
"You need to re-run the original query."
),
error_type=SupersetErrorType.RESULTS_BACKEND_ERROR,
level=ErrorLevel.ERROR,
),
status=404,
)
def run(self) -> dict[str, Any]:
"""Runs arbitrary sql and returns data as json"""
self.validate()
payload = utils.zlib_decompress(
self._blob, decode=not results_backend_use_msgpack
)
try:
obj = _deserialize_results_payload(
payload, self._query, cast(bool, results_backend_use_msgpack)
)
except SerializationError as ex:
raise SupersetErrorException(
SupersetError(
message=__(
"Data could not be deserialized from the actual
results backend. The "
"storage format might have changed, rendering the
old data "
"stake. You need to re-run the original query."
),
error_type=SupersetErrorType.RESULTS_BACKEND_ERROR,
level=ErrorLevel.ERROR,
),
status=404,
) from ex
if self._rows:
obj = apply_display_max_row_configuration_if_require(obj,
self._rows)
return obj
```
By following these steps, you can integrate `actual_results_backend` into
your Superset setup and use it in your ORM operations
<sup>[[1]](https://github.com/apache/superset/blob/master/superset/extensions/__init__.py)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset/sql_lab.py)</sup><sup>[[3]](https://github.com/apache/superset/blob/master/superset/commands/sql_lab/results.py)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
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]