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


##########
superset/mcp_service/chart/tool/get_chart_preview.py:
##########
@@ -422,22 +436,27 @@ def generate(self) -> VegaLitePreview | ChartError:
             # Build columns list: include both x_axis and groupby
             columns = _build_query_columns(form_data)
 
+            prepare_form_data_for_query(
+                form_data,
+                self.chart.datasource_id,
+                self.chart.datasource_type,
+            )
+            query_dict = {
+                "columns": columns,
+                "metrics": form_data.get("metrics", []),
+                "row_limit": 1000,  # More data for visualization
+                "order_desc": True,
+            }
+            apply_form_data_filters_to_query(query_dict, form_data)
+
             # Create query context for data retrieval
             factory = QueryContextFactory()
             query_context = factory.create(
                 datasource={
                     "id": self.chart.datasource_id,
                     "type": self.chart.datasource_type,
                 },
-                queries=[
-                    {
-                        "filters": form_data.get("filters", []),
-                        "columns": columns,
-                        "metrics": form_data.get("metrics", []),
-                        "row_limit": 1000,  # More data for visualization
-                        "order_desc": True,
-                    }
-                ],
+                queries=[query_dict],
                 form_data=form_data,
                 force=self.request.force_refresh,
             )

Review Comment:
   **Suggestion:** Preview generation now injects a handcrafted query using 
only `columns` and `metrics`, where `metrics` is read only from the plural key. 
Charts that store measures/dimensions in alternative fields (such as singular 
`metric`, `entity`/`series`, or secondary series fields) can produce 
empty/incorrect previews even though the chart is valid. Reuse the same 
chart-type-aware metric/groupby resolution used by data/SQL paths before 
constructing preview queries. [incomplete implementation]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ MCP table previews can be empty for valid bubble charts.
   - ❌ Vega-Lite previews drop mixed_timeseries secondary series data.
   - ⚠️ LLM chart previews may misrepresent underlying saved charts.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Save a chart whose measures live in non-`metrics` fields, for example a 
`bubble` chart
   using `x`/`y`/`size` metrics or a big-number-style chart using singular 
`metric` (these
   chart-type-specific fields are documented in the data tool fallback at
   superset/mcp_service/chart/tool/get_chart_data.py:37-68 and bubble handling 
at 50-57).
   
   2. From an MCP client, call `get_chart_preview`
   (superset/mcp_service/chart/tool/get_chart_preview.py:17-23, 43-62) with
   `format="vega_lite"` or `format="table"` so the request is routed to
   `VegaLitePreviewStrategy.generate` or `TablePreviewStrategy.generate`.
   
   3. Inside the selected strategy, `form_data` is loaded from 
`self.chart.params` and passed
   through `prepare_form_data_for_query` (TablePreviewStrategy at 327-333,
   VegaLitePreviewStrategy at 439-443), then `query_dict` is built with 
`columns` from
   `_build_query_columns(form_data)` and `metrics = form_data.get("metrics", 
[])` at lines
   335-337 and 444-447, so alternate metric fields like `metric`, 
`x`/`y`/`size`, or
   `metrics_b` for `mixed_timeseries` are ignored.
   
   4. `QueryContextFactory.create(..., queries=[query_dict], 
form_data=form_data, ...)` is
   called (superset/mcp_service/chart/tool/get_chart_preview.py:342-351, 
453-462;
   superset/common/query_context_factory.py:45-89) and `ChartDataCommand.run()` 
executes with
   this incomplete query, which for charts lacking a `metrics` array or 
standard `groupby`
   yields empty or erroring results, even though the same chart returns data 
correctly via
   SQL/data paths that use richer metric/groupby resolution
   (superset/mcp_service/chart/tool/get_chart_sql.py:78-88, 122-146, 184-200).
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0A%0A%2A%2APath%3A%2A%2A%20superset%2Fmcp_service%2Fchart%2Ftool%2Fget_chart_preview.py%0A%2A%2ALine%3A%2A%2A%20444%3A462%0A%2A%2AComment%3A%2A%2A%0A%09%2AIncomplete%20Implementation%3A%20Preview%20generation%20now%20injects%20a%20handcrafted%20query%20using%20only%20%60columns%60%20and%20%60metrics%60%2C%20where%20%60metrics%60%20is%20read%20only%20from%20the%20plural%20key.%20Charts%20that%20store%20measures%2Fdimensions%20in%20alternative%20fields%20%28such%20as%20singular%20%60metric%60%2C%20%60entity%60%2F%60series%60%2C%20or%20secondary%20series%20fields%29%20can%20produce%20empty%2Fincorrect%20previews%20even%20though%20the%20chart%20is%20valid.%20Reuse%20the%20same%20chart-type-aware%20metric%2Fgroupby%20resolution%20used%20by%20data%2FSQL%20paths%20before%20constructing%20preview%20queries.%0A%0AValidate%20the%20correctness%20of%20the%20flagged
 
%20issue.%20If%20correct%2C%20How%20can%20I%20resolve%20this%3F%20If%20you%20propose%20a%20fix%2C%20implement%20it%20and%20please%20make%20it%20concise.%0AOnce%20fix%20is%20implemented%2C%20also%20check%20other%20comments%20on%20the%20same%20PR%2C%20and%20ask%20user%20if%20the%20user%20wants%20to%20fix%20the%20rest%20of%20the%20comments%20as%20well.%20if%20said%20yes%2C%20then%20fetch%20all%20the%20comments%20validate%20the%20correctness%20and%20implement%20a%20minimal%20fix%0A)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0A%0A%2A%2APath%3A%2A%2A%20superset%2Fmcp_service%2Fchart%2Ftool%2Fget_chart_preview.py%0A%2A%2ALine%3A%2A%2A%20444%3A462%0A%2A%2AComment%3A%2A%2A%0A%09%2AIncomplete%20Implementation%3A%20Preview%20generation%20now%20injects%20a%20handcrafted%20query%20using%20only%20%60columns%60%20and%20%60metrics%60%2C%20where%20%60metrics%60%20is%20read%20only%20from%20the%20pl
 
ural%20key.%20Charts%20that%20store%20measures%2Fdimensions%20in%20alternative%20fields%20%28such%20as%20singular%20%60metric%60%2C%20%60entity%60%2F%60series%60%2C%20or%20secondary%20series%20fields%29%20can%20produce%20empty%2Fincorrect%20previews%20even%20though%20the%20chart%20is%20valid.%20Reuse%20the%20same%20chart-type-aware%20metric%2Fgroupby%20resolution%20used%20by%20data%2FSQL%20paths%20before%20constructing%20preview%20queries.%0A%0AValidate%20the%20correctness%20of%20the%20flagged%20issue.%20If%20correct%2C%20How%20can%20I%20resolve%20this%3F%20If%20you%20propose%20a%20fix%2C%20implement%20it%20and%20please%20make%20it%20concise.%0AOnce%20fix%20is%20implemented%2C%20also%20check%20other%20comments%20on%20the%20same%20PR%2C%20and%20ask%20user%20if%20the%20user%20wants%20to%20fix%20the%20rest%20of%20the%20comments%20as%20well.%20if%20said%20yes%2C%20then%20fetch%20all%20the%20comments%20validate%20the%20correctness%20and%20implement%20a%20minimal%20fix%0A)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <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_preview.py
   **Line:** 444:462
   **Comment:**
        *Incomplete Implementation: Preview generation now injects a 
handcrafted query using only `columns` and `metrics`, where `metrics` is read 
only from the plural key. Charts that store measures/dimensions in alternative 
fields (such as singular `metric`, `entity`/`series`, or secondary series 
fields) can produce empty/incorrect previews even though the chart is valid. 
Reuse the same chart-type-aware metric/groupby resolution used by data/SQL 
paths before constructing preview queries.
   
   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%2F40099&comment_hash=80b26baa560ee433a51a60f0f361a83bd734f47402879ecf0bad1644c80de692&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40099&comment_hash=80b26baa560ee433a51a60f0f361a83bd734f47402879ecf0bad1644c80de692&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