codeant-ai-for-open-source[bot] commented on code in PR #43176:
URL: https://github.com/apache/superset/pull/43176#discussion_r3786506106


##########
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
+    if query.time_range is not None:
+        query_data["time_range"] = query.time_range
+    if url_params := form_data.get("url_params"):
+        query_data["url_params"] = url_params
+    return query_data
+
+
+def set_query_context_form_data(
+    query_context: QueryContext,
+    datasource_id: int | str,
+    datasource_type: str,
+) -> None:
+    """Expose a programmatically-created query like a chart data API 
request."""
+    form_data = query_context.form_data or {}
+    set_form_data(
+        {
+            "datasource": {"id": datasource_id, "type": datasource_type},
+            "queries": [
+                _serialize_query(query, form_data) for query in 
query_context.queries
+            ],
+            "form_data": form_data,
+        }

Review Comment:
   **Suggestion:** The synthetic global form data keeps the original chart 
fields only under the nested `form_data` key, while `get_form_data()` exposes 
the first serialized query as the top-level form data. Consequently, fields 
such as `adhoc_filters` and `extra_form_data` from `query_context.form_data` 
are invisible to Jinja macros that inspect top-level form data, notably 
`get_time_filter(column)`, which can render a fallback or the wrong temporal 
range even though the executed query has the filter. Merge the relevant 
original form-data fields into the top level or serialize them into the query 
using the chart-data request shape. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ MCP SQL output can diverge from executed chart SQL.
   - ⚠️ Column-specific Jinja time filters are ignored.
   - ⚠️ Virtual dataset previews can show incorrect temporal ranges.
   ```
   </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=54f8d9f4043e43ea89f925f371d71c48&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=54f8d9f4043e43ea89f925f371d71c48&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:** 55:62
   **Comment:**
        *Api Mismatch: The synthetic global form data keeps the original chart 
fields only under the nested `form_data` key, while `get_form_data()` exposes 
the first serialized query as the top-level form data. Consequently, fields 
such as `adhoc_filters` and `extra_form_data` from `query_context.form_data` 
are invisible to Jinja macros that inspect top-level form data, notably 
`get_time_filter(column)`, which can render a fallback or the wrong temporal 
range even though the executed query has the filter. Merge the relevant 
original form-data fields into the top level or serialize them into the query 
using the chart-data request shape.
   
   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%2F43176&comment_hash=65e2f73154011f53eec9ae7e16fdccae5924dca127276faebae67144da8ee8c7&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43176&comment_hash=65e2f73154011f53eec9ae7e16fdccae5924dca127276faebae67144da8ee8c7&reaction=dislike'>👎</a>



##########
superset/mcp_service/chart/tool/get_chart_sql.py:
##########
@@ -185,6 +187,11 @@ def _sql_from_saved_query_context(
         query_context = ChartDataQueryContextSchema().load(qc_json)
         query_context.result_type = ChartDataResultType.QUERY
 
+        set_query_context_form_data(
+            query_context,
+            chart.datasource_id,
+            chart.datasource_type,
+        )

Review Comment:
   **Suggestion:** The saved-query-context path executes the datasource encoded 
in `query_context`, but the new Jinja fallback is populated with 
`chart.datasource_id` and `chart.datasource_type`. If a chart's saved query 
context is stale after its datasource changes, `metric()` or 
`get_dataset_id_from_context()` resolves the chart's current datasource while 
SQL generation queries the saved context's datasource, producing inconsistent 
or cross-dataset SQL. Use the datasource carried by the parsed `query_context` 
so the template context and execution target cannot diverge. [stale reference]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Saved chart SQL can target the wrong dataset.
   - ⚠️ Jinja `metric()` expansion can mismatch execution.
   - ⚠️ Chart datasource changes can produce misleading SQL previews.
   ```
   </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=b6527dd9e4d24f40bf7e53c14b27c39b&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=b6527dd9e4d24f40bf7e53c14b27c39b&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/mcp_service/chart/tool/get_chart_sql.py
   **Line:** 190:194
   **Comment:**
        *Stale Reference: The saved-query-context path executes the datasource 
encoded in `query_context`, but the new Jinja fallback is populated with 
`chart.datasource_id` and `chart.datasource_type`. If a chart's saved query 
context is stale after its datasource changes, `metric()` or 
`get_dataset_id_from_context()` resolves the chart's current datasource while 
SQL generation queries the saved context's datasource, producing inconsistent 
or cross-dataset SQL. Use the datasource carried by the parsed `query_context` 
so the template context and execution target cannot diverge.
   
   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%2F43176&comment_hash=f7f119f66cd8a512ed35566da6f421ef2c6e8cc95699fda4bf8d0eaf08c2e9f9&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F43176&comment_hash=f7f119f66cd8a512ed35566da6f421ef2c6e8cc95699fda4bf8d0eaf08c2e9f9&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: [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