Most Text-to-SQL demos look impressive on a 5-table toy database and fall apart the moment they meet an enterprise data warehouse. The bottleneck isn't SQL syntax—it's context bloat, ambiguous domain boundaries, and tribal schema knowledge.
In their engineering deep dive, QueryGPT – Natural Language to SQL Using Generative AI, Uber shared how they evolved a 2023 hackathon prototype through 20+ iterations into a production system serving ~1.2 million interactive queries per month (with Operations alone driving ~36% of volume), cutting average query authoring time from ~10 minutes down to ~3 minutes.
Here is an architectural breakdown of why naive RAG failed at Uber's scale—and the multi-agent pipeline they built to fix it.
1. Why V1 Naive RAG Collapsed at Scale
Uber's initial May 2023 prototype (Hackdayz V1) followed a classic single-pass RAG pattern:
- Embed the user's natural language prompt.
- Run k-nearest neighbor (k-NN) similarity search across a small pool of 7 Tier-1 tables and 20 sample SQL queries.
- Retrieve the top 3 tables and top 7 SQL samples, combine them with custom Uber business instructions (such as date formatting rules), and generate SQL in one LLM call.
flowchart LR P["User Natural Language Prompt"] --> E["Vector Embedding + k-NN Search"] E --> R1["Top 3 Table DDLs"] E --> R2["Top 7 SQL Samples"] C["Custom Uber Date & Business Rules"] --> LLM["Single LLM Call"] R1 --> LLM R2 --> LLM LLM --> OUT["SQL Query + Explanation"] As Uber onboarded more datasets into production, three systemic bottlenecks broke this architecture:
- DDL Embedding Mismatch: Vector similarity between a plain-English business question ("trips completed yesterday in Seattle") and raw
CREATE TABLEDDL statements degraded rapidly as hundreds of tables were added. - Context Window Exhaustion: Wide Tier-1 tables at Uber often exceed 200+ columns, consuming 40K–60K tokens per table. Retrieving just three tables blew past early 32K token limits—and continued causing intermittent overflows even after upgrading to GPT-4 Turbo (1106) with a 128K token window.
- Silent Semantic Hallucinations: Queries frequently compiled and executed without syntax errors while returning zero rows because the LLM guessed wrong enum values (e.g., filtering
status = 'Finished'when the table stores'Completed').
2. Production Architecture: A Progressive Filtering Funnel
Instead of dumping raw schemas into a single prompt, Uber redesigned QueryGPT as a multi-stage agentic pipeline that progressively narrows the search radius before generating SQL.
flowchart TD U["User Prompt: Tesla trips completed in Seattle yesterday"] --> IA["1. Intent Agent"] IA --> WS["Domain Workspace Routing: Mobility selected out of 12 System Workspaces"] WS --> RAG["Scoped Vector Search: Mobility Tables & SQL Samples"] RAG --> TA["2. Table Agent"] TA --> HITL{"Human-in-the-Loop Checkpoint"} HITL -- "User edits table list" --> TA HITL -- "User clicks Looks Good" --> CPA["3. Column Prune Agent"] CPA --> SLIM["Pruned Schemas: Irrelevant Columns Stripped"] SLIM --> GEN["4. SQL Generation LLM + Custom Date Rules"] GEN --> FINAL["Verified SQL Query + Plain-English Explanation"] Stage 1: Domain Workspaces + Intent Agent
Uber partitioned its data catalog into 12 curated System Workspaces—Mobility plus 11 other core domains including Ads (Uber Advertising), Core Services, Platform Engineering, and IT (Information Technology)—alongside user-defined Custom Workspaces for specialized teams.
Every request first hits an Intent Agent (an LLM classifier) that maps the prompt to one or more workspaces. By locking onto the Mobility workspace first, QueryGPT avoids searching unrelated Ads or IT schemas altogether.
Stage 2: Table Agent with Human-in-the-Loop (HITL)
Early user feedback revealed that when upstream table retrieval missed the mark, the final SQL was guaranteed to fail. Uber introduced a Table Agent paired with an explicit UI checkpoint:
- The agent proposes candidate tables (e.g.,
uber.trips_dataanduber.dim_city). - The user can either click "Looks Good" to proceed or manually add/remove tables before SQL synthesis begins.
Stage 3: Column Prune Agent
To eliminate 128K token overflows and reduce inference latency, Uber added a Column Prune Agent. Before generating SQL, an intermediate LLM call inspects the user's question and strips out irrelevant columns from each selected table's DDL—shrinking a 200-column schema down to only the handful of fields needed for joins, filters, and projections.
3. Complete End-to-End Walkthrough
Here is how a real request flows through every stage of the pipeline (note: uber.trips_data is an illustrative table name used in Uber's blog):
- User Prompt: > "Write a query to find the number of trips that were completed by Teslas in Seattle yesterday"
- Intent Agent Output: Maps prompt to the
Mobilitysystem workspace. - Table Agent + User Confirmation: Suggests
uber.trips_dataanduber.dim_city→ User clicks "Looks Good". - Column Prune Agent Output: Reduces
uber.trips_datafrom 200+ columns down totrip_id,city_id,vehicle_make,trip_status, anddatestr(partition key). - Final Generated SQL (incorporating Uber's custom date instructions):
SELECT COUNT(DISTINCT t.trip_id) AS completed_tesla_trips FROM uber.trips_data AS t INNER JOIN uber.dim_city AS c ON t.city_id = c.city_id WHERE c.city_name = 'Seattle' AND t.vehicle_make = 'Tesla' AND t.trip_status = 'Completed' AND t.datestr = DATE_FORMAT(CURRENT_DATE - INTERVAL '1' DAY, '%Y-%m-%d'); (Accompanied by a plain-English explanation of the join, filter conditions, and date partition logic.)
4. How Uber Evaluates QueryGPT (Vanilla vs. Decoupled)
Because LLM outputs are non-deterministic (~5% run-to-run variance), Uber built a golden question-to-SQL benchmark and tracks regressions across two test modes:
- Vanilla Flow: End-to-end execution where QueryGPT infers intent, tables, and SQL autonomously.
- Decoupled Flow: Injects ground-truth intent and tables directly into the generator so engineers can isolate SQL synthesis quality from upstream retrieval errors.
flowchart LR G["Golden Benchmark Question"] --> M1["1. Intent Accuracy"] G --> M2["2. Table Overlap Score: 0.0 to 1.0"] G --> M3["3. Successful Run: Compiles & Executes"] G --> M4["4. Run Has Output: Rows Greater Than 0"] G --> M5["5. Qualitative Query Similarity: LLM Judge 0.0 to 1.0"] Each benchmark run captures 5 distinct signals:
- Intent Accuracy: Did the Intent Agent select the right workspace?
- Table Overlap (0 to 1): Measures retrieval precision. For example, if "How many trips were canceled by drivers last week in Los Angeles?" requires
[fact_trip_state, dim_city]and QueryGPT selects[dim_city, fact_eats_trip], the overlap score is 0.5 (1 of 2 required tables matched). - Successful Run: Does the SQL execute on the engine without syntax or missing-partition errors?
- Run Has Output (
rows > 0): Catches silent semantic bugs—such asWHERE status = 'Finished'instead of'Completed'—where a query runs cleanly but returns zero records. - Qualitative Query Similarity (0 to 1): An LLM judge compares the generated SQL against the golden SQL to verify whether joins, columns, and aggregations are structurally sound even when a minor rule (like a missing partition filter) causes execution to fail.
Key Architectural Takeaways
- Bound your retrieval space first: Domain workspaces + an Intent Agent outperform flat vector search across monolithic enterprise catalogs.
- Put humans at high-leverage checkpoints: Confirming 2–3 table names takes a user two seconds and prevents cascading hallucination in the final SQL.
- Prune schemas aggressively: Treating DDL pruning as its own lightweight agentic step cuts token cost, prevents context overflows, and improves SQL attention accuracy.
- Measure silent failures, not just syntax: A Text-to-SQL query that compiles with zero rows returned is a failure—track non-empty execution alongside structural similarity.

