Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public class GetTablesTool implements DataAgentTool {
description =
"""
Get synced semantic-layer tables and ordinary views, including name, object type, domain, \
description, data granularity (what one row represents) and enabled relations.\
description, data granularity (what one row represents), business key columns and enabled relations. \
Business keys are administrator declarations, not database-enforced or data-validated uniqueness guarantees.\
""")
public ToolResultBo getTables(
@ToolParam(
Expand Down Expand Up @@ -114,6 +115,12 @@ private TablePromptResponse filterTable(
rel.targetTableName(),
rel.targetColumnNames()))
.toList();
return table.toBuilder().relations(relations).build();
return table.toBuilder()
.relations(relations)
.businessKeyColumns(
scope.canColumns(table.name(), table.businessKeyColumns())
? table.businessKeyColumns()
: List.of())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public static ColumnPromptResponse mapColumnPrompt(ColumnInfo column) {
}

public static TablePromptResponse mapTablePrompt(
TableInfo table, List<TableRelationPromptResponse> resolvedRelations) {
TableInfo table,
List<TableRelationPromptResponse> resolvedRelations,
List<String> businessKeyColumns) {
if (!SemanticAvailabilityHelper.isTableAvailable(table)) {
return null;
}
Expand All @@ -56,6 +58,7 @@ public static TablePromptResponse mapTablePrompt(
.domain(SemanticUtils.normalizeDomain(table.getDomain()))
.description(SemanticUtils.trimToNull(table.getTableDescription()))
.dataGranularity(SemanticUtils.trimToNull(table.getDataGranularity()))
.businessKeyColumns(businessKeyColumns)
.relations(resolvedRelations)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.github.malonetalk.service.semantic.relation.LogicalTableRelationHelper;
import io.github.malonetalk.utils.SemanticUtils;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -40,7 +41,8 @@ public class SemanticConverter {

private final LogicalTableRelationHelper logicalTableRelationHelper;

public TableSemanticResponse toResponse(TableInfo tableInfo) {
public TableSemanticResponse toResponse(
TableInfo tableInfo, Set<String> physicalColumnNames, boolean canReadKey) {
Boolean isVisible = tableInfo.getIsVisible();
boolean hasPhysicalTable = SemanticAvailabilityHelper.hasPhysicalTable(tableInfo);
return TableSemanticResponse.builder()
Expand All @@ -52,6 +54,13 @@ public TableSemanticResponse toResponse(TableInfo tableInfo) {
SemanticUtils.trimToNull(tableInfo.getPhysicalTableDescription()))
.tableDescription(SemanticUtils.trimToNull(tableInfo.getTableDescription()))
.dataGranularity(SemanticUtils.trimToNull(tableInfo.getDataGranularity()))
.businessKeyColumns(canReadKey ? tableInfo.getBusinessKeyColumns() : null)
.businessKeyInvalidReason(
canReadKey
? SemanticAvailabilityHelper.businessKeyInvalidReason(
tableInfo, physicalColumnNames)
.orElse(null)
: null)
.isVisible(isVisible)
.hasPhysicalTable(hasPhysicalTable)
.invalidReason(SemanticAvailabilityHelper.tableInvalidReason(tableInfo))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ public record TablePromptResponse(
String domain,
String description,
String dataGranularity,
List<String> businessKeyColumns,
List<TableRelationPromptResponse> relations) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.github.malonetalk.dto.semantic;

import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;

@Builder
Expand All @@ -29,6 +30,8 @@ public record TableSemanticResponse(
String physicalTableDescription,
String tableDescription,
String dataGranularity,
List<String> businessKeyColumns,
String businessKeyInvalidReason,
Boolean isVisible,
Boolean hasPhysicalTable,
String invalidReason,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.Builder;

@Builder
public record TableSemanticUpdateRequest(
@NotNull(message = "datasourceId 不能为空") Integer datasourceId,
@NotBlank(message = "tableName 不能为空") String tableName,
String domain,
String tableDescription,
String dataGranularity,
List<@NotBlank(message = "业务唯一键字段不能为空") String> businessKeyColumns,
@NotNull(message = "isVisible 不能为空") Boolean isVisible) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package io.github.malonetalk.entity;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.Data;

@Data
Expand All @@ -29,10 +31,15 @@ public class TableInfo {
private String physicalTableDescription;
private String tableDescription;
private String dataGranularity;
private List<String> businessKeyColumns;
private String domain;
private Integer datasourceId;
private Boolean isVisible;
private Boolean physicalStatus;
private LocalDateTime createTime;
private LocalDateTime updateTime;

public List<String> getBusinessKeyColumns() {
return Optional.ofNullable(businessKeyColumns).orElseGet(List::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import io.github.malonetalk.entity.TableInfo;
import io.github.malonetalk.service.semantic.enums.ColumnInvalidReasonEnum;
import io.github.malonetalk.service.semantic.enums.TableInvalidReasonEnum;
import io.github.malonetalk.utils.SemanticUtils;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public final class SemanticAvailabilityHelper {

Expand Down Expand Up @@ -71,4 +74,19 @@ public static String unavailableMessage(String fieldName, String objectName, Str
String fallbackReason = reason == null || reason.isBlank() ? "unavailable" : reason;
return fieldName + " " + objectName + " is unavailable: " + fallbackReason;
}

public static Optional<String> businessKeyInvalidReason(
TableInfo table, Set<String> physicalColumnNames) {
if (table.getBusinessKeyColumns().isEmpty()) {
return Optional.empty();
}
if (!hasPhysicalTable(table)) {
return Optional.of("数据库对象缺失,业务唯一键已失效");
}
boolean missingColumn =
table.getBusinessKeyColumns().stream()
.map(name -> SemanticUtils.normalizeObjectName(name, "业务唯一键字段不能为空"))
.anyMatch(name -> !physicalColumnNames.contains(name));
return missingColumn ? Optional.of("业务唯一键字段缺失,请重新选择") : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ public List<TablePromptResponse> listVisibleTablesByDomains(
relationsBySourceId.getOrDefault(
table.getId(), List.of()),
tablesById,
availableColumnsByTableId)))
availableColumnsByTableId),
hasUnavailableColumn(
availableColumnsByTableId,
table.getId(),
table.getBusinessKeyColumns())
? List.of()
: table.getBusinessKeyColumns()))
.filter(Objects::nonNull)
.toList();
}
Expand Down
Loading
Loading