Structured query language rewriting method and device, storage medium and electronic equipment

By obtaining and filtering the context information associated with SQL, and using a large language model to rewrite the structured query language, the problem of low SQL rewriting accuracy is solved, and a more accurate and efficient rewriting process is achieved.

CN120407593APending Publication Date: 2025-08-01TENCENT TECH (BEIJING) CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510473819.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-15
Publication Date
2025-08-01

AI Technical Summary

Technical Problem

The rewriting accuracy of structured query languages in the prior art is low, and the factors related to SQL are ignored, resulting in inaccurate rewriting results.

Method used

By obtaining the context information associated with the first structured query language, filtering out key information, and rewriting SQL based on the rewriting requirements, expanding the rewriting scope to related factors, and rewriting using a large language model.

Benefits of technology

Improve the accuracy of SQL rewriting, ensure that the rewritten SQL can be executed correctly and meet actual needs, reduce redundant information interference, and improve rewriting efficiency and accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120407593A_ABST
    Figure CN120407593A_ABST
Patent Text Reader

Abstract

The invention discloses a structured query language rewriting method and device, a storage medium and electronic equipment. The method comprises the steps that in response to a rewriting request for a first structured query language, context information related to the first structured query language is obtained, and the context information is an information set which is directly or indirectly related to the first structured query language and used for supporting rewriting; based on a rewriting demand corresponding to the rewriting request and the first structured query language, the context information is screened to obtain at least one piece of key information, and the key information is information capable of providing reference in the rewriting process of the first structured query language; and rewriting the first structured query language through the at least one piece of key information to obtain a second structured query language. The technical problem that the rewriting accuracy of the structured query language is low is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computers, and in particular to a method, device, storage medium, and electronic device for rewriting a structured query language. Background Art

[0002] When rewriting Structured Query Language (SQL), one typically only rewrites the SQL itself, ignoring factors related to the SQL. However, these factors often determine the success or failure of SQL execution and the accuracy of the results, leading to low SQL rewriting accuracy. Therefore, the problem of low SQL rewriting accuracy exists.

[0003] To address the above-mentioned problems, no effective solutions have been proposed so far. Summary of the Invention

[0004] The embodiments of the present application provide a method, apparatus, storage medium, and electronic device for rewriting a structured query language (SQL) to at least solve the technical problem of low accuracy in rewriting a SQL.

[0005] According to one aspect of an embodiment of the present application, a method for rewriting a structured query language is provided, comprising: in response to a rewrite request for a first structured query language, obtaining context information associated with the first structured query language, wherein the context information is a set of information directly or indirectly related to the first structured query language and used to support the rewriting; based on a rewriting requirement corresponding to the rewrite request and the first structured query language, filtering the context information to obtain at least one key information, wherein the key information is information that can provide reference during the rewriting process of the first structured query language; and rewriting the first structured query language using the at least one key information to obtain a second structured query language.

[0006] According to another aspect of the embodiments of the present application, there is also provided a device for rewriting a Structured Query Language (SQL), including: an acquisition unit, configured to obtain context information associated with the first SQL in response to a rewriting request for the first SQL, where the context information is an information set that is directly or indirectly related to the first SQL and supports rewriting; a screening unit, configured to screen the context information based on the rewriting requirements corresponding to the rewriting request and the first SQL to obtain at least one key piece of information, where the key piece of information is information that can provide a reference during the rewriting process of the first SQL; a rewriting unit, configured to rewrite the first SQL through the at least one key piece of information to obtain a second SQL.

[0007] As an optional solution, the acquisition unit includes: a first acquisition module, configured to obtain source information of the first SQL, where the source information is used to represent the source of the first SQL; a second acquisition module, configured to obtain the context information based on the source information.

[0008] As an optional solution, the first acquisition module includes: a first parsing sub-module, configured to parse a corresponding library table name from the first SQL, where the library table name is used to represent the data storage location to be operated by the first SQL, and the source information includes the library table name; the second acquisition module includes: a retrieval sub-module, configured to use the library table name as a query condition to retrieve a meta-information database to obtain field information of the library table corresponding to the first SQL, where the context information includes the field information.

[0009] As an optional solution, the first acquisition module includes: a first acquisition sub-module, configured to obtain data source information selected on a SQL interface or parsed from the first SQL, where the SQL interface is the interface that triggers the rewriting request, and the data source information is used to represent the data source of the first SQL, and the source information includes the data source information; the second acquisition module includes: a second acquisition sub-module, configured to obtain a SQL dialect corresponding to the first SQL through the data source information; a third acquisition sub-module, configured to obtain dialect knowledge related to the SQL dialect, where the context information includes the dialect knowledge.

[0010] As an alternative solution, the above-mentioned acquisition unit includes: a third acquisition module configured to acquire the execution result obtained after executing the above-mentioned first Structured Query Language; a fourth acquisition module configured to acquire the above-mentioned context information based on the above-mentioned execution result.

[0011] As an alternative solution, the above-mentioned third acquisition module includes: a fourth acquisition sub-module configured to acquire the data result table obtained after multiple adjustments and submissions for execution during the process of writing the above-mentioned first Structured Query Language, wherein the execution result includes the above-mentioned data result table; the above-mentioned fourth acquisition module includes: a second parsing sub-module configured to parse the above-mentioned data result table and convert it into natural language text, wherein the above-mentioned context information includes the above-mentioned natural language text.

[0012] As an alternative solution, the above-mentioned screening unit includes: a first input module configured to input the above-mentioned rewriting requirement, the above-mentioned first Structured Query Language, and the above-mentioned context information into a classification model, wherein the classification model is a large language model trained using a plurality of first sample data and is used to screen the above-mentioned key information; a fifth acquisition module configured to acquire the classification result output by the above-mentioned classification model, wherein the classification result includes the above-mentioned at least one key information.

[0013] As an alternative solution, the above-mentioned device further includes: a sixth acquisition module configured to acquire the above-mentioned plurality of first sample data before inputting the above-mentioned rewriting requirement, the above-mentioned first Structured Query Language, and the above-mentioned context information into the classification model, wherein the above-mentioned first sample data is composed of a first Structured Query Language sample, a first rewriting requirement of the above-mentioned first Structured Query Language sample, and a plurality of context information samples corresponding to the above-mentioned first Structured Query Language sample; a guiding module configured to use a small amount of labeled first sample data as a prompt to guide the initial classification model to learn the classification pattern before inputting the above-mentioned rewriting requirement, the above-mentioned first Structured Query Language, and the above-mentioned context information into the classification model, wherein the label is used to label the above-mentioned key information in the above-mentioned plurality of context information samples.

[0014] As an alternative solution, the above-mentioned rewriting unit includes: a second input module configured to input the above-mentioned at least one key information, the rewriting requirement corresponding to the above-mentioned rewriting request, and the above-mentioned first Structured Query Language into a rewriting model, wherein the rewriting model is a large language model trained using a plurality of second sample data and is used to rewrite the Structured Query Language; a seventh acquisition module configured to acquire the rewriting result output by the above-mentioned rewriting model, wherein the rewriting result includes the above-mentioned second Structured Query Language.

[0015] As an alternative, the above-mentioned device further includes: an eighth acquisition module, configured to acquire the plurality of second sample data before inputting the at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into the rewriting model, wherein the second sample data is composed of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample; a training module, configured to, before inputting the at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into the rewriting model, use the plurality of second sample data to train an initial rewriting model to obtain a training result; an adjustment module, configured to, before inputting the at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into the rewriting model, in the case that the training result does not meet the convergence condition, adjust the low-rank matrix deployed in the rewriting model until the obtained training result meets the convergence condition.

[0016] As an alternative, the above-mentioned device further includes: a display unit, configured to display the first structured query language in a structured query language editing area before acquiring the context information associated with the first structured query language in response to a rewriting request for the first structured query language, wherein the structured query language editing area is set to allow triggering of a corresponding rewriting request for the first structured query language or a part of the language in the first structured query language; the above-mentioned device further includes: a display unit, configured to, after rewriting the first structured query language through the at least one key information to obtain a second structured query language, perform a line-by-line comparison display of the first structured query language and the second structured query language.

[0017] According to another aspect of the embodiments of the present application, there is provided a computer program product or a computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the electronic device executes the rewriting method of the structured query language as described above.

[0018] According to another aspect of the embodiments of the present application, there is further provided an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor executes the above-mentioned rewriting method of the structured query language through the computer program.

[0019] In an embodiment of the present application, in response to a rewrite request for a first Structured Query Language (SQL), context information associated with the first SQL is obtained, where the context information is a set of information that is directly or indirectly related to the first SQL and is used to support the rewrite; based on the rewrite requirements corresponding to the rewrite request and the first SQL, the context information is filtered to obtain at least one key piece of information, where the key piece of information is information that can provide reference during the rewrite process of the first SQL; the first SQL is rewritten through the at least one key piece of information to obtain a second SQL.

[0020] By obtaining context information associated with the first SQL, the rewrite scope is extended from the SQL itself to related factors, providing comprehensive basic data support for subsequent accurate rewriting, and solving the problem in related technologies that ignores related factors during the rewrite of SQL, because these factors directly affect SQL execution and results, and comprehensive consideration is required to ensure that the rewritten SQL is correctly executed and accurate results are obtained.

[0021] Further considering that the obtained context information may be redundant, and redundant information will introduce noise and affect the rewrite accuracy, in this embodiment, the context information is also filtered based on the rewrite requirements corresponding to the rewrite request and the first SQL to obtain at least one key piece of information, so that filtering the key information can remove the noise brought by redundant information, making the rewrite process focus on the information valuable for the rewrite, avoiding rewrite errors caused by interference from redundant information, and further improving the rewrite accuracy.

[0022] Then, the first SQL is rewritten through the at least one key piece of information to obtain a second SQL. The key information provides a clear direction and basis for the rewrite, and thus achieves the purpose of fully considering various factors related to SQL during the rewrite using the key information, making the rewritten SQL better adapt to actual requirements, thereby achieving the technical effect of improving the rewrite accuracy of the Structured Query Language, and further solving the technical problem of low rewrite accuracy of the Structured Query Language. BRIEF DESCRIPTION OF THE DRAWINGS

[0023] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation of the present application. In the drawings:

[0024] Figure 1 is a schematic diagram of an application environment of an optional method for rewriting a Structured Query Language according to an embodiment of the present application;

[0025] Figure 2It is a schematic diagram of the process of an optional method for rewriting a structured query language according to an embodiment of the present application;

[0026] Figure 3 It is a schematic diagram of an optional method for rewriting a structured query language according to an embodiment of the present application;

[0027] Figure 4 It is a schematic diagram of another optional method for rewriting a structured query language according to an embodiment of the present application;

[0028] Figure 5 It is a schematic diagram of another optional method for rewriting a structured query language according to an embodiment of the present application;

[0029] Figure 6 It is a schematic diagram of another optional method for rewriting a structured query language according to an embodiment of the present application;

[0030] Figure 7 It is a schematic diagram of an optional device for rewriting a structured query language according to an embodiment of the present application;

[0031] Figure 8 It is a schematic diagram of the structure of an optional electronic device according to an embodiment of the present application. Detailed implementation manners

[0032] In order to enable those skilled in the art to better understand the solutions of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.

[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged under appropriate circumstances so that the embodiments of the present application described herein can be implemented in an order different from those illustrated or described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units does not necessarily have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.

[0034] According to one aspect of the embodiments of the present application, a method for rewriting a Structured Query Language is provided. Optionally, as an alternative implementation, the method for rewriting the Structured Query Language can be but is not limited to being applied to an environment such as Figure 1 shown. Among them, it can include but is not limited to a user device 102 and a server 112. The user device 102 can include but is not limited to a display 104, a processor 106, and a memory 108. The server 112 includes a database 114 and a processing engine 116.

[0035] The specific process can be as follows:

[0036] Step S102, the user device 102 obtains a rewriting request for the first Structured Query Language;

[0037] Step S104, the rewriting request is sent to the server 112 through the network 110;

[0038] Steps S106 - S110, the server 112, through the processing engine 116, in response to the rewriting request for the first Structured Query Language, obtains the context information associated with the first Structured Query Language, and further filters the context information based on the rewriting requirements corresponding to the rewriting request and the first Structured Query Language to obtain at least one key piece of information; the first Structured Query Language is rewritten through the at least one key piece of information to obtain a second Structured Query Language;

[0039] Step S112, the second Structured Query Language is sent to the user device 102 through the network 110. The user device 102 displays the second Structured Query Language on the display 104 through the processor 106 and stores the second Structured Query Language in the memory 108.

[0040] In addition to Figure 1 the examples shown, the above user device can be a terminal device configured with a target client, and can include but is not limited to at least one of the following: mobile phones (such as Android mobile phones, iOS mobile phones, etc.), laptop computers, tablet computers, handheld computers, MIDs (Mobile Internet Devices), PADs, desktop computers, smart TVs, etc. The target client can be a video client, an instant messaging client, a browser client, an education client, etc. The above network can include but is not limited to: wired networks, wireless networks, where the wired network includes: local area networks, metropolitan area networks, and wide area networks, and the wireless network includes: Bluetooth, WIFI, and other networks for implementing wireless communication. The above server can be a single server, or a server cluster composed of multiple servers, or a cloud server. The above is only an example, and no limitations are made in this embodiment.

[0041] As an example, the terminal device may specifically include, but is not limited to, mobile phones, computers, intelligent voice interaction devices, smart home appliances, vehicle-mounted terminals, aircraft, etc. This embodiment can be applied to various scenarios, including but not limited to digital humans, virtual humans, games, virtual reality, extended reality (XR), etc.

[0042] The server can also be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, as well as big data and artificial intelligence platforms.

[0043] Optionally, as an alternative implementation, as Figure 2 shown, the method for rewriting structured query language can be executed by an electronic device, which can be, for example, Figure 1 the user device or server shown, and the specific steps include:

[0044] S202, in response to a rewrite request for the first structured query language, obtain the context information associated with the first structured query language, where the context information is a set of information directly or indirectly related to the first structured query language and used to support the rewrite;

[0045] In an alternative embodiment, the structured query language can be a standardized programming language for managing and operating relational databases. It provides a unified set of syntax and commands, enabling users to interact with the database and perform various data operation tasks.

[0046] In an alternative embodiment, the first structured query language can be the initial SQL statement that is the object of the rewrite operation in the SQL rewrite scenario. Specifically, the first structured query language can be understood as the initial structured query language statement that needs to be rewritten, such as the original SQL statement used in database queries, which needs to be rewritten for various reasons (such as performance optimization, function adjustment, adaptation to new business rules, etc.).

[0047] In an alternative embodiment, the rewrite request can be an instruction for initiating the rewrite operation on the first SQL, indicating the need to modify the existing first SQL, such as from a database administrator, developer, or business analyst, etc., to meet specific business or technical requirements.

[0048] In an alternative embodiment, the context information may be a set of information directly or indirectly related to the first SQL and used to support rewriting. Such information may include database table structures (such as table names, field names, field types, etc.), business rules (such as data validity constraints, definitions of different states, etc.), data distribution (such as value ranges of certain fields, data volume distribution, etc.). In addition, the context information also provides a more comprehensive perspective for SQL rewriting to ensure that the rewritten SQL can be correctly executed and meet business requirements.

[0049] Specifically, when a request to rewrite an existing first Structured Query Language (first SQL) is received, this embodiment starts to collect various types of information related to this first SQL, and this information is collectively referred to as context information.

[0050] This can be understood as a triggered information collection action that, based on the rewriting request, searches for and aggregates a set of information that has a direct or indirect connection with the first SQL and can assist in the rewriting work.

[0051] For further illustration, optionally assume that the first SQL is used to query the basic information of all products in an e-commerce system, and the statement is SELECT * FROM products;. When a rewriting request is received to query product information of a specific category and within a certain price range, the system starts to obtain context information. Such context information may include the structure of the products table (such as fields product_id, product_name, category, price, etc.), the definition of product categories (such as the codes or names corresponding to categories such as electronic products, clothing, etc.), the business rules of the price range (such as the minimum and maximum prices), etc.

[0052] This step provides rich materials for the subsequent rewriting work, enabling the rewritter (or rewriting system) to understand various aspects such as the data environment and business rules involved in the first SQL, avoiding errors caused by missing information during the rewriting process, and laying a solid foundation for accurate rewriting.

[0053] S204. Based on the rewriting requirements corresponding to the rewriting request and the first Structured Query Language, filter the context information to obtain at least one key piece of information, where the key information is information that can provide reference during the rewriting process of the first Structured Query Language;

[0054] In an alternative embodiment, the rewriting requirement may be the specific requirements and objectives that should be achieved after the first SQL is rewritten as proposed in the SQL rewriting request. It can also be understood as the specific requirements specified in the rewriting request, which stipulate what the first SQL needs to be rewritten into. For example, changing the query conditions, adjusting the sorting method of the query results, optimizing the query performance, etc. It can be the basis for filtering context information and performing SQL rewriting.

[0055] In an alternative embodiment, the key information may be the information filtered from the context information and having important reference value for the rewriting of the first SQL. Such as the information obtained after filtering the context information and providing reference during the rewriting process of the first SQL. The key information can be extracted from a large amount of context information, is closely related to the rewriting requirement, and can directly affect the correctness and effectiveness of the rewritten SQL.

[0056] Specifically, after collecting a large amount of context information, this embodiment needs to filter these context information according to the specific requirements (rewriting requirements) clearly proposed in the rewriting request and the characteristics of the first SQL itself, and select the information with important value for rewriting from them, that is, the key information.

[0057] This can be understood as a process of information filtering and refinement, filtering out the key content with reference significance for rewriting from the context information based on the rewriting requirement and the first SQL.

[0058] For further illustration, taking the above e-commerce system as an example, the rewriting requirement is to query product information of a specific category and within a certain price range. Among the obtained context information, the category field is used to distinguish product categories, and the price field is used to represent product prices. These two fields and their related business rules (such as the encoding of specific categories, the price range) are the key information. While other descriptive fields of the product (such as product color, weight, etc.) may not be key information under the current rewriting requirement.

[0059] By filtering to obtain the key information, a large amount of information irrelevant or less relevant to the rewriting is removed, avoiding interference caused by this information during the rewriting process. The key information is more focused, enabling the rewritter (or rewriting system) to concentrate on the factors that truly affect the rewriting, improving the efficiency and accuracy of the rewriting.

[0060] S206, rewrite the first structured query language through at least one piece of key information to obtain a second structured query language;

[0061] In an alternative embodiment, the second Structured Query Language may be a new SQL statement based on the first Structured Query Language obtained after a rewriting operation. For example, a new Structured Query Language statement obtained by rewriting the first Structured Query Language using at least one key piece of information. The second Structured Query Language better meets the rewriting requirements and can be correctly executed in the database and satisfy the business requirements.

[0062] Specifically, using the filtered key information, in this embodiment, the first SQL is modified and adjusted to meet the rewriting requirements, and finally the rewritten second Structured Query Language (second SQL) is obtained.

[0063] This can be understood as an operation of restructuring the first SQL based on the key information, integrating the key information into the new SQL statement to achieve the rewriting goal.

[0064] For further illustration, optionally, for example, according to the key information (category field, price field, and related business rules) filtered previously, the first SQL is rewritten. Suppose the specific category is electronic products and the price range is 100 - 500, and the corresponding second SQL is obtained after rewriting.

[0065] By rewriting with the key information, the obtained second SQL can better meet the actual business requirements. It accurately reflects the requirements in the rewriting request, improving the pertinence and efficiency of the query. At the same time, since the rewriting is based on comprehensive and accurate key information, the second SQL is more likely to succeed in execution and can return results that meet expectations, enhancing the quality and practicality of SQL rewriting.

[0066] Optionally, the content of this embodiment is applicable to or can be combined with the above-mentioned other embodiments.

[0067] It should be noted that this embodiment can be understood as a systematic method for rewriting the Structured Query Language (SQL). When receiving a rewriting request for the first Structured Query Language (first SQL), first, the context information associated with the first SQL is obtained. These are a set of information directly or indirectly related and used to support the rewriting. Then, based on the rewriting requirements corresponding to the rewriting request and the first SQL itself, the context information is filtered to obtain at least one key piece of information, which can provide reference during the rewriting process. Finally, these key information are used to rewrite the first SQL, thereby obtaining the second Structured Query Language (second SQL). This solution aims to improve the accuracy of SQL rewriting and ensure that the rewritten SQL can better meet the actual requirements.

[0068] For further illustration, optionally, for example Figure 3As shown, in response to a rewrite request for the first Structured Query Language 302, context information 304 associated with the first Structured Query Language 302 (such as Information A, Information B, Information C, etc.) is obtained. Among them, the context information 304 is a set of information that is directly or indirectly related to the first Structured Query Language 302 and is used to support the rewrite. Based on the rewrite requirements corresponding to the rewrite request and the first Structured Query Language 302, the context information 304 is filtered to obtain at least one key information 306 (such as Information A and Information C). Among them, the key information 306 is information that can provide reference during the rewrite process of the first Structured Query Language 302. The first Structured Query Language 302 is rewritten through at least one key information 306 to obtain a second Structured Query Language 308.

[0069] Through the embodiments provided in this application, by obtaining the context information associated with the first Structured Query Language, the rewrite scope is extended from the SQL itself to related factors, providing comprehensive basic data support for subsequent accurate rewriting, and solving the problem in related technologies that the rewrite of SQL ignores related factors. Because these factors directly affect the SQL execution and results, comprehensive consideration is required to ensure that the rewritten SQL is correctly executed and accurate results are obtained.

[0070] Further considering that the obtained context information may be redundant, and redundant information will introduce noise and affect the rewrite accuracy. In this embodiment, the context information is also filtered based on the rewrite requirements corresponding to the rewrite request and the first Structured Query Language to obtain at least one key information, so as to filter the key information to remove the noise brought by redundant information, make the rewrite process focus on the information valuable for the rewrite, avoid rewrite errors caused by the interference of redundant information, and further improve the rewrite accuracy.

[0071] Then, the first Structured Query Language is rewritten through at least one key information to obtain a second Structured Query Language. The key information provides a clear direction and basis for the rewrite, and thus achieves the purpose of fully considering various factors related to SQL when using the key information for rewriting, making the rewritten SQL better adapt to the actual requirements, and thereby realizing the technical effect of improving the rewrite accuracy of the Structured Query Language.

[0072] As an optional solution, obtaining the context information associated with the first Structured Query Language includes:

[0073] S1-1, obtaining the source information of the first Structured Query Language, where the source information is used to represent the source of the first Structured Query Language;

[0074] S1-2, based on the source information, obtaining the context information.

[0075] Optionally, the content of this embodiment is applicable to or combined with the above other embodiments.

[0076] In an alternative embodiment, the source information can be used to indicate the source of the first SQL, which can be the system module that generates the SQL, user operation records, specific business processes, etc. For example, the SQL can come from the order management module of an e-commerce system or be manually input by an administrator through a database management tool at a specific time.

[0077] Specifically, this embodiment can conduct in-depth research on the source information to understand the business background and data associations of the first SQL. For example, analyze the functions and data processing flows of the order management module to determine other data tables and fields related to order queries.

[0078] Further, according to the analysis results, collect context information related to the first SQL from various channels such as databases, system documents, and business rules. For example, obtain the structure information of the order table, product table, and user table from the database, and understand the definition of order status and business processes from the system documents.

[0079] It should be noted that this embodiment focuses on obtaining context information associated with the first Structured Query Language (first SQL), which is specifically refined into two key steps: obtaining the source information of the first SQL and obtaining context information based on the source information. Through these two steps, various types of information related to the first SQL can be comprehensively and accurately collected, providing a solid foundation for subsequent operations such as SQL rewriting.

[0080] Through the embodiments provided in this application, obtain the source information of the first Structured Query Language, where the source information is used to indicate the source of the first Structured Query Language; based on the source information, obtain context information. Through the organic combination of these two steps, the context information associated with the first SQL can be systematically obtained, and thus the purpose of providing a clear direction for obtaining context information by obtaining source information is achieved, making the collection of context information more accurate and efficient, thereby realizing the technical effect of improving the accuracy of context information.

[0081] As an alternative solution, obtaining the source information of the first Structured Query Language includes:

[0082] Parse the corresponding database table name from the first Structured Query Language, where the database table name is used to indicate the data storage location that the first Structured Query Language is to operate on, and the source information includes the database table name;

[0083] Based on the source information, obtaining context information includes:

[0084] Use the database table name as a query condition to retrieve the metadata database, and obtain the field information of the database table corresponding to the first Structured Query Language (SQL). Among them, the context information includes the field information.

[0085] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0086] In an alternative embodiment, the database table name can be used to represent the data storage location that the first SQL is to operate on.

[0087] In an alternative embodiment, the metadata database can be used to store the metadata information of various objects in the database (such as tables, fields, indexes, etc.), and record the detailed information such as the structure of each table, the type, length, and constraints of the fields.

[0088] In an alternative embodiment, the field information may include attributes such as the name, type, length, and whether it is nullable of each field in the database table. For example, for the students table, the field information may include student_id (integer type, primary key), name (string type), age (integer type), etc.

[0089] Specifically, this embodiment performs a syntax analysis on the first SQL to clarify its various components, such as the SELECT, FROM, WHERE, and other clauses. This is the basis for parsing the database table name. Only by understanding the structure of the SQL can the location of the database table name be accurately found. After identifying the SQL statement structure, locate the FROM clause and extract the database table name from it. For the SQL statement SELECT name, age FROM students WHERE grade = 'A', it can be determined through parsing that students is the database table name.

[0090] Use the parsed database table name as a query condition to construct a query statement for the metadata database. For example, if the metadata database uses SQL for querying, the query statement can be SELECT * FROM metadata_table WHERE table_name ='students', where metadata_table is the table that stores table information in the metadata database. Submit the constructed query statement to the metadata database for execution to obtain the query result, that is, the field information of the database table corresponding to the first SQL.

[0091] It should be noted that this embodiment is directed to obtaining the source information of the first Structured Query Language (first SQL) and obtaining context information based on the source information. Specifically, the table name in the database is first parsed from the first SQL as part of the source information, and then the table name in the database is used as a query condition to retrieve the field information of the corresponding table in the database from the meta-information database as context information. Through these two key steps, the key information closely related to the first SQL can be accurately obtained, providing support for subsequent operations (such as SQL rewriting).

[0092] Through the embodiment provided by the present application, the corresponding table name in the database is parsed from the first Structured Query Language, where the table name in the database is used to represent the data storage location to be operated by the first Structured Query Language, and the source information includes the table name in the database; the table name in the database is used as a query condition to retrieve the field information of the table corresponding to the first Structured Query Language from the meta-information database, where the context information includes the field information, thereby achieving the purpose of efficiently obtaining the source information of the first SQL and the relevant context information, and thus realizing the technical effect of improving the accuracy and efficiency of SQL operations.

[0093] As an alternative solution, obtaining the source information of the first Structured Query Language includes:

[0094] Obtaining the data source information selected on the Structured Query Language interface or parsed from the first Structured Query Language, where the Structured Query Language interface is the interface that triggers the rewriting request, and the data source information is used to represent the data source of the first Structured Query Language, and the source information includes the data source information;

[0095] In an alternative embodiment, the Structured Query Language interface can be the interface that triggers the rewriting request, and the user can perform operations such as inputting, editing, and submitting SQL on this interface. For example, the SQL editing interface provided by database management tools (such as Navicat, DBeaver).

[0096] In an alternative embodiment, the data source information can be used to represent the data source of the first SQL, which can be the database connection information selected by the user on the interface or the data source identifier to which a specific database object (such as a table, view, etc.) parsed from the first SQL belongs. For example, when the connection to a database named company_db is selected on the interface, or it is parsed from the SQL that the employees table in company_db is being operated on, company_db is the data source information.

[0097] Specifically, in this embodiment, when the user operates on the Structured Query Language interface, a data source can be selected from the list of database connections provided on the interface. The system captures the user's selection operation and obtains the information of the selected data source. For example, if the user has connected multiple databases in Navicat and selects the company_db database when editing SQL, the system records company_db as the data source information.

[0098] Perform syntax analysis on the first SQL to identify the database objects involved therein (such as table names, view names, etc.), and based on the internal database mapping relationship of the system, parse out the data source information to which these objects belong. For example, for SELECT * FROM employees, the system knows through analysis that the employees table belongs to the company_db database, thereby obtaining the data source information company_db.

[0099] Based on the source information, obtain context information, including:

[0100] S2-1, through the data source information, obtain the structured query dialect corresponding to the first Structured Query Language;

[0101] S2-2, obtain the dialect knowledge related to the structured query dialect, where the context information includes dialect knowledge.

[0102] In an alternative embodiment, the structured query dialect can be such that different database systems can have their own unique SQL dialects, which differ in aspects such as syntax, functions, data types, etc. For example, MySQL, Oracle, SQL Server, etc. all have their own SQL dialects. Through the data source information, the database system corresponding to the first SQL can be determined, thereby determining the structured query dialect it uses.

[0103] In an alternative embodiment, the dialect knowledge can include detailed information such as the syntax rules, function definitions, data type characteristics, and specific optimization techniques of the structured query dialect. For example, the dialect knowledge of MySQL includes the string functions it supports (such as CONCAT), date functions (such as NOW), and specific SQL syntax (such as the LIMIT clause for pagination).

[0104] Specifically, in this embodiment, according to the obtained data source information, query the internal database dialect mapping table of the system to determine the structured query dialect corresponding to the first SQL. For example, if the data source information is company_db and the database uses MySQL, then the corresponding structured query dialect can be determined as the MySQL dialect.

[0105] Retrieve knowledge related to the determined structured query dialect from the dialect knowledge base. The dialect knowledge base can be a pre-constructed database or document collection that stores detailed information about various database dialects. For example, by querying the dialect knowledge base, knowledge such as the syntax rules and function lists of the MySQL dialect can be obtained.

[0106] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0107] It should be noted that this embodiment is directed to obtaining the source information of the first structured query language (the first SQL) and obtaining context information based on this source information. Specifically, first obtain the data source information selected on the structured query language interface or parsed from the first SQL as part of the source information, then obtain the structured query dialect corresponding to the first SQL through the data source information, and further obtain the dialect knowledge related to this dialect as context information. Through these two key steps, key information closely related to the first SQL can be obtained comprehensively and accurately, providing support for subsequent operations such as SQL rewriting.

[0108] Through the embodiment provided by this application, obtain the data source information selected on the structured query language interface or parsed from the first structured query language. Among them, the structured query language interface is the interface that triggers the rewriting request, and the data source information is used to represent the data source of the first structured query language. The source information includes the data source information; through the data source information, obtain the structured query dialect corresponding to the first structured query language; obtain the dialect knowledge related to the structured query dialect. Among them, the context information includes the dialect knowledge, and thus the purpose of being able to efficiently obtain the source information and related context information of the first SQL is achieved, thereby realizing the technical effect of improving the acquisition efficiency of the context information.

[0109] As an alternative solution, obtaining the context information associated with the first structured query language includes:

[0110] S3-1, obtain the execution result obtained after executing the first structured query language;

[0111] In an alternative embodiment, the execution result may refer to the data set or related information returned after executing the first SQL. For example, if SELECT name, age FROM students WHERE grade = 'A' is the first SQL, the execution result may be a record set of the names and ages of students who meet the conditions, such as [(John, 20), (Alice, 22)].

[0112] Specifically, in this embodiment, the first SQL is submitted to the database management system (DBMS) for execution. This can be achieved through database client tools, application code, etc. For example, in Python, use the pymysql library to connect to the MySQL database and execute SQL statements.

[0113] After the DBMS executes the SQL, it returns the execution result to the requester. The requester needs to write code to receive and process this result. For example, in Python, use the cursor.fetchall() method to obtain the execution result.

[0114] S3-2. Obtain context information based on the execution result.

[0115] Specifically, in this embodiment, a detailed statistical analysis is performed on the execution result, including aspects such as the quantity, type, distribution, and outliers of the data. For example, statistically analyze the age distribution of students in the execution result, and calculate the average age, maximum age, and minimum age, etc.

[0116] According to the analysis result, extract the context information closely related to the first SQL. For example, if it is found that the ages of students in the execution result are all between 7 and 25 years old, then the value range of age [7, 25] can be used as the context information.

[0117] Optionally, the content of this embodiment is applicable to the above-mentioned other embodiments or combined with the above-mentioned other embodiments.

[0118] It should be noted that this embodiment is directed to obtaining the context information associated with the first structured query language (the first SQL). Specifically, by obtaining the execution result after executing the first SQL and obtaining the context information based on this execution result. This solution can utilize the actual execution feedback to more accurately collect various types of information related to the first SQL, providing support for subsequent operations (such as SQL rewriting, data analysis, etc.).

[0119] Through the embodiments provided in this application, obtain the execution result obtained after executing the first structured query language; based on the execution result, obtain the context information, thereby achieving the purpose of being able to efficiently obtain the context information of the first SQL, and thus realizing the technical effect of improving the acquisition efficiency of the context information.

[0120] As an optional solution, obtaining the execution result obtained after executing the first structured query language includes:

[0121] Obtain the data result table obtained after multiple adjustments and submissions for execution during the process of writing the first structured query language, where the execution result includes the data result table;

[0122] In an alternative embodiment, the writing process may refer to the entire process from the initial conception to the final determination of the first SQL, during which multiple operations such as modifying query conditions and selecting fields may be involved.

[0123] In an alternative embodiment, multiple adjustments may be multiple modifications and optimizations made to the SQL statement when writing the first SQL according to actual requirements and data feedback. For example, all student information can be queried first, and then adjusted to only query the names and ages of students with a grade of A according to business requirements.

[0124] In an alternative embodiment, the data result table may be the result presented in tabular form of the data set returned by the database each time the first SQL is submitted for execution.

[0125] Specifically, in this embodiment, during the process of writing the first SQL, the content and reasons for each adjustment are recorded in detail. For example, recording the adjustment from SELECT * to SELECT name, age is because only the name and age information of students is needed.

[0126] After each adjustment of the first SQL, it is submitted to the database management system for execution, and the returned data result table is collected. This process can be implemented using a database client tool or writing code. For example, in Python, use the pymysql library to connect to the database and execute the SQL, and store the result in a variable.

[0127] Based on the execution result, context information is obtained, including:

[0128] The data result table is parsed and converted into natural language text, where the context information includes the natural language text.

[0129] In an alternative embodiment, the natural language text may be the information of the data result table expressed in the language commonly used by humans in daily life. For example, for the above data result table, the natural language text may be "The query result shows that the students with a grade of A are John and Alice, and their ages are 20 and 22 years old respectively."

[0130] Specifically, this embodiment analyzes information such as the column names, data types, and data values of the data result table. For example, it is recognized that the table has two columns, namely name (student name) and age (student age), and the specific student information corresponding to each row of data.

[0131] According to the parsing result, using a natural language generation algorithm or template, the data result table is converted into natural language text. Natural language processing (NLP) techniques, such as pre-trained language models, can be used to implement this process. For example, by writing code to call the language model interface, the parsed data is input into the model to generate the corresponding natural language text.

[0132] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0133] It should be noted that this embodiment is directed to obtaining the execution result of the first Structured Query Language (first SQL) and obtaining context information based on the execution result. Specifically, first, obtain the data result table obtained after multiple adjustments and submissions during the writing of the first SQL as part of the execution result, and then parse and convert the data result table into natural language text as context information. Through these two key steps, key information closely related to the first SQL can be comprehensively and deeply obtained, providing support for subsequent operations (such as SQL analysis, optimization, document generation, etc.).

[0134] Through the embodiment provided by this application, during the process of writing the first Structured Query Language, after multiple adjustments and submissions, the obtained data result table is obtained, where the execution result includes the data result table; the data result table is parsed and converted into natural language text, where the context information includes the natural language text, thereby achieving the purpose of efficiently obtaining the execution result of the first SQL and related context information, and thus realizing the technical effect of improving the acquisition efficiency of context information.

[0135] As an optional solution, based on the rewriting requirements corresponding to the rewriting request and the first Structured Query Language, screen the context information to obtain at least one key information, including:

[0136] S4-1, input the rewriting requirements, the first Structured Query Language, and the context information into a classification model, where the classification model is a large language model trained using multiple first sample data and used to screen key information;

[0137] In an optional embodiment, the rewriting requirements may clarify the specific requirements for rewriting the first SQL. For example, rewrite the SQL from one database dialect to another dialect, or optimize the query performance of the SQL, etc. For example, the rewriting requirement may be to rewrite the SQL in MySQL dialect to Oracle dialect.

[0138] In an optional embodiment, the first Structured Query Language (first SQL) may be the initial SQL statement that needs to be rewritten, such as SELECT name, age FROM students WHERE grade = 'A'.

[0139] In an alternative embodiment, the context information can be various types of information related to the first SQL, such as execution results, data distribution, field meanings, etc. For example, the execution result table shows that the ages of students are concentrated between 7 and 25 years old, and the field meaning explanation indicates that grade represents the student's grade level.

[0140] In an alternative embodiment, the classification model can be a large language model trained using multiple first sample data for screening key information. The first sample data includes different rewriting requirements, SQL statements, and context information, as well as the corresponding key information annotations. For example, the sample data can include an SQL with the rewriting requirement of dialect conversion, the corresponding context information, and the key information such as specific functions or syntax structures marked.

[0141] Specifically, in this embodiment, the rewriting requirement, the first SQL, and the context information are integrated to form a format suitable for inputting into the classification model. For example, they are combined into a data structure containing multiple fields, and each field corresponds to the rewriting requirement, the SQL statement, and the context information respectively.

[0142] The integrated data is input into the classification model. The input of the data can be achieved by calling the API interface of the model or using the corresponding programming library. For example, in Python, use the transformers library to load the pre-trained large language model and pass the data to the model for processing.

[0143] S4-2, Obtain the classification result output by the classification model, where the classification result includes at least one key information.

[0144] In an alternative embodiment, the classification result can be the result output by the classification model after analyzing the input information, which includes at least one key information. For example, for the rewriting requirement of rewriting an SQL in MySQL dialect to Oracle dialect, the classification result can indicate that the key information is that the LIMIT clause in MySQL needs to be converted to the ROWNUM condition in Oracle.

[0145] In an alternative embodiment, the key information can be information that has an important impact on SQL rewriting, such as specific functions, syntax structures, field mapping relationships, etc.

[0146] Specifically, in this embodiment, the classification result is extracted from the output of the classification model. The output of the classification model can be presented in a specific format, such as JSON format, and code needs to be written to parse and extract the key information. For example, use the json library in Python to parse the JSON data output by the model and obtain the values of the key information fields.

[0147] Organize the extracted key information to make it clearer and easier to use. The key information can be stored in a list or dictionary for convenient subsequent operations.

[0148] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0149] It should be noted that in this embodiment, for the rewriting requirements corresponding to the rewriting request and the first structured query language (the first SQL), a classification model is used to screen the context information to obtain at least one key information. By inputting the rewriting requirements, the first SQL, and the context information into the trained large language classification model, the classification result output by the model is obtained, thereby realizing the accurate extraction of key information and providing support for subsequent operations such as SQL rewriting.

[0150] Through the embodiment provided by this application, the rewriting requirements, the first structured query language, and the context information are input into the classification model, where the classification model is a large language model trained using multiple first sample data and used to screen key information; the classification result output by the classification model is obtained, where the classification result includes at least one key information. Using the large language classification model for information screening has high accuracy and intelligence, can handle complex semantic and context relationships, and thus achieves the purpose of efficiently screening the context information based on the rewriting requirements and the first SQL to obtain at least one key information, thereby realizing the technical effect of improving the acquisition efficiency of key information.

[0151] As an optional solution, before inputting the rewriting requirements, the first structured query language, and the context information into the classification model, the method further includes:

[0152] S5-1, obtain multiple first sample data, where the first sample data consists of a first structured query language sample, the first rewriting requirement of the first structured query language sample, and multiple context information samples corresponding to the first structured query language sample;

[0153] In an optional embodiment, the first sample data may be a data set consisting of a first structured query language sample, the first rewriting requirement of the first structured query language sample, and multiple context information samples corresponding to the first structured query language sample.

[0154] In an optional embodiment, the first structured query language sample may be multiple different initial SQL statement examples, such as SELECT*FROM employees, SELECT product_name,price FROM products WHEREcategory='Electronics', etc.

[0155] In an alternative embodiment, the first rewriting requirement may be a rewriting requirement proposed for each first Structured Query Language sample, such as rewriting SQL from the MySQL dialect to the PostgreSQL dialect, or optimizing the query performance of SQL, etc.

[0156] In an alternative embodiment, the context information sample may be various types of information related to each first Structured Query Language sample, such as execution results, data distribution, field meanings, etc. For example, for SELECT * FROM employees, the context information sample may include the structure information of the employee table, the statistical characteristics of employee data, etc.

[0157] Specifically, in this embodiment, the first Structured Query Language samples, the corresponding first rewriting requirements, and the context information samples are collected from multiple channels. It can be obtained through database logs, open-source SQL datasets, manual construction, etc. For example, different types of SQL statements are obtained from open-source SQL datasets as samples.

[0158] The collected data is sorted and integrated to form a first sample data format that meets the requirements. For example, each SQL sample, the corresponding rewriting requirement, and the context information sample are stored in a data structure, such as a dictionary or a database record.

[0159] S5-2, using a small amount of labeled first sample data as a prompt to guide the initial classification model to learn the classification pattern, where the label is used to mark the key information in multiple context information samples.

[0160] In an alternative embodiment, the labeled first sample data may be that in the first sample data, some data is marked with key information, and these marked information are the labels. For example, for an SQL sample with a rewriting requirement of dialect conversion, the label can mark the specific functions or syntax structures that need to be converted.

[0161] In an alternative embodiment, the initial classification model may be a large language model that has not been trained or has only been simply pre-trained, with basic language understanding and processing capabilities, but has not learned the classification pattern for key information screening.

[0162] In an alternative embodiment, the classification pattern may be the rules and methods for the classification model to determine which content belongs to key information based on the input information.

[0163] Specifically, in this embodiment, a small amount of first sample data is labeled manually or using an automatic labeling tool. The labeling process needs to accurately identify the key information related to the rewriting requirement and the first SQL. For example, for the rewriting requirement of dialect conversion, the dialect-specific syntax that needs to be converted in the SQL is marked.

[0164] Input the first sample data with tags as a prompt into the initial classification model to guide the model to learn the classification pattern. Prompt Learning technology can be used to input the sample data and tags into the model in a specific format, enabling the model to automatically adjust its internal parameters and learn the classification rules of key information based on an understanding of the sample data and tags. For example, in Python, use the transformers library to fine-tune the model by constructing an input sequence containing the prompt and tags.

[0165] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0166] It should be noted that before inputting the rewriting requirement, the first structured query language (the first SQL), and the context information into the classification model in this embodiment, it is necessary to first obtain multiple first sample data and use a small amount of first sample data with tags to guide the initial classification model to learn the classification pattern. Through these preliminary preparation steps, a classification model capable of effectively screening key information can be constructed, providing support for subsequent key information screening based on the rewriting requirement and the first SQL.

[0167] Through the embodiment provided by this application, multiple first sample data are obtained, where the first sample data consists of a first structured query language sample, a first rewriting requirement for the first structured query language sample, and multiple context information samples corresponding to the first structured query language sample; use a small amount of first sample data with tags as a prompt to guide the initial classification model to learn the classification pattern, where the tags are used to label the key information in the multiple context information samples. Obtaining multiple first sample data provides rich materials for model training, making the model widely applicable; using a small amount of first sample data with tags for prompt learning to guide the model to quickly learn the classification pattern of key information, thereby achieving the purpose of constructing an efficient and accurate classification model, and thus realizing the technical effect of improving the training quality of the classification model.

[0168] As an optional solution, rewriting the first structured query language through at least one key information to obtain a second structured query language includes:

[0169] S6-1, input at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into a rewriting model, where the rewriting model is a large language model trained using multiple second sample data and used for rewriting structured query languages;

[0170] In an alternative embodiment, at least one key information may be the information obtained after filtering the context information based on the rewriting requirement and the first SQL, which plays a crucial role in SQL rewriting. For example, when rewriting an SQL in MySQL dialect to Oracle dialect, the key information may be the LIMIT clause in MySQL.

[0171] In an alternative embodiment, the rewriting requirement corresponding to the rewriting request may specify the specific requirements for rewriting the first SQL, such as dialect conversion, performance optimization, etc. For example, the rewriting requirement is to rewrite the SQL from SQLServer dialect to PostgreSQL dialect.

[0172] In an alternative embodiment, the first Structured Query Language (the first SQL) may be the initial SQL statement that needs to be rewritten, such as SELECT name,age FROM students WHERE grade='A'.

[0173] In an alternative embodiment, the rewriting model may be a large language model trained using multiple second sample data, which is specifically used for rewriting Structured Query Language. The second sample data includes different rewriting requirements, the first SQL, key information, and the corresponding rewritten SQL (the second SQL). For example, the sample data may include an SQL with a rewriting requirement of optimizing query performance, key information such as index usage, and the optimized rewritten SQL.

[0174] Specifically, in this embodiment, at least one key information, the rewriting requirement, and the first SQL are integrated to form a format suitable for input to the rewriting model. They can be combined into a data structure containing multiple fields, and each field corresponds to the key information, the rewriting requirement, and the first SQL respectively.

[0175] The integrated data is input into the rewriting model. The input of the data can be achieved by calling the API interface of the model or using the corresponding programming library. For example, in Python, the transformers library is used to load the pre-trained large language rewriting model, and the data is passed to the model for processing.

[0176] S6-2, Obtain the rewriting result output by the rewriting model, where the rewriting result includes the second Structured Query Language.

[0177] In an alternative embodiment, the rewriting result may be the result output after the rewriting model analyzes and processes the input information, which includes the second Structured Query Language. For example, for the rewriting requirement of rewriting SQL in SQL Server dialect to PostgreSQL dialect, the rewriting result may be SELECT name,age FROM students WHERE grade='A' LIMIT 10 (assuming the original SQL Server statement has a similar pagination requirement and the rewritten statement conforms to the PostgreSQL syntax).

[0178] Specifically, in this embodiment, the rewriting result is extracted from the output of the rewriting model. The output of the rewriting model can be presented in a specific format, such as JSON format, and code needs to be written to parse and extract the second SQL from it. For example, use the json library in Python to parse the JSON data output by the model and obtain the value of the second SQL field.

[0179] Verify the extracted second SQL to ensure that it conforms to the syntax rules and rewriting requirements. Verification can be performed through a syntax checking tool or manual checking. For example, use a database management tool to check whether the second SQL can be correctly executed in the target database.

[0180] Optionally, the content of this embodiment is applicable to or combined with the above-mentioned other embodiments.

[0181] It should be noted that in this embodiment, the second Structured Query Language (second SQL) is obtained through the rewriting model by using key information, rewriting requirements, and the first Structured Query Language (first SQL). Input the key information, rewriting requirements, and the first SQL into the trained large language rewriting model, and obtain the rewriting result output by the model to achieve the automated rewriting of SQL to meet different business requirements and technical requirements.

[0182] Through the embodiments provided in this application, at least one key information, the rewriting requirement corresponding to the rewriting request, and the first Structured Query Language are input into the rewriting model, where the rewriting model is a large language model trained using multiple second sample data and used for rewriting the Structured Query Language; obtain the rewriting result output by the rewriting model, where the rewriting result includes the second Structured Query Language. Using the large language rewriting model to rewrite SQL has a high degree of automation and intelligence, can handle complex rewriting requirements and syntax conversions, and thus achieves the purpose of efficiently using key information, rewriting requirements, and the first SQL to obtain the second SQL through the rewriting model, thereby realizing the technical effects of reducing the workload and error rate of manual rewriting, and improving the development efficiency and SQL quality.

[0183] As an alternative solution, before inputting at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into the rewriting model, the method further includes:

[0184] S7-1, obtaining a plurality of second sample data, where the second sample data is composed of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample;

[0185] In an alternative embodiment, the second sample data may be a data set composed of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample.

[0186] In an alternative embodiment, the second structured query language sample may be a plurality of different rewritten SQL statement examples. For example, the SQL statement after rewriting the MySQL dialect to the Oracle dialect, such as SELECT name, age FROM students WHERE grade = 'A' AND ROWNUM <= 10 (assuming the original MySQL statement has a similar paging requirement).

[0187] In an alternative embodiment, the second rewriting requirement may be the original rewriting requirement corresponding to each second structured query language sample, such as dialect conversion, performance optimization, etc. For example, the second rewriting requirement may be to rewrite SQL from the SQL Server dialect to the PostgreSQL dialect.

[0188] Specifically, in this embodiment, the second structured query language samples and the corresponding second rewriting requirements are collected from multiple channels. It can be obtained through database migration projects, open-source SQL rewriting data sets, manual construction, etc. For example, collect the SQL statements after different dialect conversions and their rewriting requirements from database migration projects.

[0189] Sort and integrate the collected data to form a second sample data format that meets the requirements. For example, store each second SQL sample and the corresponding second rewriting requirement in a data structure, such as a dictionary or a database record.

[0190] S7-2, using a plurality of second sample data to train the initial rewriting model to obtain a training result;

[0191] In an alternative embodiment, the initial rewriting model: a large language model that has not been trained or has only been simply pre-trained, has basic language understanding and processing capabilities, but has not learned specific patterns for SQL rewriting.

[0192] In an alternative embodiment, the training result can be the output obtained after the initial rewriting model is trained with the second sample data, and can include metrics such as the loss function value and accuracy of the model, which reflect the fitting degree of the model to the training data.

[0193] Specifically, in this embodiment, the second sample data is input into the initial rewriting model, and the parameters of the model are adjusted through the backpropagation algorithm, enabling the model to learn the rewriting patterns and rules in the second sample data. Deep learning frameworks such as TensorFlow or PyTorch can be used to implement the training process of the model. For example, define the loss function and optimizer in PyTorch, and update the weights of the model through iterative training.

[0194] During the training process, the training results are evaluated regularly. The validation set data can be used to calculate metrics such as the loss function value and accuracy of the model. For example, after training a certain number of rounds, use the validation set data to test the model and calculate the loss and accuracy of the model on the validation set.

[0195] S7-3, in the case where the training result does not meet the convergence condition, adjust the low-rank matrix deployed in the rewriting model until the obtained training result meets the convergence condition.

[0196] In an alternative embodiment, the convergence condition can be a criterion for judging whether the model training is completed, and can include that the loss function value reaches a certain threshold, the accuracy stabilizes at a relatively high level, etc. For example, set the loss function value to be less than 0.1 and the accuracy to be greater than 90% as the convergence condition.

[0197] In an alternative embodiment, the low-rank matrix can be a special matrix deployed in the rewriting model. By adjusting its parameters, the learning ability and representation ability of the model can be changed. The low-rank matrix has fewer parameters, can alleviate the overfitting problem of the model to a certain extent, and improve the training efficiency of the model.

[0198] Specifically, in this embodiment, after each training evaluation, it is judged whether the training result meets the convergence condition. If not, proceed to the next step of adjusting the low-rank matrix. Adjust the parameters of the low-rank matrix deployed in the rewriting model, such as changing the element values or structure of the matrix. Optimization algorithms such as the gradient descent method can be used to adjust the parameters of the low-rank matrix, making the training result of the model gradually approach the convergence condition. Repeat the operations of model training, result evaluation, and matrix adjustment until the training result meets the convergence condition. For example, set a loop, and in each loop, perform a certain number of rounds of training, then evaluate the training result. If the convergence condition is not met, adjust the low-rank matrix and continue the next loop.

[0199] Optionally, the content of this embodiment is applicable to or can be combined with the above-mentioned other embodiments.

[0200] It should be noted that before the key information, rewriting requirements, and the first SQL input rewriting model in this embodiment, it is necessary to first obtain a plurality of second sample data, use these sample data to train the initial rewriting model, and adjust the low-rank matrix deployed in the model when the training result does not meet the convergence condition until the convergence condition is met. Through these preliminary preparation steps, an efficient and accurate rewriting model can be constructed to provide support for subsequent SQL rewriting.

[0201] Through the embodiment provided by the present application, a plurality of second sample data are obtained, where the second sample data consists of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample; the initial rewriting model is trained using the plurality of second sample data to obtain a training result; when the training result does not meet the convergence condition, the low-rank matrix deployed in the rewriting model is adjusted until the obtained training result meets the convergence condition. Obtaining a plurality of second sample data provides rich materials for model training, making the model have wide applicability; training the initial rewriting model using the second sample data enables the model to learn the patterns and rules of SQL rewriting; adjusting the low-rank matrix when the training result does not meet the convergence condition further optimizes the training effect of the model, and thus achieves the purpose that the trained rewriting model can more accurately rewrite the first SQL into the second SQL, improving the rewriting efficiency and quality, reducing the workload and error rate of manual rewriting, and thereby realizing the technical effect of constructing an efficient and accurate rewriting model.

[0202] As an optional solution, before obtaining the context information associated with the first structured query language in response to a rewriting request for the first structured query language, the method further includes:

[0203] In the structured query language editing area, display the first structured query language, where the structured query language editing area is set to allow triggering a corresponding rewriting request for the first structured query language or a part of the language in the first structured query language;

[0204] In an optional embodiment, the structured query language editing area may be a specific interface area for displaying and editing structured query languages, where users can input, modify, and view SQL statements. For example, in the interface of a database management tool, a text box area specifically divided for writing SQL code.

[0205] In an optional embodiment, the first structured query language (the first SQL) may be an initial SQL statement that needs to be rewritten, such as SELECT name,age FROM students WHERE grade='A'.

[0206] In an alternative embodiment, the rewrite request may be an operation instruction initiated by a user to rewrite the first SQL, which may target the entire first SQL or a part of its language. For example, the user may wish to rewrite the query condition part in the SQL for dialect conversion.

[0207] Specifically, in this embodiment, the first SQL is loaded and presented in the structured query language editing area, enabling the user to visually see the SQL statement that needs to be rewritten. The display of the SQL statement in the specified area can be achieved through a programming interface or interface operations. For example, in a web application, JavaScript is used to fill the SQL statement into a text box element.

[0208] The structured query language editing area is configured to recognize the user's operations on the first SQL or a part of its language and trigger the corresponding rewrite request. This can be achieved through an event detector. For example, when the user selects a certain part of the code in the first SQL and clicks a specific button, the rewrite request is triggered.

[0209] After rewriting the first structured query language into a second structured query language through at least one key piece of information, the method further includes:

[0210] The first structured query language and the second structured query language are presented in a line-by-line comparison.

[0211] In an alternative embodiment, the second structured query language (second SQL) may be a new SQL statement obtained after rewriting, which meets the rewrite requirements. For example, the MySQL dialect SELECT name,age FROM students WHERE grade='A' LIMIT 10 is rewritten into the Oracle dialect SELECT name,age FROM students WHERE grade='A' AND ROWNUM <= 10.

[0212] In an alternative embodiment, the line-by-line comparison display may be to present the first SQL and the second SQL in a one-to-one correspondence by row, highlighting the differences between the two to facilitate the user's visual comparison. For example, in a comparison window, the left side shows each row of the first SQL, and the right side shows the corresponding row of the second SQL, with the different parts marked in a special color or with a special mark.

[0213] Specifically, after the rewriting is completed in this embodiment, the first SQL and the rewritten second SQL are obtained. These SQL statements can be obtained from the output of the rewriting model and the initial input. A dedicated comparison tool or code can be used to implement the function of comparing and displaying line by line. A text comparison algorithm, such as the longest common subsequence algorithm, can be adopted to find the differences between two lines of SQL and present them to the user in an intuitive way. For example, use HTML and CSS to create a comparison table and mark the different parts with different colors.

[0214] Optionally, the content of this embodiment is applicable to the above-mentioned other embodiments or combined with the above-mentioned other embodiments.

[0215] It should be noted that specific operations are set before and after the rewriting request is triggered in this embodiment. Before the rewriting request is triggered, the first SQL is displayed in the structured query language editing area and the rewriting request is allowed to be triggered; after the rewriting is completed, a line-by-line comparison and display of the first SQL and the rewritten second structured query language (second SQL) is performed. These operations improve the user's interaction experience with the rewriting system, facilitating the user to perform SQL rewriting operations and intuitively view the rewriting results.

[0216] Through the embodiment provided by the present application, in the structured query language editing area, the first structured query language is displayed, where the structured query language editing area is set to allow a corresponding rewriting request to be triggered for the first structured query language or a part of the first structured query language; a line-by-line comparison and display of the first structured query language and the second structured query language is performed. Before the rewriting request is triggered, the user can conveniently view and select the SQL content to be rewritten in the structured query language editing area and initiate the rewriting request efficiently; after the rewriting is completed, the line-by-line comparison and display function enables the user to intuitively compare the SQL statements before and after the rewriting, understand the rewriting results, and ensure the accuracy and effectiveness of the rewriting, thereby achieving the technical effect of improving the user's interaction experience with the SQL rewriting system.

[0217] As an optional solution, for ease of understanding, the above-mentioned method for rewriting structured query language is applied to the general code model in the SQL field. This embodiment proposes a SQL rewriting solution based on the enhancement of multiple meta-informations and dynamic selection. By inputting various information such as table meta-information, execution results, and dialect documents into the rewriting model for capacity enhancement, and a classification model is added in advance to streamline the information during each request. At the same time, the large model for SQL field rewriting is fine-tuned to solve the problems of weak capacity and lack of knowledge in the general code model in the SQL field.

[0218] In an optional embodiment, such as Figure 4On the SQL analysis interface 404 of the data analysis platform 402 shown, the user can use the "SQL rewrite" function in the SQL editing area 406, input an instruction (such as "Convert the date of the result to the time of Monday of the current week"), obtain the rewritten result 408 of the SQL, and compare it line by line with the original code 410.

[0219] Furthermore, based on Figure 4 the scenario shown, continue as Figure 5 shown, support the user to perform word selection in the original code 410 through an instruction to perform local code rewriting.

[0220] Optionally, the content of this embodiment is applicable to the above-mentioned other embodiments or combined with the above-mentioned other embodiments.

[0221] It should be noted that this embodiment proposes an SQL rewrite scheme based on natural language interaction and supporting various meta-information enhancement and selection. This technical solution includes:

[0222] S8-1, support the user to interact in the code editing area using natural language: The user only needs to input the code rewrite requirement in text form, and this scheme can directly generate the corresponding rewritten SQL code in the code editing area, and at the same time support the user to ask questions about the entire script or select part of the code by mouse word selection to ask questions.

[0223] S8-2, introduce various information such as library table meta-information to enhance the SQL rewrite effect: This scheme parses the user's SQL and accesses the library table metadata information to obtain the field and value information of the original library table, so that the rewrite model can further follow the field type and format requirements of the original table on the basis of changing the code framework, improve the code rewrite quality and reduce the large model hallucination problem; introduce the code execution result data to increase the "user perspective" and enhance the model's understanding of the user's query; introduce the SQL dialect document to enhance the model's understanding of the differences between different dialects.

[0224] S8-3, dynamic information reduction: Before entering the rewrite model, the classification model is used to understand and judge each user request text, and the input of each information is dynamically selected to achieve information reduction.

[0225] S8-4, SQL rewrite model for vertical fields: Use the parameter-efficient fine-tuning method Lora to train the large model in the SQL rewrite field and support the output of the specified SQL dialect.

[0226] For further illustration, optionally, for example Figure 6As shown in the figure, the user inputs the SQL code to be rewritten. The code is parsed by a parser to extract the metadata of the library tables, and the information is enhanced by combining external information such as execution results and data. Subsequently, the enhanced information is used to retrieve the SQL dialect knowledge base to obtain the corresponding SQL dialect knowledge. Then, an information reduction model is used to rewrite the SQL to be rewritten in combination with the retrieved SQL dialect knowledge, generating the rewritten SQL. Finally, the rewritten SQL is output to the front end for the user to use.

[0227] Specifically, the overall technical solution in this embodiment can be summarized into three steps, namely information enhancement, information selection, and SQL rewriting model, which are as follows:

[0228] S9-1, Information Enhancement Module: By parsing the SQL submitted by the user and combining the user query to retrieve the knowledge base, recall multiple paths of information as the input supplement for the rewriting model:

[0229] Enhancement of Library Table Metadata: According to the entire SQL in the user's editing area, use the SQL parse toolkit and regular expressions to parse the corresponding library table names from the SQL, and request to retrieve the metadata database with the parsed library table names, returning the field information of the corresponding library tables, including the field names, field types, and field value examples in each table.

[0230] Enhancement of SQL Dialect Knowledge: According to the data source information selected by the user in the product interface or the table source information parsed from the user's SQL, judge the SQL dialect written by the user currently. For example, if the impala data source is selected, it corresponds to the impala dialect. Build an SQL dialect knowledge base and use the query to retrieve under the specified dialect to obtain the dialect knowledge related to the user's needs.

[0231] Enhancement of Execution Result Data: When the user writes SQL code, in most cases, they will make multiple adjustments and modifications and submit them for execution to check the data results. In this embodiment, the executed data result table is parsed and converted into natural language text, which is submitted to the model together with the code result before rewriting to enhance the model's understanding.

[0232] This part of the information can enhance the rewriting effect. Taking the user's real demand of "help me calculate how much the current indicator has increased year-on-year compared to a year ago" as an example, starting from the user's thinking of writing SQL: one should first find the time field, so as to be able to locate the value of the indicator a year ago through the subtraction of the time field.

[0233] Whether the subtraction value of the time field is correct depends on (1) the understanding of the time field format (timestamp or string format, with the value accurate to year, month, day, or hour, minute, second) and (2) how to use the time processing function in the current dialect (the parameters, input formats, and return result formats of the time processing functions in each dialect are very different).

[0234] In the absence of this part of the information, the AI assistant usually assumes a non-existent time field and completes incorrect code rewriting based on it. This embodiment improves the SQL rewriting effect through information combination and document retrieval technology.

[0235] S9-2, Information Selection Model:

[0236] In the actual process of writing SQL, not every change requires referring to all possible information. For example, some simple requirements like "change to limit 100" only involve syntax changes and do not require knowing all the field information in the original table. Experiments show that redundant and useless information input will mislead the code generation model and lead to a decrease in the code rewriting accuracy rate.

[0237] Based on this result, referring to the idea that users will judge the required information in advance and then consult the information when rewriting the code, this embodiment designs and adds a classification model to make a dynamic selection for each user request. Input the user's SQL, rewriting requirements, and multiple pieces of information to be judged (obtained in step S9-1). Through the few-shot learning method, a large language model is enabled to learn to judge whether a certain piece of information needs to be referred to, and decide whether the information will be input to the code generation model subsequently based on the output of the model, thus achieving information refinement.

[0238] S9-3, SQL Rewriting Model:

[0239] This module uses a fine-tuned large language model, receives the information selected in step S9-1 and the user's rewriting instructions, outputs the rewritten SQL fragment, and enables the model to output only the code fragment that needs to be changed through training data format preparation and instructions, which can be directly applied to the code editing area without further post-processing.

[0240] This embodiment adopts the Low-Rank Adaptation of Large Language Models (LoRA) technology in the Parameter-Efficient Fine-Tuning (PEFT) method. Only a small number of parameters need to be trained to achieve significant performance improvement. This efficient fine-tuning strategy enables the model to quickly adapt to the needs of specific domains while maintaining a low computational cost.

[0241] During model training, internal user real SQL data is used for training. Especially in the industrial field, the SQL is more difficult, and the constructed training data effectively improves the model's ability to rewrite complex SQL and enhances its domain knowledge.

[0242] Through the embodiments provided in this application, it helps users to quickly rewrite SQL code in natural language, liberating the manpower of data analysts from the cumbersome data processing process. Since it was launched, the accuracy rate has been about 70%. Before the code generation model, a variety of meta-information inputs were designed to be combined with the document retrieval ability and the information refinement classification model, effectively achieving knowledge enhancement and effectively improving the accuracy of the SQL rewriting module. Using the SQL rewriting code large model fine-tuned on domain data solves the problem that ordinary code large models perform poorly in SQL language.

[0243] It can be understood that in the specific implementation of this application, data related to user information, etc. is involved. When the above embodiments of this application are applied to specific products or technologies, user permission or consent needs to be obtained, and the collection, use, and processing of relevant data need to comply with the relevant laws, regulations, and standards of relevant countries and regions.

[0244] It should be noted that for the foregoing method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that this application is not limited by the described action sequence, because according to this application, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0245] According to another aspect of the embodiments of this application, there is also provided a structured query language rewriting device for implementing the above-mentioned structured query language rewriting method. As Figure 7 shown, the device includes:

[0246] An acquisition unit 702, configured to acquire context information associated with a first Structured Query Language (SQL) in response to a rewrite request for the first SQL, where the context information is information directly or indirectly related to the first SQL and is a set of information used to support the rewrite;

[0247] A filtering unit 704, configured to filter the context information based on the rewrite requirements corresponding to the rewrite request and the first SQL to obtain at least one key piece of information, where the key piece of information is information that can provide a reference during the rewrite process of the first SQL;

[0248] A rewrite unit 706, configured to rewrite the first SQL through at least one key piece of information to obtain a second SQL.

[0249] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting a structured query language, and details are not described herein again.

[0250] As an alternative solution, the acquisition unit 702 includes:

[0251] A first acquisition module, configured to acquire source information of the first SQL, where the source information is used to represent the source of the first SQL;

[0252] A second acquisition module, configured to acquire context information based on the source information.

[0253] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting a structured query language, and details are not described herein again.

[0254] As an alternative solution, the first acquisition module includes:

[0255] A first parsing sub-module, configured to parse a corresponding library table name from the first SQL, where the library table name is used to represent the data storage location to be operated on by the first SQL, and the source information includes the library table name;

[0256] The second acquisition module includes:

[0257] A retrieval sub-module, configured to use the library table name as a query condition to retrieve the meta-information database to obtain field information of the library table corresponding to the first SQL, where the context information includes the field information.

[0258] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting a structured query language, and details are not described herein again.

[0259] As an alternative solution, the first acquisition module includes:

[0260] The first acquisition sub-module is used to acquire data source information selected on a Structured Query Language (SQL) interface or parsed from a first SQL, where the SQL interface is the interface that triggers the rewriting request, and the data source information is used to represent the data source of the first SQL, and the source information includes the data source information;

[0261] The second acquisition module includes:

[0262] The second acquisition sub-module is used to acquire the structured query dialect corresponding to the first SQL through the data source information;

[0263] The third acquisition sub-module is used to acquire dialect knowledge related to the structured query dialect, where the context information includes the dialect knowledge.

[0264] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting SQL. Such examples are not elaborated herein.

[0265] As an alternative solution, the acquisition unit 702 includes:

[0266] The third acquisition module is used to acquire the execution result obtained after executing the first SQL;

[0267] The fourth acquisition module is used to acquire context information based on the execution result.

[0268] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting SQL. Such examples are not elaborated herein.

[0269] As an alternative solution, the third acquisition module includes:

[0270] The fourth acquisition sub-module is used to acquire the data result table obtained after multiple adjustments and submissions for execution during the process of writing the first SQL, where the execution result includes the data result table;

[0271] The fourth acquisition module includes:

[0272] The second parsing sub-module is used to parse the data result table and convert it into natural language text, where the context information includes the natural language text.

[0273] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting SQL. Such examples are not elaborated herein.

[0274] As an alternative solution, the screening unit 704 includes:

[0275] A first input module for inputting a rewriting requirement, a first structured query language, and context information into a classification model, where the classification model is a large language model trained using multiple first sample data and used for screening key information;

[0276] A fifth acquisition module for acquiring the classification result output by the classification model, where the classification result includes at least one key information.

[0277] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting structured query language, and details are not elaborated herein.

[0278] As an alternative solution, the device further includes:

[0279] A sixth acquisition module for acquiring multiple first sample data before inputting the rewriting requirement, the first structured query language, and the context information into the classification model, where the first sample data consists of a first structured query language sample, a first rewriting requirement for the first structured query language sample, and multiple context information samples corresponding to the first structured query language sample;

[0280] A guiding module for using a small amount of labeled first sample data as a prompt to guide the initial classification model to learn the classification pattern before inputting the rewriting requirement, the first structured query language, and the context information into the classification model, where the label is used to label the key information in multiple context information samples.

[0281] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting structured query language, and details are not elaborated herein.

[0282] As an alternative solution, the rewriting unit 706 includes:

[0283] A second input module for inputting at least one key information, the rewriting requirement corresponding to the rewriting request, and the first structured query language into a rewriting model, where the rewriting model is a large language model trained using multiple second sample data and used for rewriting the structured query language;

[0284] A seventh acquisition module for acquiring the rewriting result output by the rewriting model, where the rewriting result includes a second structured query language.

[0285] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting structured query language, and details are not elaborated herein.

[0286] As an alternative solution, the device further includes:

[0287] An eighth acquisition module, configured to obtain a plurality of second sample data before inputting at least one key information, a rewriting requirement corresponding to a rewriting request, and a first structured query language into a rewriting model, where the second sample data consists of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample;

[0288] A training module, configured to train an initial rewriting model by using a plurality of second sample data before inputting at least one key information, a rewriting requirement corresponding to a rewriting request, and a first structured query language into the rewriting model, and obtain a training result;

[0289] An adjustment module, configured to, before inputting at least one key information, a rewriting requirement corresponding to a rewriting request, and a first structured query language into the rewriting model, in the case that the training result does not meet the convergence condition, adjust a low-rank matrix deployed in the rewriting model until the obtained training result meets the convergence condition.

[0290] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting a structured query language, and details are not described herein again in this example.

[0291] As an optional solution, the apparatus further includes:

[0292] A display unit, configured to display a first structured query language in a structured query language editing area before obtaining context information associated with the first structured query language in response to a rewriting request for the first structured query language, where the structured query language editing area is set to allow triggering of a corresponding rewriting request for the first structured query language or a part of the language in the first structured query language;

[0293] The apparatus further includes:

[0294] A display unit, configured to perform a line-by-line comparison display on the first structured query language and the second structured query language after rewriting the first structured query language by using at least one key information to obtain a second structured query language.

[0295] For specific embodiments, reference may be made to the examples shown in the above-mentioned method for rewriting a structured query language, and details are not described herein again in this example.

[0296] According to another aspect of the embodiments of the present application, there is further provided an electronic device for implementing the above-mentioned method for rewriting a structured query language. The electronic device may be, but is not limited to, Figure 1 the user device 102 or the server 112 shown in Figure 8As shown, the electronic device includes a memory 802 and a processor 804. A computer program is stored in the memory 802, and the processor 804 is configured to execute the steps in any of the above method embodiments through the computer program.

[0297] In an alternative embodiment, the above electronic device may be at least one network device among multiple network devices of a computer network.

[0298] In an alternative embodiment, the above processor may be configured to execute the following steps through a computer program:

[0299] S10-1, in response to a rewrite request for a first structured query language, obtain context information associated with the first structured query language, where the context information is a set of information directly or indirectly related to the first structured query language and used to support the rewrite;

[0300] S10-2, based on the rewrite requirements corresponding to the rewrite request and the first structured query language, filter the context information to obtain at least one key piece of information, where the key piece of information is information that can provide a reference during the rewrite process of the first structured query language;

[0301] S10-3, rewrite the first structured query language through at least one key piece of information to obtain a second structured query language.

[0302] Optionally, those of ordinary skill in the art can understand that Figure 8 the structure shown is only schematic Figure 8 and does not limit the structure of the above electronic device. For example, the electronic device may further include more or fewer components (such as a network interface, etc.) than those shown Figure 8 here, or have a different configuration from that shown Figure 8 here.

[0303] Among them, the memory 802 can be used to store software programs and modules, such as the program instructions / modules corresponding to the method and apparatus for rewriting structured query language in the embodiments of the present application. The processor 804 executes various functional applications and data processing by running the software programs and modules stored in the memory 802, that is, implements the above-mentioned method for rewriting structured query language. The memory 802 may include a high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memories. In some instances, the memory 802 may further include a memory remotely disposed relative to the processor 804, and these remote memories can be connected to the electronic device through a network. Examples of the above network include but are not limited to the Internet, intranet, local area network, mobile communication network, and combinations thereof. Among them, the memory 802 can specifically but not limitedly be used to store information such as the first structured query language, context information, key information, and the second structured query language. As an example, as Figure 8 shown, the above-mentioned memory 802 may include, but not limited to, the acquisition unit 702, the screening unit 704, and the rewriting unit 706 in the above-mentioned apparatus for rewriting structured query language. In addition, it may further include, but not limited to, other module units in the above-mentioned apparatus for rewriting structured query language, which will not be elaborated in this example.

[0304] Optionally, the above-mentioned transmission device 806 is used to receive or send data via a network. Specific examples of the above-mentioned network may include wired networks and wireless networks. In one instance, the transmission device 806 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices and routers through a network cable, thereby enabling communication with the Internet or local area network. In one instance, the transmission device 806 is a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0305] In addition, the above-mentioned electronic device further includes: a display 808, which is used to display information such as the first structured query language, context information, key information, and the second structured query language; and a connection bus 810, which is used to connect each module component in the above-mentioned electronic device.

[0306] In other embodiments, the above-mentioned user equipment or server may be a node in a distributed system. Among them, the distributed system may be a blockchain system, and the blockchain system may be a distributed system formed by connecting multiple nodes through network communication. Among them, the nodes can form a peer-to-peer network, and any form of computing device, such as electronic devices like servers and user equipment, can become a node in the blockchain system by joining the peer-to-peer network.

[0307] According to one aspect of the present application, a computer program product is provided, which includes computer programs / instructions containing program codes for performing the methods shown in the flowcharts. In such an embodiment, the computer program can be downloaded and installed from a network through the communication part and / or installed from a removable medium. When the computer program is executed by the central processing unit, various functions provided by the embodiments of the present application are performed.

[0308] The serial numbers of the embodiments of the present application above are only for description and do not represent the advantages or disadvantages of the embodiments.

[0309] It should be noted that the computer system of the electronic device is only an example and should not impose any restrictions on the functions and usage scope of the embodiments of the present application.

[0310] The computer system includes a central processing unit (CPU), which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) or the program loaded from the storage part into the random access memory (RAM). In the random access memory, various programs and data required for system operation are also stored. The central processing unit, the read-only memory, and the random access memory are connected to each other through a bus. The input / output interface (Input / Output interface, i.e., I / O interface) is also connected to the bus.

[0311] The following components are connected to the input / output interface: an input part including a keyboard, a mouse, etc.; an output part including a cathode ray tube (CRT), a liquid crystal display (LCD), etc. and a speaker, etc.; a storage part including a hard disk, etc.; and a communication part including a network interface card such as a local area network card, a modem, etc. The communication part performs communication processing via a network such as the Internet. A drive is also connected to the input / output interface as needed. Removable media, such as magnetic disks, optical discs, magneto-optical discs, semiconductor memories, etc., are installed on the drive as needed so that the computer programs read from them can be installed into the storage part as needed.

[0312] In particular, according to an embodiment of the present application, the processes described in each method flowchart can be implemented as computer software programs. For example, an embodiment of the present application includes a computer program product that includes a computer program carried on a computer-readable medium, and the computer program includes program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication part, and / or installed from a removable medium. When the computer program is executed by a central processing unit, various functions defined in the system of the present application are executed.

[0313] According to one aspect of the present application, there is provided a computer-readable storage medium, and a processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the methods provided in the above various alternative implementation manners.

[0314] In an alternative embodiment, the above computer-readable storage medium may be configured to store a computer program for executing the following steps:

[0315] S10-1, in response to a rewrite request for a first structured query language, obtain context information associated with the first structured query language, where the context information is a set of information directly or indirectly related to the first structured query language and used to support the rewrite;

[0316] S10-2, based on the rewrite requirements corresponding to the rewrite request and the first structured query language, filter the context information to obtain at least one key piece of information, where the key piece of information is information that can provide a reference during the rewrite process of the first structured query language;

[0317] S10-3, rewrite the first structured query language through at least one key piece of information to obtain a second structured query language.

[0318] Optionally, in an embodiment of the present application, the term "module" or "unit" refers to a computer program or a part of a computer program with a predetermined function, which works together with other related parts to achieve a predetermined goal, and can be fully or partially implemented by using software, hardware (such as a processing circuit or a memory), or a combination thereof. Similarly, one processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be a part of an overall module or unit that includes the function of that module or unit.

[0319] In an alternative embodiment, those of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by a program instructing the relevant hardware of the electronic device. The program can be stored in a computer-readable storage medium, which can include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disc, etc.

[0320] The serial numbers of the embodiments of the present application above are only for description and do not represent the superiority or inferiority of the embodiments.

[0321] If the integrated unit in the above embodiments is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in the above computer-readable storage medium. Based on such an understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing one or more computer devices (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present application.

[0322] In the above embodiments of the present application, the descriptions of the various embodiments each have their own focuses. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0323] In several embodiments provided by the present application, it should be understood that the disclosed user equipment can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of units is only a logical function division. In actual implementation, there can be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed mutual coupling or direct coupling or communication connection can be through some interfaces. The indirect coupling or communication connection of units or modules can be in an electrical or other form.

[0324] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0325] In addition, in each embodiment of the present application, each functional unit can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of a software functional unit.

[0326] The above are only the preferred embodiments of the present application. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present application, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present application.

Claims

1. A method for rewriting a Structured Query Language, characterized in that Including: In response to a rewrite request for a first Structured Query Language, obtain context information associated with the first Structured Query Language, where the context information is a set of information directly or indirectly related to the first Structured Query Language and used to support the rewrite; Based on the rewrite requirements corresponding to the rewrite request and the first Structured Query Language, filter the context information to obtain at least one key piece of information, where the key piece of information is information that can provide a reference during the rewrite process of the first Structured Query Language; Rewrite the first Structured Query Language through the at least one key piece of information to obtain a second Structured Query Language.

2. The method according to claim 1, characterized in that The obtaining of the context information associated with the first Structured Query Language includes: Obtain source information of the first Structured Query Language, where the source information is used to represent the source of the first Structured Query Language; Based on the source information, obtain the context information.

3. The method according to claim 2, wherein: The obtaining of the source information of the first Structured Query Language includes: Parse a corresponding library table name from the first Structured Query Language, where the library table name is used to represent the data storage location that the first Structured Query Language is to operate on, and the source information includes the library table name; The obtaining of the context information based on the source information includes: Use the library table name as a query condition to retrieve a meta-information database to obtain field information of the library table corresponding to the first Structured Query Language, where the context information includes the field information.

4. The method according to claim 2, wherein: The obtaining of the source information of the first Structured Query Language includes: Obtain data source information selected on a Structured Query Language interface or parsed from the first Structured Query Language, where the Structured Query Language interface is the interface that triggers the rewrite request, the data source information is used to represent the data source of the first Structured Query Language, and the source information includes the data source information; The obtaining of the context information based on the source information includes: Obtain the Structured Query Dialect corresponding to the first Structured Query Language through the data source information; Obtain dialect knowledge related to the Structured Query Dialect, where the context information includes the dialect knowledge.

5. The method according to claim 1, wherein The obtaining of the context information associated with the first Structured Query Language includes: Obtain the execution result obtained after executing the first Structured Query Language; Based on the execution result, obtain the context information.

6. The method according to claim 5, wherein: The obtaining of the execution result obtained after executing the first Structured Query Language includes: Obtain a data result table obtained after multiple adjustments and submissions for execution during the process of writing the first Structured Query Language, where the execution result includes the data result table; The obtaining of the context information based on the execution result includes: Parse the data result table and convert it into natural language text, where the context information includes the natural language text.

7. The method according to claim 1, wherein Based on the rewriting requirements corresponding to the rewriting request and the first structured query language, filter the context information to obtain at least one key information, including: Input the rewriting requirements, the first structured query language, and the context information into a classification model, where the classification model is a large language model trained using multiple first sample data and used to filter the key information; Obtain the classification result output by the classification model, where the classification result includes the at least one key information.

8. The method according to claim 7, wherein Before inputting the rewriting requirements, the first structured query language, and the context information into the classification model, the method further includes: Obtain the multiple first sample data, where the first sample data consists of a first structured query language sample, a first rewriting requirement of the first structured query language sample, and multiple context information samples corresponding to the first structured query language sample; Use a small amount of labeled first sample data as a prompt to guide the initial classification model to learn the classification pattern, where the label is used to label the key information in the multiple context information samples.

9. The method according to claim 1, characterized in that, The rewriting of the first structured query language through the at least one key information to obtain a second structured query language includes: Input the at least one key information, the rewriting requirements corresponding to the rewriting request, and the first structured query language into a rewriting model, where the rewriting model is a large language model trained using multiple second sample data and used to rewrite the structured query language; Obtain the rewriting result output by the rewriting model, where the rewriting result includes the second structured query language.

10. The method according to claim 9, characterized in that, Before inputting the at least one key information, the rewriting requirements corresponding to the rewriting request, and the first structured query language into the rewriting model, the method further includes: Obtain the multiple second sample data, where the second sample data consists of a second structured query language sample and a second rewriting requirement corresponding to the second structured query language sample; Use the multiple second sample data to train the initial rewriting model to obtain a training result; In the case where the training result does not meet the convergence condition, adjust the low-rank matrix deployed in the rewriting model until the obtained training result meets the convergence condition.

11. The method according to any one of claims 1 to 10, characterized in that Before responding to a rewriting request for the first structured query language and obtaining the context information associated with the first structured query language, the method further includes: Display the first structured query language in a structured query language editing area, where the structured query language editing area is set to allow triggering of a corresponding rewriting request for the first structured query language or a part of the first structured query language; After rewriting the first Structured Query Language to obtain a second Structured Query Language by using the at least one key information, the method further includes: Performing a line-by-line comparison display on the first Structured Query Language and the second Structured Query Language.

12. A device for rewriting a structured query language, characterized in that, Including: An obtaining unit, configured to obtain context information associated with the first Structured Query Language in response to a rewriting request for the first Structured Query Language, where the context information is a set of information directly or indirectly related to the first Structured Query Language and used to support rewriting; A filtering unit, configured to filter the context information based on the rewriting requirement corresponding to the rewriting request and the first Structured Query Language to obtain at least one key information, where the key information is information that can provide a reference during the rewriting process of the first Structured Query Language; A rewriting unit, configured to rewrite the first Structured Query Language by using the at least one key information to obtain a second Structured Query Language.

13. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored program, where the program, when run by an electronic device, executes the method described in any one of claims 1 to 11.

14. A computer program product, comprising a computer program / instructions, characterized in that, The computer program / instructions, when executed by a processor, implement the steps of the method described in any one of claims 1 to 11.

15. An electronic device, comprising a memory and a processor, characterized in that, A computer program is stored in the memory, and the processor is configured to execute the method described in any one of claims 1 to 11 through the computer program.

Citation Information

Cited By

  • Structured query statement rewriting method and device based on large language model and medium

    CN121542285A