Skip to content

World's first open-source enterprise world model.

Scope entity degree and fact count subqueries to their knowledge base across store queries. (#1072)

plpycoincd4279a8b4cd
diff --git a/crates/utopia-store/src/governance.rs b/crates/utopia-store/src/governance.rs
index 0eb8af84..2317c264 100644
--- a/crates/utopia-store/src/governance.rs
+++ b/crates/utopia-store/src/governance.rs
@@ -843,7 +843,7 @@ pub async fn namesakes(
let rows: Vec<Namesake> = sqlx::query_as(
"SELECT e.canonical_name AS name, t.label AS type_label,
(SELECT count(*) FROM facts f
- WHERE f.invalidated_at IS NULL
+ WHERE f.kb_id = e.kb_id AND f.invalidated_at IS NULL
AND (f.subject_id = e.id OR f.object_id = e.id)) AS facts,
(e.merged_into IS NOT NULL) AS merged
FROM entities e
diff --git a/crates/utopia-store/src/graph.rs b/crates/utopia-store/src/graph.rs
index ec77e855..d2bf3d82 100644
--- a/crates/utopia-store/src/graph.rs
+++ b/crates/utopia-store/src/graph.rs
@@ -1274,7 +1274,7 @@ fn node_sql(as_of: Option<usize>, owner: Option<usize>) -> String {
coalesce(t.shape, 'circle') AS shape,
e.disambiguator,
(SELECT count(*) FROM facts f
- WHERE ({subject} = e.id OR {object} = e.id) AND {held} AND {not_name}) AS degree
+ WHERE f.kb_id = e.kb_id AND ({subject} = e.id OR {object} = e.id) AND {held} AND {not_name}) AS degree
FROM entities e LEFT JOIN entity_types t ON t.id = e.type_id"
)
}
diff --git a/crates/utopia-store/src/ontology.rs b/crates/utopia-store/src/ontology.rs
index 225cabcf..07d9e105 100644
--- a/crates/utopia-store/src/ontology.rs
+++ b/crates/utopia-store/src/ontology.rs
@@ -39,8 +39,9 @@ pub async fn entity_instances(
let rows: Vec<EntityInstance> = sqlx::query_as(
"SELECT e.id, e.canonical_name AS name,
(SELECT count(*) FROM facts f
- WHERE (f.subject_id = e.id OR f.object_id = e.id)
- AND f.invalidated_at IS NULL) AS fact_count
+ WHERE f.kb_id = e.kb_id
+ AND (f.subject_id = e.id OR f.object_id = e.id)
+ AND f.invalidated_at IS NULL) AS fact_count
FROM entities e
WHERE e.kb_id = $1 AND e.type_id = $2 AND e.merged_into IS NULL
ORDER BY lower(e.canonical_name), e.id
diff --git a/crates/utopia-store/src/resolution.rs b/crates/utopia-store/src/resolution.rs
index d17854a0..ae406c4a 100644
--- a/crates/utopia-store/src/resolution.rs
+++ b/crates/utopia-store/src/resolution.rs
@@ -337,7 +337,7 @@ async fn resolve_by_name(
let candidates: Vec<Candidate> = sqlx::query_as(&format!(
"SELECT e.id, e.canonical_name, e.profile_embedding, e.profile_n,
(SELECT count(*) FROM facts f
- WHERE (f.subject_id = e.id OR f.object_id = e.id)
+ WHERE f.kb_id = e.kb_id AND (f.subject_id = e.id OR f.object_id = e.id)
AND f.invalidated_at IS NULL AND {not_name}) AS degree
FROM entities e
-- IS NOT DISTINCT FROM 而不是 =(0009 的那个陷阱):开放图谱里的实体都没有类
@@ -1254,7 +1254,7 @@ pub async fn existing_by_name(
AND e.description IS NULL
AND (lower(e.canonical_name) = ANY($2) OR {named})
ORDER BY (SELECT count(*) FROM facts f
- WHERE (f.subject_id = e.id OR f.object_id = e.id) AND {not_name}) DESC,
+ WHERE f.kb_id = e.kb_id AND (f.subject_id = e.id OR f.object_id = e.id) AND f.invalidated_at IS NULL AND {not_name}) DESC,
e.created_at
LIMIT 1",
named = crate::names::has_name_in("e", 1, 2),
@@ -1398,7 +1398,7 @@ async fn review_side(pool: &PgPool, kb_id: Uuid, entity_id: Uuid) -> AppResult<R
"SELECT e.id, e.canonical_name AS name, t.label AS type_label,
coalesce(t.color, '#94a3b8') AS color, e.disambiguator,
(SELECT count(*) FROM facts f
- WHERE (f.subject_id = e.id OR f.object_id = e.id)
+ WHERE f.kb_id = e.kb_id AND (f.subject_id = e.id OR f.object_id = e.id)
AND f.invalidated_at IS NULL AND {not_name}) AS degree
-- LEFT JOIN:没判出类型的实体照样要能进审核(0009)。
-- 内连接会让它整条审核项取不出来,而漂移审核恰恰最常发生在它们身上