SQL Generation and Grounding

Why Solid's SQL generation is an agentic flow with a self-correction loop, not a single LLM call — and the grounding mechanisms that keep it accurate.

Solid's SQL generation is not a single LLM call. It is an agentic flow with a self-correction loop that boosts accuracy from ~77% (semantic model + LLM alone) to ~97% (after the full fixing loop).

Step-by-Step SQL Generation Flow

Business Question (from agent or user)
        │
        ▼
Get / Identify Semantic Model(s)
        │
        ▼
Generate SQL (first attempt from semantic model context)
        │
        ▼
┌───────────────────────────────────────┐
│         CORRECTION LOOP (up to 3×)   │
│                                       │
│  Execute Query via DB Agent            │
│        │                              │
│  Pass? ──YES──► Choose Best Result ──►│── Return Query + Explanation
│        │                              │
│        NO                             │
│        ▼                              │
│  Identify Failure Type:               │
│   • Fix Syntax (invalid SQL syntax)   │
│   • Fix Content (wrong tables/joins)  │
│   • Fix Execution (query runs but     │
│     returns wrong/empty results)      │
│   • Fix Performance (long-running     │
│     query — EXPLAIN-plan analysis     │
│     and rewrite)                      │
│        │                              │
│  Regenerate SQL with fix              │
│        │                              │
│  Retry (up to 3 attempts)             │
└───────────────────────────────────────┘

Accuracy Improvement

StageAccuracy
Semantic model + LLM only (no correction loop)~77%
With the full agentic SQL flow (correction loop)~97%

Multi-Step Validation Before Execution

Solid runs multiple layers of static verification before even attempting DB Agent execution:

  1. SQL syntax analysis — not LLM-based; deterministic code analysis
  2. Alignment check against DDL / information schema — does the SQL reference tables and columns that actually exist in the schema?
  3. Alignment check against semantic model bounds — does the SQL stay within the tables, columns, and join paths defined in the model? No arbitrary columns or tables can be added.
  4. Only if all static checks pass does Solid send the query to the DB Agent for execution validation.

How "Best Result" Is Chosen

When the correction loop produces multiple candidate SQLs, Solid selects the best result based on:

  • Whether the query executes successfully (returns results, no error)
  • Whether the results are non-empty — Solid treats empty results from what should be a data-returning query as a synthetic failure
  • Quality signals from the semantic model alignment

Grounding by Information

What context is injected into the prompt:

MechanismDescription
Governed Schema ScopeThe engine is strictly bound to the tables and columns defined in the semantic model — no "out-of-bounds" hallucinations.
Contextual Metadata InjectionBusiness logic (relationships, metrics, certified golden queries) is injected into every LLM prompt for consistency.
Glossary & Synonym MappingBusiness terminology is explicitly mapped to the technical schema, so the LLM "speaks the same language" as end users.
Categorical Value MappingThe engine is grounded with valid categorical data values per column, so filters align with actual database content (e.g., "US" vs "USA" vs "United States").
Few-Shot Qualified Queries (Golden SQLs)A curated list of certified, high-accuracy queries is provided as templates for complex or high-stakes questions.

Grounding by Process

What validation and correction mechanisms enforce quality:

MechanismDescription
Automated Self-CorrectionThe validation layer captures execution errors and syntax warnings, feeding them back into the engine for real-time refinement — the correction loop above.
Dynamic Instruction TuningSQL generation is guided by custom instructions, continuously monitored and updated by modelers.
Intelligent Model RoutingIn multi-model MCP calls, the engine selects the most relevant semantic model(s) by analyzing model descriptions and related business questions.
Performance BenchmarkingSide-by-side evaluation of how the same question is interpreted across different engine iterations or model versions.
Proactive Model TuningAnalysis of benchmark runs provides actionable recommendations; semantic model adjustments are proposed automatically.
Reasoning Transparency ("Why this query")The engine returns a structured explanation of how the question was interpreted, which model was selected, which terms resolved, and why specific joins/filters were chosen.
Multi-Engine ComparisonThe same semantic model can be evaluated against multiple SQL generators (Solid Text2SQL, native warehouse Text2SQL) using Solid's benchmarking harness.

See Query Qualification Pipeline for how the certified/golden queries referenced above are selected in the first place, and Benchmarking for how accuracy is measured. See When Does Solid Execute SQL? for exactly who executes SQL, under what identity, at each stage of this flow.


Did this page help you?