A method and device for detecting illegal queries

By generating and comparing structured query statements, the difficulties in violation detection in traditional technologies have been solved, achieving efficient violation query identification and improving detection accuracy and recall.

CN123115181APending Publication Date: 2026-11-17ALIBABA (CHINA) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611142504.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-07-29
Publication Date
2026-11-17

AI Technical Summary

Technical Problem

Traditional techniques based on static access control and keyword matching struggle to identify sensitive data queries in dynamic and ever-changing database operations, and lack a deep understanding of the semantics of structured query statements, making it difficult to implement violation detection strategies.

Method used

By acquiring natural language violation indicators, we generate structured query statements and prompt words, and use a large language model to convert them into original and variant structured query statements, constructing a library of violation structured query statements, and identifying violation queries based on semantic similarity comparison.

Benefits of technology

It effectively identifies semantically equivalent but differently written structured query statements, significantly improving the detection recall and accuracy of illegal queries and lowering the barrier to entry for users.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123115181A_ABST
    Figure CN123115181A_ABST
Patent Text Reader

Abstract

This invention discloses a method and apparatus for detecting illegal queries. The method involves: acquiring multiple natural language violation indicators; generating prompt templates based on structured query statements; generating corresponding prompt words for each structured query statement; inputting these prompts into a large language model to obtain original structured query statements; performing equivalent transformations on each original structured query statement to obtain multiple variants; constructing an illegal structured query statement library based on all original structured query statements and their variants; acquiring the structured query statement to be tested; determining the semantic similarity between the statement and each structured query statement in the illegal structured query statement library; and identifying a structured query statement with a semantic similarity higher than a preset similarity threshold as an illegal query. This method significantly improves detection recall and accuracy by comparing the semantic consistency of structured query statements, performing query matching at the semantic level rather than the string level.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of data security and access control technology, and more specifically, to a method and apparatus for detecting unauthorized queries. Background Technology

[0002] In the process of enterprise digital transformation, data has become a core production factor, and its security and compliance management has become a key focus. With the increasing complexity of enterprise systems and the diversification of data access scenarios, identifying query behaviors related to sensitive data has become a crucial link in ensuring data asset security. Traditional technologies based on static access control and keyword matching are gradually showing limitations in dealing with dynamic and ever-changing database operations. On the one hand, access control is extremely difficult; core data tables have thousands of access holders across multiple departments, making it incredibly labor-intensive and difficult to continuously track each holder's actions. On the other hand, Structured Query Language (SQL) statements are highly flexible and expressive, allowing the same query intent to be achieved through various syntactic forms, making it difficult for simple rule matching to cover all potential violation scenarios. Furthermore, applications typically describe data sensitivity in natural language, while the technical side needs to map this to specific table fields and query logic. This semantic gap leads to a disconnect between the formulation and execution of violation detection strategies. Existing technologies largely rely on human experience to build rule bases, lacking a deep understanding of the semantics of structured query statements, making it difficult to adapt to rapidly changing application needs and complex data access patterns. Summary of the Invention

[0003] In view of this, embodiments of the present invention provide a method and apparatus for detecting illegal queries, which significantly improves the detection recall and accuracy by comparing the semantic consistency of structured query statements.

[0004] Firstly, a method for detecting violations of query rules is provided, the method comprising: Obtain multiple natural language violation indicators; Based on each of the aforementioned natural language violation indicators and the preset structured query statements, a prompt template is generated, and a corresponding structured query statement is generated to generate prompt words; Each of the structured query statements is fed into a large language model to generate prompt words, and the corresponding original structured query statement and multiple variant structured query statements are output. Based on the original structured query statements and variant structured query statements corresponding to the various natural language violation indicators, a violation structured query statement library is constructed. Obtain the user's query request, which includes the structured query statement to be tested; Determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library; In response to the existence of structured query statements with semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as an illegal query.

[0005] Secondly, a violation detection device is provided, the device comprising: The first acquisition module is used to acquire multiple natural language violation indicators; The first generation module is used to generate prompt templates based on each of the natural language violation indicators and the preset structured query statements, and to generate corresponding structured query statements and prompt words. The statement output module is used to generate prompt words for each of the structured query statements and input them into the large language model, and output the corresponding original structured query statement and the corresponding multiple variant structured query statements. The first construction module is used to construct a library of illegal structured query statements based on the original structured query statements and variant structured query statements corresponding to the multiple natural language violation indicators respectively. The second acquisition module is used to acquire the user's query request, which includes the structured query statement to be tested. The first determining module is used to determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library; The violation identification module is used to identify a structured query request as a violation in response to the existence of a semantic similarity higher than a preset similarity threshold.

[0006] Thirdly, an electronic device is provided, including a memory and a processor, the memory being used to store one or more computer program instructions, wherein the one or more computer program instructions are executed by the processor to implement the method as described in the first aspect above.

[0007] Fourthly, a computer-readable storage medium is provided, wherein a computer program is stored therein, and when executed by a processor, the computer program implements the method described in the first aspect.

[0008] Fifthly, a computer program product is provided, including a computer program / instructions that, when executed by a processor, implement the method described in the first aspect.

[0009] The technical solution of this invention first obtains multiple illegal query statements in natural language, then generates prompt templates by combining structured query statements, and generates corresponding structured query prompt words. These are then input into a large language model to obtain the corresponding original structured query statements. Each original structured query statement undergoes an equivalent transformation to obtain multiple variant structured query statements. An illegal structured query statement library is constructed based on all original and variant structured query statements. Then, query requests including the structured query statement to be tested are obtained. The semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library is determined. If a structured query statement has a semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as an illegal query. This technical solution automatically converts illegal queries described in natural language into structured query statements and their equivalent variants, achieving automatic mapping from application semantics to technical implementation, significantly reducing the usage threshold. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, effectively identifying semantically equivalent but differently written structured query statements, significantly improving detection recall and accuracy. Attached Figure Description

[0010] The above and other objects, parameters, and advantages of the present invention will become more apparent from the following description of embodiments of the invention with reference to the accompanying drawings, in which: Figure 1 This is a flowchart of the violation query detection method according to an embodiment of the present invention; Figure 2 This is a flowchart of a variant structured query statement generation method according to an embodiment of the present invention; Figure 3 This is a flowchart of the semantic similarity determination method according to an embodiment of the present invention; Figure 4 This is a schematic diagram of the violation query detection system according to an embodiment of the present invention; Figure 5 This is a data flow diagram of the violation query detection method according to an embodiment of the present invention; Figure 6 This is a schematic diagram of the violation query detection device according to an embodiment of the present invention; Figure 7 This is a schematic diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation

[0011] The present application is described below based on embodiments, but it is not limited to these embodiments. In the detailed description of the present application below, certain specific details are described in detail. Those skilled in the art can fully understand the present application without these details. To avoid obscuring the substance of the present application, well-known methods, processes, flows, elements, and circuits are not described in detail.

[0012] Furthermore, those skilled in the art should understand that the accompanying drawings provided herein are for illustrative purposes only and are not necessarily drawn to scale.

[0013] Unless the context explicitly requires it, words such as "including" or "contains" throughout the application should be interpreted as including rather than exclusive or exhaustive; that is, meaning "including but not limited to".

[0014] In the description of this application, it should be understood that the terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance. Furthermore, in the description of this application, unless otherwise stated, "a plurality of" means two or more.

[0015] The solutions described in this specification and embodiments, if involving the processing of personal information, will be processed only on the premise of having a legal basis (such as obtaining the consent of the personal information subject, or being necessary for the performance of a contract), and will only be processed within the scope stipulated or agreed upon. A user's refusal to process personal information beyond what is necessary for basic functions will not affect the user's use of basic functions.

[0016] Figure 1 This is a flowchart of the violation query detection method according to an embodiment of the present invention. Figure 1 As shown, the method includes the following steps: Step S101: Obtain multiple natural language violation indicators.

[0017] Among them, natural language violation indicators refer to the descriptions of violation indicators or sensitive indicators by users in natural language. For example, the total transaction amount of goods during the Double Eleven promotion, the number of orders for a certain product category, or individual order details.

[0018] In one possible implementation, the natural language violation indicator can be a table field or intermediate data that can be determined based on the table field. For example, the table field may include the transaction amount of each order, and the natural language violation indicator may be the transaction amount of a certain order or the total transaction amount obtained by accumulating the transaction amounts of a large number of orders.

[0019] Furthermore, in addition to the indicator itself, natural language violation indicators can also carry the corresponding sensitive operations or associated time windows.

[0020] It's easy to understand that different metrics can correspond to different types of violations. Optional violation types include unauthorized access, sensitive data extraction, high-risk operation intent, bypassing application scenario rules, and fuzzy probing queries. For example, unauthorized access could be "check the CEO's salary," sensitive data extraction could be "export all users' phone numbers," high-risk operation intent could be "delete all order records from last month," bypassing application scenario rules could be "directly change the status of order XX to 'refunded'," and fuzzy probing queries could be "which tables contain password fields," etc.

[0021] Natural language violation indicators also have corresponding time limits (i.e., the aforementioned associated time windows). For example, if the total transaction amount of a certain product was a violation / sensitive indicator from January to March 2026, it will no longer be a violation / sensitive indicator starting from April 2026. In one possible implementation, natural language violation indicators can be proactively compiled by data security experts or internal control managers based on common attack patterns, application sensitivities, and permission boundaries, creating representative violation indicators (and their corresponding operations). This approach offers strong controllability and high targeting, making it suitable for building high-quality initial datasets of natural language violation indicators.

[0022] In one possible implementation, natural language violation indicators can also be extracted from queries that have been rejected by the system, triggered alarms, or identified as violations in subsequent audits, as filtered out from existing violation detection systems. This type of data truly reflects the expression habits of attackers or users who make erroneous operations, and is semantically diverse and highly concealed.

[0023] In one possible implementation, variants can be automatically generated based on existing natural language violation indicators using natural language processing techniques such as synonym replacement, sentence transformation, and adding distractors. For example, "contact information for all customers" could be rewritten as "can we provide a list of contact information for all customers?" This method can quickly expand the diversity of the corpus.

[0024] It is easy to understand that in practical applications, one or more of the above methods can be used to obtain natural language violation indicators. Furthermore, the above methods for obtaining natural language violation indicators are merely examples, and this embodiment does not limit the specific methods of obtaining them.

[0025] In one possible implementation, natural language violation indicators can be further categorized and labeled according to application scenarios, including but not limited to the application domain, the relevant enterprise department, the associated time window, and the statement violation level. The application domain and associated time window facilitate narrowing the scope of the query table, while the statement violation level can be used for subsequent query request violation determination. Enterprise departments may include internal control departments, finance departments, and technology departments, and the natural language violation indicators corresponding to different departments may be different.

[0026] In one possible implementation, a corresponding database (i.e., a query indicator database) can be established based on multiple (classified and labeled) natural language violation indicators to facilitate subsequent use and management. Furthermore, a version management mechanism can be established for the natural language violation indicators to continuously update and iterate the query indicator database. The natural language violation indicators may carry a corresponding version number.

[0027] Step S102: Generate prompt templates based on each of the natural language violation indicators and the preset structured query statements, and generate corresponding structured query statements to generate prompt words.

[0028] The preset structured query generation prompt template includes at least placeholders for natural language violation indicators and output format requirements. By injecting the natural language violation indicators into the corresponding placeholders, the corresponding structured query generation prompts can be generated.

[0029] In one possible implementation, to make the subsequently generated structured query statements more accurate and efficient, the structured query statement generation prompt template also includes data table metadata related to natural language violation indicators. Specifically, the data table metadata related to each of the natural language violation indicators is determined, and corresponding structured query statement generation prompt words are generated based on the structured query statement generation prompt template, each of the natural language violation indicators, and the corresponding data table metadata.

[0030] The metadata of the data tables includes, but is not limited to, information such as table descriptions, field descriptions, indicator definitions, and relationships between tables.

[0031] Optionally, first identify the data table related to the natural language violation index, and then obtain the metadata of the data table (i.e., the metadata of the aforementioned data table).

[0032] Optionally, metadata for all data tables is stored in the corresponding metadata repository database. When retrieving data table metadata, users can directly access the metadata repository database or obtain it through the metadata repository data service. Through deep integration of metadata, existing table descriptions, field descriptions, indicator definitions, permission data, and query logs are fully utilized to form a complete closed loop from definition to detection, reducing system construction costs.

[0033] In one possible implementation, the application scenario to which each of the natural language violation indicators belongs can be determined first. Based on the application scenario, the application domain, enterprise department, and associated time window corresponding to the natural language violation indicator can be determined. Then, based on the application domain, enterprise department, and / or the associated time window, the data table and data table metadata related to the corresponding natural language violation indicator can be determined. The aforementioned method quickly locates the relevant data table by using the application scenario, application domain, enterprise department, and associated time window, which can reduce the query scope and subsequent data processing volume, and also provides a high-quality data foundation for matching subsequent structured query statements.

[0034] Optionally, determining the application scenario corresponding to the natural language violation indicator can be based on its semantic content and contextual intent. This can be achieved by analyzing the data objects that the indicator may involve (e.g., user passwords, salary records, etc.), operation verbs (e.g., delete, export, modify, etc.), the target operation scope (e.g., information of all customers, the general manager, etc.), and implicit permission requests (e.g., modifying data that one's own permissions do not allow). This analysis can be combined with data sensitivity, user role permissions, and typical operation boundaries to determine which specific application system (or enterprise department) the query, if executed, would affect. Examples include the human resources system and the financial system. In short, the determination of the application scenario in this embodiment is essentially based on semantic understanding, combined with a comprehensive judgment that integrates domain knowledge and access control policies.

[0035] In one possible implementation, if the application scenarios are not pre-categorized and labeled, the corresponding application domain and associated time window can be directly determined based on the natural language violation index. The application domain can be determined by at least one of the following: the application scenario, data object, operational intent, and application terminology that the index may involve. For example, in "Export the ID numbers of all users in the last three months," both "user" and "ID number" point to the personal information protection domain, and in "Query the average salary of xx department," "salary" points to the finance domain. The associated time window comes from the event qualifiers in the statement and application scenario and is used to limit the scope of associated events in the data table. For example, "last three months" in the above example is an associated time window.

[0036] In one possible implementation, the structured query statement generation prompt template further includes original structured query statement output requirements and corresponding variant structured query statement output requirements. The original structured query statement output requirements guide the large language model to determine the most direct or most suitable structured query statement corresponding to the natural language violation index, and output this as the original structured query statement. The variant structured query statement output requirements guide the large language model to perform equivalent transformations on the original structured query statement to determine and output its multiple corresponding variant structured query statements.

[0037] Equivalent transformations include at least one of the following: aggregation method transformation, join method transformation, and query condition combination method transformation. They may also include transformations such as field alias replacement, expression rewriting, condition reordering, and subquery expansion / collapse.

[0038] Specifically, equivalent transformations of structured query statements refer to rewriting the statement structure without changing the query results. Common methods include: rewriting subqueries (such as IN, EXISTS) into JOIN (inner join, left join, etc.); using CASE WHEN to merge multiple aggregated conditions to avoid duplicate table scans; converting NOT IN / NOT EXISTS into LEFT JOIN + ISNULL; using window functions to replace correlated subqueries; eliminating duplicate rows using GROUP BY or DISTINCT; and pre-aggregating scalar subqueries before outer joining, etc.

[0039] Optionally, the structured query statement generation prompt template may also include a specified equivalent transformation method.

[0040] Step S103: Input the prompt words generated from each of the structured query statements into the large language model, and output the corresponding original structured query statement and the corresponding multiple variant structured query statements.

[0041] This step is based on the Natural Language to Structured Query Language (NL2SQL) technology of the large language model. It can automatically convert the illegal query indicators described in natural language into executable SQL query statements by leveraging the semantic understanding capabilities of the large language model. That is, the original structured query statement and its corresponding multiple variant structured query statements.

[0042] For example, the natural language violation indicator “total transaction amount during the Double Eleven promotion” will be translated into SELECTSUM(gmv) FROM dwd_table WHERE ds BETWEEN'20261110'AND'20261130' and its various equivalent notations.

[0043] In one possible implementation, the generation of original and variant structured query statements can be achieved in two stages. The first stage generates the original structured query statement, and the second stage performs an equivalent transformation on the original structured query statement to generate multiple corresponding variant structured query statements. Specifically, the prompt words for each of the structured query statements are first input into the large language model, and the corresponding original structured query statements are output. Then, an equivalent transformation is performed on each of the original structured query statements to generate multiple corresponding variant structured query statements.

[0044] Optionally, the first stage is implemented through a large language model, and the second stage is implemented through a rule engine. Specifically, the structured query statement generation prompt template in step S102 above only contains the original structured query statement output requirements. After the structured query statement generation prompt words generated based on the template are input into the large language model, the model outputs the corresponding original structured query statement. Then, the original structured query statement is rewritten based on the pre-set equivalent transformation rules to generate multiple corresponding variant structured query statements.

[0045] Optionally, both stages can be implemented using a large language model. The two stages can use the same large language model or different large language models. Specifically, the structured query statement generation prompt template in step S102 only contains the original structured query statement output requirements. After the structured query statement generation prompt words generated based on this template are input into the large language model, the model outputs the corresponding original structured query statement. Then, prompt words are constructed based on the original structured query statement to guide the large language model to output the corresponding variant structured query statement.

[0046] Furthermore, the structured query statements obtained from the equivalent transformation can be verified to ensure that the variant structured query statements and the original structured query statements are semantically consistent.

[0047] Figure 2 This is a flowchart illustrating a variant of the structured query statement generation method according to an embodiment of the present invention. Figure 2 As shown, the method includes the following steps: Step S201: Generate corresponding statement transformation prompt words based on the original structured query statement and the preset statement transformation prompt template.

[0048] Step S202: Input the statement transformation prompt words into the large language model to guide the large language model to perform equivalent transformation on the original structured query statement to generate multiple structured query statements to be verified with different grammatical structures.

[0049] Step S203: Perform semantic equivalence verification on the structured query statement to be verified using preset equivalence transformation rules.

[0050] In one possible implementation, variant SQL statements can be normalized and rewritten using preset equivalence transformation rules (such as selection pushdown, projection merging, join commutative law, subquery to JOIN, etc. in relational algebra), gradually converting them into the same standard form as the original statements (such as normalized query trees or intermediate representations). If both result in execution plans with consistent syntax or logical equivalence after transformation using the same set of rules (such as equivalence of relational algebra expressions), then their semantics are determined to be consistent.

[0051] In one possible implementation, the equivalent transformation rule can also be a rule based on semantic similarity. Specifically, the semantic similarity between the original structured query statement and its corresponding structured query statement to be verified is determined. If the semantic similarity is higher than a preset variant threshold, it is considered to have passed verification; otherwise, it has failed. The method for determining semantic similarity can be found in step S106, which will not be described in detail here.

[0052] In one possible implementation, the structured query statement to be validated may be further validated to ensure that it is correctly formatted and executable.

[0053] Step S204: The structured query statement to be verified that has passed the verification is identified as a variant structured query statement.

[0054] NL2SQL translation can automatically convert non-compliant query statements described in natural language into standard original structured query statements and their equivalent variants, achieving automatic mapping from scenario semantics to technical implementation and significantly reducing the barrier to entry.

[0055] Step S104: Construct a library of illegal structured query statements based on the original structured query statements and variant structured query statements corresponding to the multiple natural language violation indicators.

[0056] The illegal structured query statement library refers to a database used to store all illegal structured query statements (including original structured query statements and variant structured query statements).

[0057] In one possible implementation, the illegal structured query statement library is associated with a query statement database, and the illegal structured query statement library is updated as the query statement database is updated. That is, the illegal structured query statement library is updated according to the version number.

[0058] The above steps S101-S104 can directly maintain the illegal structured query statement library in natural language without understanding the underlying data model, thus achieving low-cost and sustainable maintenance of detection.

[0059] Step S105: Obtain the user's query request, which includes the structured query statement to be tested.

[0060] In one possible implementation, query requests can be received via an API interface or query service, and their original input can be intercepted and recorded before the request reaches the query database.

[0061] In one possible implementation, the query request can also obtain historical query records within a specified time window and a specified population range through the query log storage service. The historical query records contain at least one query request.

[0062] In one possible implementation, query requests can also be retrieved via a database proxy or middleware to obtain all query requests sent to the query database.

[0063] In one possible implementation, query requests can also be obtained by embedding points in the front end or BI tools, so that when a user submits a query, the corresponding query request can be retrieved.

[0064] Step S106: Determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library.

[0065] Semantic similarity refers to the degree of equivalence or proximity between two structured query statements in terms of logical intent and data access behavior. It aims to determine whether they substantially access the same tables and fields, apply equivalent filtering conditions, or perform the same type of aggregation or join operations, thereby producing the same or highly overlapping result sets.

[0066] Figure 3 This is a flowchart of a semantic similarity determination method according to an embodiment of the present invention. Figure 3 As shown, the method includes the following steps: Step S301: Perform syntax tree parsing on the structured query statement to be tested and each structured query statement in the illegal structured query statement library, and extract the corresponding semantic features.

[0067] The semantic features include at least one of the following: table references, output fields, aggregation methods, query condition combination methods, join methods, grouping fields, and sorting fields. These semantic features are obtained through deep semantic parsing of each structured query statement.

[0068] Furthermore, the multiple semantic features corresponding to each structured prompt statement can be structured to facilitate subsequent comparison.

[0069] Step S302: Standardize the semantic features.

[0070] The standardization process includes alias resolution, expression normalization, and unified sorting of query conditions.

[0071] Step S303: Based on the standardized semantic features, determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library.

[0072] Specifically, semantic similarity scores can be obtained through multi-dimensional comparison and weighted calculation.

[0073] Unlike simple string matching, this comparison can identify semantically equivalent variants of structured queries, including transformations such as field alias replacement, expression rewriting, condition reordering, and subquery expansion / collapse.

[0074] Step S107: In response to the existence of a structured query statement with a semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as an illegal query.

[0075] In one possible implementation, relevant information about the illegal query can also be recorded simultaneously, including user ID, the structured query statement to be tested, query time, the structured query statement to be tested and its matching illegal structured query statement (i.e., the structured query statement in the illegal structured query statement library), etc.

[0076] In one possible implementation, a detection result may also be output. This result includes relevant information and criteria for identifying the query request as an illegal query. The relevant information includes a user identifier and the structured query statement to be tested. The criteria include structured query statements whose semantic similarity to the structured query statement to be tested is higher than a preset similarity. The output can be given to internal control personnel, database administrators, etc.

[0077] In one possible implementation, the statement violation level of the natural language violation index can also be determined based on the application scenario. Then, the request violation level of the corresponding query request can be determined based on the statement violation level of the structured query statement.

[0078] Steps S101-S104 above are used to establish a structured query statement library, and steps S105-S107 are used for query statement detection. It is easy to understand that S101-S104 and S105-S107 are isolated from each other, and their execution cycles can be different. After the initial establishment of the structured query statement library, version updates of the structured query statement library and query statement detection can be performed independently.

[0079] It is easy to understand that the query statement detection in S105-S107 above can be performed at any time or in batches afterward.

[0080] The method of this invention first obtains multiple natural language violation indicators, then generates prompt templates by combining structured query statements, and generates corresponding structured query prompt words. These are then input into a large language model to obtain the corresponding original structured query statements. Each original structured query statement undergoes an equivalent transformation to obtain multiple variant structured query statements. A violation structured query statement library is constructed based on all original and variant structured query statements. Then, query requests including the structured query statement to be tested are obtained. The semantic similarity between the structured query statement to be tested and each structured query statement in the violation structured query statement library is determined. If a structured query statement with a semantic similarity higher than a preset similarity threshold exists, the corresponding query request is identified as a violation query. This method automatically converts violation queries described in natural language into structured query statements and their equivalent variants, achieving automatic mapping from application semantics to technical implementation, significantly reducing the barrier to entry. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, effectively identifying semantically equivalent but differently written structured query statements, significantly improving detection recall and accuracy.

[0081] Figure 4 This is a schematic diagram of a violation query detection system according to an embodiment of the present invention. Figure 4 As shown, the system includes the following units: Database management unit 410 is used to build and manage a library of illegal structured query statements. This library contains original structured query statements and variant structured query statements, which are collectively referred to as illegal structured query statements during the subsequent determination of semantic consistency of the structured query statements.

[0082] The statement translation unit 420 is used to translate natural language violation indicators into corresponding structured query statements.

[0083] The semantic consistency determination unit 430 is used to determine the semantic similarity between the user-input structured query statement to be tested and the illegal structured query statement.

[0084] In one possible implementation, the system further includes a detection result output unit 440 for outputting detection results. The detection results include relevant information and discrimination criteria for query requests identified as illegal queries. The relevant information includes a user identifier and a structured query statement to be tested. The discrimination criteria include structured query statements whose semantic similarity to the structured query statement to be tested is higher than a preset similarity.

[0085] The connection relationship between the units is as follows: the output of the database management unit 410 is used as the input of the statement translation unit 420; the output of the statement translation unit 420 and the structured query statement to be tested obtained from the historical query log or the system plugin are used as the input of the semantic consistency determination unit 430; the output of the semantic consistency determination unit 430 is used as the input of the detection result output unit 440, and finally the detection result is obtained.

[0086] Furthermore, the statement translation unit 420 includes a metadata retrieval subunit 421, a prompt word construction subunit 422, a statement generation and verification subunit 423, and a variant extension subunit 424.

[0087] The metadata retrieval subunit 421 is used to retrieve data table metadata related to natural language violation indicators. A combination of keyword extraction and semantic matching can be used to retrieve data table metadata.

[0088] The prompt word construction subunit 422 is used to generate prompt templates, various natural language violation indicators and corresponding data table metadata based on the structured query statement, generate corresponding structured query statement prompt words, and generate corresponding statement transformation prompt words based on the original structured query statement and the preset statement transformation prompt template.

[0089] The statement generation and validation subunit 423 is used to receive the original structured query statement returned by the large language model and perform syntax and semantic validation on it, such as determining whether the referenced tables and fields exist, whether the aggregation logic is reasonable, and filtering invalid structured query statements.

[0090] The variant extension subunit 424 is used to generate semantically equivalent variant structured query statements from the validated original structured query statements using a rule engine and / or a large language model, thereby expanding the coverage of the library of non-compliant structured query statements.

[0091] The semantic consistency determination unit 430 includes a semantic parsing subunit 431, a semantic standardization subunit 432, and a semantic comparison subunit 433.

[0092] The semantic parsing subunit 431 is used to perform syntax tree parsing on the structured query statement and extract the corresponding semantic features.

[0093] The semantic standardization subunit 432 is used to standardize semantic features. The standardization process includes alias parsing, expression normalization, and query condition sorting.

[0094] The semantic comparison subunit 433 is used to determine the semantic similarity between the structured query statement to be tested and each structured query statement in the library of illegal structured query statements based on the standardized semantic features.

[0095] The functions of each of the above sub-units can be found in the descriptions of the corresponding parts in the above method embodiments, and will not be repeated here.

[0096] The system in this embodiment of the invention automatically converts illegal queries described in natural language into structured query statements and their equivalent variants, realizing automatic mapping from application semantics to technical implementation, significantly reducing the threshold for use. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, which can effectively identify semantically equivalent but differently written structured query statements, significantly improving the detection recall and accuracy.

[0097] Figure 5 This is a data flow diagram of the violation query detection method according to an embodiment of the present invention. Figure 5 As shown, the data flow includes: Step S501: Obtain multiple natural language violation indicators.

[0098] Step S502: Generate a prompt template based on each natural language violation indicator and a preset structured query statement, and generate the corresponding structured query statement to generate prompt words.

[0099] Step S503: Generate prompt words for each structured query statement and input them into the large language model, outputting the corresponding original structured query statement and multiple variant structured query statements.

[0100] Step S504: Construct a library of illegal structured query statements.

[0101] Step S505: Obtain the structured query statement to be tested.

[0102] Step S506: Determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library.

[0103] Step S507: If there is a structured query statement with a semantic similarity higher than the preset similarity threshold, the corresponding query request will be identified as an illegal query.

[0104] Step S508: If there is no structured query statement with semantic similarity higher than the preset similarity threshold, it is determined that there is no violation.

[0105] Step S509: Output the detection results.

[0106] The method of this invention automatically converts illegal queries described in natural language into structured query statements and their equivalent variants, realizing automatic mapping from application semantics to technical implementation, significantly reducing the threshold for use. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, which can effectively identify semantically equivalent but differently written structured query statements, significantly improving the detection recall and accuracy.

[0107] Figure 6 This is a schematic diagram of a violation query detection device according to an embodiment of the present invention. Figure 6 As shown, the device includes the following modules: The first acquisition module 601 is used to acquire multiple natural language violation indicators.

[0108] The first generation module 602 is used to generate prompt templates based on each of the natural language violation indicators and the preset structured query statements, and to generate corresponding structured query statement prompt words.

[0109] The statement output module 603 is used to generate prompt words for each of the structured query statements and input them into the large language model, and output the corresponding original structured query statement and the corresponding multiple variant structured query statements.

[0110] The first construction module 604 is used to construct a library of illegal structured query statements based on the original structured query statements and variant structured query statements corresponding to the multiple natural language violation indicators.

[0111] The second acquisition module 605 is used to acquire the user's query request, which includes the structured query statement to be tested.

[0112] The first determining module 606 is used to determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library.

[0113] The violation determination module 607 is used to determine the corresponding query request as a violation query in response to the existence of a structured query statement with a semantic similarity higher than a preset similarity threshold.

[0114] The device in this embodiment of the invention first acquires multiple natural language violation indicators, then generates prompt templates by combining structured query statements, and generates corresponding structured query prompt words. These are then input into a large language model to obtain the corresponding original structured query statements. Each original structured query statement undergoes an equivalent transformation to obtain multiple variant structured query statements. A violation structured query statement library is constructed based on all original and variant structured query statements. Then, query requests including the structured query statement to be tested are acquired, and the semantic similarity between the structured query statement to be tested and each structured query statement in the violation structured query statement library is determined. If a structured query statement has a semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as a violation query. This device automatically converts violation queries described in natural language into structured query statements and their equivalent variants, achieving automatic mapping from application semantics to technical implementation, significantly reducing the barrier to entry. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, effectively identifying semantically equivalent but differently written structured query statements, significantly improving detection recall and accuracy.

[0115] Figure 7 This is a schematic diagram of an electronic device according to an embodiment of the present invention. In this embodiment, the electronic device 700 includes a server, a terminal, etc. Figure 7 As shown, the electronic device 700 includes at least one processor 701; a memory 702 communicatively connected to at least one processor 701; and a communication component 703, which receives and transmits data under the control of the processor 701; wherein the memory 702 stores instructions executable by at least one processor 701, which are executed by at least one processor 701 to implement the above-mentioned violation query detection method.

[0116] Specifically, the electronic device includes: one or more processors 701 and a memory 702. Figure 7 Taking a processor 701 as an example, the processor 701 and the memory 702 can be connected via a bus or other means. Figure 7 Taking a bus connection as an example, memory 702, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer-executable programs, and modules. Processor 701 executes various functional applications and data processing of the device by running the non-volatile software programs, instructions, and modules stored in memory 702, thereby implementing the aforementioned violation query detection method.

[0117] Memory 702 may include a program storage area and a data storage area, wherein the program storage area may store the operating system and applications required for at least one function; the data storage area may store an option list, etc. Furthermore, memory 702 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some embodiments, memory 702 may optionally include memory remotely located relative to processor 701, and these remote memories may be connected to external devices via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0118] One or more modules are stored in memory 702, and when executed by one or more processors 701, they execute the violation query detection method in any of the above method embodiments.

[0119] The above-mentioned products can perform the methods provided in the embodiments of this application, and have the corresponding functional modules and beneficial effects of performing the methods. For technical details not described in detail in this embodiment, please refer to the methods provided in the embodiments of this application.

[0120] The technical solution of this invention first obtains multiple natural language violation indicators, then generates prompt templates by combining structured query statements, and generates corresponding structured query prompt words. These are then input into a large language model to obtain the corresponding original structured query statements. Each original structured query statement undergoes an equivalent transformation to obtain multiple variant structured query statements. A violation structured query statement library is constructed based on all original and variant structured query statements. Then, query requests including the structured query statement to be tested are obtained. The semantic similarity between the structured query statement to be tested and each structured query statement in the violation structured query statement library is determined. If a structured query statement with a semantic similarity higher than a preset similarity threshold exists, the corresponding query request is identified as a violation query. This technical solution automatically converts violation queries described in natural language into structured query statements and their equivalent variants, achieving automatic mapping from application semantics to technical implementation, significantly reducing the usage threshold. Furthermore, by comparing the semantic consistency of structured query statements, query matching is performed at the semantic level rather than the string level, effectively identifying semantically equivalent but differently written structured query statements, significantly improving detection recall and accuracy.

[0121] Another embodiment of the present invention relates to a computer-readable storage medium storing a computer program that, when executed by a processor, implements some or all of the method embodiments described above.

[0122] Another embodiment of the present invention relates to a computer program product, including a computer program / instructions that, when executed by a processor, implement some or all of the above-described method embodiments.

[0123] That is, those skilled in the art will understand that all or part of the steps in the methods of the above embodiments can be implemented by a program instructing related hardware. This program is stored in a storage medium and includes several instructions to cause a device (which may be a microcontroller, chip, etc.) or processor to execute all or part of the steps of the methods described in the embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk.

[0124] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.

Claims

1. A method for detecting illegal queries, characterized in that, The method includes: Obtain multiple natural language violation indicators; Based on each of the aforementioned natural language violation indicators and the preset structured query statements, a prompt template is generated, and a corresponding structured query statement is generated to generate prompt words; Each of the structured query statements is fed into a large language model to generate prompt words, and the corresponding original structured query statement and multiple variant structured query statements are output. Based on the original structured query statements and variant structured query statements corresponding to the various natural language violation indicators, a violation structured query statement library is constructed. Obtain the user's query request, which includes the structured query statement to be tested; Determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library; In response to the existence of structured query statements with semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as an illegal query.

2. The method according to claim 1, characterized in that, The step of generating prompt templates based on each of the natural language violation indicators and preset structured query statements, and generating corresponding structured query statements to generate prompt words, includes: Determine the metadata of the data tables associated with each of the aforementioned natural language violation indicators; Based on the structured query statement, generate prompt templates, natural language violation indicators, and corresponding data table metadata, and generate corresponding structured query statement prompt words.

3. The method according to claim 2, characterized in that, The method further includes: Determine the application scenario to which each of the aforementioned natural language violation indicators belongs; Based on the application scenario, determine the application domain and associated time window corresponding to the natural language violation indicator; The step of determining the metadata of the data tables associated with each of the natural language violation indicators includes: Based on the application domain and / or the associated time window, determine the data table and data table metadata related to the corresponding natural language violation index.

4. The method according to claim 3, characterized in that, The method further includes: Based on the application scenario, determine the statement violation level of the natural language violation indicator; The response to the existence of a structured query statement with semantic similarity higher than a preset similarity threshold, identifying the corresponding query request as an illegal query, includes: In response to the existence of a structured query statement with a semantic similarity higher than a preset similarity threshold, the corresponding query request is identified as a violation query, and the request violation level of the corresponding query request is determined according to the statement violation level of the structured query statement.

5. The method according to claim 3, characterized in that, The natural language violation indicator carries a corresponding version number; The method further includes: Update the library of non-compliant structured query statements according to the stated version number.

6. The method according to claim 1, characterized in that, The step involves generating prompt words for each structured query statement and inputting them into a large language model, outputting the corresponding original structured query statement and multiple variant structured query statements, including: Each of the structured query statements is used to generate prompt words, which are then input into the large language model to output the corresponding original structured query statement. Each of the original structured query statements is transformed into an equivalent form to generate multiple variant structured query statements.

7. The method according to claim 6, characterized in that, The step of performing equivalent transformations on each of the original structured query statements to generate multiple corresponding variant structured query statements includes: Based on the original structured query statement and the preset statement transformation prompt template, generate corresponding statement transformation prompt words; The statement transformation prompts are input into the large language model to guide the large language model to perform equivalent transformations on the original structured query statement to generate multiple structured query statements to be verified with different syntactic structures. The equivalent transformations include at least one of aggregation transformation, connection transformation, and query condition combination transformation. The semantic equivalence of the structured query statement to be verified is performed using preset equivalence transformation rules. The validated structured query statements are identified as variant structured query statements.

8. The method according to claim 1, characterized in that, The step of determining the semantic similarity between the structured query statement to be tested and each structured query statement in the database of illegal structured query statements includes: The syntax tree is parsed for the structured query statement to be tested and each structured query statement in the illegal structured query statement library, and the corresponding semantic features are extracted. The semantic features include at least one of the following: table reference, output field, aggregation method, query condition combination method, join method, grouping field and sorting field. The semantic features are standardized, including alias parsing, expression normalization, and query condition sorting. Based on the standardized semantic features, the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library is determined.

9. The method according to claim 1, characterized in that, The method further includes: Output detection results, which include relevant information and criteria for identifying query requests as illegal queries. The relevant information includes user identifiers and the structured query statement to be tested. The criteria for identification include structured query statements whose semantic similarity to the structured query statement to be tested is higher than a preset similarity.

10. A violation detection device, characterized in that, The device includes: The first acquisition module is used to acquire multiple natural language violation indicators; The first generation module is used to generate prompt templates based on each of the natural language violation indicators and the preset structured query statements, and to generate corresponding structured query statements and prompt words. The statement output module is used to generate prompt words for each of the structured query statements and input them into the large language model, and output the corresponding original structured query statement and the corresponding multiple variant structured query statements. The first construction module is used to construct a library of illegal structured query statements based on the original structured query statements and variant structured query statements corresponding to the multiple natural language violation indicators respectively. The second acquisition module is used to acquire the user's query request, which includes the structured query statement to be tested. The first determining module is used to determine the semantic similarity between the structured query statement to be tested and each structured query statement in the illegal structured query statement library; The violation identification module is used to identify a structured query request as a violation in response to the existence of a semantic similarity higher than a preset similarity threshold.

11. An electronic device comprising a memory and a processor, characterized in that, The memory is used to store one or more computer program instructions, wherein the one or more computer program instructions are executed by the processor to implement the method as described in any one of claims 1-9.

12. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method as described in any one of claims 1-9.

13. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instructions are executed by the processor, they implement the method as described in any one of claims 1-9.