FFFF perf(query): fix O(n²) Or/OrJoin deduplication (issue #35) by adityamukho · Pull Request #68 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
12 changes: 10 additions & 2 deletions src/query/datalog/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,9 @@ pub(crate) fn apply_or_clauses(
for clause in clauses {
match clause {
WhereClause::Or(branches) => {
let mut seen: std::collections::BTreeSet<
Vec<(String, crate::graph::types::Value)>,
> = std::collections::BTreeSet::new();
let mut result: Vec<Binding> = Vec::new();
for branch in branches {
let branch_result = evaluate_branch(
Expand All @@ -1160,7 +1163,8 @@ pub(crate) fn apply_or_clauses(
valid_at.clone(),
)?;
for b in branch_result {
if !result.contains(&b) {
let key: Vec<_> = b.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
if seen.insert(key) {
result.push(b);
}
}
Expand All @@ -1175,6 +1179,9 @@ pub(crate) fn apply_or_clauses(
let outer_keys: std::collections::HashSet<String> =
bindings.iter().flat_map(|b| b.keys().cloned()).collect();

let mut seen: std::collections::BTreeSet<
Vec<(String, crate::graph::types::Value)>,
> = std::collections::BTreeSet::new();
let mut result: Vec<Binding> = Vec::new();
for branch in branches {
let branch_result = evaluate_branch(
Expand All @@ -1195,7 +1202,8 @@ pub(crate) fn apply_or_clauses(
// (1) the parser enforces join_vars ⊆ outer_bound, so join_vars ⊆ outer_keys, and
// (2) retaining outer_keys is safe because those variables were stable before the or-join.
b.retain(|k, _| outer_keys.contains(k));
if !result.contains(&b) {
let key: Vec<_> = b.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
if seen.insert(key) {
result.push(b);
}
}
Expand Down
Loading
0