Skip to content

Feature: AI SQL Query Assistant for database-sourced charts#1266

Open
vytisbulkevicius wants to merge 1 commit intodevelopmentfrom
feature/ai-sql-query-assistant
Open

Feature: AI SQL Query Assistant for database-sourced charts#1266
vytisbulkevicius wants to merge 1 commit intodevelopmentfrom
feature/ai-sql-query-assistant

Conversation

@vytisbulkevicius
Copy link
Contributor

@vytisbulkevicius vytisbulkevicius commented Mar 6, 2026

Summary

Adds a natural-language SQL assistant panel to the Source → Import from database tab of the chart editor. Users describe the data they want in plain English and the AI generates a correctly structured, ready-to-run SQL query tailored to the active chart type and database schema.

Features

  • Natural language to SQL — Users type a plain-English description and the AI generates a complete SQL query with a brief explanation of what it retrieves.
  • Chart-type awareness — The query always returns the correct number and type of columns for the active chart (e.g. pie = 2 cols, scatter = 3 cols, etc.).
  • Table awareness — Available tables and their columns are passed to the AI so it references real schema instead of guessing.
  • Conversational refinement — Follow-up prompts modify the previously generated query rather than starting fresh. The last generated query is sent as context alongside the new prompt.
  • Multi-provider support — Works with OpenAI (GPT-4), Google Gemini, and Anthropic Claude. The provider dropdown only shows providers with a configured API key.
  • Suggestion chips — Each response includes 2–3 related query ideas as clickable chips that pre-fill and trigger the next generation.
  • One-click apply — "Use This Query" injects the generated SQL directly into the CodeMirror editor.
  • MySQL compatibility layer — Detects environments where wrapping a date column in a function inside a WHERE clause crashes the connection (e.g. Local by Flywheel). The AI is instructed to use safe direct comparisons (WHERE post_date < DATE_SUB(CURDATE(), INTERVAL 1 YEAR)) and a post-processor rewrites any unsafe patterns in the generated query as a safety net.
  • Empty-result handling — When a valid WHERE clause matches 0 rows and MySQL drops the connection (a known Local by Flywheel quirk), the server pings the DB after the error to distinguish a query-specific drop from a real outage, and returns a friendly "No results found" message instead of a misleading connection error.

Files changed

File Change
classes/Visualizer/Module/AI.php New — AJAX handler, AI provider calls, SQL system prompt, MySQL rewriter
js/ai-sql-query.js New — UI interactions, AJAX, CodeMirror injection
classes/Visualizer/Render/Layout.php Added AI assistant panel inside _renderDbQuery()
classes/Visualizer/Module/Chart.php Registered/enqueued new script, added nonce_sql to visualizerAI, improved empty-result and connection-drop detection in getQueryData()
js/frame.js Added error: callback to DB query AJAX, improved error display, handle empty-result success response

Test plan

  • Panel appears in Source tab only when at least one AI API key is configured
  • Generating a query for a pie chart returns exactly 2 columns
  • Follow-up prompt "rename the value column" modifies the existing query, not creates a new one
  • Clicking "Use This Query" populates the CodeMirror SQL editor
  • On a WHERE clause that matches 0 rows, "Show Results" shows "No results found" instead of a connection error
  • Suggestion chips pre-fill the prompt and trigger generation

Closes https://github.com/Codeinwp/visualizer-pro/issues/549

Adds a natural-language SQL assistant panel to the Source → Import from
database tab of the chart editor. Users describe the data they want in
plain English and the AI generates a correctly structured, ready-to-run
SQL query tailored to the active chart type and database schema.

Key features:
- Multi-provider support: OpenAI, Gemini, Claude (panel only renders
  when at least one API key is configured)
- Chart-type-aware query generation (correct column count/types per chart)
- Table-aware: passes available tables and columns to the AI
- Conversational refinement: follow-up prompts modify the existing query
  rather than starting fresh, using the last generated query as context
- MySQL compatibility layer: detects environments where applying date
  functions to columns in WHERE crashes the connection (e.g. Local by
  Flywheel) and rewrites generated queries to use safe direct comparisons
- Empty-result detection: when a valid WHERE clause returns 0 rows and
  MySQL drops the connection, pings the DB to distinguish this from a
  real outage and shows a friendly 'No results found' message instead
- Suggestion chips for related query ideas
- One-click 'Use This Query' button injects generated SQL into CodeMirror

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@pirate-bot pirate-bot added the pr-checklist-incomplete The Pull Request checklist is incomplete. (automatic label) label Mar 6, 2026
@pirate-bot
Copy link
Contributor

Plugin build for 6e60911 is ready 🛎️!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-checklist-incomplete The Pull Request checklist is incomplete. (automatic label)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants