fix: commit semantic repair changes
This commit is contained in:
@@ -309,7 +309,7 @@ class APIClient:
|
||||
except (requests.exceptions.RequestException, KeyError) as e:
|
||||
SupersetAuthCache.invalidate(self._auth_cache_key)
|
||||
raise NetworkError(f"Network or parsing error during authentication: {e}") from e
|
||||
# [/DEF:authenticate:Function]
|
||||
# [/DEF:APIClient.authenticate:Function]
|
||||
|
||||
@property
|
||||
# [DEF:headers:Function]
|
||||
|
||||
@@ -34,6 +34,8 @@ class PreviewCompilationPayload:
|
||||
preview_fingerprint: str
|
||||
template_params: Dict[str, Any]
|
||||
effective_filters: List[Dict[str, Any]]
|
||||
|
||||
|
||||
# [/DEF:PreviewCompilationPayload:Class]
|
||||
|
||||
|
||||
@@ -47,6 +49,8 @@ class SqlLabLaunchPayload:
|
||||
preview_id: str
|
||||
compiled_sql: str
|
||||
template_params: Dict[str, Any]
|
||||
|
||||
|
||||
# [/DEF:SqlLabLaunchPayload:Class]
|
||||
|
||||
|
||||
@@ -61,11 +65,25 @@ class SupersetCompilationAdapter:
|
||||
# [DEF:SupersetCompilationAdapter.__init__:Function]
|
||||
# @COMPLEXITY: 2
|
||||
# @PURPOSE: Bind adapter to one Superset environment and client instance.
|
||||
def __init__(self, environment: Environment, client: Optional[SupersetClient] = None) -> None:
|
||||
def __init__(
|
||||
self, environment: Environment, client: Optional[SupersetClient] = None
|
||||
) -> None:
|
||||
self.environment = environment
|
||||
self.client = client or SupersetClient(environment)
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter.__init__:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter._supports_client_method:Function]
|
||||
# @COMPLEXITY: 2
|
||||
# @PURPOSE: Detect explicitly implemented client capabilities without treating loose mocks as real methods.
|
||||
def _supports_client_method(self, method_name: str) -> bool:
|
||||
client_dict = getattr(self.client, "__dict__", {})
|
||||
if method_name in client_dict:
|
||||
return callable(client_dict[method_name])
|
||||
return callable(getattr(type(self.client), method_name, None))
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter._supports_client_method:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter.compile_preview:Function]
|
||||
# @COMPLEXITY: 4
|
||||
# @PURPOSE: Request Superset-side compiled SQL preview for the current effective inputs.
|
||||
@@ -79,7 +97,10 @@ class SupersetCompilationAdapter:
|
||||
if payload.dataset_id <= 0:
|
||||
logger.explore(
|
||||
"Preview compilation rejected because dataset identifier is invalid",
|
||||
extra={"dataset_id": payload.dataset_id, "session_id": payload.session_id},
|
||||
extra={
|
||||
"dataset_id": payload.dataset_id,
|
||||
"session_id": payload.session_id,
|
||||
},
|
||||
)
|
||||
raise ValueError("dataset_id must be a positive integer")
|
||||
|
||||
@@ -155,6 +176,7 @@ class SupersetCompilationAdapter:
|
||||
},
|
||||
)
|
||||
return preview
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter.compile_preview:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter.mark_preview_stale:Function]
|
||||
@@ -165,6 +187,7 @@ class SupersetCompilationAdapter:
|
||||
def mark_preview_stale(self, preview: CompiledPreview) -> CompiledPreview:
|
||||
preview.preview_status = PreviewStatus.STALE
|
||||
return preview
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter.mark_preview_stale:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter.create_sql_lab_session:Function]
|
||||
@@ -181,7 +204,10 @@ class SupersetCompilationAdapter:
|
||||
if not compiled_sql:
|
||||
logger.explore(
|
||||
"SQL Lab launch rejected because compiled SQL is empty",
|
||||
extra={"session_id": payload.session_id, "preview_id": payload.preview_id},
|
||||
extra={
|
||||
"session_id": payload.session_id,
|
||||
"preview_id": payload.preview_id,
|
||||
},
|
||||
)
|
||||
raise ValueError("compiled_sql must be non-empty")
|
||||
|
||||
@@ -204,9 +230,14 @@ class SupersetCompilationAdapter:
|
||||
if not sql_lab_session_ref:
|
||||
logger.explore(
|
||||
"Superset SQL Lab launch response did not include a stable session reference",
|
||||
extra={"session_id": payload.session_id, "preview_id": payload.preview_id},
|
||||
extra={
|
||||
"session_id": payload.session_id,
|
||||
"preview_id": payload.preview_id,
|
||||
},
|
||||
)
|
||||
raise RuntimeError(
|
||||
"Superset SQL Lab launch response did not include a session reference"
|
||||
)
|
||||
raise RuntimeError("Superset SQL Lab launch response did not include a session reference")
|
||||
|
||||
logger.reflect(
|
||||
"Canonical SQL Lab session created successfully",
|
||||
@@ -217,6 +248,7 @@ class SupersetCompilationAdapter:
|
||||
},
|
||||
)
|
||||
return sql_lab_session_ref
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter.create_sql_lab_session:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter._request_superset_preview:Function]
|
||||
@@ -227,7 +259,81 @@ class SupersetCompilationAdapter:
|
||||
# @POST: returns one normalized upstream compilation response including the chosen strategy metadata.
|
||||
# @SIDE_EFFECT: issues one or more Superset preview requests through the client fallback chain.
|
||||
# @DATA_CONTRACT: Input[PreviewCompilationPayload] -> Output[Dict[str,Any]]
|
||||
def _request_superset_preview(self, payload: PreviewCompilationPayload) -> Dict[str, Any]:
|
||||
def _request_superset_preview(
|
||||
self, payload: PreviewCompilationPayload
|
||||
) -> Dict[str, Any]:
|
||||
direct_compile_preview = getattr(self.client, "compile_preview", None)
|
||||
if self._supports_client_method("compile_preview") and callable(
|
||||
direct_compile_preview
|
||||
):
|
||||
try:
|
||||
logger.reason(
|
||||
"Attempting preview compilation via direct client capability",
|
||||
extra={
|
||||
"dataset_id": payload.dataset_id,
|
||||
"session_id": payload.session_id,
|
||||
},
|
||||
)
|
||||
response = direct_compile_preview(payload)
|
||||
except TypeError:
|
||||
response = direct_compile_preview(
|
||||
payload.dataset_id,
|
||||
template_params=payload.template_params,
|
||||
effective_filters=payload.effective_filters,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.explore(
|
||||
"Direct client preview capability failed; falling back to dataset preview strategies",
|
||||
extra={
|
||||
"dataset_id": payload.dataset_id,
|
||||
"session_id": payload.session_id,
|
||||
"error": str(exc),
|
||||
},
|
||||
)
|
||||
else:
|
||||
normalized = self._normalize_preview_response(response)
|
||||
if normalized is not None:
|
||||
return normalized
|
||||
|
||||
direct_compile_dataset_preview = getattr(
|
||||
self.client, "compile_dataset_preview", None
|
||||
)
|
||||
if self._supports_client_method("compile_dataset_preview") and callable(
|
||||
direct_compile_dataset_preview
|
||||
):
|
||||
try:
|
||||
logger.reason(
|
||||
"Attempting deterministic Superset preview compilation through supported endpoint strategies",
|
||||
extra={
|
||||
"dataset_id": payload.dataset_id,
|
||||
"session_id": payload.session_id,
|
||||
"filter_count": len(payload.effective_filters),
|
||||
"template_param_count": len(payload.template_params),
|
||||
},
|
||||
)
|
||||
response = direct_compile_dataset_preview(
|
||||
dataset_id=payload.dataset_id,
|
||||
template_params=payload.template_params,
|
||||
effective_filters=payload.effective_filters,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.explore(
|
||||
"Superset preview compilation failed across supported endpoint strategies",
|
||||
extra={
|
||||
"dataset_id": payload.dataset_id,
|
||||
"session_id": payload.session_id,
|
||||
"error": str(exc),
|
||||
},
|
||||
)
|
||||
raise RuntimeError(str(exc)) from exc
|
||||
|
||||
normalized = self._normalize_preview_response(response)
|
||||
if normalized is None:
|
||||
raise RuntimeError(
|
||||
"Superset preview compilation response could not be normalized"
|
||||
)
|
||||
return normalized
|
||||
|
||||
try:
|
||||
logger.reason(
|
||||
"Attempting deterministic Superset preview compilation through supported endpoint strategies",
|
||||
@@ -238,10 +344,45 @@ class SupersetCompilationAdapter:
|
||||
"template_param_count": len(payload.template_params),
|
||||
},
|
||||
)
|
||||
response = self.client.compile_dataset_preview(
|
||||
dataset_id=payload.dataset_id,
|
||||
template_params=payload.template_params,
|
||||
effective_filters=payload.effective_filters,
|
||||
if self._supports_client_method("compile_dataset_preview"):
|
||||
response = self.client.compile_dataset_preview(
|
||||
dataset_id=payload.dataset_id,
|
||||
template_params=payload.template_params,
|
||||
effective_filters=payload.effective_filters,
|
||||
)
|
||||
normalized = self._normalize_preview_response(response)
|
||||
if normalized is None:
|
||||
raise RuntimeError(
|
||||
"Superset preview compilation response could not be normalized"
|
||||
)
|
||||
return normalized
|
||||
|
||||
errors: List[str] = []
|
||||
for endpoint in (
|
||||
f"/dataset/{payload.dataset_id}/preview",
|
||||
f"/dataset/{payload.dataset_id}/sql",
|
||||
):
|
||||
try:
|
||||
response = self.client.network.request(
|
||||
method="POST",
|
||||
endpoint=endpoint,
|
||||
data=self._dump_json(
|
||||
{
|
||||
"template_params": payload.template_params,
|
||||
"effective_filters": payload.effective_filters,
|
||||
}
|
||||
),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
normalized = self._normalize_preview_response(response)
|
||||
if normalized is not None:
|
||||
return normalized
|
||||
errors.append(f"{endpoint}:unrecognized_response")
|
||||
except Exception as exc:
|
||||
errors.append(f"{endpoint}:{exc}")
|
||||
|
||||
raise RuntimeError(
|
||||
"; ".join(errors) or "Superset preview compilation failed"
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.explore(
|
||||
@@ -254,10 +395,6 @@ class SupersetCompilationAdapter:
|
||||
)
|
||||
raise RuntimeError(str(exc)) from exc
|
||||
|
||||
normalized = self._normalize_preview_response(response)
|
||||
if normalized is None:
|
||||
raise RuntimeError("Superset preview compilation response could not be normalized")
|
||||
return normalized
|
||||
# [/DEF:SupersetCompilationAdapter._request_superset_preview:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter._request_sql_lab_session:Function]
|
||||
@@ -270,10 +407,20 @@ class SupersetCompilationAdapter:
|
||||
# @DATA_CONTRACT: Input[SqlLabLaunchPayload] -> Output[Dict[str,Any]]
|
||||
def _request_sql_lab_session(self, payload: SqlLabLaunchPayload) -> Dict[str, Any]:
|
||||
dataset_raw = self.client.get_dataset(payload.dataset_id)
|
||||
dataset_record = dataset_raw.get("result", dataset_raw) if isinstance(dataset_raw, dict) else {}
|
||||
database_id = dataset_record.get("database", {}).get("id") if isinstance(dataset_record.get("database"), dict) else dataset_record.get("database_id")
|
||||
dataset_record = (
|
||||
dataset_raw.get("result", dataset_raw)
|
||||
if isinstance(dataset_raw, dict)
|
||||
else {}
|
||||
)
|
||||
database_id = (
|
||||
dataset_record.get("database", {}).get("id")
|
||||
if isinstance(dataset_record.get("database"), dict)
|
||||
else dataset_record.get("database_id")
|
||||
)
|
||||
if database_id is None:
|
||||
raise RuntimeError("Superset dataset does not expose a database identifier for SQL Lab launch")
|
||||
raise RuntimeError(
|
||||
"Superset dataset does not expose a database identifier for SQL Lab launch"
|
||||
)
|
||||
|
||||
request_payload = {
|
||||
"database_id": database_id,
|
||||
@@ -305,7 +452,10 @@ class SupersetCompilationAdapter:
|
||||
extra={"target": candidate["target"], "error": str(exc)},
|
||||
)
|
||||
|
||||
raise RuntimeError("; ".join(errors) or "No Superset SQL Lab surface accepted the request")
|
||||
raise RuntimeError(
|
||||
"; ".join(errors) or "No Superset SQL Lab surface accepted the request"
|
||||
)
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter._request_sql_lab_session:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter._normalize_preview_response:Function]
|
||||
@@ -339,6 +489,7 @@ class SupersetCompilationAdapter:
|
||||
"raw_response": response,
|
||||
}
|
||||
return None
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter._normalize_preview_response:Function]
|
||||
|
||||
# [DEF:SupersetCompilationAdapter._dump_json:Function]
|
||||
@@ -348,7 +499,10 @@ class SupersetCompilationAdapter:
|
||||
import json
|
||||
|
||||
return json.dumps(payload, sort_keys=True, default=str)
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter._dump_json:Function]
|
||||
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter:Class]
|
||||
|
||||
# [/DEF:SupersetCompilationAdapter:Module]
|
||||
# [/DEF:SupersetCompilationAdapter:Module]
|
||||
|
||||
Reference in New Issue
Block a user