Data query method and system for converting natural language into database query language
By combining graph database technology with large language models and utilizing multimodal understanding agents and multi-agent collaborative working modes, the NL2SQL problem in complex database scenarios is solved, achieving high accuracy and high efficiency in natural language to database queries in complex database scenarios.
Patent Information
- Application Number
- CN202511379668.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-25
- Publication Date
- 2026-08-25
- Estimated Expiration
- 2045-09-25
AI Technical Summary
In complex database scenarios, existing natural language to SQL (NL2SQL) technologies face challenges such as difficulty in accurately identifying relationships between tables during multi-table join queries, ambiguity caused by fields with the same name or similar semantics, ensuring the syntactic correctness of complex nested queries, and query efficiency issues in large-scale databases.
By combining graph database technology with large language models, a multimodal understanding agent parses natural language input, eliminates ambiguity by combining historical conversation information and a business terminology list, generates standardized queries, optimizes the processing through a multi-agent collaborative working mode, and finally verifies the SQL query results in an isolated environment.
It improves the accuracy of converting natural language to database query language in complex database scenarios. Through multi-agent collaborative decision-making and business knowledge enhancement, it solves the semantic gap and query efficiency problems, and improves the accuracy and reliability of queries.
Smart Images

Figure CN121210495B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of natural language processing and database query technology, and more specifically, to a data query method and system for converting natural language to database query language, so as to improve the accuracy of natural language to SQL (NL2SQL) in complex database scenarios. Background Technology
[0002] With the advent of the big data era, database systems have become increasingly complex due to the large number of tables, fields, and intricate relationships between them. Traditional SQL (Structured Query Language) queries require users to possess specialized database knowledge, which limits the data access capabilities of non-technical users. Natural Language to SQL (NL2SQL) technology is a technique that converts natural language queries into SQL, aiming to lower the barrier to database queries through a natural language interface. However, this technology still faces the following challenges in complex database scenarios: 1) It is difficult to accurately identify the relationships between tables when performing multi-table join queries; 2) Ambiguity issues caused by fields having the same name or similar meanings; 3) Guarantee the syntactic correctness of complex nested queries; 4) Mapping of domain-specific terminology to database structure; 5) Query efficiency issues in large-scale databases.
[0003] Therefore, there is a need to find a solution to improve the accuracy of NL2SQL in complex database scenarios. Summary of the Invention
[0004] In view of the above problems, the purpose of this invention is to provide a data query method and system for converting natural language to database query language. By combining graph database technology with large language models, it solves the problems of poor recognition accuracy and efficiency in complex database scenarios in existing NL2SQL technology.
[0005] On one hand, the present invention provides a data query method for converting natural language to database query language, including: Based on the multimodal understanding agent, the query requirements of natural language input are parsed, and combined with historical conversation information and business terminology, ambiguity is eliminated and standardized queries are generated. The query complexity is determined based on the standardized query, and a corresponding query strategy is selected based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph; The preliminary query results are optimized using a multi-agent collaborative working mode to obtain the initial SQL query results; The initial query result of the SQL was simulated in an isolated environment. By performing full-link verification on the execution result, the SQL query that passed the verification was obtained. Execute the SQL query to obtain the data query results.
[0006] Alternatively, the knowledge graph can be constructed using the following methods: Metadata is extracted from the target database based on a preset data extraction strategy; wherein, the metadata includes table structure, field types, primary and foreign key constraints, and index information; The extracted metadata is standardized to obtain standardized metadata; A knowledge graph is constructed based on the standardized metadata; wherein the nodes of the knowledge graph include tables, fields and business terms, and the edges include primary and foreign key relationships, business logic relationships and semantic association relationships.
[0007] In addition, an optional approach is to standardize the extracted metadata, including: performing data cleaning on the extracted metadata; performing business semantic enhancement on the cleaned metadata; and standardizing and storing the enhanced metadata according to the MCP protocol format.
[0008] In addition, an alternative approach is to, after constructing the knowledge graph, further include: real-time monitoring of changes in the target database by a graph-based self-optimizing agent, and updating the topology of the knowledge graph according to the changes in the target database.
[0009] Alternatively, the multimodal understanding agent may employ a domain-adapted large language model to understand the intent of the natural language through multiple stages, including: By using basic semantic parsing, the query subject, conditions, and operation type of the natural language are identified, and the referential problems in the natural language are solved by combining the conversation history. Transform the colloquial expressions in the natural language into business terminology; For tabular data, the TAPEX model is used to understand Excel data, automatically identify the relationship between table headers and data, and extract query conditions; for image processing, OCR technology is used to identify key information in screenshots, and the CLIP model is used to determine the image service type.
[0010] In addition, an optional approach is that the criteria for determining the simple query include single-table operation, no nesting, and no more than 3 conditions; the cache template is stored in a high-frequency query template library maintained in the background. The criteria for determining complex queries include multi-table JOINs, subqueries, aggregate functions, and special operators; the traversal of the knowledge graph includes: performing precise path exploration through a graph agent, anchoring the starting point to the core table node, loading all attribute fields and related edges of the core table node, exploring multiple paths to discover all possible paths, calculating weights based on historical query success rate, data freshness, and association strength, and selecting the optimal path; simultaneously, the graph agent supplements business rules and data permissions in real time to generate the final optimized execution plan.
[0011] In addition, an optional approach is to optimize the preliminary query results through a multi-agent collaborative working mode, including: Four specialized intelligent agents are deployed: syntax generation, business logic, performance optimization, and access control. Each agent independently generates candidate SQL versions from different dimensions. Among them, the syntax generation agent is used to ensure that the statement conforms to the standard specification, the business logic agent is used to inject industry rule constraints, the performance optimization agent is used to optimize the execution strategy, and the access control agent is used to add data permission filtering. A voting mechanism based on business priority is used to filter the candidate SQL versions to obtain the filtered candidate solutions; The candidate solutions are pre-evaluated by performing a cost evaluation model, which scores them on a 100-point scale based on three dimensions: business compliance, execution efficiency, and security. The optimal solution is then selected as the initial SQL query result after calculating the comprehensive score.
[0012] On the other hand, the present invention also provides a data query system for converting natural language to database query language, comprising: The Query rewriting module is used to parse the query requirements of natural language input based on the multimodal understanding agent, and combine historical conversation information and business terminology to eliminate ambiguity and generate standardized queries. The semantic retrieval module is used to determine the query complexity based on the standardized query, and select the corresponding query strategy based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph; The SQL generation and optimization module is used to optimize the preliminary query results through a multi-agent collaborative working mode to obtain the initial SQL query results. The SQL detection module is used to simulate the execution of the initial SQL query results in an isolated environment, and obtain the verified SQL query by performing full-link verification on the execution results; The SQL execution module is used to execute the SQL query and obtain the data query results.
[0013] In addition, an optional solution includes a metadata extraction module and a knowledge graph construction module, wherein, The metadata extraction module is used to extract metadata from the target database based on a preset data extraction strategy, and to standardize the extracted metadata to obtain standardized metadata; wherein, the metadata includes table structure, field types, primary and foreign key constraints, and index information; The knowledge graph construction module is used to construct a knowledge graph based on the standardized metadata, and to monitor changes in the target database in real time based on the graph self-optimizing agent, and update the topology of the knowledge graph according to the changes in the target database; wherein, the nodes of the knowledge graph include tables, fields and business terms, and the edges include primary and foreign key relationships, business logic relationships and semantic association relationships.
[0014] On the other hand, the present invention also provides an electronic device, the electronic device including a memory, a processor, and a data query program for converting natural language to database query language stored in the memory and executable on the processor, wherein when the data query program for converting natural language to database query language is executed by the processor, it implements the data query method for converting natural language to database query language as described above.
[0015] In another aspect, the present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the data query method described above for converting natural language to database query language.
[0016] The natural language to database query language method provided by this invention is essentially a natural language to SQL method based on multi-agent collaborative decision-making. It decomposes the SQL generation process into four specialized agents: metadata, business, performance, and security. Through a weighted scoring mechanism, collaborative decision-making and conflict resolution are achieved, ultimately outputting a comprehensive optimal solution, realizing a holistic "division of labor-evaluation-decision" process. A query intent context construction method based on business knowledge enhancement utilizes synchronously constructed business dictionaries and knowledge graphs to provide real-time semantic mapping and associative reasoning for natural language queries, eliminating ambiguity and realizing the idea of "empowering large models with business context." By using a sandbox pre-execution verification and self-correction method for NL2SQL generation results, the generated SQL undergoes triple verification in an isolated environment—syntax, permissions, and business rules—and feeds back failed results to the generation end to form a closed-loop optimization, realizing a "pre-execution verification-feedback" self-correction mechanism. This invention differs from traditional end-to-end models or single-model methods; it improves accuracy and reliability through division of labor and collaboration.
[0017] Compared to existing technologies, this invention combines graph database technology with a large language model, utilizing a business terminology table and knowledge graph to provide rich context for the model, thus bridging the "semantic gap." Through multi-agent collaborative processing, the generation of a single SQL query undergoes specialized processing and verification by multiple agents, and then an optimal solution is generated through voting and scoring by an expert committee of agents. This effectively improves the accuracy of data queries in complex data application scenarios, converting natural language into database query language. The multimodal understanding agents can not only process text but also parse user-uploaded images and table data. The system uses OCR and table recognition technology to extract key conditions from tables and provides these conditions as context to the SQL generation module, greatly improving the accuracy of complex queries. The sandbox environment of the SQL detection module, with its pre-execution verification and feedback loop, effectively solves the problems of "illusion" and "error" in data queries.
[0018] To achieve the foregoing and related objectives, one or more aspects of the invention include the features which will be described in detail below and specifically pointed out in the claims. The following description and accompanying drawings illustrate certain exemplary aspects of the invention. However, these aspects indicate only a few of the various ways in which the principles of the invention can be used. Furthermore, the invention is intended to include all such aspects and their equivalents. Attached Figure Description
[0019] Other objects and results of the invention will become more apparent and readily understood with reference to the following description taken in conjunction with the accompanying drawings and the contents of the claims, and with a more complete understanding of the invention. In the drawings: Figure 1This is a schematic diagram of a data query method for converting natural language to database query language according to an embodiment of the present invention; Figure 2 This is a schematic diagram illustrating the process of generating a standardized query from a natural language input query request according to an embodiment of the present invention. Figure 3 This is a schematic diagram illustrating the knowledge graph construction process according to an embodiment of the present invention; Figure 4 This is a flowchart illustrating the standardization process of the extracted metadata according to an embodiment of the present invention. Figure 5 This is a schematic diagram illustrating the optimization process of the expert committee intelligent agent using a multi-agent collaborative working mode according to an embodiment of the present invention. Figure 6 This is a schematic diagram of the end-to-end verification process according to an embodiment of the present invention; Figure 7 This is a schematic diagram of the framework structure of a data query system for converting natural language to database query language according to an embodiment of the present invention; Figure 8 This is a schematic diagram of the framework structure of a data query system for converting natural language to database query language according to another embodiment of the present invention; Figure 9 This is a schematic diagram of the logical structure of an electronic device for implementing a data query method for converting natural language to database query language according to an embodiment of the present invention.
[0020] In all the accompanying drawings, the same reference numerals indicate similar or corresponding features or functions. Detailed Implementation
[0021] In the following description, numerous specific details are set forth for illustrative purposes and to provide a thorough understanding of one or more embodiments. However, it will be apparent that these embodiments may also be implemented without these specific details. In other instances, well-known structures and devices are shown in block diagram form for ease of description of one or more embodiments. Various modifications and embodiments can be applied to the invention, and specific embodiments are illustrated in the accompanying drawings. However, the invention is not limited to this particular implementation and all modifications, equivalents, and substitutions falling within the spirit and scope of the invention are to be understood and included.
[0022] In this invention, ordinal terms such as "first," "second," etc., are used to describe various constituent elements, but the constituent elements are not limited to these terms. The terms are used only to distinguish one constituent element from another. For example, without departing from the scope of the claims, a second constituent element may be named a first constituent element, and similarly, a first constituent element may be named a second constituent element. Terms and / or include combinations of multiple associated items or one of multiple associated items.
[0023] The terminology used in this invention is for illustrative purposes only and is not intended to limit the invention. Unless the context clearly specifies otherwise, singular expressions include plural expressions. In this invention, it should be understood that terms such as "comprising" or "having" are used to specify the presence of features, numbers, steps, operations, constituent elements, components, or combinations thereof described in the specification, and do not preclude the presence or additional possibilities of one or more other features, numbers, steps, operations, constituent elements, components, or combinations thereof.
[0024] Furthermore, unless otherwise defined, including technical or scientific terms, all terms used herein have the same meaning as commonly understood by those skilled in the art. Terms as defined in commonly used dictionaries should be interpreted in a meaning consistent with their meaning in the context of the relevant art, and should not be construed as having an ideal or overly formal meaning unless explicitly defined in this invention.
[0025] To more clearly illustrate the technical solution of the present invention, the following is a brief explanation of some of the technical terms involved in the present invention.
[0026] DeepSeekV3 is an advanced Large Language Model (LLM) with powerful natural language understanding and generation capabilities, widely used in tasks such as text generation, code completion, question answering, and logical reasoning. The core features of DeepSeekV3 are as follows: It has a massive number of parameters, based on a Transformer architecture with hundreds of billions of parameters, resulting in stronger semantic understanding and reasoning capabilities; it supports long texts, with a 128K context window, capable of handling extremely long documents, complex code, and multi-turn dialogues; it has multilingual capabilities, optimizing the understanding and generation of Chinese, English, code (Python / SQL, etc.), and some less common languages; it has knowledge augmentation capabilities, integrating industry knowledge bases and performing exceptionally well in fields such as finance, law, and healthcare; and it has efficient reasoning capabilities, reducing inference costs and improving response speed through dynamic computation optimization.
[0027] DeepSeekR1 is a model optimized for Retrieval-Augmented Generation (RAG). Combining a large language model with external knowledge bases, it provides more accurate and traceable answers. DeepSeekR1's core features include: Integrated Retrieval-Generation: A built-in high-efficiency retrieval module can query external databases and document repositories in real time and generate accurate answers based on the search results. Dynamic Knowledge Updates: Supports incremental indexing, ensuring the model always uses the latest data and avoiding the "knowledge freeze" problem of traditional large models. High Interpretability: Provides source citations for answers, facilitating user verification of information reliability. Low Illusion Output: By constraining the generation process through retrieval, it significantly reduces the risk of the model "fabricating" information. Typical Application Scenarios: Enterprise Knowledge Base Question Answering: Provides accurate answers based on internal company documents (such as product manuals and technical documents). Legal / Financial Consulting: Generates compliant and real-time professional advice based on regulations and market data. Academic Research Assistance: Searches papers and patent databases and generates review or analysis reports.
[0028] MCP (Model Context Protocol) is an open-source protocol introduced by Anthropic, designed to integrate large language models with external data sources and tools. It establishes a secure, bidirectional connection between large models and data sources, serving as a universal interface designed for efficiently obtaining the context information needed by the model. This protocol can promote the standardization and decentralization of large language model applications. It handles both local and remote resources through the same protocol, providing an open standard similar to OpenAPI. It defines a set of common communication protocols, data formats, and rules, featuring simplified development, flexibility, real-time response, security and compliance, and scalability.
[0029] Graph databases are data management systems designed for efficient storage and retrieval of graph data, using nodes and edges as the basic storage units. They belong to the category of NoSQL databases. Graph data structures directly store the dependencies between nodes, while relational databases and other types of NoSQL databases represent these relationships in a less direct way. Their main applications include online transaction processing (OLTP) and ACID transaction processing.
[0030] Natural Language Understanding (NLU) is an important branch of artificial intelligence that focuses on enabling computer systems to deeply understand the meaning of human language, rather than simply processing surface-level vocabulary or grammatical structures. Its goal is to extract semantic information such as intent, entities, sentiment, and contextual relationships from text or speech input and transform it into a structured representation that can be processed by machines.
[0031] Natural Language Processing (NLP) is an important research area in artificial intelligence. It encompasses two main aspects: natural language understanding and natural language generation. Its research covers multiple levels, including characters, words, phrases, sentences, paragraphs, and texts. It serves as a bridge between machine language and human language. Its aim is to enable machines to understand, interpret, and generate human language, achieving effective communication between humans and machines, and enabling computers to perform tasks such as language translation, sentiment analysis, and text summarization.
[0032] RBAC (Role-Based Access Control) is a permission model that manages access control by associating users and permissions with roles. Its core idea is to assign permissions to roles, and then assign roles to users, thereby simplifying permission management.
[0033] PII (Personally Identifiable Information) field: This is a data field that can directly or indirectly identify a specific individual.
[0034] The specific embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0035] Example 1 To address the shortcomings of the aforementioned existing methods, this invention provides a data query method for converting natural language to database query language, which improves the accuracy of natural language to database query language conversion in complex data application scenarios. Figure 1 The flowchart of a data query method for converting natural language to database query language according to an embodiment of the present invention is shown, such as... Figure 1 As shown, the data query method for converting natural language to database query language provided by this invention mainly includes the following steps: S110: Based on multimodal understanding, the intelligent agent parses the query requirements of natural language input, combines historical conversation information and a business terminology table to eliminate ambiguity and generate standardized queries.
[0036] In this step, the semantic parsing and standardization of user queries (through natural language input) can be achieved through multimodal understanding agents. Specifically, this includes: multimodal input processing, receiving user natural language input and attachments (images / tables), and extracting structured query conditions through OCR technology.
[0037] The historical session information is derived from each successful interaction between the user and the system, which forms a session record. All session records (including context) are anonymized and stored in a dedicated session database as historical session information. The business terminology table is derived from existing enterprise documents, domain expert interviews, and database metadata. The existing enterprise documents are manually extracted and sorted from documents such as the enterprise's data governance platform, data dictionary, API documentation, business specification manual, and training materials. The domain expert interviews are conducted with professionals from business departments (such as government departments, marketing, and risk control) to clarify the accurate definition and calculation methods of core business concepts. The database metadata consists of table names, field names, comments, and other data extracted from the database, which can serve as the initial technical names for the terms.
[0038] Figure 2 This illustrates a process for generating a standardized query from a natural language input query request, according to an embodiment of the present invention. For example... Figure 2 As shown, as an example, step S110 further includes: S111: Through basic semantic parsing, identify the query subject, conditions, and operation type of the natural language, and solve the referential problem in the natural language by combining the conversation history; S112: Convert the colloquial expressions in the natural language into business terms.
[0039] In this embodiment, the query is rewritten using a large model and historical sessions to obtain the rewritten question and intent. More specifically, the query rewriting process includes: Problem normalization: Using a large language model to perform spelling correction, terminology standardization, and spoken-to-written language processing on user input; Intent recognition: Identify the user's core query intent based on business concept nodes in the knowledge graph; Contextual supplementation: Supplementing missing query conditions or entity references by combining historical session information; context enhancement utilizes business dictionaries and knowledge graphs to provide rich context for the model, which can solve the existing "semantic gap" problem.
[0040] Multi-intent splitting: Break down complex query problems into multiple independent subqueries and mark their execution priorities.
[0041] In this embodiment, the query rewriting combines historical conversations and a business terminology table. Through question generalization, semantic disambiguation, and context supplementation, the multimodal understanding agent rewrites the user's natural language input query data into historical conversations and a business terminology table stored according to the MCP protocol specification. Specifically, by calling an NLP model to parse the text input or using OCR technology to process image / table attachments, a structured standardized query is generated. The extracted conditions and the rewritten standardized query are then written into the context storage according to the MCP format for downstream modules to call.
[0042] In this embodiment, the modal understanding agent employs a domain-adapted large language model to understand user intent through multiple stages. First, it performs basic semantic parsing to identify the query subject, conditions, and operation type. It then combines conversation history to resolve referential issues and avoid conversational ambiguity. Finally, it converts colloquial expressions into business terminology. For some tabular data, the TAPEX model is used to understand Excel data, automatically identifying the relationship between table headers and data, and extracting query conditions. For some image processing, OCR technology is used to identify key information in screenshots, and the CLIP model is used to determine the image service type.
[0043] After generating the standardized query, proceed to step S120, determine the query complexity based on the standardized query, and select the corresponding query strategy based on the query complexity to perform the query on the standardized query and obtain preliminary query results.
[0044] This step is primarily implemented by the retrieval routing agent. Specifically, the retrieval routing agent selects the corresponding query strategy based on the query complexity of the standardized query. The query complexity includes simple queries and complex queries. The query strategy for simple queries is to directly match the cached template, while the query strategy for complex queries is to traverse the pre-built knowledge graph and return the relevant tables and their fields, the join paths between tables, the business constraints, and the query example.
[0045] Specifically, as an example, the process by which the retrieval routing agent selects a strategy based on query complexity is as follows: The retrieval routing agent achieves precise traffic distribution through multi-dimensional analysis. If it is a single-table operation, without nesting, and with no more than three conditions, it meets the characteristics of a simple query. The agent directly queries the high-frequency query template library maintained in the backend. For queries that match exactly, the agent directly returns a cached execution plan. Similar queries are parameterized and rewritten, and pre-compiled statements are reused. If it is a multi-table JOIN, subquery, aggregate function, or special operator, the graph agent performs precise path exploration. The starting point is anchored to the core table node, all its attribute fields and related edges are loaded, and multi-path exploration is performed to discover all possible paths. The agent calculates weights based on historical query success rate, data freshness, and association strength, and selects the optimal path. At the same time, the retrieval routing agent supplements business rules and data permissions in real time, and finally generates the optimal execution plan.
[0046] In one embodiment of the present invention, the specific implementation method for obtaining the above preliminary query results by performing semantic retrieval and intent understanding based on a graph database is as follows: 1. Semantic retrieval: - Quickly locate candidate tables and field nodes using structural indexes; - Expand synonym and near-synonym queries using semantic indexes; - Matching similar business scenarios based on business indexes; 2. Path discovery: - Use a graph traversal algorithm to find the optimal join path between tables; - Choose between primary key or intermediate table join based on the estimated join cost; 3. Constraint Derivation: - Automatically associate field business rules to generate filter conditions; - Combine the permission model to filter the set of accessible fields.
[0047] Figure 3 The process of constructing a knowledge graph according to an embodiment of the present invention is illustrated. For example... Figure 3 As shown, the knowledge graph construction method provided in this embodiment includes the following steps: a1: Extract metadata from the target database based on a preset data extraction strategy; wherein, the metadata includes table structure, field types, primary and foreign key constraints, and index information; a2: Standardize the extracted metadata to obtain standardized metadata; a3: Construct a knowledge graph based on the standardized metadata.
[0048] The nodes of the knowledge graph include tables, fields, and business terms, while the edges include primary and foreign key relationships, business logic relationships, and semantic association relationships.
[0049] The process of traversing a knowledge graph is essentially a process of retrieving relevant metadata based on the knowledge graph. Specifically, semantic indexing can quickly locate candidate table / field nodes related to the user's intent in natural language input, and then the optimal connection path is discovered based on a graph traversal algorithm. Next, a retrieval routing agent dynamically selects a retrieval strategy based on query complexity (rule template matching for simple queries, graph traversal for complex queries) and stores high-frequency paths in a caching system according to the MCP protocol to support subsequent fast retrieval.
[0050] In one specific embodiment of the present invention, during the data acquisition phase, to meet industry needs and support model training, metadata such as table structure, field types, primary and foreign key constraints, and index information of the target database can be extracted automatically or manually. Simultaneously, a field business dictionary and an inter-table business relationship document are constructed. The business dictionary includes the business meaning, value range, and association rules of the fields. Data sources may include: production databases, databases directly connected to online or test environments (such as MySQL, Oracle, PostgreSQL); data warehouses: table definitions extracted from the Hive platform; documents and scripts: parsed SQLDDL scripts, Excel structure documents, or table design documents from a knowledge base. The purpose of simultaneously constructing the field business dictionary and the inter-table business relationship document is primarily to bridge the gap between business semantics and technical implementation. The business dictionary provides a unique and clear business explanation for each technical field name.
[0051] Specifically, as an example, in the Query understanding part: when a user asks about the business situation of a certain year, the intelligent agent model will first match the corresponding business dictionary to obtain the corresponding table structure field information. Subsequently, the SQL generated by the intelligent agent will automatically add the corresponding field conditions and generate the corresponding field results.
[0052] The system automatically extracts metadata from the target database by selecting the appropriate database driver, automatically connecting to the database, and scanning for basic data such as table structure, field types, primary and foreign key constraints, and index information. After obtaining the basic data, it adjusts any non-standard or ambiguous expressions to standardize the metadata.
[0053] During the metadata extraction phase, various types of metadata can be precisely extracted from the target database, including basic table information (such as table name and storage engine), field details (such as field name, data type, and whether null values are allowed), various constraints (such as primary keys and foreign keys), and index definitions (such as index name and included fields). Based on the table data, a business dictionary can be created by explaining the meaning of some enumerated fields. Specialized terms in the professional field can be explained based on industry information. Then, a field business dictionary and a document outlining inter-table business relationships are constructed, along with descriptions of business example scenarios to better match query results during standardized query operations.
[0054] After extracting the metadata, it needs to be standardized. Specifically, as an example, a metadata governance agent can analyze field sensitivity, detect naming conflicts, automatically label sensitive field levels, and store the metadata in a standard format in the context repository based on the MCP protocol. Specifically, as an example, the metadata governance agent uses a rule engine and machine learning model to analyze field sensitivity (such as PII identification), detect naming conflicts, generate standardization suggestions, and finally output standardized metadata conforming to the MCP protocol (including sensitivity levels, conflict correction records, and suggested names).
[0055] Figure 4 The flowchart illustrating the standardization process of extracted metadata according to an embodiment of the present invention is shown, such as... Figure 4 As shown, the process of standardizing the extracted metadata in this embodiment includes: a21: Perform data cleaning processing on the extracted metadata.
[0056] During the data cleaning and processing phase, the large-scale model agent leverages natural language understanding capabilities to deeply analyze the semantics of field names, automatically identifying fields with different names but the same meaning (e.g., "cust_id" and "client_no" both represent customer identifiers). Based on embedding vector similarity calculations, the agent can establish a semantic association network for fields, accurately merging duplicate fields. For conflicts involving fields with the same name but different business meanings (e.g., "account" refers to a bank account in a payment system but a login account in a CRM system), the agent automatically marks the conflict and generates a detailed comparison report. By integrating a rule engine and a deep learning model into the agent system, a multi-layered sensitive data detection system is constructed. The basic layer uses reinforcement learning-optimized regular expressions to accurately match structured PII data such as ID card numbers and bank card numbers; the advanced layer utilizes the large-scale model's contextual understanding capabilities to identify potentially sensitive information from unstructured annotations (e.g., "Employee health data is detailed in the attachment"). The detection results are continuously optimized through a federated learning architecture to ensure that data security policies across various business domains are updated synchronously.
[0057] a22: Perform business semantic enhancement processing on the cleaned metadata.
[0058] During the business semantic enhancement processing phase, the agent, based on a finely tuned domain-wide model (such as a financial version of LLaMA), automatically transforms technical field names into business-friendly expressions (e.g., "acct_bal" → "account available balance"). For fields lacking annotations, the agent generates descriptive text conforming to industry terminology standards by analyzing data features, related fields, and business context. The system has a built-in terminology alignment mechanism that automatically maintains a three-level mapping relationship of "technical field - business terminology - industry standard," ensuring cross-system semantic consistency of over 98%. The agent also deeply parses unstructured text such as field annotations and constraints, automatically constructing a business relationship network.
[0059] a23: Standardize and store the enhanced metadata according to the MCP protocol format.
[0060] During the standardized storage phase, the processed metadata is stored in a standardized format according to the MCP protocol. It can be stored in a relational database (such as MySQL) or output as a JSON or Protobuf file. At the same time, a version control mechanism is established to fully record the change history of the metadata.
[0061] In the process of constructing a knowledge graph based on the standardized metadata, the first step is to model and initialize the knowledge graph. Specifically, as an example, the large model agent parses the metadata in MCP format and automatically designs the graph ontology; it identifies entities, mapping tables to graph nodes (e.g., Customer table → "Customer" entity) and fields to node attributes (e.g., customer_name → "Name" attribute); it generates edge relationships based on primary and foreign key constraints (e.g., the "attribution" relationship between the order table and the customer table), and supplements implicit relationships through NLP analysis of business documents (e.g., "the customer table and the risk control scoring table have a risk assessment association"); when it detects an undefined business concept (e.g., a newly discovered "supply chain partner" table), the agent automatically expands the ontology and generates suggested classifications. Finally, the agent integrates structured metadata and unstructured business documents to generate a knowledge graph with semantic annotations.
[0062] Specifically, as an example, the knowledge graph constructed by this invention includes the following node types: Table node: contains information such as table name, table description, and estimated data volume; Field node: Contains attributes such as field name, data type, and business description; Business concept nodes: represent domain terms and business entities.
[0063] The knowledge graph constructed in this invention defines the following types of edge relationships: Structural relationships: including primary and foreign key associations, inheritance relationships, etc.; Semantic relationships include synonyms, near-synonyms, and hierarchical relationships. Business relationships: including business process associations, statistical associations, etc.; The knowledge graph constructed in this invention also features a three-layer index structure: Structured indexes are used to accelerate table field retrieval; Semantic indexes are used to support synonym expansion and concept retrieval; Business indexes are used to optimize scenario-based queries.
[0064] The knowledge graph provided by this invention can also automatically update its topology by monitoring database changes in real time through a graph self-optimizing agent. In other words, after modeling and initialization, the knowledge graph in this embodiment can monitor changes to the target database in real time based on the graph self-optimizing agent, and update the knowledge graph's topology according to these changes, achieving real-time updates through a real-time graph self-optimization mechanism. For example, using the graph self-optimizing agent, it can monitor schema change events in real time through database triggers, encapsulate incremental update content into structured events according to the MCP protocol format through an incremental update mechanism, and broadcast these events to downstream modules via a message queue, driving local topology adjustments in the knowledge graph.
[0065] Specifically, the operation of automatically synchronizing the knowledge graph when the database structure changes through the incremental update mechanism is as follows: Retrieve items to be synchronized from metadata and use a rule engine to convert DDL operations into graph operations. Database change type graph operation rules include: `CREATE TABLE order` adds a table node `order` and automatically attaches child nodes to the field; `ALTER TABLE order ADD COLUMN discount DECIMAL` adds a field node `discount` and associates it with the parent table `order`; `ALTER TABLE order RENAME COLUMN price TO amount` updates the field node attribute: `name:"price"→"amount"`. DROP TABLE temp_data cascades the deletion of table nodes and associated edges. ADD FOREIGN KEY (product_id) REFERENCES product(id) creates a structural edge: order-[FOREIGN_KEY]->product Then, atomic transactions are used for the synchronization layer of the graph, and finally, the real-time index and semantic index are updated.
[0066] Specifically, as an example, in CDC event handling, Debezium captures database DDL / DML events (such as adding fields or changing foreign keys) to trigger the agent pipeline, which includes: Syntax parsing decodes the ALTER TABLE statement into graph manipulation instructions; Impact analysis is used to identify affected nodes and downstream dependencies (e.g., deleting a primary key requires deleting foreign key edges in a cascading manner). Conflict detection verifies whether changes comply with business rules (such as prohibiting changing the PII field to a public type), and finally quickly completes the topology adjustment.
[0067] After obtaining the preliminary query results, proceed to step S130, where the preliminary query results are optimized using a multi-agent collaborative working mode to obtain the initial SQL query results.
[0068] In this invention, step S130 is implemented through an expert committee intelligent agent. Specifically, the expert committee intelligent agent generates SQL through multi-stage collaborative processes, performs a pre-evaluation of the execution plan, and selects the version with the highest overall score as the initial SQL query result. The SQL generation and optimization through the expert committee intelligent agent includes: building a framework using a rule engine; first, generating a basic SQL skeleton (SELECT [fields] FROM [tables] WHERE [conditions]) based on the output table / field / constraint set; subsequently, supplementing complex logic based on business scenarios using a large model (such as the window function RANK() OVER(PARTITION BY category ORDER BY sales DESC)); and generating multiple SQL statements using the large model based on the search results and preset prompt word templates. The aforementioned pre-evaluation of the execution plan includes: verifying the candidate SQL using an EXPLAIN function through a cost evaluation intelligent agent to explore its executability; subsequently, comparing the intent matching degree with the user's original query and estimating performance and memory consumption; scoring based on syntax, semantics, and performance; performing self-consistency verification; eliminating logically contradictory generated results through cross-validation; and finally finding the optimal SQL.
[0069] Specifically, Figure 5 The flowchart illustrates the process of optimization processing using a multi-agent collaborative working mode by an expert committee agent according to an embodiment of the present invention. For example... Figure 5 As shown, the expert committee's intelligent agent generates high-quality SQL query solutions through a rigorous multi-stage process, specifically including: S131: Deploy four specialized intelligent agents: syntax generation, business logic, performance optimization, and access control. Each agent independently generates candidate SQL versions from different dimensions. Among them, the syntax generation agent ensures that the statements conform to standard specifications, the business logic agent injects industry rule constraints, the performance optimization agent optimizes the execution strategy, and the access control agent adds data permission filtering.
[0070] S132: Entering the conflict resolution phase, a voting mechanism based on business priorities is used to filter candidate SQL versions to obtain the filtered candidate solutions. A typical scenario is that the business logic agent exercises veto power over solutions lacking key constraints, while other agents rank candidate solutions using Borda counting.
[0071] S133: A pre-evaluation of the execution plan is performed on the candidate solutions. A pre-defined cost evaluation model is used to score the solutions on a 100-point scale across three dimensions: business compliance, execution efficiency, and security. The optimal solution is selected as the initial SQL query result after calculating the comprehensive score. Specifically, as an example, the final top 3 candidate solutions can be included in the execution plan pre-evaluation stage. The cost evaluation model scores these solutions on a 100-point scale across three dimensions: business compliance (weight 50%), execution efficiency (30%), and security (20%). The optimal solution is selected after calculating the comprehensive score.
[0072] Furthermore, this invention provides a dynamic optimization mechanism that adjusts agent weights in reverse by recording actual execution performance and sets version rollback strategies to ensure stability. When encountering severe agent disagreements or timeouts, the system can trigger manual intervention or downgrade to a conservative solution, maintaining service availability while ensuring core functionality. The entire process achieves efficient collaboration through a large model, combining a rule engine with machine learning predictions. The resulting SQL satisfies business rules and possesses high-performance characteristics, improving quality by more than 40% compared to traditional single-model solutions.
[0073] After obtaining the initial SQL query result, proceed to step S140: simulate the execution of the initial SQL query result in an isolated environment, and obtain the verified SQL query by performing full-link verification on the execution result.
[0074] In S140, a test agent is used to perform security verification and performance pre-checks on the initial SQL query results. The test agent pre-executes the initial SQL query results in a completely isolated sandbox environment, performs end-to-end verification of the execution results, and monitors risks from multiple dimensions to ensure its security, reliability, and compliance. The system first completely clones the production database structure in a containerized test environment, but only includes metadata definitions and not real business data. This data anonymization method ensures the authenticity of the test while avoiding the risk of information leakage.
[0075] Figure 6 The full-link verification process according to an embodiment of the present invention is illustrated, such as... Figure 6 As shown, in this embodiment, the end-to-end verification adopts a layered verification mechanism, including: S141: Call the database engine's pre-compilation interface at the syntax layer to check the SQL syntax correctness in the initial SQL query results, identify and correct basic errors in the initial SQL query results; S142: Simulate users with different roles to execute queries at the permission layer to verify whether field-level access control is strictly effective, and perform permission review on fields marked as sensitive data (such as ID card number, transaction amount, etc.); S143: In the business logic layer, the rules engine checks whether the query meets the preset business constraints, such as ensuring that queries involving customer information must include data permission filtering conditions.
[0076] Specifically, as an example, regarding grammatical validity, errors are captured from the database engine while simple errors are automatically corrected. Specific grammar validation operations include: - Check the SQL syntax validity using the database interpreter; - Identify potentially dangerous operations (such as unrestricted full table scans); Regarding permission compliance, unauthorized fields are dynamically replaced by comparing RBAC rules. Specific permission verification operations include: - Simulate user role verification for field-level access permissions; - Automatically de-identify or aggregate fields that exceed permissions; To meet performance red lines, optimization tips are added to SQL queries with estimated execution times exceeding 5 seconds by identifying operations such as full table scans and high-cost sorting (Sort Method: external merge). Specific business compliance operations include: - Ensure that the SQL contains the necessary business constraints; - Validate integrity constraints for multi-table joins.
[0077] The test agent verifies SQL syntax and performance in a sandbox using pre-execution plans. It dynamically de-identifies unauthorized fields based on RBAC rules stored using the MCP protocol, detects vulnerabilities by actively injecting SQL injection samples, and writes security events and optimization suggestions into a knowledge base in MCP format. Finally, it synchronizes these to the entire system via a message queue. The execution process of all test cases is fully recorded, including the actual query plan, resource consumption, and virtual return results. The system automatically analyzes these logs to generate test reports, marking potential risks (such as detecting full table scans) or permission vulnerabilities (such as unauthorized access to customer phone number fields). For queries that fail verification, the test agent automatically generates detailed error diagnostic reports, specifying the exact reasons for failure (such as "missing department field filtering leading to unauthorized access risk") and providing correction suggestions. Typical cases are added to the regression test set for continuous monitoring. The entire testing process is typically completed within 30 seconds, ensuring rigorous verification without affecting overall query response efficiency. Only SQL queries that pass all checks are marked as executable and proceed to the next stage.
[0078] After obtaining the validated SQL query, proceed to step S150 to execute the validated SQL query and obtain the data query results. Alternatively, intelligent methods can be used to perform in-depth processing and optimization of the data query results to better present the results. Specifically, as an example, processing and optimizing the query results may include: - Result pagination: Automatically paginates large result sets; - Data anonymization: Hides sensitive fields based on permission models; - Interpretation generation: Outputs query descriptions in natural language form; - Visualization: Presents the data entities and relationships used in the form of graphs.
[0079] Example 2 Corresponding to the data query method of converting natural language to database query language in Embodiment 1, this Embodiment 2 provides a data query system of converting natural language to database query language to implement the data query method of converting natural language to database query language in Embodiment 1, so as to improve the accuracy of converting natural language to database query language in complex data application scenarios based on a large model.
[0080] The system architecture of this invention provides a dual-routing engine for query complexity in NL2SQL, which can automatically select two distinct execution paths, "cache template matching" or "knowledge graph semantic retrieval", based on the complexity.
[0081] Figure 7 The logical framework structure of a data query system for converting natural language to database query language according to this embodiment is shown. For example... Figure 7 As shown, the natural language to database query language data query system 700 provided by the present invention mainly includes a query rewriting module 710, a semantic retrieval module 720, an SQL generation and optimization module 730, an SQL detection module 740, and an SQL execution module 750. The modules of the present invention can also be referred to as units, which refer to a series of computer program segments that can be executed by the processor of an electronic device and can perform a fixed function, and are stored in the memory of the electronic device.
[0082] Among them, the Query rewriting module 710 is used to parse the query requirements of natural language input based on the multimodal understanding agent, and combine historical conversation information and business terminology table to eliminate ambiguity and generate standardized queries.
[0083] The Query Rewriting module acts as a hub for converting natural language to structured queries, processing user input through a multimodal understanding agent. The system not only parses text queries but also handles additional unstructured data such as images and tables. By combining a business terminology table and historical conversation context, it eliminates ambiguity and generates standardized query requests. In typical scenarios, the Query Rewriting module can complete the semantic parsing and structured transformation of complex queries within 500ms.
[0084] The semantic retrieval module 720 is used to determine the query complexity based on the standardized query, and select a corresponding query strategy based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph.
[0085] The semantic retrieval module enables intelligent query routing decisions, selecting the optimal execution strategy based on request complexity. For simple queries (such as single-table conditional searches), it directly matches pre-stored template solutions; for complex queries involving multi-table joins, it traverses the knowledge graph to obtain the complete business context and relational constraints. The system's built-in caching mechanism supports sub-millisecond response times for over 85% of query requests.
[0086] The SQL generation and optimization module 730 is used to optimize the preliminary query results through a multi-agent collaborative working mode to obtain the initial SQL query results.
[0087] The SQL generation and optimization module works collaboratively with an expert committee of intelligent agents to generate and evaluate candidate SQL solutions from four dimensions: syntax compliance, business rules, execution efficiency, and security. Through multiple rounds of iterative optimization, the system selects the execution plan with the highest overall score, achieving optimal query performance while ensuring business correctness. The initial SQL query results generated by this module all undergo rigorous pre-execution cost evaluation.
[0088] The SQL detection module 740 is used to simulate the execution of the initial SQL query results in an isolated environment, and obtain the verified SQL query by performing full-link verification on the execution results.
[0089] The SQL detection module executes the generated SQL preliminary query results in an isolated sandbox environment and performs comprehensive verification of the results. This includes key steps such as syntax validity checks, field-level permission verification, and business rule compliance testing. Any SQL preliminary query result that fails the detection will trigger an automatic correction process and be fed back to the generation module for iterative optimization, ensuring that the final executed query is 100% compliant with all system specifications.
[0090] SQL execution module 750 is used to execute the SQL query and obtain the data query result.
[0091] The SQL execution module, as the final stage of the process, is responsible for query execution and result delivery. The system intelligently selects the execution engine based on the query type; simple queries connect directly to the production database, while complex analyses are routed to the data warehouse cluster. The result processing pipeline can further perform post-processing operations such as sensitive information anonymization, visualization scheme recommendations, and data lineage marking, ultimately outputting well-structured, secure, and compliant query results.
[0092] In the underlying model of the intelligent agent in this embodiment of the invention, DeepSeekR1 and DeepSeekV3 can be selected according to specific needs. DeepSeekV3 focuses on semantic understanding, while DeepSeekR1 focuses on decision generation. For example, in the query rewriting module, DeepSeekV3's natural language understanding capabilities can accurately capture user intent, identify textual concepts, and map them to specific entities and attributes in the knowledge graph, effectively eliminating ambiguity and providing a solid foundation for generating standardized queries. Furthermore, in the QL generation and optimization section, after the semantic retrieval module returns relevant tables, fields, and business constraints, this information is constructed into a detailed prompt and input to DeepSeekR1. DeepSeekR1, optimized for code generation, can generate syntactically correct, logically accurate, and high-performance SQL code based on detailed context.
[0093] In another embodiment of the present invention, the data query system 700 for converting natural language to database query language may further include a metadata extraction module 760 and a knowledge graph construction module 770, wherein, The metadata extraction module 760 is used to extract metadata from the target database based on a preset data extraction strategy, and to standardize the extracted metadata to obtain standardized metadata; the metadata includes table structure, field types, primary and foreign key constraints, and index information.
[0094] The metadata extraction module 760 serves as the data foundation layer of the entire system, primarily responsible for automatically collecting core metadata such as table structure, field attributes, and constraints from various database sources. Through its built-in metadata governance agent, the system can automatically identify sensitive fields (such as PII information like ID card numbers and bank card numbers), detect naming conflicts (such as fields with the same name but different meanings in different business systems), and store and manage the standardized metadata according to the MCP protocol format. This module supports the extraction and governance of metadata from thousands of data tables per hour, providing high-quality structured data for subsequent processes.
[0095] The knowledge graph construction module 770 is used to construct a knowledge graph based on the standardized metadata, and to monitor changes in the target database in real time based on the graph self-optimizing agent, and update the topology of the knowledge graph according to the changes in the target database; wherein, the nodes of the knowledge graph include tables, fields and business terms, and the edges include primary and foreign key relationships, business logic relationships and semantic association relationships.
[0096] The knowledge graph construction module 770 constructs a dynamically scalable domain knowledge graph. It maps database table structures to graph nodes, transforming foreign key relationships into associated edges, forming a complete topological network. By monitoring database change events in real time, the graph self-optimizing agent can respond to structural changes (such as adding fields or modifying constraints) in milliseconds, automatically maintaining the timeliness of the graph. This module supports fast traversal and querying of tens of thousands of nodes, providing rich contextual relationships for semantic understanding.
[0097] More specific implementations of the data query system for converting natural language to database query language can be found in the aforementioned embodiments of the data query method for converting natural language to database query language, and will not be described in detail here.
[0098] like Figure 9As shown, the present invention also provides an electronic device 1 for implementing a data query method for converting natural language to a database query language. The electronic device 1 may include a processor 10, a memory 11, and a bus, and may also include a computer program stored in the memory 11 and executable on the processor 10, such as a data query program 12 for converting natural language to a database query language. The memory 11 may include both internal storage units of the data query system for converting natural language to a database query language and external storage devices. The memory 11 can be used not only to store application software and various types of data, such as the code of the data query program for converting natural language to a database query language, but also to temporarily store data that has been output or will be output.
[0099] The memory 11 includes at least one type of readable storage medium, such as flash memory, portable hard drive, multimedia card, card-type memory (e.g., SD or DX memory), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the memory 11 can be an internal storage unit of the electronic device 1, such as a portable hard drive. In other embodiments, the memory 11 can be an external storage device of the electronic device 1, such as a plug-in portable hard drive, smart memory card, secure digital card, flash memory card, etc., equipped on the electronic device 1. Furthermore, the memory 11 can include both internal and external storage units of the electronic device 1. The memory 11 can be used not only to store application software and various types of data installed on the electronic device 1, such as the code of a data query program that converts natural language to database query language, but also to temporarily store data that has been output or will be output.
[0100] In some embodiments, the processor 10 may be composed of integrated circuits, such as a single packaged integrated circuit or multiple integrated circuits packaged with the same or different functions, including combinations of one or more central processing units, microprocessors, digital processing chips, graphics processors, and various control chips. The processor 10 is the control core of the electronic device, connecting various components of the entire electronic device via various interfaces and lines. It executes programs or modules stored in the memory 11 (e.g., data query programs that convert natural language to database query language) and calls data stored in the memory 11 to perform various functions of the electronic device 1 and process data.
[0101] The bus can be a Peripheral Component Interconnect Standard (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This bus can be divided into an address bus, a data bus, a control bus, etc. The bus is configured to enable communication between the memory 11 and at least one processor 10, etc.
[0102] Figure 9 Only electronic devices with components are shown; it will be understood by those skilled in the art that... Figure 9 The structure shown does not constitute a limitation on the electronic device 1, and may include fewer or more components than shown, or combine certain components, or have different component arrangements.
[0103] For example, although not shown, the electronic device 1 may also include a power supply (such as a battery) to power various components. This power supply can be logically connected to the at least one processor 10 through a power management system, thereby enabling functions such as charging management, discharging management, and power consumption management. The power supply may also include one or more DC or AC power sources, a recharging system, a power fault detection circuit, a power converter or inverter, a power status indicator, or any other components. The electronic device 1 may also include various sensors, Bluetooth modules, Wi-Fi modules, etc., which will not be elaborated further here.
[0104] Furthermore, the electronic device 1 may also include a network interface. Optionally, the network interface may include a wired interface and / or a wireless interface (such as a Wi-Fi interface, a Bluetooth interface, etc.), which is typically used to establish communication connections between the electronic device 1 and other electronic devices.
[0105] Optionally, the electronic device 1 may further include a user interface, which may be a display or an input unit. Optionally, the user interface may also be a standard wired interface or a wireless interface. Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, or an OLED (organic light-emitting diode) touchscreen, etc. The display may also be appropriately referred to as a screen or display unit, used to display information processed in the electronic device 1 and to display a visual user interface.
[0106] It should be understood that the embodiments described are for illustrative purposes only and are not limited to this structure in terms of the scope of patent protection.
[0107] The data query program 12, which converts natural language to database query language and is stored in the memory 11 of the electronic device 1, is a combination of multiple instructions. When run in the processor 10, it can achieve the following: S110: Based on the multimodal understanding agent, it parses the query requirements of natural language input, combines historical conversation information and business terminology, eliminates ambiguity and generates standardized queries; S120: Determine the query complexity based on the standardized query, and select the corresponding query strategy based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph; S130: The preliminary query results are optimized through a multi-agent collaborative working mode to obtain the initial SQL query results; S140: Simulate the execution of the initial SQL query result in an isolated environment, and obtain the verified SQL query by performing full-link verification on the execution result; S150: Execute the SQL query to obtain the data query results.
[0108] Specifically, the processor 10's implementation method for the above instructions can be found in [reference needed]. Figure 2 The descriptions of the relevant steps in the corresponding embodiments are not repeated here. Furthermore, if the modules / units integrated in the electronic device 1 are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. The computer-readable medium may include: any entity or system 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, or a read-only memory (ROM).
[0109] This invention also provides a computer-readable storage medium, which may be non-volatile or volatile, and stores a computer program that, when executed by a processor, implements the aforementioned data query method for converting natural language to database query language.
[0110] Specifically, the specific implementation method when the computer program is executed by the processor can be referred to the description of the relevant steps in the data query method for converting natural language to database query language in the embodiment, and will not be repeated here.
[0111] In the several embodiments provided by this invention, it should be understood that the disclosed devices, systems, and methods can be implemented in other ways. For example, the system embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and other division methods may be used in actual implementation.
[0112] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0113] Furthermore, the functional modules in the various embodiments of this invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or in a combination of hardware and software functional modules. Clearly, the word "comprising" does not exclude other units or steps, and the singular does not exclude the plural. Multiple units or systems stated in the system claims can also be implemented by a single unit or system through software or hardware.
[0114] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the invention can be implemented in other specific forms without departing from its spirit or essential characteristics. Therefore, the embodiments should be considered in all respects as exemplary and non-limiting, and the scope of the invention is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be embraced within the present invention. No appended diagram markings in the claims should be construed as limiting the scope of the claims.
[0115] The data query method and system for converting natural language to database query language according to the present invention have been described above by way of example with reference to the accompanying drawings. However, those skilled in the art should understand that various modifications can be made to the data query method and system for converting natural language to database query language proposed in the present invention without departing from the scope of the present invention. Therefore, the scope of protection of the present invention should be determined by the content of the appended claims.
Claims
1. A data query method for converting natural language to a database query language, characterized in that, include: Based on the multimodal understanding agent, the query requirements of natural language input are parsed, and combined with historical conversation information and business terminology, ambiguity is eliminated and standardized queries are generated. The query complexity is determined based on the standardized query, and a corresponding query strategy is selected based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph; The preliminary query results are optimized using a multi-agent collaborative working mode to obtain the initial SQL query results; The initial query result of the SQL was simulated in an isolated environment. By performing full-link verification on the execution result, the SQL query that passed the verification was obtained. Execute the SQL query to obtain the data query results.
2. The data query method for converting natural language to database query language as described in claim 1, characterized in that, The methods for constructing the knowledge graph include: Metadata is extracted from the target database based on a preset data extraction strategy; wherein, the metadata includes table structure, field types, primary and foreign key constraints, and index information; The extracted metadata is standardized to obtain standardized metadata; A knowledge graph is constructed based on the standardized metadata; wherein the nodes of the knowledge graph include tables, fields and business terms, and the edges include primary and foreign key relationships, business logic relationships and semantic association relationships.
3. The data query method for converting natural language to database query language as described in claim 2, characterized in that, The standardization process for the extracted metadata includes: The extracted metadata is cleaned. Perform business semantic enhancement processing on the cleaned metadata; The enhanced metadata is stored in a standardized manner according to the MCP protocol format.
4. The data query method for converting natural language to database query language as described in claim 2 or 3, characterized in that, After constructing the knowledge graph, the following is also included: The knowledge graph self-optimizing agent monitors changes to the target database in real time and updates the topology of the knowledge graph based on these changes.
5. The data query method for converting natural language to database query language as described in claim 4, characterized in that, The multimodal understanding agent employs a domain-adapted large language model to understand the intent of the natural language through multiple stages, including: By using basic semantic parsing, the query subject, conditions, and operation type of the natural language are identified, and the referential problems in the natural language are solved by combining the conversation history. Transform the colloquial expressions in the natural language into business terminology; For tabular data, the TAPEX model is used to understand Excel data, automatically identify the relationship between table headers and data, and extract query conditions; for image processing, OCR technology is used to identify key information in screenshots, and the CLIP model is used to determine the image service type.
6. The data query method for converting natural language to database query language as described in claim 5, characterized in that, The criteria for determining a simple query include single-table operation, no nesting, and no more than 3 conditions; the cache template is stored in a high-frequency query template library maintained in the background. The criteria for determining complex queries include multi-table JOINs, subqueries, aggregate functions, and special operators; the traversal of the knowledge graph includes: performing precise path exploration through a graph agent, anchoring the starting point to the core table node, loading all attribute fields and related edges of the core table node, exploring multiple paths to discover all possible paths, calculating weights based on historical query success rate, data freshness, and association strength, and selecting the optimal path; simultaneously, the graph agent supplements business rules and data permissions in real time to generate the final optimized execution plan.
7. The data query method for converting natural language to database query language as described in claim 6, characterized in that, The optimization of the preliminary query results through a multi-agent collaborative working mode includes: Four specialized intelligent agents are deployed: syntax generation, business logic, performance optimization, and access control. Each agent independently generates candidate SQL versions from different dimensions. Among them, the syntax generation agent is used to ensure that the statement conforms to the standard specification, the business logic agent is used to inject industry rule constraints, the performance optimization agent is used to optimize the execution strategy, and the access control agent is used to add data permission filtering. A voting mechanism based on business priority is used to filter the candidate SQL versions to obtain the filtered candidate solutions; The candidate solutions are pre-evaluated by performing a cost evaluation model, which scores them on a 100-point scale based on three dimensions: business compliance, execution efficiency, and security. The optimal solution is then selected as the initial SQL query result after calculating the comprehensive score.
8. The data query method for converting natural language to database query language as described in claim 7, characterized in that, The end-to-end verification adopts a layered verification mechanism, which includes: At the syntax level, the database engine's pre-compilation interface is called to check the SQL syntax correctness in the initial SQL query results, and to identify and correct the basic errors in the initial SQL query results. Simulate users with different roles to execute queries at the permission layer to verify whether field-level access control is strictly effective, and perform permission review on fields marked as sensitive data. At the business logic layer, the rules engine checks whether the query meets the preset business constraints.
9. The data query method for converting natural language to database query language as described in claim 8, characterized in that, After obtaining the data query results, it also includes: The data query results are then subjected to in-depth processing and optimization.
10. A data query system that converts natural language to a database query language, characterized in that, include: The Query rewriting module is used to parse the query requirements of natural language input based on the multimodal understanding agent, and combine historical conversation information and business terminology to eliminate ambiguity and generate standardized queries. The semantic retrieval module is used to determine the query complexity based on the standardized query, and select the corresponding query strategy based on the query complexity to perform the query on the standardized query to obtain preliminary query results; wherein, the query complexity includes simple query and complex query, the query strategy corresponding to the simple query is to directly match the cached template, and the query strategy corresponding to the complex query is to traverse the knowledge graph; The SQL generation and optimization module is used to optimize the preliminary query results through a multi-agent collaborative working mode to obtain the initial SQL query results. The SQL detection module is used to simulate the execution of the initial SQL query results in an isolated environment, and obtain the verified SQL query by performing full-link verification on the execution results; The SQL execution module is used to execute the SQL query and obtain the data query results.
11. The data query system for converting natural language to database query language as described in claim 10, characterized in that, It also includes a metadata extraction module and a knowledge graph construction module, among which, The metadata extraction module is used to extract metadata from the target database based on a preset data extraction strategy, and to standardize the extracted metadata to obtain standardized metadata; wherein, the metadata includes table structure, field types, primary and foreign key constraints, and index information; The knowledge graph construction module is used to construct a knowledge graph based on the standardized metadata, and to monitor changes in the target database in real time based on the graph self-optimizing agent, and update the topology of the knowledge graph according to the changes in the target database; wherein, the nodes of the knowledge graph include tables, fields and business terms, and the edges include primary and foreign key relationships, business logic relationships and semantic association relationships.
12. An electronic device, characterized in that, The electronic device includes a memory, a processor, and a data query program in a natural language to database query language stored in the memory and executable on the processor. When the data query program in the natural language to database query language is executed by the processor, it implements the data query method in the natural language to database query language as described in any one of claims 1 to 9.
13. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by the processor, it implements the data query method for converting natural language to database query language as described in any one of claims 1 to 9.
Citation Information
Patent Citations
Intelligent SQL query generation method and system based on large language model
CN118861081A
SQL generation method and apparatus based on large language model, device and storage medium
WO2025179827A1