Skip to content

fix(nl2sql): extract last SQL block from model output#522

Open
ofyc-666 wants to merge 1 commit into
spring-ai-alibaba:mainfrom
ofyc-666:fix-486-final-sql-propagation
Open

fix(nl2sql): extract last SQL block from model output#522
ofyc-666 wants to merge 1 commit into
spring-ai-alibaba:mainfrom
ofyc-666:fix-486-final-sql-propagation

Conversation

@ofyc-666

Copy link
Copy Markdown

Describe what this PR does / why we need it

This PR fixes an NL2SQL issue where SQL cleanup may extract the first intermediate SQL block instead of the final SQL when the model output contains multiple fenced SQL code blocks.

In the current flow, SqlGenerateNode cleans the model output through Nl2SqlService.sqlTrim() and stores the result in SQL_GENERATE_OUTPUT. The downstream SemanticConsistencyNode and SqlExecuteNode then consume this SQL. The previous logic extracted the first fenced code block, which could pass a non-final SQL to downstream nodes if the first SQL block was only an intermediate query.

Does this pull request fix one issue?

Fixes #486

Describe how you did it

  • Kept the existing behavior of MarkdownParserUtil.extractRawText() unchanged to avoid affecting other Markdown code block parsing scenarios, such as JSON, Python, and chart configuration parsing.
  • Added MarkdownParserUtil.extractLastRawText() to extract the last fenced code block.
  • Updated Nl2SqlService.sqlTrim() to use extractLastRawText(), so SQL cleanup uses the last fenced block when multiple code blocks are present.
  • Added regression tests to cover model output with multiple fenced SQL outputs and verify that the final SQL output is returned.

The reason for choosing the last block in the SQL cleanup path is that the model output is generated in one pass. It does not actually execute the first SQL and then generate the second SQL based on runtime results. In multiple-SQL-block outputs, earlier SQL blocks are often intermediate or helper queries, while the later SQL block is more likely to be the final executable SQL that already combines the intermediate logic through a subquery or JOIN. Since the current execution pipeline handles a single SQL statement, passing the last SQL block downstream better matches the existing architecture.

Describe how to verify it

Ran the following local tests:

.\mvnw.cmd -pl data-agent-management "-Dtest=MarkdownParserUtilTest,Nl2SqlServiceImplTest" test

Result:

Tests run: 26, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS

Special notes for reviews

This PR only changes code block selection in the SQL cleanup path. It does not modify SqlGenerateNode, the Graph flow, prompts, or add support for sequential execution of multiple SQL statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Sql生成节点传递的sql非最终sql

1 participant