Skip to content

Conversation

james-huangzhen
Copy link
Contributor

Describe what this PR does / why we need it

Does this pull request fix one issue?

Describe how you did it

Describe how to verify it

Special notes for reviews

- 添加了一种新方法来处理用户查询中的相对时间表达式,并将其转换为特定日期。
- 引入了用于时间转换的新提示模板。
- 更新了 PlannerNode 以利用已处理的查询进行规划。
- 增强了 BaseNl2SqlService 以处理查询重写期间的时间表达式。
- 添加了时间表达式处理功能的单元测试。
- 添加了一种新方法来处理用户查询中的相对时间表达式,并将其转换为特定日期。
- 引入了用于时间转换的新提示模板。
- 更新了 PlannerNode 以利用已处理的查询进行规划。
- 增强了 BaseNl2SqlService 以处理查询重写期间的时间表达式。
- 添加了时间表达式处理功能的单元测试。
@github-actions github-actions bot added the area/nl2sql SAA nl2sql module label Aug 23, 2025
@james-huangzhen james-huangzhen changed the title feat(nl2sql):在查询中实现时间表达式处理 feat(nl2sql):在查询中实现时间表达式处理 Aug 23, 2025
@james-huangzhen james-huangzhen changed the title feat(nl2sql):在查询中实现时间表达式处理 feat(nl2sql):在查询中实现时间表达式处理 Aug 23, 2025
@james-huangzhen james-huangzhen changed the title feat(nl2sql):在查询中实现时间表达式处理 feat(nl2sql): 在查询中实现时间表达式处理 Aug 23, 2025
@VLSMB
Copy link
Collaborator

VLSMB commented Aug 23, 2025

Please resolve conflicts

- 更新了 Nl2sqlConfiguration,将 DatasourceService 包含在 TableRelationNode 中。
- 在 TableRelationNode 中实现了基于代理 ID 动态检索数据库配置的方法。
- 增强的 BaseNl2SqlService 支持使用特定数据库配置进行模式选择,从而改进查询处理。
- 为模式添加了示例数据扩充,以帮助在查询处理期间理解模型。
@zhangshenghang
Copy link
Collaborator

Please fix CI

james-huangzhen and others added 4 commits August 26, 2025 08:49
- 将vectorStoreService.getDocuments的参数匹配器更改为使用anyString()而不是eq(query),以便更灵活地进行测试。
- 更新了aiService.call的返回值,以便在多个测试用例中包含对processTimeExpressions的查询。
@zhangshenghang zhangshenghang requested a review from Copilot August 26, 2025 09:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements time expression processing in Natural Language to SQL (NL2SQL) queries, allowing the system to convert relative time expressions (like "today", "this month", "last year") into specific dates and time ranges before SQL generation.

Key changes include:

  • Added a dedicated time conversion prompt template with examples
  • Integrated time expression processing into the query rewrite workflow
  • Modified test cases to accommodate the additional AI service call for time processing
  • Enhanced schema selection with sample data enrichment capabilities

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
BaseNl2SqlService.java Added processTimeExpressions method and sample data enrichment logic for schema selection
time-conversion.txt New prompt template for converting relative time expressions to specific dates
PromptHelper.java Added buildTimeConversionPrompt method to generate time conversion prompts
PromptConstant.java Added getTimeConversionPromptTemplate method for accessing the time conversion template
BaseNl2SqlServiceTest.java Updated test mocks to handle additional AI service calls for time processing
TableRelationNode.java Enhanced with agent-specific database configuration support
PlannerNode.java Modified to use processed queries with time expressions converted
Nl2sqlConfiguration.java Updated constructor call to include datasourceService dependency

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +175 to +176
// TODO 调试AgentID不一致,暂时手动处理
agentDatasources = datasourceService.getAgentDatasources(agentId - 999999);
Copy link
Preview

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hardcoded magic number 999999 and TODO comment indicate a temporary workaround. Consider implementing a proper agent ID mapping mechanism or configuration property instead of this hardcoded offset.

Suggested change
// TODO 调试AgentID不一致,暂时手动处理
agentDatasources = datasourceService.getAgentDatasources(agentId - 999999);
// Use configurable agentIdOffset to handle AgentID inconsistency
agentDatasources = datasourceService.getAgentDatasources(agentId - agentIdOffset);

Copilot uses AI. Check for mistakes.

Comment on lines +556 to +558
.filter(sample -> sample != null && !sample.trim().isEmpty())
.distinct()
.limit(5) // 最多保留5个样例值
Copy link
Preview

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 5 for sample limit should be extracted to a named constant or configuration property to improve maintainability and allow easy adjustment.

Suggested change
.filter(sample -> sample != null && !sample.trim().isEmpty())
.distinct()
.limit(5) // 最多保留5个样例值
.limit(SAMPLE_LIMIT_PER_COLUMN) // 最多保留5个样例值

Copilot uses AI. Check for mistakes.

}
expressionList.addAll(dateTimeList);
logger.debug("Processed {} datetime expressions", dateTimeList.size());
// 时间处理已经在查询重写阶段完成,这里不再需要处理
Copy link
Preview

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment is in Chinese. Consider using English comments for consistency with the rest of the codebase, or establish a clear language policy for code comments.

Suggested change
// 时间处理已经在查询重写阶段完成,这里不再需要处理
// Time expressions have already been processed in the query rewrite phase; no further handling needed here

Copilot uses AI. Check for mistakes.

@chickenlj chickenlj changed the base branch from main to a2a-support September 21, 2025 09:37
Copy link
Collaborator

@zxuexingzhijie zxuexingzhijie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@VLSMB VLSMB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@VLSMB VLSMB merged commit 729cb4d into alibaba:a2a-support Sep 21, 2025
3 checks passed
VLSMB added a commit to zxuexingzhijie/spring-ai-alibaba that referenced this pull request Sep 22, 2025
* feat(nl2sql):在查询中实现时间表达式处理

- 添加了一种新方法来处理用户查询中的相对时间表达式,并将其转换为特定日期。
- 引入了用于时间转换的新提示模板。
- 更新了 PlannerNode 以利用已处理的查询进行规划。
- 增强了 BaseNl2SqlService 以处理查询重写期间的时间表达式。
- 添加了时间表达式处理功能的单元测试。

* feat(nl2sql):在查询中实现时间表达式处理

- 添加了一种新方法来处理用户查询中的相对时间表达式,并将其转换为特定日期。
- 引入了用于时间转换的新提示模板。
- 更新了 PlannerNode 以利用已处理的查询进行规划。
- 增强了 BaseNl2SqlService 以处理查询重写期间的时间表达式。
- 添加了时间表达式处理功能的单元测试。

* feat(nl2sql): 增强 TableRelationNode 和 BaseNl2SqlService 以进行动态数据源处理

- 更新了 Nl2sqlConfiguration,将 DatasourceService 包含在 TableRelationNode 中。
- 在 TableRelationNode 中实现了基于代理 ID 动态检索数据库配置的方法。
- 增强的 BaseNl2SqlService 支持使用特定数据库配置进行模式选择,从而改进查询处理。
- 为模式添加了示例数据扩充,以帮助在查询处理期间理解模型。

* refactor(nl2sql): clean up whitespace in TableRelationNode and BaseNl2SqlService

* chore(tests): add license header to AiTimeProcessorTest.java

* feat(nl2sql): 移除测试类

* refactor(tests): 更新 BaseNl2SqlServiceTest 中的模拟行为

- 将vectorStoreService.getDocuments的参数匹配器更改为使用anyString()而不是eq(query),以便更灵活地进行测试。
- 更新了aiService.call的返回值,以便在多个测试用例中包含对processTimeExpressions的查询。

* refactor(tests): spring-javaformat:apply

* rename var

* fix bugs after merge

---------

Co-authored-by: VLSMB <2047857654@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/nl2sql SAA nl2sql module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants