Containment Feedback for View-Aware Text-to-SQL

Companies keep curated saved queries in their data warehouses. These are often materialized views, meaning query results computed in advance and stored as tables. They encode the official definitions of business concepts such as "active customer" or "net revenue," and they answer questions far faster than recomputing from raw tables. Language-model agents that turn questions into SQL ignore most of these views. Two vocabularies get in the way. The user's words ("customers who are still subscribed") rarely match a view's name, and they match the raw encodings even less (status='A'). Classical view rewriting, the database optimizer step that swaps part of a query for a stored view, needs the query to be algebraically equivalent to the view definition. An agent that writes its own plausible but slightly different definition therefore loses twice. Its answer disagrees with the organization's official definition, and the optimizer can no longer recognize that a fast precomputed view would have answered it.

We propose closing this gap with a specific feedback signal. The agent first drafts SQL over the base tables. A formal query-containment checker then compares the draft against the definitions of nearby candidate views. Query containment asks whether every answer of one query is also an answer of another. The checker returns a precise difference to the agent, for example: "your draft matches view active_subscribers except that you filter status IN ('active','trial') where the view filters status='A'." The agent must then do one of three things. It can adopt the view, adopt the view and add an explicit correction predicate, or justify a deliberate departure from it. In this design the view catalog serves two roles. It is a dictionary from human vocabulary to schema vocabulary, and it is a target for precomputation. The containment checker replaces vague feedback such as "the query returned no rows" with exact, checkable feedback.

Core Claim

When a database holds curated views whose names are in human vocabulary and whose definitions are in schema vocabulary, a refinement loop whose feedback is a formal containment difference against candidate views should beat two simpler designs. The first is execution-only refinement. The second is view-aware prompting, where the agent sees the views and picks one in a single pass. We expect the advantage to show in two places. Answers should agree more often with the organization's view-defined semantics, and the agent should use near-miss views without a correction less often, an error we call silent drift. Speedups from hitting materialized views come as a consequence of choosing the right view, not as a separate objective. The claim is narrow and falsifiable. If view-aware prompting reaches the same fidelity and the same drift rate, then the containment feedback is not earning its cost.

Relation to Prior Work

The closest work is DART-SQL (Enhancing Text-to-SQL Parsing through Question Rewriting and Execution-Guided Refinement). It rewrites questions using database content to bridge the gap between user words and stored encodings (for example "female" versus 'f'). It then runs up to three run-and-debug iterations that fire on errors or empty results. DART-SQL motivates our work and is our main baseline. It works only against raw relations and has no notion of views. Its feedback also cannot tell a wrong but non-empty answer from a correct one, and that is exactly the case where a slightly wrong definition of "active" goes unnoticed.

The view-rewriting literature supplies our verifier and also bounds what it can do. Afrati and Li (Rewriting queries using views in the presence of arithmetic comparisons) show that equivalent rewritings are decidable for conjunctive queries with arithmetic comparisons. A conjunctive query is a join of tables with equality and comparison filters. They give polynomial algorithms for restricted comparison classes and also note that some cases require recursive programs. Benedikt, Pradic and Wernhard (Synthesizing nested relational queries from implicit specifications: via model theory and via proof theory) synthesize rewritings for nested data from logical specifications. Both works assume the query is already written in the view's vocabulary. Our contribution is to put such a checker inside a language-model loop, where its output becomes repair instructions rather than a yes-or-no rewrite decision. Real SQL with aggregation and outer joins falls outside the decidable fragment, so our checker is sound but incomplete. Outside the fragment it falls back to searching for counterexamples on generated databases and flags its verdicts as unverified. We also scope out stale views. Stale view cleaning (Stale View Cleaning: Getting Fresh Answers from Stale Materialized Views) shows that stale views need their own repair machinery, so we assume views are fresh.

The refinement literature limits the loop design. Yan et al. (execution feedback plateaus) find that gains concentrate in the first one or two iterations and that agents then cycle between wrong variants. Srivastava et al. (iterative prompting can reinforce errors) show that ambiguous feedback can lock in wrong assumptions. Our hypothesis is that much of this behavior comes from feedback that is too vague, and that a containment difference is unambiguous by construction. We borrow POLO's practice (POLO: An LLM-Powered Project-Level Code Performance Optimization Framework) of accepting a rewrite only after measuring it, with containment playing the role that benchmarking plays there.

The vocabulary-mismatch literature supplies the retrieval step and one warning. MINDER (Multiview Identifiers Enhanced Generative Retrieval) indexes documents by titles, substrings and generated pseudo-queries. We index each view the same way, by its name, its column names and a set of generated questions it answers. The ECLASS product-search study (ECLASS-Augmented Semantic Product Search for Electronic Components) found two things. Taxonomy metadata bridges non-expert queries to catalogs, but LLM query rewriting hurt retrieval because it dropped technical qualifiers. That finding is the main risk for any design that rewrites a question toward a view: forcing a question onto the nearest view throws away its qualifiers. We address this risk by requiring an explicit correction predicate or an explicit departure whenever the containment check is not exact. CoDA-Bench (CoDA-Bench: Can Code Agents Handle Data-Intensive Tasks?) motivates the focus on discovery. Agents gained 25–28% accuracy when handed the right data, and they often wrote valid code over lexically similar wrong files. That failure is the file-system version of picking the wrong view.

Research Program

The first component is a benchmark, because no existing Text-to-SQL suite in our file ships with view catalogs. We build one on BIRD and Spider (with Spider-Realistic questions). For each database we split the questions into a mining half and an evaluation half. From the gold SQL of the mining half we extract recurring subexpressions, such as filtered joins and grouped aggregates, and turn them into materialized views. Annotators verify a name and a one-line description for each view, written in business vocabulary that deliberately differs from the column names. This mirrors the gap Spider-Realistic creates by removing explicit column mentions. We also add distractor views with similar names and near-miss definitions, such as a different status code or an inclusive versus exclusive date bound. Evaluation questions fall into three groups. The first group is answerable exactly from a view. The second is answerable from a view plus a correction predicate. The third is not answerable from any view. We decide each question's group by running the checker over its gold SQL and confirm by manual audit. Latency is meaningless on small databases, so we also build scaled copies of a subset of BIRD databases, enlarged by row replication with key remapping.

The second component is the loop. A retriever ranks views using the MINDER-style multi-view index. The agent drafts SQL over base tables. The checker compares the draft with each top-ranked view and reports one of four outcomes: exact match, contained with a residual predicate, overlapping with a specific predicate or join difference, or unrelated. If the checker cannot decide, it reports a counterexample row set instead. The agent revises the draft given this report and must label its final query as view-exact, view-plus-correction, or base-only with a stated reason. We cap the loop at three rounds to match DART-SQL. Execution errors are still fed back as in DART-SQL, so the only new ingredient is the containment report.

The third component is analysis. For each question we record which view was retrieved, which the agent chose, the containment verdict at each round, and whether the final query silently drifted, meaning it used a view that does not match the question and added no correction. We also measure how often the database's own automatic view matcher would have caught the opportunity on the baseline's base-table SQL. That number tells us how much speedup current agents forfeit.

Baselines

The naive reference is a single-pass Text-to-SQL prompt over base tables with no views and no refinement. It shows how much the views and the loop add at all.

The strongest existing method is DART-SQL, run with its question rewriting, content-aware row sampling and three-round execution loop. The reported reference is 82.5 execution accuracy on Spider with GPT-3.5-turbo and a 15.2% exact-match gain on Spider-Realistic, both from Enhancing Text-to-SQL Parsing through Question Rewriting and Execution-Guided Refinement. I found no reported result on Lacuna for DART-SQL's BIRD numbers, and no reported result on Lacuna for any method on view-augmented versions of these benchmarks, since we construct them. We rerun DART-SQL on our splits with the same backbone model, the same three-round cap and the same token budget. We give it the views as ordinary tables so that it has the same information. Beating DART-SQL means higher view-semantic fidelity on the view-answerable groups with no loss in execution accuracy on the base-only group.

The second strong baseline is view-aware prompting. The same retriever places the top-ranked view definitions in the prompt, and the model writes SQL in one pass. It may also optionally run the DART-SQL execution loop. This baseline isolates whether seeing the views is enough. It receives the same retrieval results and the same budget.

The key ablation keeps our full loop but replaces the containment report with a generic message saying a related view exists, giving only its name. This tells us whether the precise difference matters or whether any prompt to reconsider views helps. A second ablation removes the multi-view index and retrieves views by name with BM25. BM25's 31.4% Hit_Rate@5 against 94.3% for metadata-aware retrieval in the ECLASS study suggests this ablation will expose retrieval as a bottleneck.

Why not just…?

Why not just show the agent the views and let it choose? This is the real threat, and view-aware prompting is a full baseline for that reason. Our expectation that it falls short rests on the ECLASS finding that LLMs drop qualifiers when they map queries onto standard forms. A model shown a view called active_subscribers will plausibly use it even when the question says "including trials." The experiment settles the matter on the near-miss distractors and the correction-predicate group. If view-aware prompting matches our silent-drift rate there, we will report that containment feedback is unnecessary, and the contribution reduces to the benchmark and the diagnostic.

Why not let the agent write base-table SQL and rely on the database's own view matcher? That matcher needs algebraic equivalence. Under vocabulary mismatch the agent writes its own definition of the concept, and the result is usually close to the view's but not equivalent. We measure this directly by running the matcher on DART-SQL outputs for the view-answerable questions. A high match rate would remove most of the motivation, so we report it whichever way it comes out.

Why not train a query rewriter, as Think-Then-Rewrite does? Think Then Rewrite trains a small model with reinforcement learning to rewrite queries toward a corpus's vocabulary, and it does well on legal and medical retrieval. Its reward measures retrieval relevance, which cannot tell a view that matches the question from one that differs by a single filter. Training would also have to be repeated for every warehouse. A trained rewriter could still feed our loop as the retriever, and we would test it in that role if the BM25 and dense retrieval ablations show retrieval to be the bottleneck.

Why not just use dense embeddings to match questions to views? Dense retrieval is part of our pipeline. It cannot be the whole method, because similar embeddings do not establish equivalent semantics. The ECLASS study also found that loading embeddings with many detailed attributes made retrieval worse. The containment check is precisely the component that embeddings cannot replace.

Proposed Experiments and Decisive Checks

All methods run with the same backbone model on our view-augmented BIRD and Spider/Spider-Realistic splits. We report five measures. The first is execution accuracy against the gold SQL. The second is view-semantic fidelity, meaning agreement with the view-defined answer on the two view-answerable groups. The third is the silent-drift rate. The fourth is the false-use rate on questions that no view answers. The fifth is wall-clock latency on the scaled databases, plus tokens and refinement rounds per question, as well as the rate at which the loop cycles between variants.

Three outcomes decide the proposal. First, on the near-miss and correction-predicate groups, containment feedback must cut silent drift relative to both view-aware prompting and the generic-message ablation. The generic-message comparison is the one that isolates our key ingredient. Second, on base-only questions, our false-use rate and execution accuracy must stay within noise of DART-SQL. Otherwise the method is merely pushing views onto questions they do not fit, which is the ECLASS distortion in a new setting. Third, the cycling analysis should show fewer repeated variants than execution-only refinement. That would support the hypothesis that the plateau reported by Yan et al. comes partly from vague feedback. We will also stratify by whether the checker's verdict was proven or only tested, so that gains are not credited to the incomplete fragment without saying so.