gabotorresruiz commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3784861047


##########
superset/mcp_service/dataset/tool/query_dataset.py:
##########
@@ -310,6 +310,10 @@ async def query_dataset(  # noqa: C901
                 custom_cache_timeout=request.cache_timeout,
             )
 
+            from superset.charts.data.form_data import 
set_query_context_form_data
+
+            set_query_context_form_data(query_context, dataset.id, "table")

Review Comment:
   Not a blocker, but a symmetry question: `ChartDataCommand` also runs in 
`_query_from_form_data` in get_chart_data (the `form_data_key` branch, around 
line 1021), plus `get_chart_sql`, `get_chart_preview`, `preview_utils`, 
`compile`, and `semantic_layer/get_table`, none of which get the new helper. 
Concretely, after this PR the same chart resolves its time range in 
`get_chart_data` but `get_chart_sql` still renders `No filter`, so the SQL we 
show a user can diverge from the SQL we ran. Should the helper cover those 
paths too, or is that deliberately deferred?



##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,57 @@
+# 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
+
+from typing import Any, TYPE_CHECKING
+
+from flask import g
+
+if TYPE_CHECKING:
+    from superset.common.query_context import QueryContext
+    from superset.common.query_object import QueryObject
+
+
+def set_form_data(form_data: dict[str, Any]) -> None:
+    """Expose chart data request fields to Jinja template macros."""
+    g.form_data = form_data
+
+
+def _serialize_query(query: QueryObject) -> dict[str, Any]:
+    """Serialize query fields consumed by the Jinja form-data fallback."""
+    query_data = dict(query.to_dict())

Review Comment:
   The remaining blocker is that the serializer still does not carry what most 
of the targeted macros read. `QueryObject.to_dict()` emits `"filter"` 
(singular, query_object.py:399) and never `url_params`, while the Jinja 
fallback only converts `"filters"` (`convert_legacy_filters_into_adhoc`, 
utils/core.py:1432) and `url_param()` reads `"url_params"` 
(jinja_context.py:320). I verified on this head with a real `QueryObject`: 
after `set_query_context_form_data`, `filter_values()` and `get_filters()` 
return `[]` and `url_param()` returns its default; only `get_time_filter()` and 
`get_dataset_id_from_context()` change behavior. The unit tests pass because 
`Mock.to_dict()` returns `"filters"` and `"url_params"`, keys the real method 
never produces, the same gap the earlier `time_range` comment found.
   
   Also worth knowing: `filter_values()` inside a virtual dataset's SQL already 
resolves without `g.form_data`, via the native-filter fallback from #39594 
(jinja_context.py:614), and 
`test_get_sqla_query_virtual_dataset_filter_values_drill_to_detail` pins that. 
I confirmed by rendering a templated virtual dataset through 
`get_query_str_extended` on this head with and without the helper: the filter 
lands in the SQL in both cases and the only delta is the time range.
   
   So I would either add `"filters": query.filter` here (and drop the 
`url_param` claim, or hoist `url_params` from `query_context.form_data` if MCP 
ever carries them), or narrow the PR description and tests to `time_range` plus 
the implicit `metric()` dataset lookup. Either way, could the regression tests 
build a real `QueryObject` instead of mocking `to_dict()`? A real object is 
what would have caught this and the `time_range` omission.



##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,57 @@
+# 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
+
+from typing import Any, TYPE_CHECKING
+
+from flask import g
+
+if TYPE_CHECKING:
+    from superset.common.query_context import QueryContext
+    from superset.common.query_object import QueryObject
+
+
+def set_form_data(form_data: dict[str, Any]) -> None:
+    """Expose chart data request fields to Jinja template macros."""
+    g.form_data = form_data
+
+
+def _serialize_query(query: QueryObject) -> dict[str, Any]:
+    """Serialize query fields consumed by the Jinja form-data fallback."""
+    query_data = dict(query.to_dict())
+    if query.time_range is not None:
+        query_data["time_range"] = query.time_range
+    return query_data
+
+
+def set_query_context_form_data(
+    query_context: QueryContext,
+    datasource_id: int,
+    datasource_type: str,
+) -> None:
+    """Expose a programmatically-created query like a chart data API 
request."""
+    set_form_data(
+        {
+            "datasource": {"id": datasource_id, "type": datasource_type},
+            "queries": [
+                _serialize_query(query)
+                for query in getattr(query_context, "queries", ())

Review Comment:
   This block worries me a bit. The commit message says the `getattr` exists to 
tolerate mocked query contexts, but a real `QueryContext` always has `queries`, 
so the only thing this guard can ever do in production is silently publish an 
empty `queries` list to Jinja when something is genuinely broken, instead of 
failing loudly. I would revert this and fix the test instead: 
`TestOAuthErrorRouting._patch_query_path` stubs `QueryContextFactory.create` to 
return a bare `object()` (test_get_chart_data.py:1410); returning 
`SimpleNamespace(queries=[], form_data={})` there keeps those tests passing 
without shaping production code around a mock. I verified the two OAuth tests 
pass on this head, so this is about not letting the mock dictate the helper's 
contract, not about CI.



-- 
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]

Reply via email to