@@ -391,14 +391,14 @@ public SchemaDTO fineSelect(SchemaDTO schemaDTO, String query, List<String> evid
391
391
392
392
public SchemaDTO fineSelect (SchemaDTO schemaDTO , String query , List <String > evidenceList ,
393
393
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
+
397
397
// 增加具体的样例数据,让模型根据样例数据进行选择
398
398
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" ,
400
400
enrichedSchema .getTable () != null ? enrichedSchema .getTable ().size () : 0 );
401
-
401
+
402
402
String prompt = buildMixSelectorPrompt (evidenceList , query , enrichedSchema );
403
403
logger .debug ("Calling LLM for schema fine selection" );
404
404
String content = aiService .call (prompt );
@@ -451,8 +451,7 @@ private SchemaDTO enrichSchemaWithSampleData(SchemaDTO schemaDTO, DbConfig speci
451
451
452
452
// 使用传入的特定数据库配置,如果为null则使用默认配置
453
453
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" );
456
455
457
456
// 检查数据库配置是否有效
458
457
if (!isDatabaseConfigValid (targetDbConfig )) {
@@ -463,17 +462,18 @@ private SchemaDTO enrichSchemaWithSampleData(SchemaDTO schemaDTO, DbConfig speci
463
462
try {
464
463
// 创建SchemaDTO的深拷贝以避免修改原始对象
465
464
SchemaDTO enrichedSchema = copySchemaDTO (schemaDTO );
466
-
465
+
467
466
// 为每个表获取样例数据
468
467
for (TableDTO tableDTO : enrichedSchema .getTable ()) {
469
468
enrichTableWithSampleData (tableDTO , targetDbConfig );
470
469
}
471
-
472
- logger .info ("Successfully enriched schema with sample data for {} tables" ,
470
+
471
+ logger .info ("Successfully enriched schema with sample data for {} tables" ,
473
472
enrichedSchema .getTable ().size ());
474
473
return enrichedSchema ;
475
-
476
- } catch (Exception e ) {
474
+
475
+ }
476
+ catch (Exception e ) {
477
477
logger .warn ("Failed to enrich schema with sample data, using original schema: {}" , e .getMessage ());
478
478
return schemaDTO ;
479
479
}
@@ -489,24 +489,25 @@ private void enrichTableWithSampleData(TableDTO tableDTO, DbConfig dbConfig) {
489
489
return ;
490
490
}
491
491
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 ());
494
494
495
495
try {
496
496
// 获取表的样例数据
497
497
ResultSetBO tableData = getSampleDataForTable (tableDTO .getName (), dbConfig );
498
498
if (tableData != null && tableData .getData () != null && !tableData .getData ().isEmpty ()) {
499
499
// 将整行数据分配给对应的列
500
500
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 {
504
505
logger .debug ("No sample data found for table '{}'" , tableDTO .getName ());
505
506
}
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 ());
510
511
}
511
512
}
512
513
@@ -529,39 +530,38 @@ private ResultSetBO getSampleDataForTable(String tableName, DbConfig dbConfig) t
529
530
private void distributeTableDataToColumns (TableDTO tableDTO , ResultSetBO tableData ) {
530
531
List <String > columnHeaders = tableData .getColumn ();
531
532
List <Map <String , String >> rows = tableData .getData ();
532
-
533
+
533
534
// 为每个列创建样例数据映射
534
535
Map <String , List <String >> columnSamples = new HashMap <>();
535
-
536
+
536
537
// 遍历每一行数据
537
538
for (Map <String , String > row : rows ) {
538
539
for (String columnName : columnHeaders ) {
539
540
String value = row .get (columnName );
540
-
541
+
541
542
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 );
544
544
}
545
545
}
546
546
}
547
-
547
+
548
548
// 将样例数据分配给对应的列
549
549
for (ColumnDTO columnDTO : tableDTO .getColumn ()) {
550
550
String columnName = columnDTO .getName ();
551
551
List <String > samples = columnSamples .get (columnName );
552
-
552
+
553
553
if (samples != null && !samples .isEmpty ()) {
554
554
// 去重并限制样例数量
555
555
List <String > filteredSamples = samples .stream ()
556
556
.filter (sample -> sample != null && !sample .trim ().isEmpty ())
557
557
.distinct ()
558
558
.limit (5 ) // 最多保留5个样例值
559
559
.collect (Collectors .toList ());
560
-
560
+
561
561
if (!filteredSamples .isEmpty ()) {
562
562
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 );
565
565
}
566
566
}
567
567
}
@@ -577,23 +577,23 @@ private boolean isDatabaseConfigValid(DbConfig dbConfig) {
577
577
logger .debug ("dbConfig is null" );
578
578
return false ;
579
579
}
580
-
580
+
581
581
if (dbAccessor == null ) {
582
582
logger .debug ("dbAccessor is null" );
583
583
return false ;
584
584
}
585
-
585
+
586
586
// 检查基本的连接信息
587
587
boolean hasBasicInfo = dbConfig .getUrl () != null && !dbConfig .getUrl ().trim ().isEmpty ()
588
588
&& dbConfig .getUsername () != null && !dbConfig .getUsername ().trim ().isEmpty ();
589
-
589
+
590
590
if (!hasBasicInfo ) {
591
- logger .debug ("dbConfig missing basic connection info - url: {}, username: {}" ,
591
+ logger .debug ("dbConfig missing basic connection info - url: {}, username: {}" ,
592
592
dbConfig .getUrl () != null ? "present" : "null" ,
593
593
dbConfig .getUsername () != null ? "present" : "null" );
594
594
return false ;
595
595
}
596
-
596
+
597
597
return true ;
598
598
}
599
599
@@ -608,14 +608,15 @@ private SchemaDTO copySchemaDTO(SchemaDTO originalSchema) {
608
608
copy .setDescription (originalSchema .getDescription ());
609
609
copy .setTableCount (originalSchema .getTableCount ());
610
610
copy .setForeignKeys (originalSchema .getForeignKeys ());
611
-
611
+
612
612
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 ());
616
617
copy .setTable (copiedTables );
617
618
}
618
-
619
+
619
620
return copy ;
620
621
}
621
622
@@ -629,14 +630,15 @@ private TableDTO copyTableDTO(TableDTO originalTable) {
629
630
copy .setName (originalTable .getName ());
630
631
copy .setDescription (originalTable .getDescription ());
631
632
copy .setPrimaryKeys (originalTable .getPrimaryKeys ());
632
-
633
+
633
634
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 ());
637
639
copy .setColumn (copiedColumns );
638
640
}
639
-
641
+
640
642
return copy ;
641
643
}
642
644
@@ -653,15 +655,15 @@ private ColumnDTO copyColumnDTO(ColumnDTO originalColumn) {
653
655
copy .setRange (originalColumn .getRange ());
654
656
copy .setType (originalColumn .getType ());
655
657
copy .setMapping (originalColumn .getMapping ());
656
-
658
+
657
659
// 复制现有的样例数据
658
660
if (originalColumn .getSamples () != null ) {
659
661
copy .setSamples (new ArrayList <>(originalColumn .getSamples ()));
660
662
}
661
663
if (originalColumn .getData () != null ) {
662
664
copy .setData (new ArrayList <>(originalColumn .getData ()));
663
665
}
664
-
666
+
665
667
return copy ;
666
668
}
667
669
0 commit comments