You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "aminghadersohi (via GitHub)" <gi...@apache.org> on 2026/08/06 02:15:16 UTC

[PR] fix(mcp): expose query context to Jinja macros [superset]

aminghadersohi opened a new pull request, #42822:
URL: https://github.com/apache/superset/pull/42822

   ### SUMMARY
   
   The chart data API makes its query request available through Flask's global form-data fallback because Jinja macros such as `filter_values`, `get_filters`, and `url_param` read query inputs from that context. Programmatically constructed MCP queries skipped that step, so virtual datasets and metrics using those macros could fail despite the equivalent API query succeeding.
   
   This extracts the API's form-data assignment into a shared helper and uses it for `get_chart_data` and `query_dataset`. MCP query contexts are represented in the same datasource/queries/form-data shape consumed by the existing Jinja lookup path; no synthetic request context or duplicated macro-specific behavior is introduced.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   Not applicable; this is a backend-only query execution fix.
   
   ### TESTING INSTRUCTIONS
   
   - `pytest -q tests/unit_tests/mcp_service/chart/tool/test_get_chart_data.py tests/unit_tests/mcp_service/dataset/tool/test_query_dataset.py` (136 passed)
   - `pytest -q tests/unit_tests/mcp_service` (3318 passed; one unrelated existing health-check smoke failure because the test environment has no version string configured)
   - `uvx pre-commit run --files superset/charts/data/api.py superset/charts/data/form_data.py superset/mcp_service/chart/tool/get_chart_data.py superset/mcp_service/dataset/tool/query_dataset.py tests/unit_tests/mcp_service/chart/tool/test_get_chart_data.py tests/unit_tests/mcp_service/dataset/tool/test_query_dataset.py`
   - `pre-commit run --all-files` (Python checks passed; frontend-only hooks could not complete because frontend dependencies such as `glob` and `postcss-styled-syntax` are not installed; the formatter also reported pre-existing frontend formatting drift)
   
   The regression tests execute `filter_values` and `get_filters` during an MCP `query_dataset` call and cover `url_param` plus implicit dataset lookup for `metric()` using chart-shaped query context.
   
   ### ADDITIONAL INFORMATION
   
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in [SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   
   ## Blast radius
   
   This affects only MCP chart and dataset query execution plus the existing chart-data API context assignment. It does not change authorization, RLS, workspace isolation, schemas, migrations, feature flags, or non-MCP query behavior.
   
   ## Risk & rollback
   
   Risk is limited to the Flask-global form-data payload used while compiling MCP queries. The payload is built from the already validated query context and matches the existing API representation. Rollback is a revert of this commit.
   
   ## Review guidance
   
   Start with `superset/charts/data/form_data.py`, then verify the two MCP call sites populate it immediately before `ChartDataCommand` execution. The tests document the request-dependent macro boundary.
   
   ## Eval evidence
   
   No model or prompt evaluation was run because this change does not modify tool schemas, descriptions, prompts, model routing, or generated responses. The deterministic MCP regression coverage passes 136/136; the full MCP unit suite passes 3318/3319 with the environment-only health-check failure described above.
   
   ## Cost & latency delta
   
   No model parameters, prompts, or tool payload schemas changed, so token and model-cost delta is zero. The added work serializes the already-built query objects once per affected call; no external request or database query is added.
   
   ## Prompt / non-determinism
   
   No prompt, model routing, or non-deterministic behavior changed.
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5199621229

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5297148923

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "gabotorresruiz (via GitHub)" <gi...@apache.org>.
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5263755510

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5297335779

   Reviewed the remaining unresolved Bito thread. It conflated the direct Jinja template-context filter path with the Flask-global form-data fallback: the latter intentionally consumes the plural `filters` key via `convert_legacy_filters_into_adhoc`, while `QueryObject.to_dict()` still retains the singular `filter` key. Real-`QueryObject` regression tests and reviewer execution confirm `filter_values()` and `get_filters()` work. I replied with the technical rationale and resolved the thread.
   
   Unresolved review threads: **0**. Review status: **APPROVED**. Failed checks: **0**.


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3785376451


##########
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:
   Good point. I kept this PR scoped to the two MCP data-execution paths that motivated the fix; broadening it to every compile/preview/semantic-layer caller requires separate behavior and regression coverage. I created #43174 to track that promised follow-up: https://github.com/apache/superset/issues/43174. The blocking serializer issues and tests are addressed on this head.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5285904068

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3785376168


##########
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:
   Addressed in 9446d35f45 (included in head b1daf8b77b).  now maps the real  field to the  key consumed by the Jinja fallback, preserves , and hoists  from . Both regression tests now construct real  instances rather than mocking , so they exercise the production serialization contract.



##########
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:
   Addressed in 9446d35f45. I removed the production  guard and changed the OAuth test factory to return , keeping the helper contract aligned with a real .



##########
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:
   Good point. I kept this PR scoped to the two MCP data-execution paths that motivated the fix; broadening it to every compile/preview/semantic-layer caller would require separate behavior and regression coverage. The helper is shared so those paths can adopt it consistently in a follow-up. The blocking serializer issues and tests are addressed on this head.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3786552233


##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,63 @@
+# 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,
+    form_data: dict[str, Any],
+) -> dict[str, Any]:
+    """Serialize query fields consumed by the Jinja form-data fallback."""
+    query_data = dict(query.to_dict())
+    query_data["filters"] = query.filter

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Incorrect filter exposure level</b></div>
   <div id="fix">
   
   The `filters` assignment to `query_data` places them at the wrong nesting level. `ExtraCache` reads filters from `_context.get("filter")` (jinja_context.py:1027), not from `query_data["filters"]`. This causes `ExtraCache.filter_values()` and `ExtraCache.get_filters()` to return empty results despite filters being present in the query.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #af39ad</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5199651955

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/42822?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `60.00000%` with `6 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 56.19%. Comparing base ([`1de35d1`](https://app.codecov.io/gh/apache/superset/commit/1de35d136117e171c92acaf6cf898ab8ab395a42?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`993cf12`](https://app.codecov.io/gh/apache/superset/commit/993cf12e444ec69263ea54f084e369abbcd852e4?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/42822?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [superset/mcp\_service/chart/tool/get\_chart\_data.py](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&filepath=superset%2Fmcp_service%2Fchart%2Ftool%2Fget_chart_data.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvbWNwX3NlcnZpY2UvY2hhcnQvdG9vbC9nZXRfY2hhcnRfZGF0YS5weQ==) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [superset/mcp\_service/dataset/tool/query\_dataset.py](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&filepath=superset%2Fmcp_service%2Fdataset%2Ftool%2Fquery_dataset.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvbWNwX3NlcnZpY2UvZGF0YXNldC90b29sL3F1ZXJ5X2RhdGFzZXQucHk=) | 0.00% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [superset/charts/data/api.py](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&filepath=superset%2Fcharts%2Fdata%2Fapi.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvY2hhcnRzL2RhdGEvYXBpLnB5) | 75.00% | [1 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [superset/charts/data/form\_data.py](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&filepath=superset%2Fcharts%2Fdata%2Fform_data.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvY2hhcnRzL2RhdGEvZm9ybV9kYXRhLnB5) | 85.71% | [1 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/42822?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   > :exclamation:  There is a different number of reports uploaded between BASE (1de35d1) and HEAD (993cf12). Click for more details.
   > 
   > <details><summary>HEAD has 53 uploads less than BASE</summary>
   >
   >| Flag | BASE (1de35d1) | HEAD (993cf12) |
   >|------|------|------|
   >|hive|8|1|
   >|python|41|2|
   >|presto|8|1|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #42822      +/-   ##
   ==========================================
   - Coverage   65.73%   56.19%   -9.55%     
   ==========================================
     Files        2843     2844       +1     
     Lines      162672   162684      +12     
     Branches    37255    37255              
   ==========================================
   - Hits       106935    91415   -15520     
   - Misses      53645    70426   +16781     
   + Partials     2092      843    -1249     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [hive](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `37.96% <46.66%> (+<0.01%)` | :arrow_up: |
   | [mysql](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [postgres](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `39.87% <60.00%> (+<0.01%)` | :arrow_up: |
   | [python](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `39.92% <60.00%> (-19.29%)` | :arrow_down: |
   | [sqlite](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/42822/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/superset/pull/42822?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3786605909


##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,63 @@
+# 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,
+    form_data: dict[str, Any],
+) -> dict[str, Any]:
+    """Serialize query fields consumed by the Jinja form-data fallback."""
+    query_data = dict(query.to_dict())
+    query_data["filters"] = query.filter

Review Comment:
   <!-- Bito Reply -->
   The explanation provided is clear and addresses the concern regarding filter exposure. Since the regression tests confirm that both filter paths are correctly resolved and the human reviewer has verified the behavior, no further code changes are required for this specific issue.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "netlify[bot] (via GitHub)" <gi...@apache.org>.
netlify[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5283962051

   ### <span aria-hidden="true">✅</span> Deploy Preview for *superset-docs-preview* ready!
   
   
   |  Name | Link |
   |:-:|------------------------|
   |<span aria-hidden="true">🔨</span> Latest commit | 8f40cd94cad2c9ecbe6e87c92b6100340278204a |
   |<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/superset-docs-preview/deploys/6a7dfa3642ee14000760353d |
   |<span aria-hidden="true">😎</span> Deploy Preview | [https://deploy-preview-42822--superset-docs-preview.netlify.app](https://deploy-preview-42822--superset-docs-preview.netlify.app) |
   |<span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />![QR Code](https://app.netlify.com/qr-code/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJodHRwczovL2RlcGxveS1wcmV2aWV3LTQyODIyLS1zdXBlcnNldC1kb2NzLXByZXZpZXcubmV0bGlmeS5hcHAifQ.35ihvpf5J5yJp2sYnpLnvOsoNZ0BKFfV6lI9HksUc3U)<br /><br />_Use your smartphone camera to open QR code link._</details> |
   |<span aria-hidden="true">🤖</span> Make changes | [Run an agent on this branch](https://app.netlify.com/projects/superset-docs-preview/agent-runs#Dcq7DYAgEADQXa6WiIKQuIFjnB4IRD5BGmPcXbpXvBdKzbE0WAEG2Cumw3Vj9Ol0SKbe2fkxHoUFnwIym2tkhA37JlOu_GzUv0JNFoWSszGT5JxrxcUiCL4f) |
   ---
   <!-- [superset-docs-preview Preview](https://deploy-preview-42822--superset-docs-preview.netlify.app) -->
   _To edit notification comments on pull requests, go to your [Netlify project configuration](https://app.netlify.com/projects/superset-docs-preview/configuration/notifications#deploy-notifications)._


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5288005243

   AI Code Review is in progress (usually takes 3 to 15 minutes unless it's a very large PR).
   
   <details>
   <summary>
   Bito Usage Guide
   </summary>
   
   **Commands**
         
   Type the following command in the pull request comment and save the comment.
         
   - `/review` - Manually triggers a full AI review.
   
   - `/pause` - Pauses automatic reviews on this pull request.
   - `/resume` - Resumes automatic reviews.
   - `/resolve` - Marks all Bito-posted review comments as resolved.
   - `/abort` - Cancels all in-progress reviews.
   
         
   Refer to the <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/available-commands" target="_blank" rel="noopener noreferrer">documentation</a> for additional commands.
         
   **Configuration**
   
   This repository uses `Superset` You can customize the agent settings <a href="https://alpha.bito.ai/home/ai-agents/code-review-agent" target="_blank" rel="noopener noreferrer">here</a> or contact your Bito workspace admin at evan@preset.io.
         
   **Documentation & Help**
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/getting-started/install-run-using-bito-cloud/create-or-customize-an-agent-instance" target="_blank" rel="noopener noreferrer">Customize agent settings</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/implementing-custom-code-review-rules" target="_blank" rel="noopener noreferrer">Review rules</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent" target="_blank" rel="noopener noreferrer">General documentation</a>
   - <a href="https://docs.bito.ai/bito-dev-agents/ai-code-review-agent/faqs" target="_blank" rel="noopener noreferrer">FAQ</a>
   
   </details>
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3785376168


##########
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:
   Addressed in 9446d35f45 (included in head b1daf8b77b). The serializer now maps the real QueryObject.filter field to the filters key consumed by the Jinja fallback, preserves time_range, and hoists url_params from QueryContext.form_data. Both regression tests now construct real QueryObject instances rather than mocking to_dict(), so they exercise the production serialization contract.



##########
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:
   Addressed in 9446d35f45. I removed the production getattr guard and changed the OAuth test factory to return SimpleNamespace(queries=[], form_data={}), keeping the helper contract aligned with a real QueryContext.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5295475561

   Addressed the requested changes in the latest head:
   
   - serialize real QueryObject filters under the key consumed by Jinja macros
   - preserve time_range and hoist url_params from query-context form data
   - replace mocked to_dict() regression fixtures with real QueryObject instances
   - remove the production getattr guard and fix the OAuth test stub instead
   - move the new helper imports to module scope
   
   Pre-commit passes on all changed files. Targeted pytest collection remains blocked only in this local workspace by its stale sqlglot installation (missing sqlglot.dialects.singlestore); CI has been triggered for head b1daf8b77b.


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi merged PR #42822:
URL: https://github.com/apache/superset/pull/42822


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3763620873


##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,47 @@
+# 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
+
+
+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 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": [
+                query.to_dict() for query in getattr(query_context, "queries", [])

Review Comment:
   **Suggestion:** Serializing each `QueryObject` with `to_dict()` drops fields that Jinja macros depend on, including `time_range` (the method's returned dictionary does not contain that field). When a query context stores the time range on the query object rather than in `QueryContext.form_data`, `get_time_filter()` sees no time range and silently falls back to `No filter`, causing templated SQL to use the wrong time window. Preserve the complete query representation or explicitly copy the omitted macro-relevant fields. [data type]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ MCP-templated SQL can lose requested time bounds.
   - ❌ `get_time_filter()` may generate unbounded queries.
   - ⚠️ Results differ from equivalent chart API queries.
   ```
   </details>
   
   [![Fix in Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d9b71360924b4e498f4df6b96ba077d3&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [![Fix in VSCode Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d9b71360924b4e498f4df6b96ba077d3&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/charts/data/form_data.py
   **Line:** 43:43
   **Comment:**
   	*Data Type: Serializing each `QueryObject` with `to_dict()` drops fields that Jinja macros depend on, including `time_range` (the method's returned dictionary does not contain that field). When a query context stores the time range on the query object rather than in `QueryContext.form_data`, `get_time_filter()` sees no time range and silently falls back to `No filter`, causing templated SQL to use the wrong time window. Preserve the complete query representation or explicitly copy the omitted macro-relevant fields.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42822&comment_hash=699a0d23550baafc1a719f05fc18b7fa63a0a88f593fa22f6cb8096a2aff78b9&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42822&comment_hash=699a0d23550baafc1a719f05fc18b7fa63a0a88f593fa22f6cb8096a2aff78b9&reaction=dislike'>👎</a>



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3786604911


##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,63 @@
+# 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,
+    form_data: dict[str, Any],
+) -> dict[str, Any]:
+    """Serialize query fields consumed by the Jinja form-data fallback."""
+    query_data = dict(query.to_dict())
+    query_data["filters"] = query.filter

Review Comment:
   This does not reproduce and no code change is needed. There are two distinct filter paths: ExtraCache._get_filters_from_query_context reads the template context key filter, while the Flask-global fallback used here goes through views.utils.get_form_data() and convert_legacy_filters_into_adhoc(), which explicitly consumes the plural filters key. QueryObject.to_dict() already retains the singular filter key, and this helper adds plural filters for that fallback. The regression tests use real QueryObject instances and verify both filter_values() and get_filters(); the human reviewer also re-ran those macros on this head and confirmed they resolve correctly.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5296438004

   Final review-comment audit complete:
   
   - Both blocking human-review findings are fixed and the reviewer has re-verified the behavior with real objects and approved the PR.
   - The earlier automated suggestions are also covered: imports are module-level, the duplicate async-query setter now uses the shared helper, and the helper has dedicated unit coverage.
   - The non-blocking sibling-path follow-up promised in review is tracked in #43174: https://github.com/apache/superset/issues/43174
   
   Latest update: 8c10f89f5e. Pre-commit passes on all changed files.


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "bito-code-review[bot] (via GitHub)" <gi...@apache.org>.
bito-code-review[bot] commented on PR #42822:
URL: https://github.com/apache/superset/pull/42822#issuecomment-5262324696

   <!-- Bito Reply -->
   The flagged issue is correct. The `set_query_context_form_data` function currently only serializes `queries` and `form_data` from the `QueryContext`, which can omit critical fields like `time_range` that Jinja macros rely on for templated SQL.
   
   To resolve this, you should update `set_query_context_form_data` in `superset/charts/data/form_data.py` to explicitly include the `time_range` field if it exists on the `QueryContext` or its query objects. 
   
   Would you like me to implement this fix and check the rest of the PR comments for you?
   
   **superset/charts/data/form_data.py**
   ```
   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": [
                   query.to_dict() for query in getattr(query_context, "queries", [])
               ],
               "form_data": getattr(query_context, "form_data", None) or {},
               "time_range": getattr(query_context, "time_range", None),  # Add this
           }
       )
   ```


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(mcp): expose query context to Jinja macros [superset]

Posted by "aminghadersohi (via GitHub)" <gi...@apache.org>.
aminghadersohi commented on code in PR #42822:
URL: https://github.com/apache/superset/pull/42822#discussion_r3777489516


##########
superset/charts/data/form_data.py:
##########
@@ -0,0 +1,47 @@
+# 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
+
+
+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 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": [
+                query.to_dict() for query in getattr(query_context, "queries", [])

Review Comment:
   Fixed in 8f40cd94ca. The shared serializer now explicitly preserves `QueryObject.time_range`, and the regression test verifies `get_time_filter()` receives it.



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org