Skip to content

Commit 62855e4

Browse files
refactor(nl2sql): clean up whitespace in TableRelationNode and BaseNl2SqlService
1 parent 9d33cd9 commit 62855e4

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

spring-ai-alibaba-nl2sql/spring-ai-alibaba-nl2sql-chat/src/main/java/com/alibaba/cloud/ai/node/TableRelationNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class TableRelationNode implements NodeAction {
7777
private final BusinessKnowledgeRecallService businessKnowledgeRecallService;
7878

7979
private final SemanticModelRecallService semanticModelRecallService;
80-
80+
8181
private final DatasourceService datasourceService;
8282

8383
public TableRelationNode(BaseSchemaService baseSchemaService, BaseNl2SqlService baseNl2SqlService,
@@ -237,7 +237,7 @@ else if ("postgresql".equalsIgnoreCase(datasource.getType())) {
237237
private SchemaDTO processSchemaSelection(SchemaDTO schemaDTO, String input, List<String> evidenceList,
238238
OverAllState state) {
239239
String schemaAdvice = StateUtils.getStringValue(state, SQL_GENERATE_SCHEMA_MISSING_ADVICE, null);
240-
240+
241241
// 动态获取Agent对应的数据库配置
242242
DbConfig agentDbConfig = getAgentDbConfig(state);
243243
logger.debug("Using agent-specific dbConfig: {}", agentDbConfig != null ? agentDbConfig.getUrl() : "default");

spring-ai-alibaba-nl2sql/spring-ai-alibaba-nl2sql-chat/src/main/java/com/alibaba/cloud/ai/service/base/BaseNl2SqlService.java

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,14 @@ public SchemaDTO fineSelect(SchemaDTO schemaDTO, String query, List<String> evid
391391

392392
public SchemaDTO fineSelect(SchemaDTO schemaDTO, String query, List<String> evidenceList,
393393
String sqlGenerateSchemaMissingAdvice, DbConfig specificDbConfig) {
394-
logger.debug("Fine selecting schema for query: {} with {} evidences and specificDbConfig: {}",
395-
query, evidenceList.size(), specificDbConfig != null ? specificDbConfig.getUrl() : "default");
396-
394+
logger.debug("Fine selecting schema for query: {} with {} evidences and specificDbConfig: {}", query,
395+
evidenceList.size(), specificDbConfig != null ? specificDbConfig.getUrl() : "default");
396+
397397
// 增加具体的样例数据,让模型根据样例数据进行选择
398398
SchemaDTO enrichedSchema = enrichSchemaWithSampleData(schemaDTO, specificDbConfig);
399-
logger.debug("Schema enriched with sample data for {} tables",
399+
logger.debug("Schema enriched with sample data for {} tables",
400400
enrichedSchema.getTable() != null ? enrichedSchema.getTable().size() : 0);
401-
401+
402402
String prompt = buildMixSelectorPrompt(evidenceList, query, enrichedSchema);
403403
logger.debug("Calling LLM for schema fine selection");
404404
String content = aiService.call(prompt);
@@ -451,8 +451,7 @@ private SchemaDTO enrichSchemaWithSampleData(SchemaDTO schemaDTO, DbConfig speci
451451

452452
// 使用传入的特定数据库配置,如果为null则使用默认配置
453453
DbConfig targetDbConfig = specificDbConfig != null ? specificDbConfig : dbConfig;
454-
logger.debug("Using database config: {}",
455-
targetDbConfig != null ? targetDbConfig.getUrl() : "null");
454+
logger.debug("Using database config: {}", targetDbConfig != null ? targetDbConfig.getUrl() : "null");
456455

457456
// 检查数据库配置是否有效
458457
if (!isDatabaseConfigValid(targetDbConfig)) {
@@ -463,17 +462,18 @@ private SchemaDTO enrichSchemaWithSampleData(SchemaDTO schemaDTO, DbConfig speci
463462
try {
464463
// 创建SchemaDTO的深拷贝以避免修改原始对象
465464
SchemaDTO enrichedSchema = copySchemaDTO(schemaDTO);
466-
465+
467466
// 为每个表获取样例数据
468467
for (TableDTO tableDTO : enrichedSchema.getTable()) {
469468
enrichTableWithSampleData(tableDTO, targetDbConfig);
470469
}
471-
472-
logger.info("Successfully enriched schema with sample data for {} tables",
470+
471+
logger.info("Successfully enriched schema with sample data for {} tables",
473472
enrichedSchema.getTable().size());
474473
return enrichedSchema;
475-
476-
} catch (Exception e) {
474+
475+
}
476+
catch (Exception e) {
477477
logger.warn("Failed to enrich schema with sample data, using original schema: {}", e.getMessage());
478478
return schemaDTO;
479479
}
@@ -489,24 +489,25 @@ private void enrichTableWithSampleData(TableDTO tableDTO, DbConfig dbConfig) {
489489
return;
490490
}
491491

492-
logger.debug("Enriching table '{}' with sample table data for {} columns",
493-
tableDTO.getName(), tableDTO.getColumn().size());
492+
logger.debug("Enriching table '{}' with sample table data for {} columns", tableDTO.getName(),
493+
tableDTO.getColumn().size());
494494

495495
try {
496496
// 获取表的样例数据
497497
ResultSetBO tableData = getSampleDataForTable(tableDTO.getName(), dbConfig);
498498
if (tableData != null && tableData.getData() != null && !tableData.getData().isEmpty()) {
499499
// 将整行数据分配给对应的列
500500
distributeTableDataToColumns(tableDTO, tableData);
501-
logger.info("Successfully enriched table '{}' with {} sample rows",
502-
tableDTO.getName(), tableData.getData().size());
503-
} else {
501+
logger.info("Successfully enriched table '{}' with {} sample rows", tableDTO.getName(),
502+
tableData.getData().size());
503+
}
504+
else {
504505
logger.debug("No sample data found for table '{}'", tableDTO.getName());
505506
}
506-
507-
} catch (Exception e) {
508-
logger.warn("Failed to get sample data for table '{}': {}",
509-
tableDTO.getName(), e.getMessage());
507+
508+
}
509+
catch (Exception e) {
510+
logger.warn("Failed to get sample data for table '{}': {}", tableDTO.getName(), e.getMessage());
510511
}
511512
}
512513

@@ -529,39 +530,38 @@ private ResultSetBO getSampleDataForTable(String tableName, DbConfig dbConfig) t
529530
private void distributeTableDataToColumns(TableDTO tableDTO, ResultSetBO tableData) {
530531
List<String> columnHeaders = tableData.getColumn();
531532
List<Map<String, String>> rows = tableData.getData();
532-
533+
533534
// 为每个列创建样例数据映射
534535
Map<String, List<String>> columnSamples = new HashMap<>();
535-
536+
536537
// 遍历每一行数据
537538
for (Map<String, String> row : rows) {
538539
for (String columnName : columnHeaders) {
539540
String value = row.get(columnName);
540-
541+
541542
if (value != null && !value.trim().isEmpty()) {
542-
columnSamples.computeIfAbsent(columnName, k -> new ArrayList<>())
543-
.add(value);
543+
columnSamples.computeIfAbsent(columnName, k -> new ArrayList<>()).add(value);
544544
}
545545
}
546546
}
547-
547+
548548
// 将样例数据分配给对应的列
549549
for (ColumnDTO columnDTO : tableDTO.getColumn()) {
550550
String columnName = columnDTO.getName();
551551
List<String> samples = columnSamples.get(columnName);
552-
552+
553553
if (samples != null && !samples.isEmpty()) {
554554
// 去重并限制样例数量
555555
List<String> filteredSamples = samples.stream()
556556
.filter(sample -> sample != null && !sample.trim().isEmpty())
557557
.distinct()
558558
.limit(5) // 最多保留5个样例值
559559
.collect(Collectors.toList());
560-
560+
561561
if (!filteredSamples.isEmpty()) {
562562
columnDTO.setSamples(filteredSamples);
563-
logger.debug("Added {} sample values for column '{}.{}': {}",
564-
filteredSamples.size(), tableDTO.getName(), columnName, filteredSamples);
563+
logger.debug("Added {} sample values for column '{}.{}': {}", filteredSamples.size(),
564+
tableDTO.getName(), columnName, filteredSamples);
565565
}
566566
}
567567
}
@@ -577,23 +577,23 @@ private boolean isDatabaseConfigValid(DbConfig dbConfig) {
577577
logger.debug("dbConfig is null");
578578
return false;
579579
}
580-
580+
581581
if (dbAccessor == null) {
582582
logger.debug("dbAccessor is null");
583583
return false;
584584
}
585-
585+
586586
// 检查基本的连接信息
587587
boolean hasBasicInfo = dbConfig.getUrl() != null && !dbConfig.getUrl().trim().isEmpty()
588588
&& dbConfig.getUsername() != null && !dbConfig.getUsername().trim().isEmpty();
589-
589+
590590
if (!hasBasicInfo) {
591-
logger.debug("dbConfig missing basic connection info - url: {}, username: {}",
591+
logger.debug("dbConfig missing basic connection info - url: {}, username: {}",
592592
dbConfig.getUrl() != null ? "present" : "null",
593593
dbConfig.getUsername() != null ? "present" : "null");
594594
return false;
595595
}
596-
596+
597597
return true;
598598
}
599599

@@ -608,14 +608,15 @@ private SchemaDTO copySchemaDTO(SchemaDTO originalSchema) {
608608
copy.setDescription(originalSchema.getDescription());
609609
copy.setTableCount(originalSchema.getTableCount());
610610
copy.setForeignKeys(originalSchema.getForeignKeys());
611-
611+
612612
if (originalSchema.getTable() != null) {
613-
List<TableDTO> copiedTables = originalSchema.getTable().stream()
614-
.map(this::copyTableDTO)
615-
.collect(Collectors.toList());
613+
List<TableDTO> copiedTables = originalSchema.getTable()
614+
.stream()
615+
.map(this::copyTableDTO)
616+
.collect(Collectors.toList());
616617
copy.setTable(copiedTables);
617618
}
618-
619+
619620
return copy;
620621
}
621622

@@ -629,14 +630,15 @@ private TableDTO copyTableDTO(TableDTO originalTable) {
629630
copy.setName(originalTable.getName());
630631
copy.setDescription(originalTable.getDescription());
631632
copy.setPrimaryKeys(originalTable.getPrimaryKeys());
632-
633+
633634
if (originalTable.getColumn() != null) {
634-
List<ColumnDTO> copiedColumns = originalTable.getColumn().stream()
635-
.map(this::copyColumnDTO)
636-
.collect(Collectors.toList());
635+
List<ColumnDTO> copiedColumns = originalTable.getColumn()
636+
.stream()
637+
.map(this::copyColumnDTO)
638+
.collect(Collectors.toList());
637639
copy.setColumn(copiedColumns);
638640
}
639-
641+
640642
return copy;
641643
}
642644

@@ -653,15 +655,15 @@ private ColumnDTO copyColumnDTO(ColumnDTO originalColumn) {
653655
copy.setRange(originalColumn.getRange());
654656
copy.setType(originalColumn.getType());
655657
copy.setMapping(originalColumn.getMapping());
656-
658+
657659
// 复制现有的样例数据
658660
if (originalColumn.getSamples() != null) {
659661
copy.setSamples(new ArrayList<>(originalColumn.getSamples()));
660662
}
661663
if (originalColumn.getData() != null) {
662664
copy.setData(new ArrayList<>(originalColumn.getData()));
663665
}
664-
666+
665667
return copy;
666668
}
667669

0 commit comments

Comments
 (0)