Natural language to SQL statement generation method and device, equipment and medium
Patent Information
- Application Number
- CN202610913881.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-24
- Publication Date
- 2026-10-09
AI Technical Summary
[0004]本发明提供了一种自然语言到SQL语句的生成方法、装置、设备及介质,能够解决现有技术中自然语言到SQL语句的生成的准确性不足的问题
[0027]综上,本申请实施例通过对自然语言查询语句进行解析得到结构化的查询要素,为整个流程提供了精准的语义理解基础,将模糊的用户意图转化为机器可处理的明确信息,从源头上减少了因语言歧义导致的偏差。其次,利用预构建的跨库模式图,根据查询要素推断出模式路由键,该路由键能够将查询范围从全局庞大的数据库模式中快速收敛到最相关的局部子图,如同为后续搜索提供了精确的“语义导航”,从根本上避免了因搜索空间过大而产生的无关干扰和匹配错误,显著提升了后续操作的效率与精准度。进而,将模式路由键输入语言转换模型进行模式链接,该模型通过训练具备了联合理解自然语言与数据库模式的能力,能够输出带有置信度的候选表集合,不仅实现了语义到结构的准确映射,还通过置信度为后续步骤提供了可量化的可靠性依据。最后,基于候选表集合及其链接置信度,采用带权图搜索策略在跨库模式图中推断出最优的连接关系,该方法充分利用了模式图中边权重所蕴含的先验知识,确保了多表连接路径的结构合理性与执行高效性,并最终基于此生成SQL语句,从而在语法、语义和结构三个层面共同保障了生成结果的准确性与可执行。
Smart Images

Figure CN122885010A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of SQL statement generation, and more particularly to a method, apparatus, device, and medium for generating SQL statements from natural language. Background Technology
[0002] As enterprises deepen their digital transformation, business personnel and data analysts have an increasingly urgent need for real-time data exploration and self-service analysis. However, enterprise data environments are typically complex, characterized by multi-source, heterogeneous, and cross-database storage. Directly writing SQL queries requires not only proficiency in professional syntax but also a deep understanding of the business models and relationships scattered across various databases, creating a very high technical barrier. Therefore, automatically converting users' intuitive natural language questions into executable SQL statements has become a key technology for breaking down data access barriers and unlocking data value. It enables business personnel without technical backgrounds to "directly ask about data using natural language," greatly improving the democratization of data analysis and the efficiency of operational decision-making, especially suitable for complex multi-tenant scenarios in finance, government, and energy sectors with stringent requirements for data real-time performance, accuracy, and security compliance.
[0003] Existing technologies mainly follow two paths: one is the rule-based and template-based approach, which uses predefined pattern matching rules and SQL templates for population; the other is the statistical and deep learning-based approach, which uses sequence models such as encoder-decoder to learn mapping relationships from labeled data. However, these methods exhibit significant shortcomings in accuracy, especially in complex enterprise-level multi-source environments. The rule-template approach heavily relies on manual maintenance, has poor scalability, struggles to cover complex semantics such as cross-database joins and nested subqueries, and is extremely sensitive to changes in business definitions and field synonyms. While deep learning methods possess some generalization ability, their "black box" nature often leads to SQL statements with incorrect pattern links, inappropriate join path selection, and invalid syntax and types. Therefore, a technical solution for generating natural language to SQL statements is urgently needed to address these problems. Summary of the Invention
[0004] This invention provides a method, apparatus, device, and medium for generating natural language to SQL statements, which can solve the problem of insufficient accuracy in the generation of natural language to SQL statements in the prior art.
[0005] In a first aspect, embodiments of the present invention provide a method for generating natural language to SQL statements, comprising: Obtain the natural language query statement input by the user, and parse the natural language query statement to obtain the query elements; Based on the query elements, inference is performed in the pre-built cross-database pattern graph to obtain the pattern routing key used to limit the query scope; The pattern routing key is input into the language conversion model to perform pattern linking through the language conversion model, thereby obtaining a candidate table set and the link confidence corresponding to the candidate table set; Based on the candidate table set and the link confidence corresponding to the candidate table set, a weighted graph search strategy is adopted to infer the connection relationship corresponding to the candidate table set in a pre-built cross-database pattern graph, and SQL statements are generated based on the connection relationship.
[0006] This application's embodiments parse natural language queries to obtain structured query elements, providing a precise semantic understanding foundation for the entire process. This transforms ambiguous user intent into clear, machine-processable information, reducing biases caused by language ambiguity at the source. Secondly, utilizing a pre-built cross-database schema graph, a schema routing key is inferred from the query elements. This routing key quickly converges the query scope from the vast global database schema to the most relevant local subgraph, providing precise "semantic navigation" for subsequent searches. This fundamentally avoids irrelevant interference and matching errors caused by an excessively large search space, significantly improving the efficiency and accuracy of subsequent operations. Furthermore, the schema routing key is input into a language conversion model for schema linking. This model, through training, possesses the ability to jointly understand natural language and database schemas, outputting a candidate table set with confidence scores. This not only achieves accurate semantic-to-structural mapping but also provides quantifiable reliability data for subsequent steps through confidence scores. Finally, based on the candidate table set and its link confidence, a weighted graph search strategy is used to infer the optimal connection relationship in the cross-database pattern graph. This method makes full use of the prior knowledge contained in the edge weights in the pattern graph, ensuring the structural rationality and execution efficiency of the multi-table connection path, and finally generating SQL statements based on this, thus ensuring the accuracy and executability of the generated results at the three levels of syntax, semantics and structure.
[0007] As a preferred example of the first aspect, the method for constructing the cross-library schema graph includes: Obtain multi-source databases and perform standardization processing on each database in the multi-source databases to obtain standardized multi-source structured data; The databases, tables, and columns in the standardized multi-source structured data are converted into graph nodes, and structural edges are established between the graph nodes based on the primary and foreign key relationships and cross-table business relationships in the standardized multi-source structured data to generate the cross-database pattern graph.
[0008] In this preferred example, by standardizing the multi-source databases, the original databases with different sources and inconsistent naming and structures are mapped to a standard structural system with unified business semantics. This fundamentally eliminates semantic ambiguity and matching errors caused by "synonyms with different names" or "inconsistent unit definitions", providing a stable and consistent reference benchmark for all subsequent semantic understanding and structural alignment steps.
[0009] Building upon this foundation, the standardized databases, tables, and columns are converted into graph nodes, and structural edges are established based on primary and foreign key relationships and business relationships, thereby constructing a global cross-database schema graph. This step transforms the discrete, static data table structure into a dynamic graph containing rich connection semantics and topological relationships. This graph not only explicitly depicts the physical connections between tables established through primary and foreign keys, but also incorporates domain knowledge through business relationship edges, enabling the system to "see" the inherent connections between data.
[0010] As a preferred example of the first aspect, the method for constructing the language conversion model includes: A search sample set is constructed based on the multi-source database, and a pattern subgraph corresponding to each sample is extracted from the cross-database pattern graph based on the search sample set, so as to construct a training sample set based on each pattern subgraph. An initial language conversion model is obtained. Based on the training sample set, the parameters of the encoder and decoder in the initial language conversion model are iteratively updated using a gradient descent algorithm until the parameters of the current iteration meet a first preset condition or the number of current iterations reaches a preset number of rounds. The language conversion model is then output.
[0011] In this preferred example, a training sample set is constructed by extracting the pattern subgraphs corresponding to each question-SQL sample from a cross-database pattern graph. This ensures that each training sample simultaneously contains a natural language question, the target SQL, and the precisely associated local database structure (pattern subgraph). This approach allows the model to "see" the real data environment referred to by the question during the training phase, including the relevant tables, columns, and their connections. Secondly, an encoder-decoder method is used for training. The encoder jointly encodes the natural language text and the pattern subgraph, achieving a deep fusion and unified representation of semantic and structural information. The decoder decodes under the constraints of SQL grammar production rules and column types. This "syntax and type constraint decoding" mechanism forces the model to adhere to the SQL syntax rules and the database type system at each generation step, ensuring that the model outputs not arbitrary word sequences but syntactically correct and type-safe SQL skeletons, effectively preventing the "readable but unexecutable" illegal SQL often generated by existing pure data-driven models. Finally, gradient descent algorithms are used to iteratively optimize the model parameters until convergence, enabling the model to learn robust mapping rules from complex natural language expressions to precise SQL structures through a large number of samples. This process causes the model's internal parameters to eventually adjust to a state that enables it to generate SQL statements that are both semantically matching and structurally valid, based on the encoded pattern knowledge and guided by grammatical and type constraints, when faced with new and unseen natural language queries.
[0012] As a preferred example of the first aspect, parsing the natural language query statement to obtain query elements includes: The natural language query statement is sequentially segmented into words to obtain segmentation results, and the segmentation results are parsed to obtain structured data; Based on a preset rule base, the structured data is classified into question types to obtain the query intent, and business entity words are extracted from the structured data to obtain business domain information. Then, the query elements are obtained based on the query intent and the business domain information.
[0013] In this preferred example, a precise semantic understanding foundation is built by progressively parsing natural language queries, thereby improving the accuracy of subsequent SQL generation. Specifically, firstly, word segmentation and parsing transform unstructured statements into structured data containing key components, eliminating the ambiguity of the original expression. Next, question type classification quickly determines the core operational intent of the query, while business entity extraction identifies the specific business objects involved in the query, thus clarifying its business domain. Finally, the intent and domain information are integrated into structured query elements, providing clear and unambiguous semantic input for subsequent steps, avoiding pattern linking and SQL generation errors caused by misunderstandings from the outset.
[0014] As a preferred example of the first aspect, the step of inferring a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph includes: Based on a preset business dictionary, the matching score corresponding to each business domain in the business domain information is calculated using a literal matching scoring method and a contextual semantic matching scoring method. Based on the matching scores corresponding to each business domain, candidate business domains and candidate source domains are determined from the business dictionary, and a search is performed in the cross-database pattern graph starting from the candidate business domains and the candidate source clusters to determine candidate subgraphs; The candidate service domain, the candidate source cluster, and the candidate subgraph are combined to generate the pattern routing key.
[0015] In this preferred example, a precise "pattern routing key" inference mechanism constructs efficient and accurate search boundaries for subsequent steps, significantly improving generation quality. First, through literal and semantic dual-dimensional matching, the relevance of the query to each business domain is accurately calculated, ensuring a deep understanding of user intent and overcoming ambiguity caused by synonyms and differences in expression. Second, based on the matching results, specific candidate business domains, data source clusters, and their corresponding pattern subgraphs are quickly located, converging the global query to a highly relevant local data view, avoiding interference and resource consumption caused by global searches. Finally, this information is encapsulated into a structured pattern routing key, providing clear navigation instructions for subsequent pattern linking and JOIN inference, ensuring that all subsequent complex operations are performed within the correct semantic and structural scope, thereby reducing the risk of connection errors and field mislinking from the source and guaranteeing the accuracy and reliability of SQL generation.
[0016] As a preferred example of the first aspect, the step of inferring the connection relationships corresponding to the candidate table set based on the candidate table set, the link confidence of the candidate table set, and the link confidence of the candidate table set, using a weighted graph search strategy, inferring the connection relationships corresponding to the candidate table set in a pre-constructed cross-database pattern graph, includes: Based on the candidate table set, local candidate subgraphs are determined, and a weighted graph search strategy is used to enumerate the local candidate subgraphs to obtain several connection relationships; For each connection, the structural cost and semantic coverage score corresponding to each connection are calculated, and the comprehensive score corresponding to each connection is calculated based on the structural cost and the semantic coverage score. The connection with the highest overall score is selected as the connection corresponding to the candidate table set.
[0017] In this preferred example, optimal inference of multi-table join relationships is achieved through weighted graph search and multi-dimensional evaluation. First, enumeration is performed on local candidate subgraphs to focus the search on highly relevant regions, avoiding the inefficiency and ambiguity of global searches. Second, structural cost and semantic coverage scores are calculated comprehensively. Structural cost tends to select concise and efficient join paths, while semantic coverage ensures that the joins fully reflect the query intent; this combination overcomes the limitations of a single metric. Finally, the join relationship with the highest comprehensive score is selected, achieving the optimal balance between join efficiency and semantic fidelity. This provides a reliable join framework for SQL generation that conforms to business semantics and has good execution performance, fundamentally avoiding query result deviations caused by incorrect join paths.
[0018] As a preferred example of the first aspect, generating SQL statements based on the connection relationship includes: Based on the connection relationship, several candidate SQL statements are determined; A multi-index rearrangement method is used to sort the candidate SQL statements to obtain the sorting results; The SQL statement is obtained by filtering from the sorting results.
[0019] In this preferred example, multiple candidate SQL statements are generated and reordered based on multiple metrics to ensure that the final output SQL is globally optimal. First, multiple candidates are generated based on accurate intermediate results, avoiding local optima or accidental errors that might result from a single output path. Second, by comprehensively evaluating multiple dimensions such as semantic matching, structural coverage, executability, and execution cost, the candidates are quantitatively scored and ranked, thereby intelligently selecting the SQL that best matches the user's intent, has the most reasonable structure, and executes efficiently, ultimately significantly improving the accuracy and usability of the generated results.
[0020] Secondly, the present invention provides a natural language to SQL statement generation apparatus, comprising: a data acquisition module, a first processing module, a second processing module, and a result generation module; The data acquisition module is used to acquire the natural language query statement input by the user, and parse the natural language query statement to obtain the query elements; The first processing module is used to infer a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph. The second processing module is used to input the pattern routing key into the language conversion model, so as to perform pattern linking through the language conversion model to obtain a candidate table set and the link confidence corresponding to the candidate table set; The result generation module is used to infer the connection relationships corresponding to the candidate table set based on the candidate table set and the link confidence corresponding to the candidate table set using a weighted graph search strategy in a pre-built cross-database pattern graph, and generate SQL statements based on the connection relationships.
[0021] As a preferred example of the second aspect, the method for constructing the cross-library schema graph includes: Obtain multi-source databases and perform standardization processing on each database in the multi-source databases to obtain standardized multi-source structured data; The databases, tables, and columns in the standardized multi-source structured data are converted into graph nodes, and structural edges are established between the graph nodes based on the primary and foreign key relationships and cross-table business relationships in the standardized multi-source structured data to generate the cross-database pattern graph.
[0022] As a preferred example of the second aspect, the method for constructing the cross-library pattern graph and the method for constructing the language conversion model include: A search sample set is constructed based on the multi-source database, and a pattern subgraph corresponding to each sample is extracted from the cross-database pattern graph based on the search sample set, so as to construct a training sample set based on each pattern subgraph. An initial language conversion model is obtained. Based on the training sample set, the parameters of the encoder and decoder in the initial language conversion model are iteratively updated using a gradient descent algorithm until the parameters of the current iteration meet a first preset condition or the number of current iterations reaches a preset number of rounds. The language conversion model is then output.
[0023] As a preferred example of the second aspect, the parsing of the natural language query statement to obtain query elements includes: The natural language query statement is sequentially segmented into words to obtain segmentation results, and the segmentation results are parsed to obtain structured data; Based on a preset rule base, the structured data is classified into question types to obtain the query intent, and business entity words are extracted from the structured data to obtain business domain information. Then, the query elements are obtained based on the query intent and the business domain information.
[0024] As a preferred example of the second aspect, the step of inferring a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph includes: Based on a preset business dictionary, the matching score corresponding to each business domain in the business domain information is calculated using a literal matching scoring method and a contextual semantic matching scoring method. Based on the matching scores corresponding to each business domain, candidate business domains and candidate source domains are determined from the business dictionary. Starting from the candidate business domains and candidate source clusters, a search is performed in the cross-database pattern graph to determine candidate subgraphs. The candidate business domains, candidate source clusters, and candidate subgraphs are combined to generate the pattern routing key.
[0025] As a preferred example of the second aspect, the step of inferring the connection relationships corresponding to the candidate table set by employing a weighted graph search strategy based on the candidate table set and the link confidence corresponding to the candidate table set, within a pre-constructed cross-database pattern graph, includes: Based on the candidate table set, local candidate subgraphs are determined, and a weighted graph search strategy is used to enumerate the local candidate subgraphs to obtain several connection relationships; For each connection, the structural cost and semantic coverage score corresponding to each connection are calculated, and the comprehensive score corresponding to each connection is calculated based on the structural cost and the semantic coverage score. The connection with the highest overall score is selected as the connection corresponding to the candidate table set.
[0026] As a preferred example of the second aspect, generating SQL statements based on the connection relationship includes: Based on the connection relationship, several candidate SQL statements are determined; A multi-index rearrangement method is used to sort the candidate SQL statements to obtain the sorting results; The SQL statement is obtained by filtering from the sorting results.
[0027] In summary, this embodiment of the application parses natural language query statements to obtain structured query elements, providing a precise semantic understanding foundation for the entire process. It transforms ambiguous user intent into clear information that machines can process, reducing biases caused by language ambiguity at the source. Secondly, utilizing a pre-built cross-database pattern graph, a pattern routing key is inferred from the query elements. This routing key can quickly converge the query scope from the vast global database pattern to the most relevant local subgraph, providing precise "semantic navigation" for subsequent searches. This fundamentally avoids irrelevant interference and matching errors caused by an excessively large search space, significantly improving the efficiency and accuracy of subsequent operations. Furthermore, the pattern routing key is input into a language conversion model for pattern linking. This model, through training, possesses the ability to jointly understand natural language and database patterns, outputting a candidate table set with confidence scores. This not only achieves accurate semantic-to-structural mapping but also provides quantifiable reliability data for subsequent steps through confidence scores. Finally, based on the candidate table set and its link confidence, a weighted graph search strategy is used to infer the optimal connection relationship in the cross-database pattern graph. This method makes full use of the prior knowledge contained in the edge weights in the pattern graph, ensuring the structural rationality and execution efficiency of the multi-table connection path, and finally generating SQL statements based on this, thus ensuring the accuracy and executability of the generated results at the three levels of syntax, semantics and structure.
[0028] Another embodiment of the present invention provides a terminal device, including: a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, it implements the steps of the natural language to SQL statement generation method of the present invention.
[0029] Another embodiment of the present invention also provides a computer-readable storage medium item, including: a stored computer program, which, when the computer program is running, controls the device where the computer-readable storage medium is located to perform steps such as the natural language to SQL statement generation method of the present invention. Attached Figure Description
[0030] To more clearly illustrate the technical solution of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0031] Figure 1 This is a flowchart illustrating an embodiment of a method for generating natural language to SQL statements provided by the present invention. Figure 2This is a module structure diagram of one embodiment of a natural language to SQL statement generation device provided by the present invention. Detailed Implementation
[0032] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings of the embodiments. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0033] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.
[0034] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more, unless otherwise explicitly defined.
[0035] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.
[0036] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.
[0037] In the description of the embodiments of this application, the term "multiple" refers to two or more (including two), similarly, "multiple sets" refers to two or more (including two sets), and "multiple pieces" refers to two or more (including two pieces).
[0038] In the description of the embodiments of this application, unless otherwise expressly specified and limited, technical terms such as "installation," "connection," "joining," and "fixing" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. For those skilled in the art, the specific meaning of the above terms in the embodiments of this application can be understood according to the specific circumstances.
[0039] Example 1 See Figure 1 To address the issue of insufficient accuracy in generating natural language to SQL statements in existing technologies, an embodiment of the present invention provides a method for generating natural language to SQL statements, comprising: S1. Obtain the natural language query statement input by the user, and parse the natural language query statement to obtain the query elements; In a preferred embodiment, parsing the natural language query statement to obtain query elements includes: The natural language query statement is sequentially segmented into words to obtain segmentation results, and the segmentation results are parsed to obtain structured data; Based on a preset rule base, the structured data is classified into question types to obtain the query intent, and business entity words are extracted from the structured data to obtain business domain information. Then, the query elements are obtained based on the query intent and the business domain information.
[0040] As a preferred implementation, the step of inferring a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph includes: Based on a preset business dictionary, the matching score corresponding to each business domain in the business domain information is calculated using a literal matching scoring method and a contextual semantic matching scoring method. Based on the matching scores corresponding to each business domain, candidate business domains and candidate source domains are determined from the business dictionary, and a search is performed in the cross-database pattern graph starting from the candidate business domains and the candidate source clusters to determine candidate subgraphs; The candidate service domain, the candidate source cluster, and the candidate subgraph are combined to generate the pattern routing key.
[0041] Specifically, the process of obtaining the mode routing key can be implemented in the following preferred manner: After receiving the user's natural language, the system first performs word segmentation, named entity parsing, and time / unit parsing, converting phrases like "last 30 days," "last month," "dollars," and "don't want a certain field" into structured signals. Then, lightweight intent recognition is performed. Lightweight intent recognition refers to using rules and small-scale classification models to perform coarse-grained question type discrimination (such as statistics, filtering, Top-K, comparison, trend, etc.) and extract core business objects (such as stores, orders, customers, products, etc.). This recognition process does not rely on large-scale deep learning models, has low computational overhead, and manageable latency. Based on these clues—including parsed business keywords, time / spatial range, lightweight intent tags, and user historical routing preferences—the system infers the relevant business domain / source cluster in the cross-database pattern graph and generates a pattern routing key. This limits subsequent searches to a small portion of the graph most semantically relevant to the current query, thereby reducing ambiguity and improving efficiency. Specifically, firstly, based on a pre-built business dictionary and field synonym table, text matching scores and historical usage scores are calculated for each business domain and its subordinate source clusters. For example, the number of times business terms appearing in the question match in the description text of each business domain, the BM25 retrieval score, and the user's historical query frequency in that business domain are counted to obtain a comprehensive score for each business domain and source cluster. The business domain with the highest score and one or more source clusters are selected as the candidate execution scope for this query. Then, within the selected source clusters, based on field names, aliases, and business tags, several starting table nodes most similar to the entities / measures in the question are selected. Several hops are then extended outward from these nodes on the cross-database schema graph, incorporating the primary and foreign key adjacency tables and commonly used business relationship tables, resulting in a candidate subgraph corresponding to this query. The aforementioned business domains, source clusters, and candidate starting table set together constitute the schema routing key for this query, limiting subsequent JOIN path searches and restricted decoding to this subgraph, thereby reducing ambiguity and improving efficiency.
[0042] S2. Based on the query elements, inference is made in the pre-built cross-database pattern graph to obtain the pattern routing key used to limit the query scope; As a preferred embodiment, the method for constructing the cross-library schema graph includes: Obtain multi-source databases and perform standardization processing on each database in the multi-source databases to obtain standardized multi-source structured data; The databases, tables, and columns in the standardized multi-source structured data are converted into graph nodes, and structural edges are established between the graph nodes based on the primary and foreign key relationships and cross-table business relationships in the standardized multi-source structured data to generate the cross-database pattern graph.
[0043] Specifically, the multi-source database refers to a collection of multiple physical instances / tenant databases / business databases within an enterprise. The system takes this "multi-source database set" as input and extracts the databases, tables, columns, primary and foreign keys, data types, and a small number of representative sample values from each source. This heterogeneous structural information is then standardized under a single set of "standard names" and "standard definitions." In simpler terms, this step is like labeling each data source with a unified tag: fields with the same business meaning but different spellings are uniformly recognized as having "the same meaning," while remembering their original names in their respective sources. To avoid ambiguity caused by "one meaning, multiple spellings," the system manages business synonyms and units of measurement uniformly. For example, historically, some people have written "sales amount" as "order_amt," while others have written it as "amount" or "amount." The system uniformly maps these to the standard business name "sales amount" and retains a reversible mapping. This way, when generating SQL, it can understand the semantics based on "sales amount" while accurately returning to the actual column names in the specific source. The same applies to units / calibers: amounts, times, lengths, etc., will be converted to the base unit before comparison or filtering. Exchange rates and caliber tables can be updated daily to ensure comparability of data from different sources.
[0044] Specifically, the method for constructing the cross-database schema diagram can be implemented in the following preferred manner: ① The standardized "database-table-column" list is abstracted into database nodes, table nodes, and column nodes in the graph, and each node is assigned a globally unique identifier. The node attributes record information such as its business domain, source cluster identifier, naming convention, and data type, forming a standard node layer for the cross-database pattern graph. In the specific implementation, the cross-database pattern graph can be stored using an attribute graph data structure, for example, maintaining nodes and their inbound and outbound edge sets in the form of an adjacency list. Each edge records the edge type, source node identifier, target node identifier, and edge weight fields.
[0045] ② For each specific data source, based on the mapping relationship between "standard name and real name within the source", bind the physical library name, table name, and column name within the source to the corresponding standard library node, standard table node, and standard column node. The binding method can be to attach multiple "physical view" references to the standard node, or to introduce additional physical nodes into the graph and connect them to the corresponding standard nodes through "mapping edges", thereby explicitly representing "different physical implementations of a business meaning in multiple sources" in the graph structure.
[0046] ③ Within each data source, based on the extracted primary / foreign key constraints and pre-configured business relationships such as fact table – dimension table, master data table – business table, directional edges are established between database nodes, table nodes, and column nodes to obtain the structure subgraph of each source. For combinations of standard nodes and physical nodes, the system can simultaneously maintain "standard layer primary and foreign key edges" and "physical layer primary and foreign key edges" so that subsequent path searches can both reason at the standard layer and accurately locate specific sources.
[0047] ④ Based on the constructed field thesaurus, business domain labels, and unit / caliber information, "synonymous edges" and "convertible edges" are established between semantically equivalent or commutative column nodes in different data sources. To support subsequent join path search and cost estimation, the system statistically analyzes the three-element features of each column node in the cross-database schema graph, including its type, enumeration, and numerical range, and records statistics such as column cardinality, null value rate, and typical selectivity. Simultaneously, it records historical JOIN frequency, historical execution success rate, and typical execution cost on primary / foreign key edges, business relationship edges, and synonym / mapping edges. These statistics are encoded as edge weights or penalty factors to prioritize the "correct and efficient" join scheme among multiple feasible paths.
[0048] S3. Input the pattern routing key into the language conversion model to perform pattern linking through the language conversion model, and obtain a candidate table set and the link confidence corresponding to the candidate table set; Specifically, the step of inputting the pattern routing key into the language conversion model to perform pattern linking through the language conversion model to obtain a candidate table set and the link confidence corresponding to the candidate table set can be implemented in the following preferred manner: The scoring of candidate tables / columns can be broken down into three components: character similarity score, retrieval score, and contextual attention score. For a given candidate column... The entity phrase in the corresponding question is denoted as The system first calculates character similarity scores. For example, based on edit distance, n-gram overlap, or pinyin similarity, The similarity between the column name, alias, and business tag is normalized to the [0,1] interval; then the retrieval score is calculated. The column name, annotation, and example value are concatenated into a "column description text." Using an inverted index or vector retrieval (such as BM25 or embedding similarity), the entire question is treated as a query, and all candidate columns are retrieved to obtain corresponding relevance scores, which are also normalized to the [0,1] interval. Simultaneously, a language transformation model is used to extract the attention weights of the column nodes from the cross-attention matrix between the question token and the pattern graph node tokens. These weights are then aggregated according to the maximum or average value to obtain the contextual attention score. .
[0049] After obtaining the scores of the three components, a linear weighted average can be used to construct the comprehensive score of the candidate columns: in The weight coefficients are non-negative and can satisfy the following conditions: , by optimizing performance on the validation set.
[0050] As a preferred embodiment, the method for constructing the language conversion model includes: A search sample set is constructed based on the multi-source database, and a pattern subgraph corresponding to each sample is extracted from the cross-database pattern graph based on the search sample set, so as to construct a training sample set based on each pattern subgraph. An initial language conversion model is obtained. Based on the training sample set, the parameters of the encoder and decoder in the initial language conversion model are iteratively updated using a gradient descent algorithm until the parameters of the current iteration meet a first preset condition or the number of current iterations reaches a preset number of rounds. The language conversion model is then output.
[0051] Specifically, the method for constructing the language conversion model can be implemented in the following preferred manner; ① The encoder consists of a text encoding subnetwork and a pattern graph encoding subnetwork. The text encoding subnetwork maps the token sequence obtained after question segmentation into a series of context vectors; the pattern graph encoding subnetwork encodes the extracted pattern subgraph, which can use graph neural networks or graph-aware Transformers to integrate features such as the name token of the library / table / column node, column type one-hot encoding, and business domain label into node embedding vectors. Subsequently, through cross-attention layers or feature concatenation, the question representation and the pattern node representation are jointly modeled, enabling the model to perceive the "position of entities and constraints mentioned in the question on the pattern graph" during the encoding stage, providing context for subsequent column selection, connection paths, and operator generation.
[0052] ② The decoder adopts a sequence-to-sequence structure and uses teacher forcing during the training phase, with standardized SQL as the target sequence. Unlike ordinary decoding, this invention maintains an SQL grammar stack at each decoding step, dynamically generating a "set of available production rules" based on the non-terminal symbol at the top of the stack. The model is only allowed to select from the tokens corresponding to these production rules. For positions involving column names, table names, and constants, the candidate set is further constrained using a pattern subgraph. For example, when the current position requires a numeric column, only numeric column nodes are opened as candidates; when the current position requires a time column, only date / time column nodes are opened as candidates. Through the dual constraints of the syntax stack and type mask, the model is forced to learn "syntactically correct and type-consistent" SQL generation behavior during the training phase.
[0053] ③ In addition to calculating the conventional cross-entropy loss, this embodiment introduces executability constraints. Specifically, the intermediate SQL draft or top-k candidates generated by the decoder under the teacher's guidance can be fed into a lightweight checking engine for syntax analysis, type checking, and simple data reachability detection.
[0054] ④ Driven by the above loss function, gradient descent algorithms (such as Adam) are used to iteratively update the parameters of the encoder and decoder until the performance on the validation set converges or the preset number of rounds is reached.
[0055] S4. Based on the candidate table set and the link confidence corresponding to the candidate table set, a weighted graph search strategy is adopted to infer the connection relationship corresponding to the candidate table set in the pre-built cross-database pattern graph, and SQL statements are generated based on the connection relationship.
[0056] As a preferred implementation, the step of using a weighted graph search strategy to infer the connection relationships corresponding to the candidate table set based on the candidate table set and the link confidence corresponding to the candidate table set, in a pre-constructed cross-database pattern graph, includes: Based on the candidate table set, local candidate subgraphs are determined, and a weighted graph search strategy is used to enumerate the local candidate subgraphs to obtain several connection relationships; For each connection, the structural cost and semantic coverage score corresponding to each connection are calculated, and the comprehensive score corresponding to each connection is calculated based on the structural cost and the semantic coverage score. The connection with the highest overall score is selected as the connection corresponding to the candidate table set.
[0057] In a preferred embodiment, generating SQL statements based on the connection relationship includes: Based on the connection relationship, several candidate SQL statements are determined; A multi-index rearrangement method is used to sort the candidate SQL statements to obtain the sorting results; The SQL statement is obtained by filtering from the sorting results.
[0058] In summary, this embodiment of the application parses natural language query statements to obtain structured query elements, providing a precise semantic understanding foundation for the entire process. It transforms ambiguous user intent into clear information that machines can process, reducing biases caused by language ambiguity at the source. Secondly, utilizing a pre-built cross-database pattern graph, a pattern routing key is inferred from the query elements. This routing key can quickly converge the query scope from the vast global database pattern to the most relevant local subgraph, providing precise "semantic navigation" for subsequent searches. This fundamentally avoids irrelevant interference and matching errors caused by an excessively large search space, significantly improving the efficiency and accuracy of subsequent operations. Furthermore, the pattern routing key is input into a language conversion model for pattern linking. This model, through training, possesses the ability to jointly understand natural language and database patterns, outputting a candidate table set with confidence scores. This not only achieves accurate semantic-to-structural mapping but also provides quantifiable reliability data for subsequent steps through confidence scores. Finally, based on the candidate table set and its link confidence, a weighted graph search strategy is used to infer the optimal connection relationship in the cross-database pattern graph. This method makes full use of the prior knowledge contained in the edge weights in the pattern graph, ensuring the structural rationality and execution efficiency of the multi-table connection path, and finally generating SQL statements based on this, thus ensuring the accuracy and executability of the generated results at the three levels of syntax, semantics and structure.
[0059] Example 2 like Figure 2 As shown, based on the above method embodiments, corresponding device embodiments are provided; An embodiment of the present invention provides a natural language to SQL statement generation device, including: a data acquisition module 21, a first processing module 22, a second processing module 23, and a result generation module 24; The data acquisition module 21 is used to acquire the natural language query statement input by the user and parse the natural language query statement to obtain the query elements; The first processing module 22 is used to infer from the pre-built cross-database pattern graph based on the query elements to obtain a pattern routing key for limiting the query scope; The second processing module 23 is used to input the pattern routing key into the language conversion model so as to perform pattern linking through the language conversion model and obtain a candidate table set and the link confidence corresponding to the candidate table set. The result generation module 24 is used to infer the connection relationships corresponding to the candidate table set based on the candidate table set and the link confidence corresponding to the candidate table set using a weighted graph search strategy in a pre-built cross-database pattern graph, and generate SQL statements based on the connection relationships.
[0060] As a preferred embodiment, the method for constructing the cross-library schema graph includes: Obtain multi-source databases and perform standardization processing on each database in the multi-source databases to obtain standardized multi-source structured data; The databases, tables, and columns in the standardized multi-source structured data are converted into graph nodes, and structural edges are established between the graph nodes based on the primary and foreign key relationships and cross-table business relationships in the standardized multi-source structured data to generate the cross-database pattern graph.
[0061] As a preferred embodiment, the method for constructing the cross-library schema graph includes: A search sample set is constructed based on the multi-source database, and a pattern subgraph corresponding to each sample is extracted from the cross-database pattern graph based on the search sample set, so as to construct a training sample set based on each pattern subgraph. An initial language conversion model is obtained. Based on the training sample set, the parameters of the encoder and decoder in the initial language conversion model are iteratively updated using a gradient descent algorithm until the parameters of the current iteration meet a first preset condition or the number of current iterations reaches a preset number of rounds. The language conversion model is then output.
[0062] In a preferred embodiment, parsing the natural language query statement to obtain query elements includes: The natural language query statement is sequentially segmented into words to obtain segmentation results, and the segmentation results are parsed to obtain structured data; Based on a preset rule base, the structured data is classified into question types to obtain the query intent, and business entity words are extracted from the structured data to obtain business domain information. Then, the query elements are obtained based on the query intent and the business domain information.
[0063] As a preferred implementation, the step of inferring a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph includes: Based on a preset business dictionary, the matching score corresponding to each business domain in the business domain information is calculated using a literal matching scoring method and a contextual semantic matching scoring method. Based on the matching scores corresponding to each business domain, candidate business domains and candidate source domains are determined from the business dictionary, and a search is performed in the cross-database pattern graph starting from the candidate business domains and the candidate source clusters to determine candidate subgraphs; The candidate service domain, the candidate source cluster, and the candidate subgraph are combined to generate the pattern routing key.
[0064] As a preferred implementation, the step of using a weighted graph search strategy to infer the connection relationships corresponding to the candidate table set based on the candidate table set and the link confidence corresponding to the candidate table set, in a pre-constructed cross-database pattern graph, includes: Based on the candidate table set, local candidate subgraphs are determined, and a weighted graph search strategy is used to enumerate the local candidate subgraphs to obtain several connection relationships; For each connection, the structural cost and semantic coverage score corresponding to each connection are calculated, and the comprehensive score corresponding to each connection is calculated based on the structural cost and the semantic coverage score. The connection with the highest overall score is selected as the connection corresponding to the candidate table set.
[0065] In a preferred embodiment, generating SQL statements based on the connection relationship includes: Based on the connection relationship, several candidate SQL statements are determined; A multi-index rearrangement method is used to sort the candidate SQL statements to obtain the sorting results; The SQL statement is obtained by filtering from the sorting results.
[0066] In summary, this embodiment of the application parses natural language query statements to obtain structured query elements, providing a precise semantic understanding foundation for the entire process. It transforms ambiguous user intent into clear information that machines can process, reducing biases caused by language ambiguity at the source. Secondly, utilizing a pre-built cross-database pattern graph, a pattern routing key is inferred from the query elements. This routing key can quickly converge the query scope from the vast global database pattern to the most relevant local subgraph, providing precise "semantic navigation" for subsequent searches. This fundamentally avoids irrelevant interference and matching errors caused by an excessively large search space, significantly improving the efficiency and accuracy of subsequent operations. Furthermore, the pattern routing key is input into a language conversion model for pattern linking. This model, through training, possesses the ability to jointly understand natural language and database patterns, outputting a candidate table set with confidence scores. This not only achieves accurate semantic-to-structural mapping but also provides quantifiable reliability data for subsequent steps through confidence scores. Finally, based on the candidate table set and its link confidence, a weighted graph search strategy is used to infer the optimal connection relationship in the cross-database pattern graph. This method makes full use of the prior knowledge contained in the edge weights in the pattern graph, ensuring the structural rationality and execution efficiency of the multi-table connection path, and finally generating SQL statements based on this, thus ensuring the accuracy and executability of the generated results at the three levels of syntax, semantics and structure.
[0067] It is understood that the above-described device embodiments correspond to the method embodiments of the present invention, and can implement the method for generating natural language to SQL statements provided by any of the above-described method embodiments of the present invention.
[0068] It should be noted that the device embodiments described above are merely illustrative, and some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, in the accompanying drawings of the device embodiments provided by this invention, the connection relationships between modules indicate that they have communication connections, which can specifically be implemented as one or more communication buses or signal lines. Those skilled in the art can understand and implement this without any creative effort.
[0069] Example 3 Based on the above embodiments of the natural language to SQL statement generation method, another embodiment of the present invention provides a terminal device, which includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, it implements the natural language to SQL statement generation method of any embodiment of the present invention.
[0070] For example, in this embodiment, the computer program can be divided into one or more modules, which are stored in the memory and executed by the processor to complete the present invention. The one or more modules may be a series of computer program instruction segments capable of performing a specific function, which describe the execution process of the computer program in the terminal device.
[0071] The terminal device may be a desktop computer, laptop, handheld computer, or cloud server, etc. The terminal device may include, but is not limited to, a processor and a memory.
[0072] The processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor can be a microprocessor or any conventional processor. The processor is the control center of the terminal device, connecting all parts of the terminal device via various interfaces and lines.
[0073] Example 4 Based on the above-described method embodiments, another embodiment of the present invention provides a computer-readable storage medium including a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to execute the natural language to SQL statement generation method described in any of the above-described method embodiments of the present invention.
[0074] The modules / units integrated in the device / terminal equipment, if implemented as software functional units and sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium, etc.
[0075] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.
Claims
1. A method for generating natural language to SQL statements, characterized in that, include: Obtain the natural language query statement input by the user, and parse the natural language query statement to obtain the query elements; Based on the query elements, inference is performed in the pre-built cross-database pattern graph to obtain the pattern routing key used to limit the query scope; The pattern routing key is input into the language conversion model to perform pattern linking through the language conversion model, thereby obtaining a candidate table set and the link confidence corresponding to the candidate table set; Based on the candidate table set and the link confidence corresponding to the candidate table set, a weighted graph search strategy is adopted to infer the connection relationship corresponding to the candidate table set in a pre-built cross-database pattern graph, and SQL statements are generated based on the connection relationship.
2. The method for generating natural language to SQL as described in claim 1, characterized in that, The method for constructing the cross-database schema graph includes: Obtain multi-source databases and perform standardization processing on each database in the multi-source databases to obtain standardized multi-source structured data; The databases, tables, and columns in the standardized multi-source structured data are converted into graph nodes, and structural edges are established between the graph nodes based on the primary and foreign key relationships and cross-table business relationships in the standardized multi-source structured data to generate the cross-database pattern graph.
3. The method for generating natural language to SQL as described in claim 2, characterized in that, The method for constructing the language conversion model includes: A search sample set is constructed based on the multi-source database, and a pattern subgraph corresponding to each sample is extracted from the cross-database pattern graph based on the search sample set, so as to construct a training sample set based on each pattern subgraph. An initial language conversion model is obtained. Based on the training sample set, the parameters of the encoder and decoder in the initial language conversion model are iteratively updated using a gradient descent algorithm until the parameters of the current iteration meet a first preset condition or the number of current iterations reaches a preset number of rounds. The language conversion model is then output.
4. The method for generating natural language to SQL as described in claim 1, characterized in that, The parsing of the natural language query statement to obtain query elements includes: The natural language query statement is sequentially segmented into words to obtain segmentation results, and the segmentation results are parsed to obtain structured data; Based on a preset rule base, the structured data is classified into question types to obtain the query intent, and business entity words are extracted from the structured data to obtain business domain information. Then, the query elements are obtained based on the query intent and the business domain information.
5. The method for generating natural language to SQL as described in claim 4, characterized in that, The step of inferring a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph includes: Based on a preset business dictionary, the matching score corresponding to each business domain in the business domain information is calculated using a literal matching scoring method and a contextual semantic matching scoring method. Based on the matching scores corresponding to each business domain, candidate business domains and candidate source domains are determined from the business dictionary. Starting from the candidate business domains and candidate source clusters, a search is performed in the cross-database pattern graph to determine candidate subgraphs. The candidate business domains, candidate source clusters, and candidate subgraphs are combined to generate the pattern routing key.
6. The method for generating natural language to SQL as described in claim 1, characterized in that, The step of inferring the connection relationships corresponding to the candidate table set by using a weighted graph search strategy based on the candidate table set and the link confidence corresponding to the candidate table set, within a pre-constructed cross-database pattern graph, includes: Based on the candidate table set, local candidate subgraphs are determined, and a weighted graph search strategy is used to enumerate the local candidate subgraphs to obtain several connection relationships; For each connection, the structural cost and semantic coverage score corresponding to each connection are calculated, and the comprehensive score corresponding to each connection is calculated based on the structural cost and the semantic coverage score. The connection with the highest overall score is selected as the connection corresponding to the candidate table set.
7. A method for generating natural language to SQL as described in any one of claims 1-6, characterized in that, The step of generating SQL statements based on the connection relationship includes: Based on the connection relationship, several candidate SQL statements are determined; A multi-index rearrangement method is used to sort the candidate SQL statements to obtain the sorting results; The SQL statement is obtained by filtering from the sorting results.
8. A device for generating natural language to SQL statements, characterized in that, include: The system comprises a data acquisition module, a first processing module, a second processing module, and a result generation module. The data acquisition module is used to acquire the natural language query statement input by the user, and parse the natural language query statement to obtain the query elements; The first processing module is used to infer a pattern routing key for limiting the query scope based on the query elements in a pre-built cross-database pattern graph. The second processing module is used to input the pattern routing key into the language conversion model, so as to perform pattern linking through the language conversion model to obtain a candidate table set and the link confidence corresponding to the candidate table set; The result generation module is used to infer the connection relationships corresponding to the candidate table set based on the candidate table set and the link confidence corresponding to the candidate table set using a weighted graph search strategy in a pre-built cross-database pattern graph, and generate SQL statements based on the connection relationships.
9. A terminal device, characterized in that, The method includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein when the processor executes the computer program, it implements the method for generating natural language to SQL statements as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, include: A stored computer program, wherein, when the computer program is executed, it controls the device containing the computer-readable storage medium to perform the natural language to SQL statement generation method as described in any one of claims 1-7.