Teverant AI · Insights

2026-08-18

Text2SQL for the enterprise: architecture, data permissions, SQL safety and acceptance

A systematic guide to Text2SQL in practice, covering application boundaries, system architecture, data permissions, SQL safety validation, metric definition governance, and go-live acceptance, to help enterprises deliver reliable natural language data queries.

1. Is it a fit? The enterprise application boundaries of Text2SQL

When an enterprise adopts Text2SQL, the first question to answer is not "can the model generate SQL?" but "within what range of consequences are the generated results acceptable?" The same capability carries different levels of risk when an analyst uses it for ad hoc data exploration and when it produces inputs for business decisions. In the first case, users can inspect, modify, and rerun queries; in the second, once a wrong result flows into a report, an approval, or an automated process, the problem is no longer just a badly written statement. It may distort business judgment.

In terms of usage, an enterprise can tier its scenarios by the degree of human involvement. One scenario is SQL drafting assistance: the model turns natural language into a starting query, and an engineer or analyst reviews it before execution. This is well suited to verifying whether the model understands the table structure and basic query intent.

Application tierPrimary usersAcceptable model rolePre-launch focus
Query draftingDevelopers, analystsProvides an editable SQL starting pointSyntax, table and column selection, and human review
Semi-automated analysisData analystsHandles part of the query orchestrationResult sampling, business metric definitions, and execution scope
Self-service data accessBusiness usersAnswers questions within a restricted data domainPermission boundaries, explainability, and failure fallback
Decision inputBusiness systems or automated workflowsServes only as one step in a controlled pipelineDeterministic validation, approval, and accountability tracking

The scenarios are not limited to BI. An enterprise can place Text2SQL behind a natural language query entry point to help customer service staff retrieve business information within their authorized scope, use it as the data operation layer of low-code applications, or let analysts probe data quickly. But the acceptance criteria differ across these scenarios: customer service cares most about whether answers involve unauthorized access and whether the data source can be explained; exploratory analysis cares more about flexibility and the cost of modification; business self-service depends more on whether metric definitions are clear; and automated workflows must additionally prove that wrong results will not propagate downstream unchecked.

Some environments are not suited to putting Text2SQL up front. For example: tables lack stable relationships and the data model is held together by individual experience; the same metric is calculated differently across departments, with identical names but different meanings; queries frequently require cross-database joins while database dialects, update latency, and master data rules are inconsistent; or results directly affect budgets, risk control, performance evaluations, or other business actions. In these situations, the priority should be data modeling, metric definitions, and semantic governance. A model can only express business rules that are already explicit; it cannot fill in definitions the enterprise has not yet agreed on.

Therefore, when evaluating a project, do not look only at a model's accuracy on public leaderboards. A more practical approach is to weigh the following questions together: how sensitive the data is, whether questions often require multi-table or nested calculations, how costly a single error is to correct, whether the expected user base is large enough, and whether analysts, data engineers, or business owners are available to serve as a human fallback. If data sensitivity is high, the query path is complex, the margin for error is small, and reviewers are lacking, narrow the scope first rather than expanding the model's permissions. Conversely, if the data domain has clear boundaries, questions are relatively fixed, and users are willing to check results, Text2SQL is better suited to being validated incrementally as a controlled query assistance capability.

2. System architecture: breaking natural language queries into a governable pipeline

In enterprise settings, you should not hand a user's question straight to the model and let the model connect to the database on its own. Every stage should retain its intermediate artifacts, so that when an error occurs you can tell whether it came from understanding, table selection, generation, or execution, rather than seeing only a final answer.

StageMain responsibilityOutput to retain
Question clarificationFill in missing conditions such as time range, metric definition, object scope, and orderingStructured question or items pending confirmation
Intent recognitionDetermine the business objects, metrics, dimensions, and relationships the user wants to queryIntent, entities, constraints, and candidate concepts
Metadata retrievalFind the relevant tables, fields, metric definitions, and join paths in a controlled catalogScoped schema context
Plan generationSpecify the filtering, join, aggregation, grouping, and ordering stepsQuery plan or intermediate representation
SQL generation and checkingConvert the plan into a statement for the target database and validate its structure and constraintsSQL, check results, and revision records
Controlled execution and explanationRun within permission and resource boundaries and explain the meaning and limitations of the results to the userExecution status, result summary, and explanatory text

The point of this decomposition is not to make the process longer but to keep different kinds of problems from masking each other. Semantic understanding answers "what does the user want to query," the generation module turns the confirmed intent into a query structure, and a downstream revision module handles syntax, field references, and execution constraints. Each of the three can have its own test set and can be swapped out independently. For example, when a certain type of question keeps selecting the wrong metric, check concept recognition and metadata retrieval first instead of blindly tweaking the SQL generation prompt.

The schema catalog also cannot stop at table names, column names, and data types. The metadata that is truly useful to the model and to the review process also includes the business meaning of fields, common aliases, metric calculation methods, primary and foreign key relationships, permitted join paths, sensitivity levels, and a small number of processed sample values. Without this semantic information, a model may write a syntactically correct statement yet still pick a field with a similar meaning but a different definition.

Metadata retrieval should first narrow the candidate set and then pass only the necessary context to downstream modules. Stuffing the entire database structure into the prompt context at once adds irrelevant information and makes join paths and metric definitions harder to use reliably. A better approach is to filter by business domain, question entities, and candidate metrics, and to state explicitly which tables are available, which fields are not, and how tables are allowed to be joined. For sensitive fields, the catalog should also record their access conditions, but final approval cannot rely on the model's judgment alone.

Simple questions can be answered by generating queries from constrained templates. For questions involving multi-table joins, nested aggregation, or staged calculations, it is advisable to first produce a query plan or abstract syntax tree and then convert it into the specific database dialect. An intermediate representation makes decisions such as "what to filter first, how to join, and at which layer to aggregate" explicit, which makes them easier to check and modify and reduces the risk of structural drift when a model assembles long SQL directly. The converter should also adapt to the target database's functions, date handling, and pagination.

The execution layer must be isolated from the generation layer. The model can only submit query objects for review; it must not gain arbitrary access to production databases on its own. The execution service reapplies access boundaries, resource limits, and runtime policies, and returns a success, failure, or blocked status upstream. Result explanations should not guess at the data again either. They should be based on the actual execution results, the metric definitions applied, and the query conditions, and should state the statistical scope, the time basis, and any possible gaps.

In deployment, define observable inputs, outputs, and failure reasons for each stage, and save version information: which metadata was used, which metric definition was applied, what plan was generated, and which statement was ultimately executed. This makes it easy to replay individual requests and to pinpoint whether a change in results came from a catalog change, a definition adjustment, or model behavior. The goal of the architecture is not to have the model carry the entire pipeline, but to place it in stages that are bounded, replaceable, and auditable.

3. Enterprise data permissions: what the model can see and what users can ultimately query

Permission issues in Text2SQL cannot be solved with prompts. A prompt can remind the model not to access certain tables, but it is not an enforced boundary: the model may still generate unauthorized fields, and users may probe data by rephrasing questions, guessing column names, or constructing JOINs. The real permission decision belongs on the side of the database, the query gateway, or a controlled executor, and SQL must be bound to the current user's identity and authorization context before it is executed.

1. Permission checks must travel with the request

Before the model generates SQL, the system needs to establish who the request belongs to, which tenant it comes from, which data domains it may use, and what access scope the user has under their current role. This is not a one-time login check but a continuous constraint that runs through metadata retrieval, SQL generation, execution, and result delivery.

Control scopeQuestion to answer
Database/table levelWhether the user has access to the target dataset, table, or view
Column levelWhether certain fields are off-limits or may only return masked values
Row levelWhich organizations, regions, customers, or business records the user can see
Tenant levelWhether query conditions always stay within the current tenant boundary

Permissions cannot be checked just once at the generation stage. The executor should validate again before the SQL reaches the database and enforce tenant conditions, row-level filters, and sensitive-column restrictions.

2. Unauthorized metadata should not enter the context either

Schema retrieval is a precondition of permission control. The system should not hand the complete database structure to the model and then rely on prompts to say what cannot be used. Instead, it should first trim the visible tables, fields, enumerated values, and sample data according to the current user's permissions. Entities, keywords, and business values in a question can be used to retrieve the relevant schema, for example by mapping a region name to its standard value in the database, but the retrieval candidate set itself must already be permission-filtered.

This yields two engineering benefits: it reduces the model's exposure to unauthorized metadata, and it reduces mis-selection caused by irrelevant tables and columns entering the context. Note that hiding field names is no substitute for execution-side permissions. Even if the model has never seen a certain column, a user may still construct a request by guessing fields or functions, so the final boundary must still be enforced by the gateway or the database.

3. Add execution isolation between the model and production databases

The model should not directly hold a high-privilege connection to the production database. A safer approach is to use a read-only identity and hand requests to a controlled query gateway or executor.

Every request should leave a traceable record: the operator's identity, the tenant and data domain, the original question, the schema actually provided to the model, the generated SQL, the permission decision, the execution status, and the returned summary. Logs serve both after-the-fact audit and the diagnosis of problems such as "correct generation but wrong authorization" or "correct permissions but abnormal results." The logs themselves also need access control; audit should not become a reason to widen the exposure of sensitive data.

4. Permission acceptance cannot test only normal queries

The test set should specifically cover unauthorized-access paths: whether a user guessing an unauthorized field is blocked, whether cross-tenant conditions are impossible to satisfy, whether JOINs can be used to bypass row-level restrictions, and whether splitting filters or changing aggregation dimensions leaks information about small groups. You should also verify that when model output contains dangerous statements, unauthorized table names, or external functions, the system rejects them before execution rather than relying on database errors as a fallback.

Whether Text2SQL is ready to go live depends not on how many tables the model "knows," but on whether the system can reliably manage "what the model can see" separately from "what the user can execute," and make the latter an engineering constraint that cannot be bypassed.

4. SQL validation and execution safety: generated does not mean executable

The security boundary of Text2SQL cannot rest on "the database will throw an error." A database can only catch some syntax, object, and type problems. It cannot judge whether a query's metric definition is correct or whether the user should receive the results, and it will not proactively stop a query that is legitimate but excessively expensive. In engineering terms, a chain of required checks should sit in front of the execution entry point; if any check fails, the SQL does not enter the production execution environment.

CheckWhat it determinesHandling on failure
Syntax parsingWhether the statement can be parsed by the target database, and whether the dialect, quoting, and function usage are validEnter a restricted revision flow; direct execution is prohibited
Schema bindingWhether the referenced databases, tables, fields, and aliases exist, and whether field types support the relevant operationsRetrieve metadata again, or ask the user to specify the object
Business semanticsWhether the metric definition exists, time conditions are complete, and aggregated fields are consistent with the grouping logicReturn the definition conflict or missing information; do not let the model guess
Permission decisionWhether the current request may access the data objects involved in the SQL and the final resultsReject the request; do not bypass authorization by rewriting the SQL
Execution riskWhether it includes data writes, Cartesian products, unbounded large-scale reads, or unusually large result setsBlock it outright, or require the query scope to be narrowed
Plan costWhether the scan range, join methods, nesting structure, and estimated resource consumption in the execution plan are acceptableRewrite the query, move to sandbox validation, or stop execution

The most underestimated of these is semantic validation. SQL can run successfully and still answer the wrong question. Multi-table queries in particular require checking whether join paths fall within the scope permitted by governance rules, whether filter conditions apply correctly to the relevant tables, and whether one-to-many joins inflate aggregated results. For count, distinct-count, and ratio metrics, you should also check the denominator scope, deduplication keys, and aggregation granularity to avoid results that are syntactically correct but double-counted.

Risk control should not rely solely on keyword filtering. The system should parse the statement structure and identify write operations, large-table queries lacking effective constraints, Cartesian products, high-cost nesting, and requests that may return excessive data. The execution side should then configure upper limits on timeouts, returned rows, scan size, and number of revisions. Thresholds should be set according to the data platform's capacity and the business scenario, not decided ad hoc by the model.

Before formal execution, you can first obtain the execution plan or do a trial run in an isolated environment. The focus is not "can it run" but which objects it accesses, which join methods it uses, how large a range it is expected to scan, and whether it triggers resource limits. Queries with dynamic SQL, or whose execution plans are hard to assess reliably, should go to a sandbox first rather than being submitted directly to the production database.

A revision agent is well suited to fixing problems that database errors can clearly pinpoint, such as quoting, object names, and field types. It should not loop endlessly on error messages, nor should it change metric definitions, join relationships, or filter scopes on its own. Each round of revision must be subject to a cap on the number of attempts and should retain the SQL version, the reason for the change, the validation result, and the execution status, forming a reviewable process chain.

Failure handling is also a product capability. The system should distinguish between syntax failures, permission denials, unclear definitions, and resource overruns, and tell users what they can do next: add a time or scope condition, choose an explicit metric, reduce the data volume, or hand off to a human analyst. Reliable Text2SQL does not guarantee an executable statement every time; it stops explicitly when it cannot confirm correctness and safety.

5. Metric definition governance: settle "what to query" before "how to write the SQL"

A SQL statement can pass syntax checks and execute normally yet still answer the wrong question. The cause usually lies not in the query itself but in business concepts that have not been made explicit.

Therefore, Text2SQL should not jump directly from the user's question to the database structure. In between, you need a governable layer of metric semantics that translates business concepts into definite calculation constraints. The model should retrieve this layer of definitions first and then select fields and compose the SQL; only when the semantic layer does not cover something should it fall back to inferring from table structures and field descriptions.

Registered contentEngineering purpose
Metric name, business aliases, definitionRecognize how different departments express the same concept, and avoid matching fields by keyword alone
Calculation logic, statistical granularity, time rulesDetermine the aggregation method, grouping dimensions, and date boundaries
Filter constraints, applicable data scopeSpecify whether to exclude canceled orders, test data, or particular business types
Maintainer, definition versionSupport traceability, review, and result explanation after a definition changes

Beyond metric definitions, you also need to handle the gap between natural language and database values. In engineering terms, you can maintain a business glossary, standard enumerations, and field value mappings, then use Schema Linking to connect the terms in a question to candidate tables, fields, and specific values. This reduces model guesswork and ensures that only the database context relevant to the current question is provided.

Multi-table queries also require separate governance of relationships. Two fields sharing a name does not mean they can be joined, and similar names do not prove identical business meaning. The system can maintain confirmed join paths, recording master data identifiers, join direction, cardinality, and how results are deduplicated. When generating SQL, registered paths should take priority; when no trusted relationship can be found, automatic joining should stop and move to clarification or human confirmation, rather than letting the model complete the JOIN based on column names.

The clarification mechanism is part of metric definition governance and should not be treated as an interaction patch. If a question does not specify the date range, aggregation level, or necessary filters, silently applying implicit defaults produces results that look reasonable but are hard to audit. A more reliable pipeline first determines whether the information is sufficient, then asks targeted questions, and only proceeds to SQL generation once the user confirms. Separating clarification from statement generation also makes each easier to test: the former is checked for whether it detects ambiguity, the latter for whether it faithfully executes the confirmed definitions.

When accepting the semantic layer, do not just verify that the SQL runs successfully. A more effective check is to take multiple phrasings of the same business concept and confirm that they resolve to the same version of the metric definition, then run counterexample tests on easily confused rules for time, region, user status, and deduplication. Only when the system can state which definition, which filter conditions, and which join path it used do query results have a basis for review.

6. Go-live acceptance: proving results are reliable with real business questions

Text2SQL acceptance cannot be equated with model benchmark scores. Public datasets are useful for comparing baseline capability, but they cannot cover an enterprise's internal metric definitions, permission boundaries, and data quality issues.

Every question needs a reviewable baseline. The baseline can be a reviewed SQL statement or the expected result on a fixed data snapshot; for business metrics, you also need to record the definition, statistical period, filter conditions, and applicable organization. With only an answer and no definition, it becomes hard to tell, once the data changes, whether a deviation comes from the model, the data, or the business definition. Questions, baselines, and the data versions they depend on should be managed together so that evaluation results remain reproducible.

Every metric should have an explicit denominator and failure classification. For example, SQL rejected by a permission policy should not be counted as a generation error, and a user question that lacks a time range should not be treated as an incorrect result either. Acceptance reports should be broken down by business domain, query complexity, and risk level; otherwise, overall averages will hide areas where the system is unusable.

Robustness testing should actively manufacture anomalies rather than wait for production to expose them. Test inputs can include synonymous phrasings, typos, and missing conditions; on the data side, cover time boundaries, no matching records, and duplicate rows; on the security and runtime side, simulate tenant isolation, unauthorized requests, large-scale scans, and database failures. For every type of failure, verify whether the system rejects, clarifies, degrades, or hands off to a human, and check that the returned messages do not leak table structures or sensitive content.

The scope of the rollout should be determined by acceptance results, not by a single accuracy figure used as a go-live switch. For higher-risk queries, the interface should display the generated SQL, the metric definitions, and the key filter conditions, and add human confirmation before execution or result delivery. Complex reasoning can be improved through methods such as candidate generation, but that cannot replace permission checks, query constraints, and business review.

The acceptance set is not a one-time pre-launch deliverable either. Once in production, continuously collect user-rewritten SQL, negative feedback, failure logs, and recurring clarification requests; after de-identification and review, add them to the evaluation set and update the metric dictionary, reference examples, and validation rules accordingly. Every change to the model, prompts, schema, or definitions should be regression-tested against the same set of core questions to confirm that quality gains have not come at the cost of security or stability.

7. FAQ: common questions before deploying Text2SQL in the enterprise

Should enterprises let a large language model (LLM) connect directly to production databases?

We do not recommend handing database credentials to the model, nor letting it bypass the existing data access system to execute arbitrary SQL directly. The model is well suited to intent parsing, data object matching, and query drafting; the actual access decisions and execution should remain in a controllable service chain.

A safer architecture places an execution layer between the model and the database: the model sees only the metadata needed for the task, while the execution layer processes queries according to the current user's permissions, blocks write operations, unauthorized access, and high-risk statements, and constrains runtime, scan range, and result size. For production environments, whether a query is allowed should also depend on data sensitivity and database capacity, not on the model "thinking this SQL is fine."

During selection, focus on confirming whether the model supports controlled context injection, structured output, and stable tool calling. Single-shot generation quality is only a baseline requirement; it cannot replace access control and execution isolation.

What accuracy does Text2SQL need to reach before going live?

There is no single percentage that applies to every enterprise. Average accuracy in offline tests cannot directly tell you whether the system is ready for a business environment. What enterprises need to determine is which questions the errors occur on, whether the errors can be detected, and what consequences a wrong result would have.

The acceptance set should come from real business questions and retain each question's metric definition, expected result, and acceptable deviation. Evaluation should not just check whether the SQL matches the reference answer; it also needs to check query results, data scope, metric definitions, and permission handling. Two differently written SQL statements may return the same result, and a SQL statement that runs fine may be answering a different question.

Go-live decisions should be made scenario by scenario. Queries with low error costs and easily verifiable answers can be opened up with user confirmation prompts or by showing the basis of the calculation; scenarios involving business decisions, sensitive information, or irreversible operations require stricter human review and refusal mechanisms. The key is not chasing a good-looking overall score but making sure the system knows when it cannot answer.

Why can SQL execute successfully and still return wrong results?

Successful execution only proves that the statement is syntactically valid and that the objects it references are accessible in the current environment; it does not prove that the business question was understood correctly. Common deviations come from metric meaning, time range, data version, and join relationships.

Table joins can also introduce duplicate rows, and null handling, refund status, and historical snapshots can all change the final result.

Therefore, Text2SQL cannot rely on table and field names alone. Enterprises need to organize metric definitions, applicable scope, data sources, and calculation rules into machine-retrievable semantic information, and make the results page able to state which definition the query used. When ambiguity is detected, the system should ask a follow-up question first rather than filling in business assumptions on its own.

Which enterprises are not yet ready to deploy Text2SQL?

If the same metric has long been disputed across departments, core tables lack clear owners, the meaning of fields depends on verbal explanations from a handful of employees, or existing permissions cannot be mapped onto the query execution pipeline, putting Text2SQL straight into production will often just amplify existing data problems.

Another situation calling for caution is when the business side cannot provide representative real questions and no one is responsible for checking the answers. Without an acceptance baseline, the team can only judge whether the generated SQL "looks reasonable" and cannot prove whether the results are usable.

This does not mean comprehensive data governance must be completed first. A more practical approach is to validate within a scope that has clear boundaries, agreed-upon metric definitions, and room for human review; if even such a scope cannot be identified, first fill in the data catalog, metric definitions, permission rules, and ownership. The core deliverable of Text2SQL is not an LLM interface but a data product pipeline that can be constrained, explained, and accepted.