sadpandajoe commented on code in PR #44849: URL: https://github.com/apache/superset/pull/44849#discussion_r4190610551
########## superset/semantic_layers/result_inspection.py: ########## @@ -0,0 +1,92 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import hashlib +from dataclasses import dataclass, field +from typing import TYPE_CHECKING + +from flask import g, has_request_context, request + +from superset import security_manager +from superset.semantic_layers.metadata_binding import metadata_refresh_enabled +from superset.utils import json +from superset.utils.json import json_int_dttm_ser + +if TYPE_CHECKING: + from superset.common.query_context import QueryContext + from superset.common.query_object import QueryObject + +_IDENTITY_KEY: str = "superset.semantic_metadata.result_identities" + + +@dataclass(frozen=True) +class CapturedResultIdentity: + """A private result key and its request-local authorization/query fingerprint.""" + + key: str = field(repr=False) + fingerprint: str = field(repr=False) + + +def _fingerprint(context: QueryContext, query: QueryObject) -> str: + """Bind an existing key to its subject, stored view, query and RLS scope.""" + return hashlib.sha256( + json.dumps( + [ + id(getattr(g, "user", None)), Review Comment: The tests in `metadata_result_inspection_test.py` only vary the RLS key and the request lifetime. Capturing a key and then changing `g.user` (or the retained query's metrics) in the same request, with RLS unchanged, and asserting `captured_result_key` returns `None` is never exercised, so dropping the subject or `query.to_dict()` terms from this fingerprint keeps the suite green and would let inspection return the previous query's entry. Could a case be added that mutates each of those inputs after capture and asserts the key is no longer returned? ########## superset/semantic_layers/result_inspection.py: ########## @@ -0,0 +1,92 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import hashlib +from dataclasses import dataclass, field +from typing import TYPE_CHECKING + +from flask import g, has_request_context, request + +from superset import security_manager +from superset.semantic_layers.metadata_binding import metadata_refresh_enabled +from superset.utils import json +from superset.utils.json import json_int_dttm_ser + +if TYPE_CHECKING: + from superset.common.query_context import QueryContext + from superset.common.query_object import QueryObject + +_IDENTITY_KEY: str = "superset.semantic_metadata.result_identities" + + +@dataclass(frozen=True) +class CapturedResultIdentity: + """A private result key and its request-local authorization/query fingerprint.""" + + key: str = field(repr=False) + fingerprint: str = field(repr=False) + + +def _fingerprint(context: QueryContext, query: QueryObject) -> str: + """Bind an existing key to its subject, stored view, query and RLS scope.""" + return hashlib.sha256( + json.dumps( + [ + id(getattr(g, "user", None)), + str(getattr(context.datasource, "uuid", None)), + context.datasource.changed_on, + query.to_dict(), Review Comment: `query.to_dict()` here includes the runtime `contribution_totals` that `ensure_totals_available()` writes into `post_processing`, which `QueryObject.cache_key()` deliberately strips. Those totals are `df[col].sum()` values, so for a `float32` (or `int32`) metric they are `numpy.float32`/`numpy.int32`, and `json_int_dttm_ser` has no handler for them (`base_json_conv` only covers `np.int64`). With refresh enabled, a contribution chart on such a semantic view would raise `TypeError` in `capture_result_identity` and the request would fail with a 500 before the main query runs, even though this capture is only diagnostic. Could the fingerprint drop `contribution_totals` the same way the cache key does, or capture be made non-fatal? -- 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]
