Skip to content
gentic.news — AI News Intelligence Platform
Connecting to the Living Graph…

Listen to today's AI briefing

Daily podcast — 5 min, AI-narrated summary of top stories

A two-phase architecture diagram showing a planning phase separated from execution, with arrows connecting user…

Never Let the LLM Write the Joins

This article details a two-phase text-to-SQL pipeline: Phase A deterministically plans (intent, entity resolution, joins, RBAC) and Phase B executes with bounded LLM calls. The subject graph caches entity mappings lazily, and security is enforced before the model sees any schema.

·18h ago·5 min read··7 views·AI-Generated·Report error
Share:
Source: pub.towardsai.netvia towards_aiSingle Source
How does a deterministic planning phase improve text-to-SQL reliability and security?

The architecture separates Phase A (deterministic planning: intent, entity resolution, join paths, RBAC) from Phase B (LLM-driven SQL writing and execution). Joins come from a domain graph, security is injected by rewriting SQL, and cardinality is fixed by a planner — none are left to the model.

TL;DR

A two-phase pipeline splits query planning from SQL generation, keeping joins, security, and cardinality out of the LLM's hands.

Key Takeaways

  • This article details a two-phase text-to-SQL pipeline: Phase A deterministically plans (intent, entity resolution, joins, RBAC) and Phase B executes with bounded LLM calls.
  • The subject graph caches entity mappings lazily, and security is enforced before the model sees any schema.

The Innovation — What the Source Reports

This article is Part 3 of a series on building a conversational analytics engine. It introduces a two-phase architecture that radically separates planning from execution to solve the seven walls that break text-to-SQL on real data. The key insight: never let the LLM write the joins.

Phase A: Deterministic Planning

Phase A is entirely deterministic except for a single LLM call for intent classification. It takes a user question like "Show orders for customer Bike World" and produces a frozen plan — no SQL exists yet. The stages:

  1. User context assembly: Loads the user's role, territory, permissions, and vocabulary.
  2. Intent classification: One LLM call returns a structured verdict (intent type, entity types, confidence).
  3. Semantic resolution: A 2-pass vector search maps natural language terms to database entities by meaning.
  4. Scope resolution: Turns phrases like "my team" into row-filter hints using pre-derived rules.
  5. Routing: Maps entity types to real tables, assigns roles, finds join paths via the domain graph, and resolves entities.
  6. RBAC Checkpoint 1: Drops any table the user may not see before the model ever sees the schema.

The Subject Graph

The subject graph resolves specific instances (e.g., "Bike World" → StoreID 620). It is a cache, not a copy — it starts empty and fills lazily as users ask about entities. Resolution runs a cascade: cache → exact match → fuzzy match → semantic match (placeholder) → source-DB lookup. Ambiguity returns a clarification question, not a coin flip.

Phase B: Bounded LLM Execution

Phase B is a small state machine with three nodes: agent (LLM reasons), tools (executes exactly one tool), and synthesize (writes final answer). It loops at most 10 reasoning iterations and 15 tool calls with a 30-second timeout. The LLM writes only the SELECT and WHERE clauses — joins, security, and cardinality are all handled by deterministic code.

Security Model

Two RBAC checkpoints exist: one in Phase A (drops tables before the model sees them) and one in Phase B (injects row-level security by rewriting SQL). The author states: "Security is never the model's job."

Honest Limitations

The conversation is currently single-shot — follow-up memory is built but not wired. The semantic match rung in the entity resolution cascade is a stub (placeholder).

Why This Matters for Retail & Luxury

Retail and luxury companies operate on complex, multi-source databases: CRM, POS, inventory, e-commerce, loyalty, and supply chain. A sales rep asking "How many orders did Bike World place last quarter in my territory?" is a real, daily request. This architecture directly addresses the failure modes that make most text-to-SQL systems unusable in production:

  • Security: In retail, data access is granular — a regional manager should not see national margins, and a store associate should not see HR data. The dual RBAC gates ensure this without relying on the LLM's judgment.
  • Join complexity: Retail schemas often have 10+ tables with non-obvious join paths (e.g., linking customer orders to inventory via product SKUs). The domain graph handles this deterministically.
  • Entity ambiguity: "Bike World" could be a store name, a vendor, or a customer segment. The subject graph's cascade resolves this with confidence scores, returning "Did you mean the Seattle or Dallas store?" when ambiguous.

Business Impact

For a luxury retailer like Kering or Richemont, this architecture could:

  • Reduce time-to-insight for regional sales managers from hours (waiting for BI reports) to seconds (direct conversational queries).
  • Eliminate data leaks from LLM hallucinations on join paths or security rules.
  • Enable self-service analytics for non-technical users (store managers, merchandisers) without requiring a data engineer to build every query.

The author does not provide quantified business metrics, but the design directly addresses the top failure modes reported by enterprises deploying text-to-SQL.

Implementation Approach

  • Technical requirements: A domain graph (schema introspection + enrichment), a subject graph (entity cache), and a state machine for Phase B. Requires a vector database, an in-memory cache, and an LLM (any model that can follow structured intent classification prompts).
  • Complexity: Medium-high. Building the domain graph and entity resolution cascade is non-trivial but well-scoped. The two-phase pipeline is modular and testable.
  • Effort: Likely 2-3 months for a team of 2-3 engineers to adapt to an existing retail database, assuming schema documentation exists.

Governance & Risk Assessment

  • Privacy: The subject graph caches only entity names and IDs, not row data. This is a strong privacy design — no customer PII is copied out of the source database.
  • Bias: The deterministic planning phase reduces bias risk compared to end-to-end LLM approaches, since join paths and security rules are hard-coded. However, the single LLM call for intent classification could still reflect model bias.
  • Maturity: The architecture is well-designed but the author notes it is single-shot (no multi-turn memory yet). It is production-ready for single-query analytics but not yet for conversational follow-ups.

Source: pub.towardsai.net

Sources cited in this article

  1. The LLM
Source: gentic.news · · author= · citation.json

AI-assisted reporting. Generated by gentic.news from 1 verified source, fact-checked against the Living Graph of 4,300+ entities. Edited by Ala SMITH.

Following this story?

Get a weekly digest with AI predictions, trends, and analysis — free.

AI Analysis

**Architectural Honesty** This is one of the most honest and practical text-to-SQL designs I have seen in production. The author explicitly calls out where the architecture is incomplete (the semantic match stub, the single-shot limitation) and where design trade-offs were made. For retail AI leaders evaluating conversational analytics, this is a blueprint worth studying — not because it is flashy, but because it solves the real problems that break systems in production. **The Key Insight for Practitioners** The line between Phase A and Phase B is the product. The author's decision to keep joins, security, and cardinality out of the LLM's hands is the right call for any enterprise deployment. In retail, where schema changes quarterly (new product lines, seasonal SKUs, store acquisitions), a deterministic planning layer that can be updated independently of the LLM is a significant maintenance advantage. The subject graph's lazy-fill design also avoids the data copying that would trigger compliance reviews in regulated luxury markets. **Where to Watch** The semantic match stub is the biggest gap — it means the system cannot handle synonyms or fuzzy entity names that the fuzzy matcher misses. For a luxury retailer with multiple brand names (e.g., "Gucci" vs. "Guccio Gucci S.p.A."), this would require either a richer alias table or completing that rung of the cascade. The author's plan to use embedding similarity is promising but not yet implemented.

Mentioned in this article

Enjoyed this article?
Share:

AI Toolslive

Five one-click lenses on this article. Cached for 24h.

Pick a tool above to generate an instant lens on this article.

Related Articles

From the lab

The framework underneath this story

Every article on this site sits on top of one engine and one framework — both built by the lab.

More in Opinion & Analysis

View all