Skip to content

Commit

Permalink
refactor sql_json view endpoint: separate a response creation (apache…
Browse files Browse the repository at this point in the history
  • Loading branch information
ofekisr authored Sep 12, 2021
1 parent 1429d9d commit 51acada
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import re
from contextlib import closing
from datetime import datetime, timedelta
from typing import Any, Callable, cast, Dict, List, Optional, Union
from typing import Any, Callable, cast, Dict, List, Optional, Tuple, Union
from urllib import parse

import backoff
Expand Down Expand Up @@ -2423,22 +2423,21 @@ def sql_json(self) -> FlaskResponse:
"user_agent": cast(Optional[str], request.headers.get("USER_AGENT"))
}
execution_context = SqlJsonExecutionContext(request.json)
return self.sql_json_exec(execution_context, log_params)
return json_success(*self.sql_json_exec(execution_context, log_params))

def sql_json_exec( # pylint: disable=too-many-statements,useless-suppression
self,
execution_context: SqlJsonExecutionContext,
log_params: Optional[Dict[str, Any]] = None,
) -> FlaskResponse:
) -> Tuple[str, int]:
"""Runs arbitrary sql and returns data as json"""

session = db.session()

query = self._get_existing_query(execution_context, session)

if self.is_query_handled(query):
payload = self._convert_query_to_payload(cast(Query, query))
return json_success(payload)
return self._convert_query_to_payload(cast(Query, query)), 200

return self._run_sql_json_exec_from_scratch(
execution_context, session, log_params
Expand Down Expand Up @@ -2480,7 +2479,7 @@ def _run_sql_json_exec_from_scratch(
execution_context: SqlJsonExecutionContext,
session: Session,
log_params: Optional[Dict[str, Any]] = None,
) -> FlaskResponse:
) -> Tuple[str, int]:
execution_context.set_database(
self._get_the_query_db(execution_context, session)
)
Expand Down Expand Up @@ -2635,18 +2634,24 @@ def _execute_query( # pylint: disable=too-many-arguments
rendered_query: str,
session: Session,
log_params: Optional[Dict[str, Any]],
) -> FlaskResponse:
) -> Tuple[str, int]:
# Flag for whether or not to expand data
# (feature that will expand Presto row objects and arrays)
expand_data: bool = execution_context.expand_data
# Async request.
if execution_context.is_run_asynchronous():
return self._sql_json_async(
query, rendered_query, expand_data, session, log_params
return (
self._sql_json_async(
query, rendered_query, expand_data, session, log_params
),
202,
)
# Sync request.
return self._sql_json_sync(
query, rendered_query, expand_data, session, log_params
return (
self._sql_json_sync(
query, rendered_query, expand_data, session, log_params
),
200,
)

@classmethod
Expand All @@ -2657,7 +2662,7 @@ def _sql_json_async( # pylint: disable=too-many-arguments
expand_data: bool,
session: Session,
log_params: Optional[Dict[str, Any]],
) -> FlaskResponse:
) -> str:
"""
Send SQL JSON query to celery workers.
Expand Down Expand Up @@ -2713,9 +2718,8 @@ def _sql_json_async( # pylint: disable=too-many-arguments
# Update saved query with execution info from the query execution
QueryDAO.update_saved_query_exec_info(query_id)

resp = json_success(cls._convert_query_to_payload(query), status=202,)
session.commit()
return resp
return cls._convert_query_to_payload(query)

@classmethod
def _sql_json_sync(
Expand All @@ -2725,7 +2729,7 @@ def _sql_json_sync(
expand_data: bool,
_session: Session,
log_params: Optional[Dict[str, Any]],
) -> FlaskResponse:
) -> str:
"""
Execute SQL query (sql json).
Expand Down Expand Up @@ -2764,7 +2768,7 @@ def _sql_json_sync(
# old string-only error message
raise SupersetGenericDBErrorException(data["error"])

return json_success(payload)
return payload

@classmethod
def _get_sql_results_with_timeout( # pylint: disable=too-many-arguments
Expand Down

0 comments on commit 51acada

Please sign in to comment.