The Core Dilemma
Tabular and transactional data at scale should NEVER be converted into text chunks or vector embeddings like PDFs. The database engine must execute heavy aggregations.
The fundamental challenge is decoupling the LLM's reasoning from compute execution: the LLM understands user intent, while SQL/Analytical engines process the data.
Never pass millions of raw rows to the LLM. Pass metadata/DDL ➔ Generate Query ➔ Execute in Database ➔ Return aggregated summary.
The 4 Enterprise Patterns
Text-to-SQL / Text-to-DSL
LLM receives relevant DDL & schema metadata, generates SQL, and database engine executes query. High speed & low cost.
Semantic Layer & Tool Calling
LLM interacts with predefined metrics via Cube/dbt/Looker APIs. Zero SQL written by LLM; 100% deterministic KPIs.
Metadata RAG + Schema Routing
Vector search over data catalogs to select only the top 2-3 relevant tables out of 500+ before prompting the LLM.
Code Generation (DataFrame Sandbox)
LLM generates Python/PySpark code to execute complex multi-step transformations in a sandbox REPL environment.
Select an executive question to see how Text-to-SQL vs Semantic Layer handles it.
LLM invents SQL query from raw DDL schemas.
LLM emits Tool Call payload. Semantic layer (Cube/dbt) generates certified SQL.
When an enterprise database has 500+ tables, DDL schemas cannot fit in the prompt. Metadata RAG performs vector search over table descriptions to extract ONLY relevant DDL tables.
Test SQL queries generated by LLMs against an Abstract Syntax Tree (AST) validator to block destructive DDL/DML, enforce read-only SELECTs, and append LIMIT clauses.
Which approach should you use for your specific architecture?
| Pattern | What LLM Generates | Does LLM Write SQL? | Hallucination Risk | Best Use Case |
|---|---|---|---|---|
| Text-to-SQL | Raw SQL string | Yes | High (JOINs & metric logic) | Ad-hoc relational queries, small DBs |
| Semantic Layer | API Tool Call JSON | No (Engine compiles) | Zero (Certified metrics) | Enterprise KPIs, Executive Dashboards |
| Metadata RAG | Filtered DDL subset | No (Preprocessing) | Low | Data lakes with 100+ tables |
| Python REPL | PySpark / Pandas script | No (Writes Python) | Medium | Complex statistical & exploratory data science |