Method and system for syntax tree constraint decoding optimization of NL2SQL in ChatDB

By optimizing the syntax tree constraint decoding method of NL2SQL in ChatDB, and utilizing a fuzzy column name mapper and dynamic pruning strategy, the contradiction between high recall and precision when converting natural language to SQL is resolved, achieving efficient and accurate SQL generation in complex query scenarios.

CN122019580AActive Publication Date: 2026-05-12ZHEJIANG FULIN TECH CO LTD
View PDF 11 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ZHEJIANG FULIN TECH CO LTD
Filing Date
2026-04-13
Publication Date
2026-05-12

AI Technical Summary

Technical Problem

Existing technologies introduce a large amount of semantic noise when converting natural language into structured query language (SQL) in order to ensure high recall. This leads to a significant decrease in the accuracy of SQL generation in complex query scenarios. In particular, when dealing with queries containing complex logic such as nesting, the number of candidate field combinations explodes, resulting in the generation of illusory SQL with logical errors.

Method used

We adopt the syntax tree constraint decoding optimization method of NL2SQL in ChatDB, generate an initial candidate field set through a fuzzy column name mapper, and combine semantic confidence score and dynamic pruning threshold with a real-time pruning strategy to ensure high recall while improving generation accuracy.

Benefits of technology

It effectively suppresses the risk of combinatorial explosion in complex query scenarios, significantly improves the accuracy and stability of SQL generation, ensures the continuation of the decoding process under any circumstances, and provides reliable data analysis results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122019580A_ABST
    Figure CN122019580A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of natural language processing and databases, in particular to a syntax tree constraint decoding optimization method and system for NL2SQL in ChatDB. The method comprises the steps that S1, natural language query is received and matched with database metadata, and an initial candidate field set is generated; s2, starting a large language model, adopting a preset SQL syntax tree as a decoding constraint, and calculating a semantic confidence score for each candidate field in the initial candidate field set when a syntax tree node of a to-be-filled field is decoded; s3, calculating a dynamic pruning threshold value; s4, generating a refined candidate field subset; s5, the refined candidate field subset is provided for a large language model, and filling and decoding of a current node are completed under the constraint of an SQL syntax tree until a complete SQL query statement is generated; according to the invention, the inherent contradiction between high recall rate and high precision in the prior art is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of natural language processing and database technology, specifically to a method and system for optimizing syntax tree constraint decoding in ChatDB's NL2SQL. Background Technology

[0002] With the increasing application of natural language processing technology in the field of database querying, how to accurately convert users' natural language into structured query language SQL has become a key technology.

[0003] In existing conversion techniques, systems typically match and introduce a large number of potentially relevant database fields to ensure high recall rates for field intent recognition. While this strategy ensures that potentially correct fields are not missed, it also introduces a significant amount of semantic noise. The impact of this problem is limited when processing simple queries; however, when dealing with queries containing complex logic such as nesting, the number of candidate field combinations explodes, leading to a significant decrease in the accuracy of generated SQL, and even producing illusory SQL with logical errors.

[0004] Therefore, how to resolve the contradiction between introducing semantic noise to ensure high recall and the resulting decrease in SQL generation accuracy in complex query scenarios due to combinatorial explosion effect has become a technical problem that urgently needs to be solved in this field. Summary of the Invention

[0005] To address the aforementioned technical problems, this invention discloses a method and system for optimizing syntax tree constraint decoding in ChatDB's NL2SQL. Specifically, the technical solution of this invention is as follows:

[0006] The optimization method for decoding syntax tree constraints in ChatDB's NL2SQL includes the following steps:

[0007] S1. Receive natural language queries and match them with database metadata to generate an initial candidate field set;

[0008] S2. Start the large language model and use the preset SQL syntax tree as the decoding constraint. When decoding to the syntax tree node of the field to be filled, calculate the semantic confidence score for each candidate field in the initial candidate field set.

[0009] S3. Calculate the dynamic pruning threshold based on the query nesting depth during the decoding process;

[0010] S4. Based on semantic confidence scores and dynamic pruning thresholds, the initial candidate field set is filtered to generate a refined candidate field subset;

[0011] S5. Provide a refined subset of candidate fields to the large language model, fill in the current node under the constraints of the SQL syntax tree, and continue decoding until a complete SQL query statement is generated.

[0012] Preferably, S1 specifically includes:

[0013] S11. Using a preset fuzzy column name mapper, vector similarity matching is performed between entity references in natural language queries and metadata of all tables in the database.

[0014] S12. With the goal of high recall, include all potentially relevant database fields into the initial candidate field set.

[0015] Preferably, S2 specifically includes:

[0016] S21. Combine the initial semantic similarity of each candidate field in the initial candidate field set with the quantified value of the dynamic logical fit of the candidate field in the current SQL structure.

[0017] S22. Use the combined result as the semantic confidence score of the candidate field in the current decoding context.

[0018] Preferably, the calculation of the semantic confidence score specifically includes:

[0019] S221. The initial semantic similarity of candidate fields is derived from the vector cosine similarity calculated by the fuzzy column name mapper.

[0020] S222. The quantified value of dynamic logical fit is determined by the context fit weight output by the pre-trained classification model based on the probabilistic pattern of fields of different data types appearing in specific SQL clauses.

[0021] Preferably, S3 specifically includes:

[0022] S31. Real-time analysis of the decoding status of large language models to obtain the nesting depth of the currently generated SQL statement;

[0023] S32. A bounded linearly growing threshold model is adopted, and the query complexity can be quantified by indicators such as nesting depth as input to calculate the dynamic pruning threshold, so that the strictness of the pruning strategy increases linearly with the query complexity.

[0024] Preferably, S4 specifically includes:

[0025] S41. Traverse the initial candidate field set and retain all candidate fields whose semantic confidence scores are greater than or equal to the dynamic pruning threshold to generate a refined candidate field subset.

[0026] S42. After performing the filtering, if the refined candidate field subset is empty, then abandon the threshold filtering and directly select the single candidate field with the highest semantic confidence score from the initial candidate field set as the only candidate.

[0027] Preferably, S5 specifically includes:

[0028] S51. Provide a refined subset of candidate fields as a limited candidate range to the large language model decoder.

[0029] S52. Within the limited scope, the decoder combines the constraints of the SQL syntax tree to complete the filling of the current node and continues the decoding process of subsequent nodes.

[0030] ChatDB's NL2SQL syntax tree constraint decoding optimization system includes:

[0031] A natural language input interface is used to receive natural language queries from users.

[0032] The candidate field generation module is used to generate an initial set of candidate fields based on natural language queries and database metadata;

[0033] The syntax tree constraint decoding engine is used to embed large language models and is responsible for the structured generation of SQL based on a preset SQL syntax tree.

[0034] The pruning parameter calculation module is used to calculate the semantic confidence score and dynamic pruning threshold when the decoding engine decodes to the syntax tree node of the field to be filled.

[0035] The dynamic semantic pruning module receives the semantic confidence score and the dynamic pruning threshold, performs filtering on the initial candidate field set to generate a refined candidate field subset, and returns the refined candidate field subset to the decoding engine.

[0036] The SQL output module is used to output the complete SQL query statement generated by the decoding engine.

[0037] Compared with the prior art, the present invention has the following beneficial effects:

[0038] 1. This method employs a fuzzy column name mapper to generate an initial candidate field set with high recall through vector similarity matching. This approach can effectively address the diversity and ambiguity of user input, ensuring that correct fields are not excluded in the initial stage due to strict matching, thus providing a complete and reliable foundation for subsequent accurate filtering.

[0039] 2. This method establishes a more accurate and multi-dimensional evaluation mechanism. The semantic confidence score it calculates innovatively combines the initial semantic similarity, which reflects static text association, with the contextual appropriateness weight, which reflects dynamic logical adaptation. This contextual appropriateness weight is output by the pre-trained classification model based on the occurrence pattern of data types in specific SQL clauses. This combination allows the evaluation to go beyond surface text matching and delve into the SQL syntax logic level, effectively identifying noisy fields that are text-related but logically inconsistent, providing a high-quality decision basis for pruning.

[0040] 3. This method establishes an intelligent pruning strategy that can adaptively adjust the filtering intensity according to query complexity. It obtains the nesting depth of SQL by parsing and decoding the status in real time, and calculates the dynamic pruning threshold based on a bounded linearly increasing threshold model. When the query complexity increases, the filtering threshold increases linearly, and the pruning strategy automatically tightens, thereby effectively suppressing the risk of combinatorial explosion in complex query scenarios. When dealing with simple queries, a relatively lenient baseline threshold is used to ensure the flexibility of the solution.

[0041] 4. The method's screening process balances accuracy and robustness. Under normal conditions, it effectively filters out low-confidence fields based on the comparison of semantic confidence scores and dynamic pruning thresholds. At the same time, it also designs an exception handling mechanism: if the candidate subset is empty after screening, the system will not interrupt decoding, but will select the single field with the highest confidence in the initial set as the candidate. This design ensures that the decoding process can continue under any circumstances, greatly enhancing the system's fault tolerance and stability. Attached Figure Description

[0042] The present invention will be further explained below with reference to the accompanying drawings and embodiments:

[0043] Figure 1 This is a flowchart of the method of the present invention.

[0044] Figure 2 This is a flowchart of the system of the present invention. Detailed Implementation

[0045] To make the objectives, technical solutions, and advantages of the present invention clearer, the technical solutions of the present invention will be described in detail below. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0046] Example 1:

[0047] like Figure 1As shown, the optimization method for decoding syntax tree constraints in ChatDB's NL2SQL includes the following steps:

[0048] S1. Receive natural language queries and match them with database metadata to generate an initial candidate field set;

[0049] S2. Start the large language model and use the preset SQL syntax tree as the decoding constraint. When decoding to the syntax tree node of the field to be filled, calculate the semantic confidence score for each candidate field in the initial candidate field set.

[0050] S3. Calculate the dynamic pruning threshold based on the query nesting depth during the decoding process;

[0051] S4. Based on semantic confidence scores and dynamic pruning thresholds, the initial candidate field set is filtered to generate a refined candidate field subset;

[0052] S5. Provide a refined subset of candidate fields to the large language model, fill in the current node under the constraints of the SQL syntax tree, and continue decoding until a complete SQL query statement is generated.

[0053] This invention provides an optimization method for syntax tree constraint decoding of NL2SQL in ChatDB. Its core objective is to address the technical challenge in existing technologies where a large amount of semantic noise is introduced to ensure high recall of field intent recognition, leading to a significant decrease in SQL generation accuracy due to combinatorial explosion when processing complex queries. To solve this problem, this embodiment introduces a dynamic semantic pruning mechanism during the decoding process of a large language model, achieving a significant improvement in the accuracy of complex SQL generation while maintaining high recall.

[0054] The complete technical process of this method includes the following steps:

[0055] Step S1: Receive the natural language query and match it with the database metadata to generate an initial candidate field set. The purpose of this step is to initially filter out all database fields that may be related to the user's natural language query, providing a comprehensive foundation for subsequent precision processing. In this embodiment, the system receives a question input by the user through the business intelligence analysis platform, such as querying the names and sales of the three products with the highest sales in the last quarter, and accesses the metadata of the target database. The metadata includes information such as the table names, field names, and field comments of all tables. To ensure the robustness of the process, if the initial candidate field set generated in this step is empty, the system will stop the SQL generation and may return a prompt message to the user, suggesting that the user adjust or clarify the query intent to avoid errors in subsequent steps due to the lack of candidate objects.

[0056] Step S2: Start the large language model and use a preset SQL syntax tree as the decoding constraint. This syntax tree is defined based on the SQL dialect supported by the target database, ensuring the universality and executability of the generated statements. When decoding reaches the syntax tree node of the field to be filled, calculate the semantic confidence score for each candidate field in the initial candidate field set. The purpose of this step is to utilize the powerful natural language understanding and code generation capabilities of the large language model and strictly constrain its generation process within the legal SQL syntax structure. When the decoding process progresses to the node where a specific field needs to be filled, decoding is paused, and a key quantitative evaluation process is started, that is, calculating the semantic confidence score of each initial candidate field in the current context.

[0057] Step S3: Calculate the dynamic pruning threshold based on the query nesting depth during the decoding process. The purpose of this step is to establish an adaptive pruning threshold that is positively correlated with query complexity. The more complex the query logic, the higher the risk of combinatorial explosion, so a stricter screening criterion is needed. This step will analyze the current SQL generation status in real time, quantify its complexity, and calculate a dynamic pruning threshold accordingly.

[0058] Step S4: Based on the semantic confidence score and the dynamic pruning threshold, the initial candidate field set is filtered to generate a refined candidate field subset. This step is the core execution link of the pruning mechanism. Its purpose is to filter out semantic noise fields that are relevant in text but are inappropriate in the current business logic and SQL structure. It compares the confidence score calculated in step S2 with the dynamic threshold calculated in step S3 and only retains candidate fields that are higher than or equal to the threshold.

[0059] Step S5: Provide the refined subset of candidate fields to the large language model, complete the filling of the current node under the constraints of the SQL syntax tree, and continue decoding until a complete SQL query statement is generated. The purpose of this step is to feed back the high-quality filtered subset of fields to the LLM, narrowing its selection range from a large and noisy set to a small and refined set. This greatly reduces the probability of the LLM selecting the wrong fields, guiding it to complete the subsequent decoding on the correct path and generate a syntactically correct and logically accurate SQL query statement.

[0060] This embodiment constructs a real-time, dynamic semantic pruning closed loop in the LLM decoding process by organically combining the above steps. It ensures high recall through broad matching, without missing any potentially correct fields. At key decoding nodes, it precisely prunes candidate fields by comprehensively considering semantic and logical confidence scores and dynamic thresholds that match query complexity, effectively suppressing combinatorial explosion. Decoding is completed within a high-quality candidate range. This method resolves the inherent contradiction between high recall and high precision. Especially when dealing with complex nested queries, it can significantly improve the logical accuracy of the final generated SQL, avoid the generation of phantom SQL, and thus provide users with reliable data analysis results.

[0061] Example 2:

[0062] S1 specifically includes: S11, using a preset fuzzy column name mapper to perform vector similarity matching between entity references in natural language queries and metadata of all tables in the database;

[0063] S12. With a high recall rate as the goal, include all potentially relevant database fields into the initial candidate field set. This embodiment is a preferred implementation of step S1 in embodiment 1, and its purpose is to generate the initial candidate field set in an efficient and comprehensive manner.

[0064] Step S11: Using a pre-defined fuzzy column name mapper, vector similarity matching is performed between entity references in the natural language query and metadata of all tables in the database. The fuzzy column name mapper is a pre-configured software module whose function is to quantify the correlation between natural language expressions and database metadata through vectorization and similarity calculation. In this embodiment, the mapper embeds a pre-trained word vector model. It encodes the user's natural language query and metadata of all fields in the database into high-dimensional vectors. It calculates the cosine similarity between the vector of entity references in the query and each metadata vector. The word vector model can be pre-trained based on a large-scale general text corpus, such as Wikipedia, and can be selectively fine-tuned on professional documents related to the use scenario, such as a business terminology table, to enhance its domain semantic expression capabilities.

[0065] Step S12: With high recall as the goal, all potentially relevant database fields are included in the initial candidate field set. High recall refers to a strategy objective that aims to maximize the identification of all relevant correct fields, even if it means introducing some irrelevant fields. To achieve this objective, this embodiment sets a relatively lenient vector cosine similarity threshold. The specific value of this threshold, for example, 0.6, is determined based on experimental optimization on a standard evaluation dataset, aiming to achieve a balance between recall and the size of the initial candidate set. All database fields with calculated similarity scores greater than or equal to this threshold will be included in the initial candidate field set as input for subsequent processing.

[0066] By employing a fuzzy column name mapper based on vector similarity matching and aiming for high recall, this invention can effectively address the non-standardization, diversity, and ambiguity of user input. Even if the words used by the user are not entirely consistent with the descriptions in the database metadata, the mechanism can still capture their semantic relevance. This design ensures that in the initial stage of the process, correct candidate fields will not be accidentally excluded due to strict rule matching, providing a complete and reliable foundation for subsequent dynamic pruning and precise filtering, thereby improving the applicability and robustness of the entire system.

[0067] Example 3:

[0068] S2 specifically includes: S21, combining the initial semantic similarity of each candidate field in the initial candidate field set with the quantified value of the dynamic logical fit of the candidate field in the current SQL structure;

[0069] S22. Use the combined result as the semantic confidence score of the candidate field in the current decoding context.

[0070] This embodiment is a preferred implementation of step S2 in embodiment 1 regarding the calculation of semantic confidence score, aiming to provide a more accurate and dynamic candidate field evaluation index than single text similarity;

[0071] The calculation of semantic confidence score specifically includes: S221, the initial semantic similarity of candidate fields, which is derived from the vector cosine similarity calculated by the fuzzy column name mapper;

[0072] S222. The quantified value of dynamic logical fit is determined by the context fit weight output by the pre-trained classification model based on the probabilistic pattern of fields of different data types appearing in specific SQL clauses.

[0073] Step S21: Combine the initial semantic similarity of each candidate field in the initial candidate field set with the quantified value of the dynamic logical fit of the candidate field in the current SQL structure; the core idea of ​​this step is that whether a candidate field is good depends not only on its surface relevance to the query text, but also on whether it conforms to the syntax logic of the SQL statement being constructed.

[0074] Step S22: The combined result is used as the semantic confidence score of the candidate field in the current decoding context; to achieve the above objective, this embodiment introduces a semantic confidence score. The computational model is defined mathematically as follows:

[0075] Where: i: the index of the current SQL syntax tree node;

[0076] j: Index of the candidate field in the initial candidate field set;

[0077] : Semantic confidence score, which is a dimensionless floating-point number with a value range of [0,1]; it is calculated in this step and used for subsequent pruning decisions, representing the overall confidence of candidate field j in the context of the current SQL syntax tree node i;

[0078] The initial semantic similarity is a dimensionless floating-point number with a value range of [0,1]. It originates from step S221, specifically from the candidate fields calculated by the fuzzy column name mapper in the preceding steps. Vector cosine similarity between a field and a natural language query; it reflects the strength of the static semantic association between the field and the query text.

[0079] Context suitability weights are dimensionless floating-point numbers with values ​​ranging from [0,1]. They originate from the definition in step S222, i.e., from a pre-trained classification model based on candidate fields. Data type and current syntax tree node The type output is a context-appropriate weight; this classification model learns from massive SQL query logs to grasp the probabilistic patterns of fields of different data types appearing in specific SQL clauses; it represents the logical fit of a field in the current dynamic SQL structure; as a non-limiting concrete example, this classification model could be a small multilayer perceptron or gradient boosting tree model; its input features could include candidate fields. Data type and current syntax tree node The type here specifically refers to the SQL clause context where the field is located. For example, it can be a query column in the SELECT clause, a condition column in the WHERE clause, a grouping column in the GROUP BY clause, or a parameter column for aggregate functions such as AVG / SUM. The output of the model is a probability value between [0,1], which is the context appropriateness weight. The model is obtained through supervised learning on a dataset containing a large number of pairs of <SQL query, database Schema>. The training data is parsed to count the frequencies of fields of different data types in different SQL clause nodes, thus learning this probabilistic rule.

[0080] : Weight hyperparameters, which are two dimensionless floating-point numbers. Their values are determined offline through grid search optimization on the standard NL2SQL evaluation dataset before model deployment. The goal of this optimization is to maximize a predefined evaluation metric, usually the SQL query execution accuracy on the entire evaluation set. Their role is to balance the importance of static initial semantic similarity and dynamic context appropriateness weight in the final score calculation. These two parameters, as well as most of the key hyperparameters defined in Example 4 later, such as the complexity sensitivity coefficient and the baseline threshold , their final values are all determined offline through unified grid search optimization on the standard NL2SQL evaluation dataset before model deployment. And some weight coefficients used to quantify query complexity, such as , can be preset based on experience or experimental data.

[0081] The model does not rely solely on text similarity , but innovatively introduces context appropriateness weight ; this enables the scoring mechanism to go beyond surface text matching and delve into the logical level of SQL syntax. For example, when the user queries to count the number of people by department, the field department name and employee ID may both have a certain text association with the number of people. But when decoding the nodes inside the COUNT aggregate function, although the text association of the department name may be higher, the pre-trained classification model knows that the COUNT function usually acts on ID-type fields and will give a higher context appropriateness weight to the employee ID ; therefore, the final semantic confidence score calculated by this formula will be more inclined to the employee ID, thus guiding the LLM to make a logically correct choice;

[0082] By quantifying and combining the initial semantic similarity representing static text association with the contextual appropriateness weight representing dynamic logical adaptation, the semantic confidence score calculated by this invention can more comprehensively and accurately evaluate the true applicability of candidate fields in the current decoding context. This multi-dimensional evaluation mechanism can effectively filter out semantic noise that seems relevant but is actually inconsistent, providing a high-quality and highly discriminative decision basis for subsequent pruning, thereby significantly improving the accuracy of screening and the logical correctness of the final SQL.

[0083] Example 4:

[0084] S3 specifically includes:

[0085] S31. Real-time analysis of the decoding status of large language models to obtain the nesting depth of the currently generated SQL statement;

[0086] S32. A bounded linearly growing threshold model is adopted, with query complexity as input, to calculate the dynamic pruning threshold, so that the strictness of the pruning strategy increases linearly with query complexity. This embodiment is a preferred implementation of step S3 in embodiment 1 regarding the calculation of the dynamic pruning threshold, and its purpose is to establish an intelligent mechanism that can adaptively adjust the strictness of filtering according to query complexity;

[0087] Step S31: Real-time analysis of the decoding state of the large language model to obtain the query complexity of the currently generated SQL statement; a key indicator of query complexity is the query nesting depth. This refers to the maximum number of subqueries in the currently generated SQL statement, and is one of the key indicators for measuring the complexity of the query structure. In a preferred embodiment, to more accurately evaluate complexity, we define a comprehensive complexity index. It includes not only nesting depth It also takes into account the number of connections (JOINs). and number of aggregate functions The purpose of real-time analysis of the decoding state of large language models is to dynamically capture this complexity metric.

[0088] In this embodiment, the syntax tree constraint decoding engine maintains a syntax tree traversal stack during SQL generation. When the decoder enters a subquery, the stack depth is incremented by one, and decremented by one upon exiting. The current stack depth is defined in real time as the nesting depth. A simple query without subqueries, its The value is 1; for queries containing one level of subquery, when decoding the subquery part, its It is 2;

[0089] Step S32: Using a bounded linearly growing threshold model, the aforementioned query complexity index is used as input to calculate the dynamic pruning threshold. The bounded linearly growing threshold model refers to a specially designed mathematical model that aims to make the threshold grow linearly with complexity while ensuring that its value does not exceed the valid range. In this preferred embodiment, this model is introduced to calculate the dynamic pruning threshold. The specific calculation method is as follows:

[0090]

[0091] in: The overall complexity metric is a dimensionless floating-point number used to comprehensively quantify the complexity of the current query.

[0092] These represent the nesting depth, number of JOINs, and number of aggregate functions parsed in real time, respectively.

[0093] : These are the weighting coefficients for the three indicators mentioned above. Their values ​​are set based on experience or experimental data and are used to balance the impact of different sources of complexity on the threshold.

[0094] The dynamic pruning threshold, whose calculation method is input as a comprehensive complexity index in this preferred scheme, is used for its calculation. Subtraction terms in the formula This ensures that when the query is the simplest non-nested, non-JOIN, non-aggregate statement, then... and Its threshold is exactly the baseline threshold. ;

[0095] The baseline threshold is a dimensionless floating-point number; it is derived from a basic threshold determined through extensive experimental optimization on the evaluation dataset, for example, 0.75, and it is applicable to... Non-nested simple queries;

[0096] : Nesting depth, which is a positive integer; it is obtained by real-time parsing of the decoding state in step S31;

[0097] The adaptive control mechanism of this model lies in the fact that when processing simple queries, the formula result is: The system employs a standard, relatively lenient pruning strategy; as query complexity increases, the dynamic pruning threshold is adjusted. It will vary depending on the overall complexity index. Linear growth means that the system has higher confidence requirements for candidate fields and more severe pruning, in order to proactively combat the risk of combinatorial explosion caused by the sharp increase in query complexity. The function is a key robust design feature, ensuring that even in extremely deep nested queries, the threshold does not grow indefinitely, but rather remains consistent with the semantic confidence score. The range of values ​​[0,1] maintains dimensional consistency, ensuring the mathematical completeness and logical validity of the model;

[0098] By introducing a dynamic pruning threshold that grows linearly with the depth of query nesting, this invention implements a highly intelligent adaptive pruning strategy. This strategy enables the system to maintain flexibility and avoid excessive intervention when processing simple queries, while automatically tightening the filtering criteria and providing strong guidance when dealing with complex queries. This time-varying and situation-dependent mechanism allows the system to achieve near-optimal noise suppression in query scenarios of various complexities, thereby significantly improving its accuracy and reliability in handling complex and ever-changing business analysis problems in the real world.

[0099] Example 5:

[0100] S4 specifically includes:

[0101] S41. Traverse the initial candidate field set and retain all candidate fields whose semantic confidence scores are greater than or equal to the dynamic pruning threshold to generate a refined candidate field subset.

[0102] S42. After performing the filtering, if the refined candidate field subset is empty, then abandon the threshold filtering and directly select the single candidate field with the highest semantic confidence score from the initial candidate field set as the only candidate.

[0103] This embodiment is a preferred implementation of the screening process in step S4 of embodiment 1. Its purpose is to efficiently perform pruning operations and ensure that the decoding process can continue under any circumstances, thereby ensuring the robustness of the system.

[0104] Step S41: Traverse the initial candidate field set, retaining all candidate fields with semantic confidence scores greater than or equal to the dynamic pruning threshold to generate a refined candidate field subset; based on the semantic confidence scores calculated in the previous steps... and dynamic pruning threshold This embodiment executes the core filtering logic; specifically, the system will traverse each candidate field in the initial candidate field set. and its corresponding With the current decoding context Compare; all conditions are met Candidate fields are considered high-quality candidates and are retained, forming a refined subset of candidate fields;

[0105] Step S42: After performing the filtering, if the refined candidate field subset is empty, the threshold filtering is abandoned, and the single candidate field with the highest semantic confidence score is directly selected from the initial candidate field set as the only candidate. This is a key exception handling and robustness guarantee mechanism. Its purpose is to prevent the refined candidate field subset from being empty due to an overly strict pruning strategy, which would cause the decoding process to be interrupted due to the lack of selectable fields. In this embodiment, the system will check the size of the filtered subset. If the subset is empty, the system will abandon the threshold filtering operation. As an alternative, the system will directly search for and select the single candidate field with the highest semantic confidence score from the initial candidate field set before the current filtering, and use that field as the only member to form the refined candidate field subset.

[0106] This implementation design balances accuracy and robustness. On the one hand, the conventional threshold filtering process effectively filters out a large number of low-confidence noise fields, which is the main means of improving the accuracy of SQL generation. On the other hand, the added exception handling mechanism acts as a safety net, ensuring that the decoding process will never be stuck, even in the most extreme cases. It guarantees that the system can always provide the LLM with at least one of the most likely options to continue generation, thereby avoiding decoding failure. This design greatly enhances the fault tolerance and stability of the entire method under various complex data and query scenarios.

[0107] Example 6:

[0108] S5 specifically includes:

[0109] S51. Provide a refined subset of candidate fields as a limited candidate range to the large language model decoder.

[0110] S52. Within the limited scope, the decoder combines the constraints of the SQL syntax tree to complete the filling of the current node and continues the decoding process of subsequent nodes.

[0111] This embodiment is a preferred implementation of step S5 in embodiment 1. Its purpose is to effectively apply the results of dynamic semantic pruning to the decoding process of LLM to achieve the final accurate generation.

[0112] Step S51: Provide the refined subset of candidate fields as a limited candidate range to the large language model decoder. The purpose of this step is to dynamically focus the LLM's generation space. In the traditional decoding process, when the LLM selects the next lexical, its candidate range is the entire vocabulary. However, in this embodiment, when decoding to the syntax tree node of the field to be filled, this range is dynamically and forcibly restricted to only the field names in the refined subset of candidate fields. This is equivalent to providing the LLM with a high-quality hint or whitelist, guiding its attention from hundreds or thousands of possible database fields to a few high-probability options after filtering.

[0113] Step S52: The decoder, within a limited scope and in conjunction with the constraints of the SQL syntax tree, completes the filling of the current node and continues the decoding process of subsequent nodes; the decoder of the large language model will select within the limited scope set in the previous step to fill the current field node; since the candidate range has been filtered with high quality, the probability of the LLM making the correct selection is greatly improved; after the current node is filled, the restriction on the candidate range is lifted, and the decoder returns to its normal decoding mode, which is only constrained by the SQL syntax tree, and continues to generate other parts of the SQL statement until the entire SQL query statement is generated;

[0114] By dynamically constraining the selection range of the decoder to a refined subset of candidate fields, this invention constructs a precise guidance mechanism. It does not simply post-process the output of the LLM, but actively intervenes and guides during the generation process. This method reduces the possibility of the LLM choosing from many grammatically compliant but logically erroneous paths, greatly reducing the risk of generating phantom SQL. This strategy, together with the structured constraints of the SQL syntax tree, forms a double guarantee, ensuring that the output SQL statement is not only grammatically perfect, but also highly consistent with the user's true intent at the business logic level, thereby achieving high-precision NL2SQL conversion.

[0115] Example 7:

[0116] like Figure 2 As shown, the syntax tree constraint decoding optimization system for NL2SQL in ChatDB includes:

[0117] A natural language input interface is used to receive natural language queries from users.

[0118] The candidate field generation module is used to generate an initial set of candidate fields based on natural language queries and database metadata;

[0119] The syntax tree constraint decoding engine is used to embed large language models and is responsible for the structured generation of SQL based on a preset SQL syntax tree.

[0120] The pruning parameter calculation module is used to calculate the semantic confidence score and dynamic pruning threshold when the decoding engine decodes to the syntax tree node of the field to be filled.

[0121] The dynamic semantic pruning module receives the semantic confidence score and the dynamic pruning threshold, performs filtering on the initial candidate field set to generate a refined candidate field subset, and returns the refined candidate field subset to the decoding engine.

[0122] The SQL output module is used to output the complete SQL query statement generated by the decoding engine. This invention also provides a syntax tree constraint decoding optimization system for NL2SQL in ChatDB. This system is built based on the aforementioned method, and its structural design aims to efficiently and collaboratively complete the entire optimization process in a modular manner.

[0123] The system includes:

[0124] Natural language input interface: Its purpose is to provide users with an entry point to interact with the system; in this embodiment, it can be a web text input box integrated into the front end of the business intelligence platform, or a RESTful API endpoint that can be called by other applications to receive users' natural language query requests.

[0125] Candidate field generation module: Its purpose is to perform initial field matching and generation tasks; this module interacts directly with the database's metadata repository, and internally implements a fuzzy column name mapper to calculate the vector similarity between natural language queries and database metadata, and generates an initial candidate field set with a high recall rate as the target strategy;

[0126] Syntax Tree Constraint Decoding Engine: Its purpose is to generate SQL query statements in a structured manner; this engine is the core of the system, and it encapsulates a large language model, such as a GPT or LLaMA series model fine-tuned by the SQL generation task; crucially, this engine forces the entire decoding process of LLM to strictly follow a preset SQL syntax tree, ensuring the correctness of all outputs at the syntactic level.

[0127] The pruning parameter calculation module aims to provide a quantitative basis for dynamic pruning decisions. This module is tightly coupled with the syntax tree constraint decoding engine and is activated immediately when the decoding engine reaches any syntax tree node of the field to be filled. It is responsible for implementing the complex calculation logic in Examples 3 and 4, including real-time parsing of the decoding status to obtain query complexity metrics. Based on this, the dynamic pruning threshold is calculated. Simultaneously, a semantic confidence score is calculated for each candidate field. ;

[0128] Dynamic semantic pruning module: Its purpose is to perform the actual filtering operation; This module receives the semantic confidence score and dynamic pruning threshold output from the pruning parameter calculation module; It strictly follows the filtering logic of Example 5 to perform filtering on the initial candidate field set, and finally generates a refined candidate field subset, and returns it to the syntax tree constraint decoding engine.

[0129] SQL Output Module: Its purpose is to deliver the final generated SQL query for use; this module receives the complete and valid SQL query statement generated by the syntax tree constraint decoding engine and presents it to the user, for example, by displaying it on the front-end interface of the BI platform, or by directly passing it to the downstream database query execution engine to obtain data results;

[0130] Through the clear division of labor and close collaboration among the six modules mentioned above, this system fully implements the NL2SQL decoding optimization method proposed in this invention. The natural language input interface and candidate field generation module are responsible for high recall matching at the front end. The syntax tree constraint decoding engine, as the core generator, ensures the correctness of the syntax. The pruning parameter calculation module and the dynamic semantic pruning module constitute a dynamic pruning unit that interacts with the engine in real time, making precise interventions at key nodes. The SQL output module completes the delivery. The entire system forms an automated technical closed loop from fuzzy user intent input to high-precision and robust SQL output, effectively solving the high recall-low precision contradiction faced by existing technologies. Especially when dealing with complex business analysis queries, it demonstrates excellent performance and reliability.

[0131] This embodiment clarifies the feasibility of the technical solution of the present invention, provides a specific hardware architecture, and ensures that all the aforementioned methods and module functions have their physical execution carriers, enabling the technical solution of the present invention to be transformed from theory into practical application products, and possessing the foundation for commercialization and engineering.

[0132] The above are merely preferred embodiments of the present invention and are not intended to limit the scope of protection of the present invention; any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

[0133] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention.

Claims

1. An optimization method for decoding syntax tree constraints in ChatDB's NL2SQL, characterized in that, Includes the following steps: S1. Receive natural language queries and match them with database metadata to generate an initial candidate field set; S2. Start the large language model and use the preset SQL syntax tree as the decoding constraint. When decoding to the syntax tree node of the field to be filled, calculate the semantic confidence score for each candidate field in the initial candidate field set. S3. Calculate the dynamic pruning threshold based on the query nesting depth during the decoding process; S4. Based on semantic confidence scores and dynamic pruning thresholds, the initial candidate field set is filtered to generate a refined candidate field subset; S5. Provide a refined subset of candidate fields to the large language model, fill in the current node under the constraints of the SQL syntax tree, and continue decoding until a complete SQL query statement is generated.

2. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 1, characterized in that, S1 specifically includes: S11. Using a preset fuzzy column name mapper, vector similarity matching is performed between entity references in natural language queries and metadata of all tables in the database. S12. With the goal of high recall, include all potentially relevant database fields into the initial candidate field set.

3. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 2, characterized in that, S2 specifically includes: S21. Combine the initial semantic similarity of each candidate field in the initial candidate field set with the quantified value of the dynamic logical fit of the candidate field in the current SQL structure. S22. Use the combined result as the semantic confidence score of the candidate field in the current decoding context.

4. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 3, characterized in that, The calculation of semantic confidence score specifically includes: S221. The initial semantic similarity of candidate fields is derived from the vector cosine similarity calculated by the fuzzy column name mapper. S222. The quantified value of dynamic logical fit is determined by the context fit weight output by the pre-trained classification model based on the probabilistic pattern of fields of different data types appearing in specific SQL clauses.

5. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 4, characterized in that, S3 specifically includes: S31. Real-time analysis of the decoding status of large language models to obtain the nesting depth of the currently generated SQL statement; S32. A bounded linearly growing threshold model is adopted, with query complexity as input, to calculate the dynamic pruning threshold, so that the strictness of the pruning strategy increases linearly with query complexity.

6. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 5, characterized in that, S4 specifically includes: S41. Traverse the initial candidate field set and retain all candidate fields whose semantic confidence scores are greater than or equal to the dynamic pruning threshold to generate a refined candidate field subset. S42. After performing the filtering, if the refined candidate field subset is empty, then abandon the threshold filtering and directly select the single candidate field with the highest semantic confidence score from the initial candidate field set as the only candidate.

7. The method for optimizing syntax tree constraint decoding of NL2SQL in ChatDB according to claim 6, characterized in that, S5 specifically includes: S51. Provide a refined subset of candidate fields as a limited candidate range to the large language model decoder. S52. Within the limited scope, the decoder combines the constraints of the SQL syntax tree to complete the filling of the current node and continues the decoding process of subsequent nodes.

8. A syntax tree constraint decoding optimization system for NL2SQL in ChatDB, based on the syntax tree constraint decoding optimization method for NL2SQL in ChatDB as described in any one of claims 1-7, characterized in that, include: A natural language input interface is used to receive natural language queries from users. The candidate field generation module is used to generate an initial set of candidate fields based on natural language queries and database metadata; The syntax tree constraint decoding engine is used to embed large language models and is responsible for the structured generation of SQL based on a preset SQL syntax tree. The pruning parameter calculation module is used to calculate the semantic confidence score and dynamic pruning threshold when the decoding engine decodes to the syntax tree node of the field to be filled. The dynamic semantic pruning module receives the semantic confidence score and the dynamic pruning threshold, performs filtering on the initial candidate field set to generate a refined candidate field subset, and returns the refined candidate field subset to the decoding engine. The SQL output module is used to output the complete SQL query statement generated by the decoding engine.