Skip to content

feat(mcp): add compile check to validate chart queries before returning#38392

Closed
aminghadersohi wants to merge 2 commits intoapache:masterfrom
aminghadersohi:amin/ch98930/chart-compile-check
Closed

feat(mcp): add compile check to validate chart queries before returning#38392
aminghadersohi wants to merge 2 commits intoapache:masterfrom
aminghadersohi:amin/ch98930/chart-compile-check

Conversation

@aminghadersohi
Copy link
Contributor

SUMMARY

When generating a chart via MCP, the tool previously returned a chart URL without verifying
that the underlying query actually executes successfully. This meant users could receive
links to broken charts that fail to render.

This PR adds a compile check step that runs a lightweight test query (row_limit=2) via
QueryContextFactory + ChartDataCommand after chart generation. If the query fails,
a structured error response is returned instead of a broken chart link.

Behavior:

  • Saved charts (save_chart=True): If compile check fails, the chart is deleted from
    the database and an error is returned — no broken charts are left behind
  • Preview-only mode (save_chart=False): If compile check fails, an error is returned
    with details about what went wrong
  • Success path: Compile warnings (if any) are included in the response

The _compile_chart() helper catches ChartDataQueryFailedError, ChartDataCacheLoadError,
CommandException, ValueError, and KeyError — returning a CompileResult dataclass
with success status, error message, warnings, and row count.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A — backend-only change, no UI modifications.

TESTING INSTRUCTIONS

  1. Run the unit tests:
    pytest tests/unit_tests/mcp_service/chart/tool/test_generate_chart.py -v
  2. Verify all 14 tests pass (10 existing + 4 new compile check tests)
  3. The new TestCompileChart class covers:
    • Successful compile with row count verification
    • Query errors embedded in result payload
    • ChartDataQueryFailedError exceptions
    • ValueError from invalid configuration

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • 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

After generating a chart, execute a lightweight test query via
QueryContextFactory + ChartDataCommand to verify the chart renders
without errors. If the query fails, return a structured error instead
of a link to a broken chart.

- Add _compile_chart() helper that runs the chart query with row_limit=2
- For saved charts (save_chart=True): delete the chart on compile failure
- For preview-only mode: return compile error without saving
- Add 4 unit tests covering success, query errors, command exceptions,
  and bad config scenarios
@dosubot dosubot bot added the change:backend Requires changing the backend label Mar 4, 2026
@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.09%. Comparing base (983b633) to head (b69ec08).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #38392      +/-   ##
==========================================
+ Coverage   64.30%   65.09%   +0.78%     
==========================================
  Files        1811     2484     +673     
  Lines       71557   124205   +52648     
  Branches    22810    28886    +6076     
==========================================
+ Hits        46015    80850   +34835     
- Misses      25542    41939   +16397     
- Partials        0     1416    +1416     
Flag Coverage Δ
hive 41.28% <ø> (?)
mysql 64.31% <ø> (?)
postgres 64.38% <ø> (?)
presto 41.30% <ø> (?)
python 66.13% <ø> (?)
sqlite 63.98% <ø> (?)
unit 100.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@bito-code-review bito-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Agent Run #146e02

Actionable Suggestions - 1
  • superset/mcp_service/chart/tool/generate_chart.py - 1
    • Security: Missing permission check in preview compile · Line 519-524
Review Details
  • Files reviewed - 2 · Commit Range: a570d28..a570d28
    • superset/mcp_service/chart/tool/generate_chart.py
    • tests/unit_tests/mcp_service/chart/tool/test_generate_chart.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

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 documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Comment on lines +519 to +524
else:
from superset.daos.dataset import DatasetDAO

ds = DatasetDAO.find_by_id(request.dataset_id, id_column="uuid")
if ds:
numeric_dataset_id = ds.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security: Missing permission check in preview compile

The preview compile check fetches datasets without permission validation, unlike the save_chart path. This could allow users to probe compilation of inaccessible datasets via UUID. Add has_dataset_access checks after dataset lookup to prevent unauthorized information disclosure.

Citations

Code Review Run #146e02


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — added has_dataset_access(ds) check in the preview compile path UUID lookup (b69ec08). The save path already had this validation; the preview path was missing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, adding the has_dataset_access(ds) check in the preview compile path UUID lookup addresses the security issue by preventing unauthorized access to dataset compilation, matching the validation already in the save path.

…pile path

Add has_dataset_access() check when resolving dataset by UUID in the
preview-only compile check path. The save_chart path already validated
permissions, but the preview path allowed UUID-based dataset probing
without access verification.
@bito-code-review
Copy link
Contributor

bito-code-review bot commented Mar 4, 2026

Code Review Agent Run #67e2d8

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: a570d28..b69ec08
    • superset/mcp_service/chart/tool/generate_chart.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

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 documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@aminghadersohi
Copy link
Contributor Author

Reopening with a clean branch name.

@aminghadersohi aminghadersohi deleted the amin/ch98930/chart-compile-check branch March 4, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant