## 3 Model Design and Implementation
### Pre-Processing
```
Input: processed_query, entities and syntactic_structure Output: code_fragments
1 function generate_python_code_with_CoT (query, entities, syntactic_structure):
2 sub_tasks = decompose_query (query)
3 code_fragments = []
4 for task in sub_tasks:  intermediate_code = generate_intermediate_code(task, entities)  code_fragments.append (intermediate_code)
7 validate_code (intermediate_code)
8 final_code = integrate_code_fragments (code_fragments)  optimized_code = optimize_code (final_code)  return optimized_code
```

**Algorithm 3**Algorithms for CoT reasoning process

In Algorithm 3, Python code is generated step by step by decomposing a complex task into multiple subtasks. First, the query is decomposed into independent subtasks, and intermediate code is generated and verified for each subtask. Then, all intermediate code fragments are integrated into the complete code, and its simplicity and readability are optimized. Through step-by-step reasoning and verification, the code of each subtask is carefully checked to ensure that the final output code is accurate, efficient, and meets user requirements. ### NL2 SQL and Iteration

Using the generated Python code, we provide the question, relevant information, and Python code as inputs to the large model DeepSeek. With a Chain of Thought (CoT) approach, craft prompts to guide DeepSeek in generating an SQL statement based on database and contextual information. The output includes both the reasoning process and the generated SQL query. Various factors must be considered in designing the prompt to ensure the model fully understands the goal. As shown in Figure 6, the large language model combines the context and prompt information to generate SQL statements according to relevant requirements. These requirements are mainly for some common problems in NL2 SQL, and special requirements and specifications are made. For example, order by + limit one is preferred instead of max/min, ID is selected, and necessary fields are displayed cautiously. After generating the initial SQL, the code is executed in the database. If issues arise, they are returned to LLM, which may include errors in execution, successful execution with empty results, or results that do not satisfy the query requirements. For execution errors, the system captures the error message and sends it back to the model, prompting it to refine the code and generate a new SQL statement. This iterative process can run up to N cycles (defaulting to 5 in this case) and can be adjusted as needed. The process stops immediately if the query successfully answers the question. ### Post-Processing

After SQL generation, further refinements are necessary to handle common errors and improve accuracy. Typical issues to address include displaying an extra column, selecting incorrect fields, omitting specified fields, handling null values in calculations, duplicate values, unclear metric definitions, decimal conversions, required aggregate calculations, and order-based computations. Based on these checkpoints, a final prompt is crafted to guide the model in performing effective validation and adjustments, ultimately returning a more accurate SQL query, as shown in Figure 7.

Figure 6: Generating SQL prompts diagram. We design the prompts on the experiences and financial rules. In Figure 7, first, a syntax check is performed to ensure that the generated SQL query complies with the SQL syntax specification. Next, query optimization is performed to simplify redundant expressions or subqueries to improve query efficiency. Finally, database-specific adjustments are performed to ensure the generated SQL can be correctly executed in a specific database to avoid potential errors or performance issues. These steps ensure that the final SQL query can accurately and efficiently meet user needs. This final prompt ensures that DeepSeek evaluates the SQL query against each of these criteria, making targeted adjustments as needed. The revised SQL should then better align with the problem requirements and provide a more reliable answer.