Database query statement generation method and device, equipment and storage medium

CN123011944APending Publication Date: 2026-10-30TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511110210.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-07
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

[0004]但是,由于该方法仍然需要人工编写用于指示数据库操作的自然语言,导致耗费人力和时间,进而影响了数据库查询语句的生成效率

Benefits of technology

[0079]本申请实施例提供的方法、装置、计算机设备及存储介质,利用大语言模型,根据提示文本和数据库的参考查询语句,自动生成数据库的语句模板,从而降低了对人工编写语句模板的依赖,即使面对不同数据库或者数据库发生变化,也可以快速生成针对指定数据库的语句模板,提高了语句模板的生成效率。进而,利用智能生成的语句模板以及数据库的元数据,自动生成数据库的多个测试查询语句,以便对数据库进行模糊测试。因此,语句模板的生成过程以及数据库查询语句的生成过程均无需依赖人力,从而提高了数据库查询语句的生成效率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123011944A_ABST
    Figure CN123011944A_ABST
Patent Text Reader

Abstract

The application discloses a database query statement generation method and device, computer equipment and a storage medium, and belongs to the technical field of computers. The method comprises the following steps: obtaining a reference query statement, wherein the reference query statement is used for indicating an operation performed on a database; generating a statement template based on a prompt text and the reference query statement through a large language model, wherein the prompt text is used for instructing the large language model to generate the statement template, and the statement template is used for generating a query statement of the database; obtaining metadata of the database, wherein the metadata is used for describing data of the database; generating a plurality of test query statements based on the metadata and the statement template, wherein the plurality of test query statements are used for performing a fuzzy test on the database. In the application, the statement template for the specified database can be quickly generated, and then the test query statement is automatically generated by using the intelligently generated statement template, without relying on manpower, so that the generation efficiency of the database query statement is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for generating database query statements. Background Technology

[0002] Databases are typically operated based on database query statements such as SQL (Structured Query Language). In database fuzzing, it is necessary to generate a large number of query statements and submit them to the database for processing in order to test the potential defects of the database under extreme input conditions.

[0003] In related technologies, large language models are used to convert natural language used to instruct database operations into SQL statements, thereby generating a large number of SQL statements.

[0004] However, since this method still requires manual writing of natural language to instruct database operations, it consumes manpower and time, which in turn affects the efficiency of generating database query statements. Summary of the Invention

[0005] This application provides a method, apparatus, device, and storage medium for generating database query statements, which can improve the efficiency of database query statement generation. The technical solution is as follows:

[0006] On the one hand, a method for generating database query statements is provided, the method comprising:

[0007] Obtain a reference query statement, which is used to indicate the operation to be performed on the database;

[0008] Using a large language model, a statement template is generated based on the prompt text and the reference query statement. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database.

[0009] Obtain the metadata of the database, wherein the metadata refers to data used to describe the database;

[0010] Based on the metadata and the statement template, multiple test query statements are generated, which are used to perform fuzz testing on the database.

[0011] Optionally, the evaluation parameters for determining the first statement template include at least one of the following:

[0012] Based on the metadata and the first statement template, a plurality of first query statements are generated. Based on the accuracy of the plurality of first query statements, a first evaluation parameter of the first statement template is determined. The first evaluation parameter is used to reflect the accuracy of the first statement template.

[0013] Based on the number of atoms in the first statement template, a second evaluation parameter of the first statement template is determined. The second evaluation parameter is used to reflect the scalability of the first statement template. The atom is the basic unit used to construct the query statement of the database.

[0014] Based on the similarity between the plurality of first query statements and the first sample query statement, a third evaluation parameter is determined for the first statement template. The third evaluation parameter is used to reflect the matching degree between the first statement template and the first sample query statement.

[0015] Optionally, the atom library includes at least one of the following types of atoms:

[0016] Element atoms, which are used to represent basic elements in the database;

[0017] Operation atoms, which represent operations performed by the database;

[0018] A combined atom is obtained by combining at least one element atom and at least one operational atom.

[0019] Optionally, the reference query statement includes at least one of the following:

[0020] The historical business query statements of the database refer to the query statements that the database has executed in the business scenario.

[0021] The historical test query statements of the database refer to the query statements that the database has executed in fuzz testing;

[0022] The historical error query statements of the database refer to the query statements that encountered errors during the execution of the database.

[0023] On the other hand, a database query statement generation apparatus is provided, the apparatus comprising:

[0024] The first acquisition module is used to acquire a reference query statement, which is used to indicate the operation to be performed on the database.

[0025] The first generation module is used to generate a statement template based on the prompt text and the reference query statement using a large language model. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database.

[0026] The second acquisition module is used to acquire the metadata of the database, wherein the metadata refers to data used to describe the database;

[0027] The second generation module is used to generate multiple test query statements based on the metadata and the statement template, and the multiple test query statements are used to perform fuzz testing on the database.

[0028] Optionally, the first generation module is configured to:

[0029] The statement template is generated based on the prompt text, the reference query statement, and the atomic library using a large language model.

[0030] The atom library stores multiple atoms, which are the basic units used to construct query statements for the database.

[0031] Optionally, the first generation module includes:

[0032] The retrieval unit is used to find the reference atom in the atom library that has the highest similarity to the reference query statement;

[0033] The first generation unit is used to input the prompt text, the reference query statement, and the reference atom into the large language model to obtain the statement template output by the large language model.

[0034] Optionally, the retrieval unit is used for:

[0035] The reference query statement is parsed to obtain multiple target statement elements in the reference query statement;

[0036] In the atomic library, the reference atom with the highest similarity to each target statement element is searched.

[0037] Optionally, the retrieval unit is used for:

[0038] The reference query statement is parsed to obtain all the statement elements in the reference query statement;

[0039] The statement element that differs from the general atom in the atomic library among all statement elements is identified as the target statement element, where the general atom refers to an atom that is unrelated to business logic; or, all statement elements are identified as the target statement element.

[0040] Optionally, the first generating unit is configured to:

[0041] In the case where the plurality of target statement elements are statement elements different from the general atom, a general atom description text is determined, which is used to describe the general atom;

[0042] The prompt text, the reference query statement, the reference atom, and the general atom description text are input into the large language model to obtain the statement template output by the large language model.

[0043] Optionally, the first generation module includes:

[0044] The model training unit is used to obtain a first sample query statement and a first tag statement template, wherein the syntax structure of the first tag statement template is consistent with the syntax structure of the first sample query statement.

[0045] The model training unit is also used to train the large language model based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library to obtain a template generation model;

[0046] The second generation unit is used to input the prompt text and the reference query statement into the template generation model to obtain the statement template output by the template generation model.

[0047] Optionally, the model training unit is used for:

[0048] The prompt text, the first sample query statement, and the atomic library are input into the large language model to obtain the first sample statement template output by the large language model.

[0049] With the goal of reducing the difference between the first sample sentence template and the first tag sentence template, the large language model is trained to obtain the template generation model.

[0050] Optionally, the model training unit is used for:

[0051] Retrieve multiple candidate query statements;

[0052] For each candidate query statement, the candidate query statement, the prompt text, and the atomic library are input into the large language model to obtain the first statement template corresponding to the candidate query statement output by the large language model. The evaluation parameters of the first statement template are determined, and the evaluation parameters are used to reflect the availability of the first statement template.

[0053] For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the availability conditions, then the candidate query statement is determined as the first sample query statement, and the first statement template is determined as the first tag statement template.

[0054] Optionally, the model training unit is used for at least one of the following:

[0055] Based on the metadata and the first statement template, a plurality of first query statements are generated. Based on the accuracy of the plurality of first query statements, a first evaluation parameter of the first statement template is determined. The first evaluation parameter is used to reflect the accuracy of the first statement template.

[0056] Based on the number of atoms in the first statement template, a second evaluation parameter of the first statement template is determined. The second evaluation parameter is used to reflect the scalability of the first statement template. The atom is the basic unit used to construct the query statement of the database.

[0057] Based on the similarity between the plurality of first query statements and the first sample query statement, a third evaluation parameter is determined for the first statement template. The third evaluation parameter is used to reflect the matching degree between the first statement template and the first sample query statement.

[0058] Optionally, the model training unit is used for:

[0059] Based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library, the large language model is trained to obtain an initial template generation model;

[0060] For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the unavailable condition, then the candidate query statement is determined as the second sample query statement, and the first statement template is determined as the negative label statement template corresponding to the second sample query statement.

[0061] The first target text, the second sample query statement, the atomic library, and the negative label statement template are input into the initial template generation model to obtain the first probability output by the initial template generation model. The first probability represents the probability of generating the negative label statement template given the second sample query statement. The first target text is used to instruct the initial template generation model to predict the probability of generating the negative label statement template given the second sample query statement.

[0062] With the goal of reducing the first probability, the initial template generation model is trained to obtain the template generation model.

[0063] Optionally, the number of the second sample query statements is multiple; the model training unit is further configured to:

[0064] For each second sample query statement, the prompt text, the second sample query statement, and the atomic library are input into the initial template generation model to obtain the second statement template output by the initial template generation model;

[0065] For each second sample query statement, if the evaluation parameters of the second statement template corresponding to the second sample query statement meet the availability conditions, then the second statement template is determined as the positive label statement template corresponding to the second sample query statement.

[0066] The second target text, the second sample query statement, the atomic library, and the positive label statement template are input into the initial template generation model to obtain the second probability output by the initial template generation model. The second probability represents the probability of generating the positive label statement template given the second sample query statement. The second target text is used to instruct the initial template generation model to predict the probability of generating the positive label statement template given the second sample query statement.

[0067] With the goal of reducing the first probability and increasing the second probability, the initial template generation model is trained to obtain the template generation model.

[0068] Optionally, the atom library includes at least one of the following types of atoms:

[0069] Element atoms, which are used to represent basic elements in the database;

[0070] Operation atoms, which represent operations performed by the database;

[0071] A combined atom is obtained by combining at least one element atom and at least one operational atom.

[0072] Optionally, the reference query statement includes at least one of the following:

[0073] The historical business query statements of the database refer to the query statements that the database has executed in the business scenario.

[0074] The historical test query statements of the database refer to the query statements that the database has executed in fuzz testing;

[0075] The historical error query statements of the database refer to the query statements that encountered errors during the execution of the database.

[0076] On the other hand, a computer device is provided, the computer device including a processor and a memory, the memory storing at least one computer program, the at least one computer program being loaded and executed by the processor to perform the operations performed by the database query statement generation method as described above.

[0077] On the other hand, a computer-readable storage medium is provided that stores at least one computer program, which is loaded and executed by a processor to perform the operations performed by the database query statement generation method described above.

[0078] On the other hand, a computer program product is provided, including a computer program loaded and executed by a processor to perform the operations performed by the database query statement generation method as described above.

[0079] The methods, apparatus, computer devices, and storage media provided in this application utilize a large language model to automatically generate database statement templates based on prompt text and reference query statements in the database. This reduces reliance on manually written statement templates and allows for rapid generation of statement templates for specific databases, even when dealing with different databases or database changes, thus improving the efficiency of statement template generation. Furthermore, using the intelligently generated statement templates and database metadata, multiple test query statements are automatically generated for fuzz testing of the database. Therefore, both the statement template generation process and the database query statement generation process do not require manual intervention, thereby improving the efficiency of database query statement generation. Attached Figure Description

[0080] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0081] Figure 1 This is a schematic diagram of a computer system provided in an embodiment of this application;

[0082] Figure 2 This application provides a method for generating database query statements.

[0083] Figure 3 This is a flowchart of another method for generating database query statements provided in an embodiment of this application;

[0084] Figure 4 This is a flowchart of another method for generating database query statements provided in an embodiment of this application;

[0085] Figure 5 This is a flowchart of another method for generating database query statements provided in an embodiment of this application;

[0086] Figure 6 This is a flowchart of a model training method provided in an embodiment of this application;

[0087] Figure 7 This is a schematic diagram of a model training method provided in an embodiment of this application;

[0088] Figure 8 This is a schematic diagram of the structure of a database query statement generation device provided in an embodiment of this application;

[0089] Figure 9 This is a schematic diagram of another database query statement generation device provided in an embodiment of this application;

[0090] Figure 10 This is a schematic diagram of the structure of a terminal provided in an embodiment of this application;

[0091] Figure 11 This is a schematic diagram of the structure of a server provided in an embodiment of this application. Detailed Implementation

[0092] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.

[0093] It is understood that the terms "first," "second," etc., used in this application may be used to describe various concepts herein, but unless otherwise stated, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of this application, the first device may be referred to as the second device, and similarly, the second device may be referred to as the first device.

[0094] "At least one" refers to one or more. For example, at least one block device can be one block device, two block devices, three block devices, or any integer number of block devices greater than or equal to one. "Multiple" refers to two or more. For example, multiple block devices can be two block devices, three block devices, or any integer number of block devices greater than or equal to two. "Each" refers to each of the at least one. For example, each block device refers to each of the multiple block devices. If the multiple block devices are three block devices, then each block device refers to each of the three block devices.

[0095] It should be noted that the information (including but not limited to user equipment information, user personal information, etc.), data (including but not limited to data used for analysis, data stored, data displayed, etc.) and signals (including but not limited to signals transmitted between user terminals and other devices) involved in this application have all been fully authorized by the user or relevant parties, and the collection, use and processing of the relevant data shall comply with the relevant laws, regulations and standards of the relevant countries and regions.

[0096] For example, the query statements, databases, atomic libraries, statement templates, etc. involved in this application are all fully authorized by users or relevant parties, and the collection, use and processing of related data must comply with the relevant laws, regulations and standards of relevant countries and regions.

[0097] For ease of understanding, the concepts involved in the embodiments of this application are explained below.

[0098] 1. Large Language Model (LLM): An artificial intelligence model trained on massive amounts of data, capable of understanding and generating human language. Large language models can be used for various tasks, such as code generation, text generation, machine translation, and question-answering systems.

[0099] Large language model-based learning methods are mainly divided into two types: context learning and fine-tuning. Context learning allows large language models to adjust their output by parsing the context of the input, such as the output format (outputting according to a specific format) and the task domain (e.g., translating English into French). Fine-tuning refers to training a pre-trained large language model using domain-specific or task-specific data to improve its performance on a specific task.

[0100] 2. Retrieval-Augmented Generation (RAG): A technique that combines information retrieval and generation to enhance model performance. It retrieves relevant information from a large database and uses the retrieved information as context input into the model, thereby generating more accurate output.

[0101] For example, during reasoning, large language models are still prone to the "illusion" phenomenon, i.e., generating inaccurate or untrue information. Therefore, by using vector databases to retrieve relevant knowledge, combining the recalled knowledge with the question, and learning the context of the large language model, the "illusion" phenomenon can be mitigated. In this way, RAG technology can significantly improve the reliability and accuracy of large language models.

[0102] 3. Vector Database: A database system specifically designed for storing, managing, and querying high-dimensional vector data. Through efficient indexing structures and similarity search algorithms, it can achieve fast and accurate querying and analysis on large-scale datasets.

[0103] 4. Fuzz Testing: In the field of database testing, fuzz testing is a commonly used and effective automated testing method. Its basic idea is to automatically generate a large amount of random or semi-random input data (such as SQL statements) and submit it to the database to test for potential defects, crashes, or security vulnerabilities under abnormal or extreme inputs. Fuzz testing can effectively cover the input space that traditional testing cannot exhaustively explore, increasing the probability of defect discovery.

[0104] 5. SQL (Structured Query Language): SQL is a database query and programming language used to access, query, update, and manage relational database systems. SQL is a high-level, non-procedural programming language that allows users to work on high-level data structures.

[0105] 6. RQG (Random Query Generator): A database fuzzing tool used to automatically generate a large number of valid SQL statements based on predefined SQL statement templates and randomization strategies, and submit the generated SQL statements to the database for execution to test the database.

[0106] The RQG workflow includes the following: (1) Data configuration and template definition: Define the database table structure, field types, indexes, constraints and other metadata through configuration files (zz file), and specify the SQL statement template (yy file). (2) Randomized SQL statement generation: RQG automatically generates SQL statements based on the configured statement template and metadata using a randomization algorithm. The randomization includes table names, field names, operation types, conditional expressions, nested queries, function calls, etc. (3) SQL statement execution and result detection: RQG submits the generated SQL statements to the database for execution and detects the database response in real time, including execution results, error messages, crash logs, etc.

[0107] Databases are typically operated based on database query statements such as SQL (Structured Query Language). In database fuzzing, it is necessary to generate a large number of query statements and submit them to the database for processing in order to test the potential defects of the database under extreme input conditions.

[0108] In the field of database testing, although a large number of SQL statements have been accumulated, including actual business SQL statements, open-source regression SQL statements, and historical defect SQL statements, the limited number and types of SQL statements cannot fully cover the syntax structure and boundary conditions of SQL statements due to the high diversity and complexity of SQL statements themselves. Therefore, relying solely on these existing SQL statements for testing has the following limitations:

[0109] (1) Limited coverage: Existing SQL statements often focus on common business operations and are difficult to cover all syntax branches, complex nesting, boundary conditions and abnormal scenarios.

[0110] (2) Insufficient mutation capability: Directly executing existing SQL statements does not allow for flexible mutation of SQL statements, such as replacing table names, fields, operators, adjusting the nesting level, etc. It is difficult to automatically generate new, untested SQL statements, resulting in limited test depth and breadth.

[0111] (3) Poor adaptability: Existing SQL statements are usually bound to specific databases, making it difficult to adapt to different database table structures, field types, indexes and other metadata, affecting the relevance and effectiveness of testing.

[0112] Based on this, embodiments of this application provide a statement template that abstracts SQL statements into RQG. RQG can automatically and massively generate and execute SQL statements, greatly improving testing efficiency. Therefore, using RQG to generate SQL statements can achieve the following:

[0113] (1) Automatically generate diverse SQL statements: Based on statement templates, it can generate SQL statements with diverse structures and rich semantics in batches, which greatly improves the number and coverage of test cases.

[0114] (2) Flexible combination and variation: Supports flexible combination and variation of different SQL statement fragments (such as expressions, subqueries, operators, etc.) to automatically explore more potential database behaviors and abnormal scenarios.

[0115] (3) Dynamically adapt to the database: Combine the database's metadata to automatically generate SQL statements that match the database, thereby improving the relevance and effectiveness of the test.

[0116] (4) Continuous evolution and optimization: The statement template can be continuously expanded and optimized according to new business scenarios and defective SQL statements, thereby continuously improving testing capabilities.

[0117] However, while RQG has played an important role in database fuzzing, it still has the following limitations:

[0118] (1) Statement template driven, limited coverage: Because RQG relies on predefined SQL statement templates and randomization strategies, although the generated SQL statements are syntactically valid, they often lack the semantic complexity of business scenarios. For example, the generated SQL statements rarely involve advanced features such as complex associations, nesting, and window functions in real business scenarios, making it difficult to cover the deep logic of the database.

[0119] (2) Complex configuration and high maintenance cost: With the changes in database and business requirements, RQG configuration files and statement templates need to be adjusted frequently, which increases the maintenance burden on testers.

[0120] (3) Difficult to simulate real business load: The SQL statements generated by RQG are significantly different from the SQL statements in real business scenarios, making it difficult to simulate the database load and data access patterns in real business scenarios, which affects the representativeness and validity of the test results.

[0121] Based on this, this application proposes a database query statement generation method based on a large language model and RQG. This method can fully utilize the structured information of the database, historical business SQL statements, and historical erroneous SQL statements to automatically generate high-quality statement templates. Then, RQG is used to automatically generate SQL statements based on these templates. This alleviates problems such as insufficient semantic coverage, high configuration and maintenance costs, and deviations from real business scenarios, thereby improving the depth and breadth of database testing. The following provides a detailed description of the database query statement generation method provided in this application.

[0122] The database query statement generation method provided in this application can be used in computer devices. Optionally, the computer device is a terminal or a server. Optionally, the server is an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. Optionally, the terminal is a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, smart voice interaction device, smart home appliance, vehicle terminal, aircraft, etc., but is not limited to these. This application can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, and assisted driving.

[0123] Figure 1 This is a schematic diagram of a computer system provided in an embodiment of this application. See also: Figure 1The computer system includes a terminal 101 and a server 102. The terminal 101 and the server 102 are connected via a wireless or wired network.

[0124] Terminal 101 has an application 111 installed and running. This application 111 can be a social application, an online payment application, an online shopping application, a game application, a medical service application, a video application, or the like. When terminal 101 runs application 111, the user interface of application 111 is displayed on the screen of terminal 101. Terminal 101 is the terminal used by user 121.

[0125] Optionally, terminal 101 can refer to one of a plurality of terminals. As those skilled in the art will know, the number of terminals can be more or less. For example, there may be only one terminal, or there may be six, eight or more terminals. This application does not limit the number of terminals or the type of device.

[0126] Figure 1 Only one terminal is shown in the diagram, but in different embodiments, multiple other terminals 103 can access the server 102. Optionally, one or more terminals 103 may also be terminals corresponding to developers, on which an application development and editing platform is installed. Developers can edit and update applications on terminals 103 and transmit the updated application installation package to the server 102 via wired or wireless network. Terminal 101 can download the application installation package from the server 102 to update the application.

[0127] Terminal 101 and other terminals 103 are connected to server 102 via wired or wireless networks.

[0128] Server 102 includes at least one of a single server, multiple servers, a cloud computing platform, and a virtualization center. Server 102 is used to provide background services for applications. Optionally, server 102 undertakes the primary computing work, and terminal 101 undertakes the secondary computing work; or, server 102 undertakes the secondary computing work, and terminal 101 undertakes the primary computing work; or, server 102 and terminal 101 collaborate on computing using a distributed computing architecture.

[0129] In this embodiment, server 102 uses the database query statement generation method provided in this embodiment to automatically generate multiple test query statements, performs fuzz testing on the database using these multiple test query statements, and optimizes the database based on the test results. Terminal 101 sends an operation command to server 102 targeting the database. This operation command carries a query statement. In response to the operation command, server 102 executes the query statement in the operation command, obtains the operation result, and returns the operation result to terminal 101. Optionally, the query statement in subsequent operation commands can be used as historical business query statements. Server 102 uses these historical business query statements and the database query statement generation method provided in this embodiment to continue generating multiple test query statements to perform fuzz testing on the database, thereby achieving iterative optimization of the database based on query statements generated in the business scenario.

[0130] It should be noted that the above implementation environment is only an example. The method provided in this application embodiment can also be executed by terminal 101 or server 102 alone, or by other computer devices. This application embodiment does not limit this.

[0131] Figure 2 This is a flowchart illustrating a method for generating database query statements according to an embodiment of this application. This embodiment is executed by a computer device. See also... Figure 2 The method includes:

[0132] 201. The computer device obtains the reference query statement, which is used to indicate the operation to be performed on the database.

[0133] The reference query statement can be any type of structured query statement, such as an SQL statement. This reference query statement can be any historical query statement that has been executed on the database.

[0134] A database is a repository in a computer device used to store and manage data.

[0135] The number of reference query statements obtained by the computer device can be one or more.

[0136] 202. The computer device generates a statement template based on the prompt text and reference query statement through a large language model. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database.

[0137] Large language models are deep learning models with the ability to understand and generate semantics on a large scale. They can perform natural language processing tasks based on input content, such as text understanding, content summarization, and sentence template generation.

[0138] The prompt text is used to instruct the large language model to generate a statement template based on the reference query statement. For example, the prompt text could be "Please convert the following SQL statement into a MySQL RQG statement template".

[0139] The statement template has a syntax structure applicable to the database and includes at least one unassigned variable, each corresponding to a variable type. In this embodiment, since the statement template is generated based on a reference query statement, its syntax structure matches that of the reference query statement.

[0140] 203. Computer devices acquire database metadata, which refers to data used to describe the database.

[0141] Computer devices acquire database metadata, which refers to information used to describe the data structure and attributes in the database, such as the organization, storage structure, type, relationships, and constraints of the data in the database.

[0142] Optionally, the database's metadata includes table structure information, index information, constraint information, view information, trigger information, storage information, relationships between tables, statistical information, etc.

[0143] 204. The computer equipment generates multiple test query statements based on metadata and statement templates. These multiple test query statements are used to perform fuzz testing on the database.

[0144] Computer equipment uses the database metadata to randomly or semi-randomly assign values ​​to variables in the statement template, thereby generating multiple test query statements.

[0145] Optionally, the computer device generates multiple test query statements based on metadata and statement templates using RQG. In related technologies, the syntax templates in the RQG used for database fuzzing rely on manual writing and maintenance, resulting in problems such as large workload and low efficiency, making it difficult to quickly respond to business changes and database structure adjustments. However, the embodiments of this application utilize a large language model, which can automatically generate statement templates based on query statements in actual business scenarios, achieving a high degree of automation in configuring statement templates.

[0146] Optionally, the computer device acquires multiple different reference query statements. Through steps 201 to 203 above, multiple different statement templates can be automatically generated. Using each statement template, multiple test query statements corresponding to each statement template are automatically generated, resulting in multiple test query statements with different syntax structures and semantics. Specifically, multiple test query statements generated using the same statement template have the same syntax structure but different semantics. Multiple test query statements generated using different statement templates have different syntax structures.

[0147] The method provided in this application utilizes a large language model to automatically generate database query templates based on prompt text and reference query statements in the database. This reduces reliance on manually written query templates and allows for rapid generation of query templates for specific databases, even when dealing with different databases or database changes, thus improving query template generation efficiency. Furthermore, using the intelligently generated query templates and database metadata, multiple test query statements are automatically generated for fuzz testing of the database. Therefore, both the query template generation process and the database query statement generation process are automated, significantly improving the efficiency of database query statement generation.

[0148] In some embodiments, the computer device generates a statement template based on a large language model, prompt text, reference query statements, and an atom library. The atom library stores multiple atoms, which are the basic units used to construct query statements for the database.

[0149] Since the statement templates for the database need to be suitable for the database, when generating statement templates using a large language model, an atomic library is provided to the large language model so that the large language model can learn the structured information of the database query statements from the atomic library, thereby generating more accurate statement templates.

[0150] The generation of sentence templates using a large language model, based on prompt text, reference query statements, and an atomic library, includes two methods. The first method involves retrieval enhancement based on the atomic library, using the external knowledge obtained from this enhancement to guide the large language model in generating sentence templates. The second method involves training a template generation model specifically for sentence template generation based on the atomic library, and then using this model to generate the sentence templates. The implementation processes for these two methods are detailed in the following embodiments.

[0151] In this implementation, in addition to the prompt text and reference query statement, an atomic library is provided to the large language model. The large language model can combine the atoms in the atomic library used to construct the query statement to generate corresponding statement templates. This allows the large language model to learn structured information related to query statements applicable to the database, making the statement template generation process more structured and controllable. Furthermore, by leveraging the combination of atoms, it can more flexibly adapt to the syntax rules and query requirements of different databases, thereby improving the accuracy and diversity of generated query statements and further enhancing adaptability and automation in complex scenarios.

[0152] In one possible implementation, to achieve efficient management and flexible expansion of query templates, the query statement is divided into multiple atoms based on its syntactic structure, forming an atom library. This atom library includes at least one of the following types of atoms:

[0153] (1) Element Atom: Element atoms are used to represent basic elements in the database.

[0154] Optionally, the element atoms include at least one of the following:

[0155] Table expression atoms, such as tables, subqueries, values ​​expressions, table functions, etc.

[0156] Column expression atoms, such as column names of various types.

[0157] Value expression atoms, such as arithmetic expressions, integer expressions, date expressions, character expressions, Boolean expressions, etc.

[0158] Atoms of conditional expressions, such as WHERE, HAVING, CASE WHEN, IF, etc.

[0159] Operator atoms, such as arithmetic operators, logical operators, IN / NOT logical operators, LIKE / NOT LIKE logical operators, EXISTS logical operators, etc.

[0160] (2) Operation atoms: Operation atoms are used to represent operations performed by the database.

[0161] Optionally, the operating atoms include at least one of the following:

[0162] DML operations are atomic, such as SELECT, INSERT, UPDATE, and DELETE statements.

[0163] DDL operations are atomic, such as the ALTER, CREATE, and DROP statements in data definition languages.

[0164] DCL operations are atomic, such as the SAVEPOINT statement, ROLLBACK statement, COMMIT statement, and other data control language operations.

[0165] PL / SQL basic operations include atomic operations such as assignment statements, dynamic SQL statements, DML statements, LOOP statements, IF statements, WHILE statements, and other control flow operations.

[0166] In this context, the element atoms mentioned above belong to the first-layer atoms, and the operational atoms mentioned above belong to the second-layer atoms. Both the first-layer and second-layer atoms are simple atoms. A simple atom consists of two parts: an atom name and its contents, separated by a colon. The entire simple atom ends with a semicolon, and the items within the contents are separated by "|". For example, the following is an example of a simple atom.

[0167] opr_logical: AND|OR|AND NOT|OR NOT;

[0168] opr_between: BETWEEN|NOT BETWEEN;

[0169] opr_compare:=|>|<|! =|<=|>=|<>|=|=|=|=;

[0170] (3) Combining atoms: Combining atoms is obtained by combining at least one element atom and at least one operational atom.

[0171] Among them, a composite atom is a high-level SQL structure that can directly reflect business needs or test objectives by combining at least one element atom and at least one operation atom based on element atoms and operation atoms, combined with business scenarios and complex logic.

[0172] Optionally, the combined atoms include at least one of the following:

[0173] Complex query atoms, such as multi-table joins, nested subqueries, window function queries, grouped statistics and aggregation analysis, and queries with complex WHERE / HAVING / ORDER BY clauses.

[0174] Transaction control atoms, such as statements used to implement multi-step transaction operations, distributed transaction operations, exception handling and compensation mechanisms, etc.

[0175] Complex PL / SQL process atoms, such as stored procedures, triggers, functions, and business logic implementations with multi-level nested control structures (IF / CASE / LOOP / WHILE, etc.).

[0176] The aforementioned combined atoms belong to the third layer of atoms, which are complex atoms. Complex atoms are composed of multiple simple atoms combined according to certain logical relationships, used to express more complex operations or semantics. Unlike simple atoms, which contain only a single rule, complex atoms can organically integrate multiple complexities through nesting, combination, recursion, and other methods to achieve stronger expressive power.

[0177] Taking the subquery atom in the composite atom as an example, according to the syntax structure of the query statement, the subquery atom includes simple atoms such as distinct_clause, targetlist_clause, from_clause, where_clause, groupby_clause, having_clause, etc., and supports nested recursive combination of various simple atoms such as union, join, expr.

[0178] For example, the following is an example of a subquery atom, with the following structure:

[0179] Subquery Atom

[0180]

[0181]

[0182]

[0183] The subquery atom includes the following functions:

[0184] (1) Parameterized configuration: By configuring distinct, group by, and available tables / columns through parameters (such as $is_distinct, $is_groupby, etc.), the versatility and scalability of subquery atoms are improved. The maximum recursion depth is controlled by the parameter ($maxdepth) to ensure the complexity of subquery atoms. Complex scenarios such as correlated subqueries are covered by passing aliases (@alias_list_init).

[0185] (2) Supports custom table and field types: Test metadata can be passed through arrays of different table and column types (such as @col_int, @col_varchar, @table_list, etc.) to facilitate adaptation to different databases and business scenarios.

[0186] (3) Multiple column types: For multiple data types such as int, varchar, date, timestamp, number, etc., corresponding select, where, having expressions are generated respectively to ensure the diversity and coverage of query statements.

[0187] (4) Multi-type expressions: covering commonly used comparison, range, set, null value and other expressions, including variables, constants, functions, system functions, CASE expressions and other types.

[0188] (5) Flexible nesting of subqueries and main queries: It supports nesting subqueries in multiple places such as from, where, having, etc., and the subqueries themselves can be nested further, realizing a high degree of restoration of SQL syntax and coverage of complex scenarios.

[0189] (6) Multiple query syntaxes: including single table / multiple table, with / without GROUP BY, with / without DISTINCT, different types of JOIN, nested queries, window functions, etc., covering a variety of uses of SQL subqueries.

[0190] (7) Alias ​​and context management: By automatically counting aliases and managing arrays, we ensure that the aliases of each table / subquery are unique, and automatically maintain context variables such as select list and alias list to ensure the correct passing and recycling of variables during recursion.

[0191] This application proposes a method for standardizing the configuration of statement templates. First, the RQG statement templates are standardized by pre-setting various atoms for constructing the templates, including element atoms, operation atoms, and composite atoms, covering various operation types such as expressions, subqueries, operators, and predicates. Simultaneously, composite atoms cover advanced SQL features in business scenarios, such as complex associations, nested queries, and window functions. Therefore, statement templates generated based on multiple atoms can more comprehensively reflect actual business needs and the complexity of database operations, laying the foundation for the subsequent automatic generation of diverse query statements.

[0192] Furthermore, a three-layer atomic hierarchy (element atoms, operation atoms, and combination atoms) is adopted to decompose complex grammatical structures into reusable unit-based structures, which facilitates subsequent automatic combination and management.

[0193] In one possible implementation, the aforementioned atom library can be a vector database, thereby storing atoms in a vectorized manner. A computer device acquires an atom file, which indicates multiple atoms, and imports the atom file into the vector database to obtain the atom library.

[0194] Optionally, for an atomic file, it is first split into multiple atoms using semicolons (;) as delimiters. For each atom, the part before the colon (:) is extracted as the atom name, and the part after the colon is extracted as the atom content. The complete atom content is then saved as the `original` field. Further, the atom content is split using vertical bars (|) to obtain multiple sub-parts, each defined as an element. Each element is vectorized, and this element, along with its corresponding atom name and the `original` field, forms a record stored in a vector database.

[0195] Optionally, for each atom, a description of the atom is automatically generated using a large language model, and the description is vectorized. Each description, along with the corresponding atom name and the original field, forms a record and is stored in a vector database.

[0196] In this embodiment, efficient vectorized storage of atoms and their descriptive information is achieved, providing a solid data foundation for subsequent similarity retrieval and automated generation of sentence templates.

[0197] In this embodiment of the application, the statement template is generated using a large language model based on prompt text, reference query statements, and atomic libraries in the following two ways.

[0198] The first generation method: enhanced retrieval based on the atomic library.

[0199] This application provides a method for generating statement templates. Based on the input reference query statement, it dynamically retrieves the most relevant reference atoms and uses these reference atoms to guide a large language model in generating statement templates. This achieves highly relevant and scalable statement template generation. For detailed process, please refer to the following... Figure 3 Examples of implementations. Figure 3 This is a flowchart illustrating a method for generating database query statements according to an embodiment of this application. This embodiment is executed by a computer device. See also... Figure 3 The method includes:

[0200] 301. The computer device obtains a reference query statement, which is used to indicate the operation to be performed on the database.

[0201] The reference query statement can be any type of structured query statement, such as an SQL statement. This reference query statement can be any historical query statement that has been executed on the database. The computer device can retrieve one or more reference query statements.

[0202] In one possible implementation, the reference query statement includes at least one of the following:

[0203] (1) Historical business query statements of the database. Historical business query statements refer to the query statements that the database has executed in the business scenario.

[0204] (2) Historical test query statements of the database. Historical test query statements refer to the query statements that the database has executed in fuzz testing.

[0205] (3) Historical error query statements of the database. Historical error query statements refer to query statements that have errors during the execution of the database.

[0206] In this implementation, the reference query statements include historical business query statements, historical test query statements, and historical error query statements. This fully leverages the historical query statements accumulated in the database under different usage scenarios, thereby improving the robustness and comprehensiveness of the statement template generation and providing higher accuracy, fault tolerance, and practical value for the automatic generation of database query statements.

[0207] Furthermore, based on historical business query statements, historical test query statements, and historical error query statements, it can automatically generate diverse, structurally rich, and highly adaptable statement templates, which can then be used to generate query statements suitable for real business scenarios.

[0208] 302. The computer device searches the atomic library for the reference atom with the highest similarity to the reference query statement.

[0209] The computer device determines the similarity between each atom in the atom library and the reference query statement, and normalizes the similarity so that the similarity value is limited to [-1, 1]. The computer device identifies atoms with a similarity greater than a similarity threshold as reference atoms.

[0210] In one possible implementation, for each retrieved reference atom, the computer device obtains the original field of the reference atom from the atom library, removes duplicates from all obtained original fields, and outputs the deduplication result as a list of reference atoms. Subsequently, the list of reference atoms is used as one of the input data of the large language model.

[0211] In one possible implementation, the computer device parses the reference query statement to obtain multiple target statement elements in the reference query statement; and in the atom library, it searches for the reference atom with the highest similarity to each target statement element.

[0212] The computer equipment parses the reference query statement to obtain multiple target statement elements, which can be keywords, operators, functions, etc., from the reference query statement. Then, each target statement element is vectorized, and the atom with the highest similarity to the target statement element is retrieved from the atom library (vector database) and used as the reference atom.

[0213] Optionally, each target statement element corresponds to a reference atom, so the number of reference atoms is equal to the number of target statement elements.

[0214] Optionally, after retrieving the atom with the highest similarity to the target statement element, the computer device determines whether the similarity is greater than a similarity threshold. If it is greater than the similarity threshold, the atom is used as a reference atom; otherwise, it is discarded, meaning it is not used as a reference atom. In this implementation, a similarity threshold is set, and only atoms with a similarity greater than the threshold are retained as reference atoms, thereby ensuring the quality of the retrieved reference atoms.

[0215] Optionally, the computer device performs deduplication on the retrieved reference atoms to ensure the quality of the retrieved reference atoms.

[0216] In one possible implementation, the reference query statement is parsed, including function identification, keyword extraction, and operator extraction.

[0217] Function identification: If the Token in the reference query statement belongs to a function, the function name is extracted, standardized (e.g., converted to uppercase), and then added to the function set. Simultaneously, the parameter Token of this function is recursively processed to ensure that nested functions can also be correctly identified.

[0218] Keyword extraction: If the Token in the reference query statement is a keyword, then the Token is standardized and added to the keyword set.

[0219] Operator extraction: If the Token in the reference query statement is an operator, such as =, >, <, etc., then add the Token to the operator set.

[0220] For example, the following query statement can be used as a reference:

[0221] SELECT ABS(id),name,SUM(score+1)

[0222] FROM chqin

[0223] WHERE id not BETWEEN'a'AND name

[0224] AND name LIKE '%abc%'

[0225] AND age not in (1,2,3)

[0226] AND EXISTS(SELECT 1FROMt2 WHERE t2.x=t1.x AND t2.y LIKE'abc%')

[0227] AND status IS NULL

[0228] AND score+1>=100

[0229] After parsing the reference query, the following content is obtained:

[0230] Keyword set: ['AND','BETWEEN','EXISTS','FROM','IN','IS','NOT','NULL','SELECT','WHERE']

[0231] Operator set: ['+', '=', '>=', 'LIKE']

[0232] Function set: ['ABS', 'SUM']

[0233] In this implementation, multiple target statement elements are extracted by parsing the reference query statement, and the reference atom with the highest similarity is searched in the atom library for each target statement element. This refines the granularity and accuracy of similarity matching, which helps to enhance the deep understanding of the structure and semantics of the query statement by the large language model during the statement template generation process, thereby improving the accuracy of the generated statement template.

[0234] Optionally, the computer device determines multiple target statement elements, including in the following two ways:

[0235] The first method is to parse the reference query statement to obtain all statement elements in the reference query statement; among all statement elements, those that are different from the general atoms in the atomic library are identified as target statement elements. General atoms refer to atoms that are unrelated to business logic.

[0236] For example, general-purpose atoms include table names, column names, constants, and expressions.

[0237] For example, non-universal atoms include functions, keywords, and operators.

[0238] The second method is to parse the reference query statement to obtain all the statement elements in the reference query statement, and then determine all the statement elements as the target statement elements.

[0239] 303. The computer device inputs the prompt text, reference query statement, and reference atom into the large language model to obtain the statement template output by the large language model. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database.

[0240] The computer device uses the retrieved reference atoms as context information, and inputs them together with the prompt text and reference query statement into the large language model to guide the large language model to generate a statement template.

[0241] In this implementation, the reference atom with the highest similarity to the reference query is retrieved from the atom library. This reference atom, the prompt text, and the reference query are then input into the large language model to generate a sentence template. This effectively enhances the relevance and contextual relevance of the semantic prompts, improves the adaptability of the generated sentence template to the reference query and the database, and contributes to improving the accuracy and stability of the generated sentence template. Furthermore, by providing reference atoms to the large language model through retrieval enhancement to generate the sentence template, there is no need to train a separate model specifically for generating sentence templates, thus improving processing efficiency.

[0242] In one possible implementation, the computer device obtains a first prompt template, which includes the prompt text, fills the first prompt template with a reference query statement and reference atoms to obtain first prompt information, and inputs the first prompt information into a large language model to obtain a statement template output by the large language model.

[0243] If the search targets statement elements in the reference query that are different from the general atom:

[0244] Therefore, steps 302 to 303 above include: the computer device parses the reference query statement to obtain all statement elements in the reference query statement, and identifies the statement elements that are different from the general atoms in the atomic library as target statement elements. In the atomic library, the reference atom with the highest similarity to each target statement element is searched. The computer device determines the general atom description text, and inputs the prompt text, reference query statement, reference atom, and general atom description text into the large language model to obtain the statement template output by the large language model. Here, a general atom refers to an atom unrelated to business logic, and the general atom description text is used to describe the general atom.

[0245] Optionally, the computer device obtains a second prompt template, which includes the prompt text and a general atom description text. The reference query statement and reference atoms are filled into the second prompt template to obtain the second prompt information. The second prompt information is then input into the large language model to obtain the statement template output by the large language model.

[0246] For example, the second prompt template is as follows:

[0247] Task Description:

[0248] Please convert the following SQL statement into a MySQL RQG statement template.

[0249] Task requirements:

[0250] Automatically identify the parts of the SQL statement that should be generalized. Generalize fields and expressions in the SQL statement to expression variables according to the "Basic Rules" below; in the generated statement template, all fields / expressions appearing are replaced with generalized variables; generalized variables do not need to be defined; when multiple generalization rules are available, prioritize the more general and higher-level generalization rule.

[0251] Basic rules:

[0252] {Reference Atom 1}{Reference Atom 2}...{Reference Atom N}

[0253] {General Table Atomic Description}, {General Column Atomic Description}, {General Expression Atomic Description}

[0254] Output format:

[0255] Output only the statement template, without any other explanation; the output is wrapped in ```sql` and ```.

[0256] Example use case:

[0257] {Input: xx; Output: xx}

[0258] Input example:

[0259] ```sql;{query}```"

[0260] In this implementation, by parsing the reference query statement, target statement elements that are different from the general atoms in the atomic library are distinguished. Target statement elements related to business logic are used as retrieval enhancement information for the reference query statement. This helps improve the large language model's deep understanding of structured information related to business logic, making the generated statement template more closely aligned with specific business needs and improving the accuracy and adaptability of the statement template.

[0261] Furthermore, when multiple target statement elements only include statement elements different from the general atom, a general atom description text is introduced. The general atom description text, along with the prompt text, reference query statement, and reference atom, are input into the large language model. Therefore, for statement elements in the reference query statement that are the same as the general atom, there is no need to perform a retrieval operation in the atom library. The general atom description text can be used directly to represent the statement elements that are the same as the general atom, which helps to improve processing efficiency.

[0262] If you want to retrieve all statement elements in the referenced query:

[0263] Therefore, steps 302 to 303 above include: the computer device parses the reference query statement to obtain all statement elements in the reference query statement, identifies all statement elements as target statement elements, and searches in the atom library for the reference atom with the highest similarity to each target statement element. The computer device inputs the prompt text, the reference query statement, and the reference atoms into the large language model to obtain the statement template output by the large language model.

[0264] In this implementation, all statement elements in the reference query statement are treated as target statement elements, enabling the large language model to comprehensively learn a deep understanding of the structure and semantics of the reference query statement, thereby improving the accuracy of the statement template.

[0265] 304. Computer devices acquire database metadata, which refers to data used to describe the database.

[0266] Computer devices acquire database metadata, which refers to information used to describe the data structure and attributes in the database, such as the organization, storage structure, type, relationships, and constraints of the data in the database.

[0267] Optionally, the database's metadata includes table structure information, index information, constraint information, view information, trigger information, storage information, relationships between tables, statistical information, etc.

[0268] 305. The computer equipment generates multiple test query statements based on metadata and statement templates. These multiple test query statements are used to perform fuzz testing on the database.

[0269] Computer equipment uses the database metadata to randomly or semi-randomly assign values ​​to variables in the statement template, thereby generating multiple test query statements.

[0270] Optionally, the computer device generates multiple test query statements based on metadata and statement templates using RQG.

[0271] Figure 4 This is a flowchart of another method for generating database query statements provided in an embodiment of this application, such as... Figure 4 As shown, the computer device generates statement templates based on query statements and an atomic library using a large language model. Then, using RQG, it generates test query statements for the database based on these statement templates and the database's metadata. Multiple test query statements are used to test the database. Further development personnel can optimize the atomic library based on the test results, thereby improving the prompts provided by the large language model and enabling the entire system to self-evolve and optimize.

[0272] The method provided in this application utilizes a large language model to automatically generate database query templates based on prompt text and reference query statements in the database. This reduces reliance on manually written query templates and allows for rapid generation of query templates for specific databases, even when dealing with different databases or database changes, thus improving query template generation efficiency. Furthermore, using the intelligently generated query templates and database metadata, multiple test query statements are automatically generated for fuzz testing of the database. Therefore, both the query template generation process and the database query statement generation process are automated, significantly improving the efficiency of database query statement generation.

[0273] Furthermore, for the input query statement, retrieval enhancement technology and Prompt engineering are used to parse the query statement and retrieve the reference atom that best matches the statement elements in the query statement. By using the reference atom as contextual knowledge through the large language model, the statement template corresponding to the query statement is generated. This can make full use of the knowledge accumulation in the atom library and improve the diversity and adaptability of statement template generation.

[0274] The second generation method is to train a template generation model specifically designed for generating statement templates based on an atomic library.

[0275] This application provides a training method for a template generation model. For a pre-trained large language model, fine-tuning is performed using an atomic library and training samples to obtain a template generation model specifically for generating sentence templates. The template generation model is then used to generate sentence templates. Detailed procedures are described below. Figure 5 Examples of implementations. Figure 5 This is a flowchart illustrating a method for generating database query statements according to an embodiment of this application. This embodiment is executed by a computer device. See also... Figure 5 The method includes:

[0276] 501. The computer device obtains the first sample query statement and the first tag statement template, and the syntax structure of the first tag statement template is consistent with the syntax structure of the first sample query statement.

[0277] The first sample query statement can be any type of structured query statement, such as an SQL statement. This first sample query statement can be any historical query statement that has been executed on the database. The computer device can retrieve one or more first sample query statements.

[0278] The first tag statement template is the tag statement template for the first sample query statement.

[0279] In one possible implementation, the reference query statement includes at least one of the following: historical business query statements of the database, historical test query statements of the database, and historical error query statements of the database.

[0280] 502. The computer device trains the large language model based on the prompt text, the first sample query statement, the first label statement template, and the atomic library to obtain the template generation model.

[0281] In this implementation, the large language model is trained using the first sample query statement, the first labeled statement template, and the atomic library. This allows the large language model to fully learn the knowledge related to the atomic library during training, enhancing its understanding of the structure of query statements in the database. This results in the construction of a template generation model specifically for generating statement templates. Subsequently, when using the template generation model to generate statement templates, it is no longer necessary to provide the atomic library, which is beneficial for generating statement templates suitable for the database more efficiently.

[0282] Step 502 involves fine-tuning the pre-trained large language model to obtain a template generation model suitable for the template generation task. Fine-tuning techniques include Supervised Fine-tuning (SFT), Reinforcement Learning from Human Feedback (RLHF), and Direct Preference Optimization (DPO).

[0283] Supervised fine-tuning refers to the process of fine-tuning the parameters of a large language model in a supervised manner. First, natural language instruction instances are collected or constructed to guide the model in optimizing for specific tasks, enabling it to better understand and execute complex instructions. Common methods of supervised fine-tuning include full-parameter fine-tuning and low-rank adaptation (LoRA). After instruction fine-tuning, the large language model possesses strong instruction-following capabilities, allowing it to solve various downstream tasks through zero-shot learning.

[0284] Among them, reinforcement learning based on human feedback combines reinforcement learning and human feedback. It trains a reward model by using human evaluation and preferences of the model's output, and uses reinforcement learning algorithms to optimize the model's behavior so that the results it generates are more in line with human expectations.

[0285] Direct preference optimization (DPO) is a method that directly utilizes human feedback for optimization. Its main goal is to adjust the model's parameters directly based on human preferences, making the model's output more aligned with human expectations. Unlike reinforcement learning based on human feedback, DPO does not rely on explicit reward modeling or reinforcement learning. DPO offers a simpler and more direct training method for achieving the same goal as reinforcement learning based on human feedback.

[0286] It should be noted that, in the embodiments of this application, supervised fine-tuning (supervised training) and direct preference optimization (preference optimization training) are mainly used to fine-tune the large language model to obtain the template generation model. For details, please refer to the following... Figure 6 Examples of implementations.

[0287] 503. The computer device obtains the reference query statement, which is used to indicate the operation to be performed on the database.

[0288] The process of step 503 is the same as that of step 301 above, and will not be repeated here.

[0289] 504. The computer device inputs the prompt text and reference query statement into the template generation model, and obtains the statement template output by the template generation model.

[0290] Since the template generation model is a fine-tuned version of a large language model, it has already learned about the atomic library during the training process in step 502, making it suitable for template generation tasks. Therefore, in step 504, it is not necessary to provide the template generation model with knowledge related to the atomic library.

[0291] In one possible implementation, the computer device obtains a third prompt template, which includes the prompt text, fills the third prompt template with the reference query statement to obtain the third prompt information, and inputs the third prompt information into the large language model to obtain the statement template output by the large language model.

[0292] 505. Computer devices acquire database metadata, which refers to data used to describe the database.

[0293] The process of step 505 is the same as that of step 304 above, and will not be repeated here.

[0294] 506. The computer equipment generates multiple test query statements based on metadata and statement templates. These multiple test query statements are used to perform fuzz testing on the database.

[0295] The process of step 506 is the same as that of step 305 above, and will not be repeated here.

[0296] The method provided in this application utilizes a large language model to automatically generate database query templates based on prompt text and reference query statements in the database. This reduces reliance on manually written query templates and allows for rapid generation of query templates for specific databases, even when dealing with different databases or database changes, thus improving query template generation efficiency. Furthermore, using the intelligently generated query templates and database metadata, multiple test query statements are automatically generated for fuzz testing of the database. Therefore, both the query template generation process and the database query statement generation process are automated, significantly improving the efficiency of database query statement generation.

[0297] Furthermore, based on a pre-trained large language model, a template generation model specifically designed for generating sentence templates is trained, improving the template generation capabilities of the template generation model. Moreover, the template generation model possesses good versatility and can be extended to test scenarios using other databases.

[0298] The following details the process of training the template generation model in steps 501 to 502. This process includes four sub-processes: generating training samples for supervised training, supervised training (SFT), generating training samples for preference optimization training, and preference optimization training (DPO). Generating training samples for supervised training includes: generating candidate data (candidate query statements, atomic libraries, and suggestion templates), dynamically evaluating the quality of the label statement templates output by the model, and selecting high-scoring data as training samples. Supervised training includes: dynamically updating model parameters using high-quality training samples to obtain the initial template generation model. Generating training samples for preference optimization training includes: selecting low-scoring data as negative labels for training samples and generating positive labels for training samples using the initial template generation model. Preference optimization training includes: optimizing the model through comparative learning.

[0299] For detailed procedures, please refer to the following: Figure 6 Implementation examples, Figure 6 This is a flowchart of a model training method provided in an embodiment of this application. This embodiment is executed by a computer device. See also... Figure 6 The method includes:

[0300] Steps 601 to 603 below: Generate training samples for supervised training.

[0301] 601. The computer device retrieves multiple candidate query statements.

[0302] The candidate query statement can be any type of structured query statement, such as an SQL statement. This candidate query statement can be any historical query statement that has been executed on the database. The computer device can retrieve one or more candidate query statements.

[0303] In one possible implementation, the reference query statement includes at least one of the following: historical business query statements of the database, historical test query statements of the database, and historical error query statements of the database.

[0304] The computer equipment acquires historical business query statements, historical test query statements, and historical error query statements from the database. It parses these queries to obtain the target statement element for each query and stores the query statements and their respective target statement elements in a seed pool. If the target statement element of a query statement is the same as the target statement element of any query statement in the seed pool, that query statement and its target statement element are removed. In other words, the seed pool only stores query statements of different types, thus constructing a diverse and non-redundant seed pool. Furthermore, in step 601, candidate query statements are obtained from the seed pool, that is, the query statements in the seed pool are used as candidate query statements.

[0305] 602. For each candidate query statement, the computer device inputs the candidate query statement, prompt text, and atomic library into the large language model to obtain the first statement template corresponding to the candidate query statement output by the large language model, and determines the evaluation parameters of the first statement template.

[0306] The evaluation parameters of the first statement template are used to reflect the availability of the first statement template.

[0307] In one possible implementation, the computer device obtains a fourth prompt template, which includes prompt text and an atomic library, fills the fourth prompt template with candidate query statements to obtain fourth prompt information, and inputs the fourth prompt information into a large language model to obtain a first statement template output by the large language model.

[0308] For example, the fourth prompt template is as follows:

[0309] Task Description:

[0310] Please convert the following SQL statement into a MySQL RQG statement template.

[0311] Task requirements:

[0312] Automatically identify the parts of the SQL statement that should be generalized. Generalize fields and expressions in the SQL statement to expression variables according to the "Basic Rules" below; in the generated statement template, all fields / expressions appearing are replaced with generalized variables; generalized variables do not need to be defined; when multiple generalization rules are available, prioritize the more general and higher-level generalization rule.

[0313] Background information:

[0314] {Atomic Library}

[0315] Output format:

[0316] Output only the statement template, without any other explanation; the output is wrapped in ```sql` and ```.

[0317] Sample Use Cases

[0318] {Input: xx; Output: xx}

[0319] Input example:

[0320] ```sql;{query}```"

[0321] In one possible implementation, the evaluation parameters of the first statement template are determined, including at least one of the following:

[0322] (1) Based on metadata and first statement template, generate multiple first query statements. Based on the accuracy of multiple first query statements, determine the first evaluation parameter of the first statement template. The first evaluation parameter is used to reflect the accuracy of the first statement template.

[0323] Optionally, the accuracy of multiple first query statements can be used as the first evaluation parameter of the first statement template.

[0324] (2) Based on the number of atoms in the first statement template, determine the second evaluation parameter of the first statement template. The second evaluation parameter is used to reflect the extensibility of the first statement template. An atom is a basic unit used to construct a query statement for the database.

[0325] The second evaluation parameter of the first statement template is positively correlated with the number of atoms in the first statement template. The more atoms in the first statement template, the higher its extensibility.

[0326] (3) Based on the similarity between multiple first query statements and the first sample query statement, determine the third evaluation parameter of the first statement template. The third evaluation parameter is used to reflect the matching degree between the first statement template and the first sample query statement.

[0327] Optionally, the computer device determines the similarity between each first query statement and the first sample query statement, averages the similarities corresponding to multiple first query statements to obtain an average similarity, and determines a third evaluation parameter for the first statement template based on the average similarity. The third evaluation parameter of the first statement template is positively correlated with the average similarity.

[0328] Optionally, the computer device determines the similarity between the first query statement and the first sample query statement based on the proportion of common elements in the first query statement and the first sample query statement, wherein the similarity is positively correlated with the proportion of common elements. For example, the proportion of common elements can be used as the similarity between the first query statement and the first sample query statement.

[0329] For example, the first sample query is "SELECT c1 FROM t1 ORDER BY 1", and the first query is "SELECT c1 FROM t1 WHERE c0>1". The first sample query contains SELECT, FROM, and ORDER, while the first query contains SELECT, FROM, and WHERE. Both queries share SELECT and FROM, resulting in a shared element ratio of 66%. Therefore, the similarity between the first query and the first sample query is 66%.

[0330] In this implementation, the sentence templates are evaluated using multiple metrics across three dimensions: accuracy, extensibility, and matching degree. This comprehensive approach measures the quality of sentence templates generated by the large language model, thereby selecting more usable labeled sentence templates from the sentence templates generated by the large language model as training samples, thus improving the accuracy and reliability of the training samples.

[0331] 603. For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the availability conditions, the computer device determines the candidate query statement as the first sample query statement and the first statement template as the first label statement template.

[0332] The computer device selects candidate query statements from multiple candidate query statements whose corresponding evaluation parameters meet the usability conditions. The selected candidate query statement is used as the first sample query statement, and the corresponding first statement template is used as the first labeled statement template. This first sample query statement and the first labeled statement template serve as a training sample for subsequent supervised training.

[0333] In this implementation, through steps 601-603, the large language model is used to initially generate first statement templates corresponding to multiple candidate query statements. By screening and evaluating the generated first statement templates, the first statement template with high usability generated by the large language model and its corresponding candidate query statements are selected as training samples. This not only ensures the accuracy and reliability of the training samples, but also eliminates the need for manual annotation of training samples, thereby improving the efficiency of obtaining training samples and thus improving the efficiency of training template generation model.

[0334] In one possible implementation, the evaluation parameters of the first statement template include the aforementioned first evaluation parameter, second evaluation parameter, and third evaluation parameter. The available conditions include: the first evaluation parameter is greater than a first parameter threshold, the second evaluation parameter is greater than a second parameter threshold, and the third evaluation parameter is greater than a third parameter threshold.

[0335] For example, the first parameter threshold is equal to 0.8, the second parameter threshold is equal to 3, and the third parameter threshold is equal to 0.7. That is, data with an accuracy greater than 0.8, scalability greater than 3, and matching degree greater than 0.7 are filtered out.

[0336] Steps 604 to 605 below: Supervised training of the large language model to obtain the initial template generation model.

[0337] 604. The computer device inputs the prompt text, the first sample query statement, and the atomic library into the large language model to obtain the first sample statement template output by the large language model.

[0338] In one possible implementation, the computer device obtains a fourth prompt template, which includes prompt text and an atomic library, fills the fourth prompt template with the first sample query statement to obtain a fifth prompt message, and inputs the fifth prompt message into the large language model to obtain the first sample statement template output by the large language model.

[0339] 605. The computer equipment trains the large language model with the goal of reducing the difference between the first sample sentence template and the first labeled sentence template, and obtains the initial template generation model.

[0340] In this model, the first labeled sentence template is the target to be generated, and the first sample sentence template is the result to be generated. The smaller the difference between the first sample sentence template and the first labeled sentence template, the higher the template generation ability of the model. Therefore, the large language model is trained with the goal of reducing the difference between the first sample sentence template and the first labeled sentence template, so as to obtain the initial template generation model.

[0341] In this implementation, a first sample statement template corresponding to the first sample query statement is generated through a large language model. The goal is to reduce the difference between the first sample statement template and the first label statement template. The large language model is trained, and supervised training of the large language model is achieved, thereby obtaining a template generation model specifically for generating statement templates, which improves the ability of the template generation model to generate statement templates for the database.

[0342] In one possible implementation, the computer device determines a first loss parameter based on a first sample sentence template and a first labeled sentence template. This first loss parameter is used to reflect the degree of difference between the first sample sentence template and the first labeled sentence template. With the goal of reducing the first loss parameter, the large language model is trained to obtain an initial template generation model.

[0343] In this embodiment, during supervised training, an iterative fine-tuning method is used to continuously improve the performance of the large language model on the template generation task. The model is fine-tuned using training samples generated during the evaluation process, and then further evaluated and fine-tuned. This process is repeated until the model achieves its optimal performance, resulting in the initial template generation model. The specific process is as follows:

[0344] 1. Dataset Generation and Selection: The first sentence template generated during the evaluation process is scored to obtain the evaluation parameters of the first sentence template. Based on the set available conditions, high-quality training samples are selected to construct the training samples for SFT. Simultaneously, some low-quality training samples can be retained as negative labels for subsequent DPO.

[0345] 2. Model Fine-tuning: After obtaining the SFT training samples, set appropriate fine-tuning parameters (such as learning rate, batch size, number of training epochs, etc.) to fine-tune the pre-trained large language model. During the fine-tuning process, the model will gradually learn and adapt to the requirements of the template generation task.

[0346] (1) The learning rate determines the step size for updating model parameters. Parameter tuning is usually required to find the optimal learning rate. Example: learning_rate = 1e-5. (2) The batch size determines the number of samples used in each iteration. A larger batch size can stabilize the training process but requires more computational resources. Example: batch_size = 32. (3) The number of training epochs determines the number of training iterations. The optimal number of training epochs is usually determined using a validation set. Example: epochs = 3. (4) The optimizer is used to update model parameters. Commonly used optimizers include Adam, SGD, etc. (5) The loss function is used to determine the loss parameters. Loss functions include cross-entropy loss function, mean squared error loss function, etc.

[0347] 3. Model Evaluation: After fine-tuning, perform batch generation tests on the model, calculating its accuracy, matching degree, and scalability on the validation or test sets. Simultaneously, record performance metrics such as inference time to comprehensively evaluate the model's output quality and inference efficiency.

[0348] 4. Iterative Process: Based on the evaluation results, analyze the model's performance across various metrics. If the model has not yet achieved the expected results, further optimize the dataset selection criteria, adjust fine-tuning parameters, and introduce new training samples based on the evaluation feedback. Subsequently, regenerate the dataset, fine-tune the model, and evaluate it again to enter the next iteration.

[0349] 5. Convergence and Termination: The iterative process continues until the model achieves the set optimal performance or convergence criteria across all evaluation metrics. Ultimately, an initial template generation model that can efficiently adapt to template generation tasks is obtained.

[0350] Steps 606 to 608 below: Generate training samples for preference optimization training.

[0351] 606. For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the unavailability condition, the computer device determines the candidate query statement as the second sample query statement and determines the first statement template as the negative label statement template corresponding to the second sample query statement.

[0352] The computer device filters candidate query statements from multiple candidate query statements, selecting those whose corresponding evaluation parameters satisfy the unavailability condition. The selected candidate query statement is used as the second sample query statement, and the corresponding first statement template is used as the negative label statement template. This second sample query statement and the negative label statement template serve as a training sample for subsequent preference optimization training.

[0353] In one possible implementation, the evaluation parameters of the first statement template include the aforementioned first evaluation parameter, second evaluation parameter, and third evaluation parameter. The unavailability condition includes: the first evaluation parameter being less than the fourth parameter threshold, the second evaluation parameter being less than the fifth parameter threshold, and the third evaluation parameter being less than the sixth parameter threshold.

[0354] Optionally, the fourth parameter threshold is less than or equal to the first parameter threshold in the availability conditions, the fifth parameter threshold is less than or equal to the second parameter threshold in the availability conditions, and the sixth parameter threshold is less than or equal to the third parameter threshold in the availability conditions.

[0355] For example, the fourth parameter threshold is equal to 0.2, the fifth parameter threshold is equal to 1, and the sixth parameter threshold is equal to 0.3. That is, data with an accuracy of less than 0.2, scalability of less than 1, and matching degree of less than 0.3 are filtered out.

[0356] 607. For each second sample query statement, the computer device inputs the prompt text, the second sample query statement, and the atomic library into the initial template generation model to obtain the second statement template output by the initial template generation model, and determines the evaluation parameters of the second statement template.

[0357] The second sample contains multiple query statements.

[0358] The evaluation parameters of the second statement template are used to reflect its usability. These parameters are similar to those of the first statement template and will not be repeated here.

[0359] In one possible implementation, the computer device obtains a fourth prompt template, which includes prompt text and an atomic library, fills the fourth prompt template with a second sample query statement to obtain a sixth prompt message, and inputs the sixth prompt message into an initial template generation model to obtain a second statement template output by the initial template generation model.

[0360] 608. For each second sample query statement, if the evaluation parameters of the second statement template corresponding to the second sample query statement meet the availability conditions, the computer device determines the second statement template as the positive label statement template corresponding to the second sample query statement.

[0361] The computer device selects second-sample query statements from multiple second-sample query statements whose corresponding evaluation parameters meet the availability conditions, and uses the second statement template corresponding to the selected second-sample query statement as the positive label statement template. Then, the selected second-sample query statement and its corresponding positive label statement template and negative label statement template are used as a training sample for subsequent preference optimization training, while other second-sample query statements are discarded.

[0362] That is, after step 606, a fine-tuned initial template generation model and the selected low-quality second sample query statements and their negative label statement templates are obtained. Next, the low-quality second sample query statements are fed into the fine-tuned initial template generation model to generate new second statement templates. Then, the second statement templates are filtered using the evaluation parameters, retaining high-quality second statement templates as positive label statement templates. Finally, the selected second sample query statements, negative label statement templates, and newly generated positive label statement templates are combined to construct training samples for preference optimization training.

[0363] Optionally, the training samples for preference optimization training can be formatted as follows:

[0364]

[0365] Here, "instruction" represents the prompt text, "input" represents the second sample query statement, "new_output" represents the positive label statement template, and "output" represents the negative label statement template.

[0366] Steps 609 and 611 below: Perform preference optimization training on the initial template generation model to obtain the template generation model. type.

[0367] 609. The computer device inputs the first target text, the second sample query statement, the atomic library, and the negative label statement template into the initial template generation model to obtain the first probability output by the initial template generation model.

[0368] Here, the first probability represents the probability of generating a negative-label statement template given the second sample query statement. The first target text is used to indicate the probability predicted by the initial template generation model to generate a negative-label statement template given the second sample query statement.

[0369] In one possible implementation, the computer device obtains a fifth prompt template, which includes a first target text and an atomic library. The second sample query statement and a negative label statement template are then filled into the fifth prompt template to obtain a sixth prompt message. The sixth prompt message is then input into an initial template generation model to obtain a first probability output by the initial template generation model.

[0370] 610. The computer device inputs the second target text, the second sample query statement, the atomic library, and the positive label statement template into the initial template generation model to obtain the second probability output by the initial template generation model.

[0371] Here, the second probability represents the probability of generating a positive label statement template given a second sample query statement, and the second target text is used to indicate the probability predicted by the initial template generation model to generate a positive label statement template given a second sample query statement.

[0372] In one possible implementation, the computer device obtains a sixth prompt template, which includes a second target text and an atomic library, fills the sixth prompt template with a second sample query statement and a positive label statement template to obtain a seventh prompt message, and inputs the seventh prompt message into an initial template generation model to obtain a second probability output by the initial template generation model.

[0373] 611. The computer equipment trains the initial template generation model with the goal of reducing the first probability and increasing the second probability, thus obtaining the template generation model.

[0374] The first probability represents the probability that the initial template generation model will generate a negative-label statement template given the second sample query statement. Since a negative-label statement template is an incorrect template, the smaller the first probability, the higher the accuracy of the initial template generation model. The second probability represents the probability that the initial template generation model will generate a positive-label statement template given the second sample query statement. Since a positive-label statement template is a correct template, the larger the second probability, the higher the accuracy of the initial template generation model.

[0375] Therefore, the computer equipment trains the initial template generation model with the goal of reducing the first probability and increasing the second probability, thereby obtaining the template generation model.

[0376] In this embodiment, during preference optimization training, low-quality sentence templates are selected as negative labels, and sentence templates regenerated by the initial template generation model are used as positive labels to construct training samples for preference optimization training, thereby fine-tuning the model for preference optimization. The fine-tuned model is then used to generate new datasets for further fine-tuning. After each iteration, the model is evaluated, and the best-performing model is retained. This process is repeated until a predetermined number of iterations is reached or the model performance reaches its optimal state, thus obtaining the template generation model.

[0377] Figure 7 This is a schematic diagram of a model training method provided in an embodiment of this application, as shown below. Figure 7 As shown, the computer device generates statement templates based on candidate query statements, an atomic library, and suggestion templates using a large language model. It then determines the evaluation parameters of these statement templates and selects data with high evaluation parameters as training samples for the SFT (Simplified Language Function). These training samples are used to train the large language model using SFT to obtain an initial template generation model. Additionally, data with low evaluation parameters are selected as negative-label statement templates in the DPO (Discretionary Request for Proof) training samples. New statement templates are then generated based on these low-parameter data using the initial template generation model. Data with high evaluation parameters from these new templates are used as positive-label statement templates in the DPO training samples. Finally, the DPO training samples are used to train the initial template generation model using DPO to obtain the final template generation model.

[0378] In this embodiment, the large language model is trained using sample query statements, tag statement templates, and an atomic library. This allows the large language model to fully learn knowledge related to the atomic library during training, enhancing its understanding of the structure of query statements in the database. As a result, a template generation model specifically designed for generating statement templates is constructed. Subsequently, when using the template generation model to generate statement templates, it is no longer necessary to provide the atomic library, which facilitates more efficient generation of statement templates suitable for the database.

[0379] Furthermore, by fine-tuning the basic large language model and combining it with a large amount of real SQL statements and business scenario data, a dedicated model suitable for RQG statement template generation tasks is trained, enabling efficient parsing of input SQL statements and automatic generation of statement templates. This method can capture the deep semantic and structural features of SQL statements, improving the accuracy and intelligence level of statement template generation.

[0380] Furthermore, by evaluating the model's output, a high-quality fine-tuning dataset is automatically generated, enabling automated generation of training samples. New training samples can be continuously generated without manual labeling, thus achieving self-training of the model and improving its training efficiency.

[0381] In some embodiments, steps 507, 508, and 510 may be omitted, and the computer device may train the initial template generation model with the goal of reducing the first probability to obtain a template generation model.

[0382] In the field of database fuzzing, the generation of test query statements relies on fixed statement templates, which limits scalability. This application addresses this problem by proposing a statement template generation method based on a large language model, which mainly includes the following three key parts:

[0383] (1) Statement template atomization: By atomizing the query statement, the complex SQL syntax structure is broken down into reusable basic units, which improves the flexibility and extensibility of the statement template and facilitates subsequent automatic combination and management.

[0384] (2) Fine-tuning-based statement template generation: By fine-tuning the large language model, it can automatically generate statement templates with reasonable structure and wide coverage according to actual business scenarios and database characteristics, which significantly improves the relevance and effectiveness of the generated results.

[0385] (3) Statement template generation based on retrieval enhancement technology: By using retrieval enhancement technology, the atomic library and SQL statements and other multi-source knowledge are dynamically integrated to enhance the retrieval of the statement template generation process, thereby further improving the diversity and business adaptability of the statement template.

[0386] Furthermore, this application embodiment also introduces an execution feedback mechanism for statement templates, which can continuously optimize the atomic library based on the execution results of test query statements generated by the statement templates, thereby improving the diversity, coverage, and defect detection capabilities of query statements.

[0387] Figure 8 This is a schematic diagram of a database query statement generation device provided in an embodiment of this application. See also... Figure 8 The device includes:

[0388] The first acquisition module 801 is used to acquire a reference query statement, which is used to indicate the operation to be performed on the database.

[0389] The first generation module 802 is used to generate a statement template based on prompt text and reference query statement through a large language model. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database.

[0390] The second acquisition module 803 is used to acquire the database's metadata, which refers to the data used to describe the database.

[0391] The second generation module 804 is used to generate multiple test query statements based on metadata and statement templates. These multiple test query statements are used to perform fuzz testing on the database.

[0392] The database query statement generation apparatus provided in this application utilizes a large language model to automatically generate database statement templates based on prompt text and reference query statements in the database. This reduces reliance on manually written statement templates and allows for rapid generation of statement templates for specific databases, even when dealing with different databases or database changes, thus improving the efficiency of statement template generation. Furthermore, using the intelligently generated statement templates and database metadata, multiple test query statements are automatically generated for fuzz testing of the database. Therefore, both the statement template generation process and the database query statement generation process are free from human intervention, thereby improving the efficiency of database query statement generation.

[0393] Optionally, see Figure 9 The first generation module 802 is used for:

[0394] Using a large language model, sentence templates are generated based on prompt text, reference query statements, and atomic libraries;

[0395] The atom library stores multiple atoms, which are the basic units used to construct query statements in the database.

[0396] Optionally, see Figure 9 The first generation module 802 includes:

[0397] The retrieval unit 812 is used to find the reference atom in the atom library that has the highest similarity to the reference query statement;

[0398] The first generation unit 822 is used to input the prompt text, reference query statement and reference atom into the large language model to obtain the statement template output by the large language model.

[0399] Optionally, see Figure 9 Retrieval unit 812, used for:

[0400] The reference query statement is parsed to obtain multiple target statement elements in the reference query statement;

[0401] In the atom library, find the reference atom with the highest similarity to each target statement element.

[0402] Optionally, see Figure 9 Retrieval unit 812, used for:

[0403] Parse the reference query statement to obtain all the statement elements in the reference query statement;

[0404] Determine the target statement element as the statement element that is different from the general atom in the atomic library among all statement elements. The general atom refers to an atom that is not related to business logic; or, determine all statement elements as the target statement element.

[0405] Optionally, see Figure 9 The first generation unit 822 is used for:

[0406] In the case where multiple target statement elements are statement elements that are different from the general atom, determine the general atom description text, which is used to describe the general atom;

[0407] Input the prompt text, reference query statement, reference atom, and general atom description text into the large language model to obtain the statement template output by the large language model.

[0408] Optionally, see Figure 9 The first generation module 802 includes:

[0409] The model training unit 832 is used to obtain the first sample query statement and the first label statement template. The syntax structure of the first label statement template is consistent with the syntax structure of the first sample query statement.

[0410] The model training unit 832 is also used to train a large language model based on prompt text, first sample query statement, first label statement template and atomic library to obtain template generation model;

[0411] The second generation unit 842 is used to input the prompt text and reference query statement into the template generation model to obtain the statement template output by the template generation model.

[0412] Optionally, see Figure 9 Model training unit 832, used for:

[0413] Input the prompt text, the first sample query statement, and the atomic library into the large language model to obtain the first sample statement template output by the large language model.

[0414] With the goal of reducing the difference between the first sample sentence template and the first label sentence template, a large language model is trained to obtain a template generation model.

[0415] Optionally, see Figure 9 Model training unit 832, used for:

[0416] Retrieve multiple candidate query statements;

[0417] For each candidate query statement, the candidate query statement, prompt text, and atomic library are input into the large language model to obtain the first statement template corresponding to the candidate query statement output by the large language model. The evaluation parameters of the first statement template are determined, and the evaluation parameters are used to reflect the usability of the first statement template.

[0418] For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the availability conditions, then the candidate query statement is determined as the first sample query statement, and the first statement template is determined as the first label statement template.

[0419] Optionally, see Figure 9 Model training unit 832, used for at least one of the following:

[0420] Based on metadata and a first statement template, multiple first query statements are generated. Based on the accuracy of the multiple first query statements, a first evaluation parameter of the first statement template is determined. The first evaluation parameter is used to reflect the accuracy of the first statement template.

[0421] Based on the number of atoms in the first statement template, a second evaluation parameter for the first statement template is determined. The second evaluation parameter is used to reflect the extensibility of the first statement template. An atom is a basic unit used to construct a query statement for the database.

[0422] Based on the similarity between multiple first query statements and the first sample query statement, a third evaluation parameter for the first statement template is determined. The third evaluation parameter is used to reflect the matching degree between the first statement template and the first sample query statement.

[0423] Optionally, see Figure 9 Model training unit 832, used for:

[0424] Based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library, the large language model is trained to obtain the initial template generation model;

[0425] For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the unavailable condition, then the candidate query statement is determined as the second sample query statement, and the first statement template is determined as the negative label statement template corresponding to the second sample query statement.

[0426] The first target text, the second sample query statement, the atomic library, and the negative label statement template are input into the initial template generation model to obtain the first probability output by the initial template generation model. The first probability represents the probability of generating a negative label statement template given the second sample query statement. The first target text is used to instruct the initial template generation model to predict the probability of generating a negative label statement template given the second sample query statement.

[0427] With the goal of reducing the first probability, the initial template generation model is trained to obtain the template generation model.

[0428] Optionally, see Figure 9 The second sample contains multiple query statements; the model training unit 832 is also used for:

[0429] For each second sample query statement, the prompt text, the second sample query statement, and the atomic library are input into the initial template generation model to obtain the second statement template output by the initial template generation model;

[0430] For each second sample query statement, if the evaluation parameters of the second statement template corresponding to the second sample query statement meet the availability conditions, then the second statement template is determined as the positive label statement template corresponding to the second sample query statement.

[0431] The second target text, the second sample query statement, the atomic library, and the positive label statement template are input into the initial template generation model to obtain the second probability output by the initial template generation model. The second probability represents the probability of generating a positive label statement template given the second sample query statement. The second target text is used to instruct the initial template generation model to predict the probability of generating a positive label statement template given the second sample query statement.

[0432] With the goal of reducing the first probability and increasing the second probability, the initial template generation model is trained to obtain the template generation model.

[0433] Optionally, see Figure 9 The atomic library includes at least one of the following types of atoms:

[0434] Element atoms are used to represent basic elements in a database.

[0435] Operation atoms are used to represent operations performed by the database.

[0436] Combinatorial atoms are obtained by combining at least one elemental atom and at least one operational atom.

[0437] Optionally, see Figure 9 The reference query statement includes at least one of the following:

[0438] Historical business query statements in a database refer to query statements that have been executed in a business scenario.

[0439] Historical test query statements of the database refer to the query statements that the database has executed in fuzz testing.

[0440] Historical error query statements in a database refer to query statements that encountered errors during database execution.

[0441] It should be noted that the database query statement generation apparatus provided in the above embodiments is only an example of the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the computer device can be divided into different functional modules to complete all or part of the functions described above. In addition, the database query statement generation apparatus and the database query statement generation method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process can be found in the method embodiments, which will not be repeated here.

[0442] This application also provides a computer device, which includes a processor and a memory. The memory stores at least one computer program, which is loaded and executed by the processor to perform the operations performed in the database query statement generation method of the above embodiments.

[0443] Optionally, the computer device is provided as a terminal. Figure 10 A schematic diagram of the structure of a terminal 1000 provided in an exemplary embodiment of this application is shown.

[0444] The terminal 1000 includes a processor 1001 and a memory 1002.

[0445] Processor 1001 may include one or more processing cores, such as a quad-core processor, an octa-core processor, etc. Processor 1001 may be implemented using at least one hardware form selected from DSP (Digital Signal Processing), FPGA (Field Programmable Gate Array), and PLA (Programmable Logic Array). Processor 1001 may also include a main processor and a coprocessor. The main processor, also known as a CPU (Central Processing Unit), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, processor 1001 may integrate a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, processor 1001 may also include an AI (Artificial Intelligence) processor, which is used to handle computational operations related to machine learning.

[0446] The memory 1002 may include one or more computer-readable storage media, which may be non-transitory. The memory 1002 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices or flash memory devices. In some embodiments, the non-transitory computer-readable storage media in the memory 1002 are used to store at least one computer program, which is used by the processor 1001 to implement the database query statement generation method provided in the method embodiments of this application.

[0447] In some embodiments, the terminal 1000 may also optionally include: a peripheral device interface 1003 and at least one peripheral device. The processor 1001, memory 1002, and peripheral device interface 1003 can be connected via a bus or signal line. Each peripheral device can be connected to the peripheral device interface 1003 via a bus, signal line, or circuit board. Optionally, the peripheral device includes at least one of: a radio frequency circuit 1004, a display screen 1005, a camera assembly 1006, an audio circuit 1007, and a power supply 1008.

[0448] Peripheral device interface 1003 can be used to connect at least one I / O (Input / Output) related peripheral device to processor 1001 and memory 1002. In some embodiments, processor 1001, memory 1002 and peripheral device interface 1003 are integrated on the same chip or circuit board; in some other embodiments, any one or two of processor 1001, memory 1002 and peripheral device interface 1003 can be implemented on separate chips or circuit boards, which is not limited in this embodiment.

[0449] The radio frequency (RF) circuit 1004 is used to receive and transmit RF (Radio Frequency) signals, also known as electromagnetic signals. The RF circuit 1004 communicates with communication networks and other communication devices via electromagnetic signals. The RF circuit 1004 converts electrical signals into electromagnetic signals for transmission, or converts received electromagnetic signals back into electrical signals. Optionally, the RF circuit 1004 includes: an antenna system, an RF transceiver, one or more amplifiers, a tuner, an oscillator, a digital signal processor, a codec chipset, a user identity module card, etc. The RF circuit 1004 can communicate with other devices via at least one wireless communication protocol. This wireless communication protocol includes, but is not limited to: metropolitan area networks (MANs), various generations of mobile communication networks (2G, 3G, 4G, and 5G), wireless local area networks (WLANs), and / or WiFi (Wireless Fidelity) networks. In some embodiments, the RF circuit 1004 may also include circuitry related to NFC (Near Field Communication), which is not limited in this application.

[0450] Display screen 1005 is used to display a UI (User Interface). This UI may include graphics, text, icons, videos, and any combination thereof. When display screen 1005 is a touch display screen, it also has the ability to collect touch signals on or above its surface. These touch signals can be input as control signals to processor 1001 for processing. In this case, display screen 1005 can also be used to provide virtual buttons and / or a virtual keyboard, also known as soft buttons and / or a soft keyboard. In some embodiments, there may be one display screen 1005, disposed on the front panel of terminal 1000; in other embodiments, there may be at least two display screens, disposed on different surfaces of terminal 1000 or in a folded design; in still other embodiments, display screen 1005 may be a flexible display screen, disposed on a curved or folded surface of terminal 1000. Furthermore, display screen 1005 may be configured as a non-rectangular, irregular shape, i.e., a non-rectangular screen. The display screen 1005 can be made of materials such as LCD (Liquid Crystal Display) and OLED (Organic Light-Emitting Diode).

[0451] The camera assembly 1006 is used to acquire images or videos. Optionally, the camera assembly 1006 includes a front-facing camera and a rear-facing camera. The front-facing camera is disposed on the front panel of the terminal 1000, and the rear-facing camera is disposed on the back of the terminal 1000. In some embodiments, there are at least two rear-facing cameras, which are any one of a main camera, a depth-sensing camera, a wide-angle camera, and a telephoto camera, to achieve background blurring by fusion of the main camera and the depth-sensing camera, panoramic shooting by fusion of the main camera and the wide-angle camera, VR (Virtual Reality) shooting, or other fusion shooting functions. In some embodiments, the camera assembly 1006 may also include a flash. The flash may be a single-color temperature flash or a dual-color temperature flash. A dual-color temperature flash refers to a combination of a warm light flash and a cool light flash, which can be used for light compensation at different color temperatures.

[0452] The audio circuit 1007 may include a microphone and a speaker. The microphone is used to collect sound waves from the user and the environment, converting the sound waves into electrical signals that are input to the processor 1001 for processing, or input to the radio frequency circuit 1004 for voice communication. For stereo sound acquisition or noise reduction purposes, multiple microphones may be used, each positioned at a different location on the terminal 1000. The microphone may also be an array microphone or an omnidirectional microphone. The speaker is used to convert electrical signals from the processor 1001 or the radio frequency circuit 1004 into sound waves. The speaker may be a conventional diaphragm speaker or a piezoelectric ceramic speaker. When the speaker is a piezoelectric ceramic speaker, it can convert electrical signals not only into audible sound waves but also into inaudible sound waves for purposes such as distance measurement. In some embodiments, the audio circuit 1007 may also include a headphone jack.

[0453] The power supply 1008 is used to power the various components in the terminal 1000. The power supply 1008 can be AC ​​power, DC power, a disposable battery, or a rechargeable battery. When the power supply 1008 includes a rechargeable battery, the rechargeable battery can support wired charging or wireless charging. The rechargeable battery can also be used to support fast charging technology.

[0454] Those skilled in the art will understand that Figure 10 The structure shown does not constitute a limitation on terminal 1000 and may include more or fewer components than shown, or combine certain components, or use different component arrangements.

[0455] Optionally, the computer device is provided as a server. Figure 11This is a schematic diagram of a server structure provided in an embodiment of this application. The server 1100 can vary significantly due to different configurations or performance. It may include one or more Central Processing Units (CPUs) 1101 and one or more memories 1102. The memories 1102 store at least one computer program, which is loaded and executed by the processor 1101 to implement the methods provided in the various method embodiments described above. Of course, the server may also have wired or wireless network interfaces, a keyboard, and input / output interfaces for input and output. The server may also include other components for implementing device functions, which will not be elaborated upon here.

[0456] This application also provides a computer-readable storage medium storing at least one computer program, which is loaded and executed by a processor to implement the operations performed by the database query statement generation method of the above embodiments.

[0457] This application also provides a computer program product, including a computer program loaded and executed by a processor to perform the operations performed by the database query statement generation method of the above embodiments.

[0458] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.

[0459] The above description is only an optional embodiment of the present application and is not intended to limit the present application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present application should be included within the protection scope of the present application.

Claims

1. A method for generating database query statements, characterized in that, The method includes: Obtain a reference query statement, which is used to indicate the operation to be performed on the database; Using a large language model, a statement template is generated based on the prompt text and the reference query statement. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database. Obtain the metadata of the database, wherein the metadata refers to data used to describe the database; Based on the metadata and the statement template, multiple test query statements are generated, which are used to perform fuzz testing on the database.

2. The method according to claim 1, characterized in that, The step of generating a statement template based on the prompt text and the reference query statement using a large language model includes: The statement template is generated based on the prompt text, the reference query statement, and the atomic library using a large language model. The atom library stores multiple atoms, which are the basic units used to construct query statements for the database.

3. The method according to claim 2, characterized in that, The step of generating the statement template based on the prompt text, the reference query statement, and the atomic library using a large language model includes: In the atom library, find the reference atom with the highest similarity to the reference query statement; The prompt text, the reference query statement, and the reference atom are input into the large language model to obtain the statement template output by the large language model.

4. The method according to claim 3, characterized in that, The step of searching the atomic library for the reference atom with the highest similarity to the reference query statement includes: The reference query statement is parsed to obtain multiple target statement elements in the reference query statement; In the atomic library, the reference atom with the highest similarity to each target statement element is searched.

5. The method according to claim 4, characterized in that, The process of parsing the reference query statement to obtain multiple target statement elements in the reference query statement includes: The reference query statement is parsed to obtain all the statement elements in the reference query statement; The statement element that differs from the general atom in the atomic library among all statement elements is identified as the target statement element, where the general atom refers to an atom that is unrelated to business logic; or, all statement elements are identified as the target statement element.

6. The method according to claim 5, characterized in that, The step of inputting the prompt text, the reference query statement, and the reference atom into the large language model to obtain the statement template output by the large language model includes: In the case where the plurality of target statement elements are statement elements different from the general atom, a general atom description text is determined, which is used to describe the general atom; The prompt text, the reference query statement, the reference atom, and the general atom description text are input into the large language model to obtain the statement template output by the large language model.

7. The method according to claim 2, characterized in that, The step of generating the statement template based on the prompt text, the reference query statement, and the atomic library using a large language model includes: Obtain the first sample query statement and the first tag statement template, wherein the syntax structure of the first tag statement template is consistent with the syntax structure of the first sample query statement; Based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library, the large language model is trained to obtain a template generation model; The prompt text and the reference query statement are input into the template generation model to obtain the statement template output by the template generation model.

8. The method according to claim 7, characterized in that, The process of training the large language model based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library to obtain a template generation model includes: The prompt text, the first sample query statement, and the atomic library are input into the large language model to obtain the first sample statement template output by the large language model. With the goal of reducing the difference between the first sample sentence template and the first tag sentence template, the large language model is trained to obtain the template generation model.

9. The method according to claim 7, characterized in that, The step of obtaining the first sample query statement and the first tag statement template includes: Retrieve multiple candidate query statements; For each candidate query statement, the candidate query statement, the prompt text, and the atomic library are input into the large language model to obtain the first statement template corresponding to the candidate query statement output by the large language model. The evaluation parameters of the first statement template are determined, and the evaluation parameters are used to reflect the availability of the first statement template. For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the availability conditions, then the candidate query statement is determined as the first sample query statement, and the first statement template is determined as the first tag statement template.

10. The method according to claim 9, characterized in that, The process of training the large language model based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library to obtain a template generation model includes: Based on the prompt text, the first sample query statement, the first tag statement template, and the atomic library, the large language model is trained to obtain an initial template generation model; For each candidate query statement, if the evaluation parameters of the first statement template corresponding to the candidate query statement meet the unavailable condition, then the candidate query statement is determined as the second sample query statement, and the first statement template is determined as the negative label statement template corresponding to the second sample query statement. The first target text, the second sample query statement, the atomic library, and the negative label statement template are input into the initial template generation model to obtain the first probability output by the initial template generation model. The first probability represents the probability of generating the negative label statement template given the second sample query statement. The first target text is used to instruct the initial template generation model to predict the probability of generating the negative label statement template given the second sample query statement. With the goal of reducing the first probability, the initial template generation model is trained to obtain the template generation model.

11. The method according to claim 10, characterized in that, The second sample query statement has multiple elements; the method further includes: For each second sample query statement, the prompt text, the second sample query statement, and the atomic library are input into the initial template generation model to obtain the second statement template output by the initial template generation model; For each second sample query statement, if the evaluation parameters of the second statement template corresponding to the second sample query statement meet the availability conditions, then the second statement template is determined as the positive label statement template corresponding to the second sample query statement. The second target text, the second sample query statement, the atomic library, and the positive label statement template are input into the initial template generation model to obtain the second probability output by the initial template generation model. The second probability represents the probability of generating the positive label statement template given the second sample query statement. The second target text is used to instruct the initial template generation model to predict the probability of generating the positive label statement template given the second sample query statement. The step of training the initial template generation model with the goal of reducing the first probability to obtain the template generation model includes: With the goal of reducing the first probability and increasing the second probability, the initial template generation model is trained to obtain the template generation model.

12. A device for generating database query statements, characterized in that, The device includes: The first acquisition module is used to acquire a reference query statement, which is used to indicate the operation to be performed on the database. The first generation module is used to generate a statement template based on the prompt text and the reference query statement using a large language model. The prompt text is used to instruct the large language model to generate the statement template, and the statement template is used to generate the query statement for the database. The second acquisition module is used to acquire the metadata of the database, wherein the metadata refers to data used to describe the database; The second generation module is used to generate multiple test query statements based on the metadata and the statement template, and the multiple test query statements are used to perform fuzz testing on the database.

13. A computer device, characterized in that, The computer device includes a processor and a memory, the memory storing at least one computer program, which is loaded and executed by the processor to perform the operations performed by the database query statement generation method as described in any one of claims 1 to 11.

14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one computer program, which is loaded and executed by a processor to perform the operations performed by the database query statement generation method as described in any one of claims 1 to 11.

15. A computer program product, comprising a computer program, characterized in that, The computer program is loaded and executed by a processor to perform the operations performed by the database query statement generation method as described in any one of claims 1 to 11.