Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsume should succeed even when the tuple is not present #487

Merged
merged 5 commits into from
Feb 3, 2025
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
14 changes: 12 additions & 2 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,20 @@ impl EGraph {
function.remove(args, self.timestamp);
}
Change::Subsume => {
if function.decl.merge.is_some() {
if function.decl.subtype != FunctionSubtype::Constructor
&& function.decl.subtype != FunctionSubtype::Relation
{
return Err(Error::SubsumeMergeError(*f));
}
function.subsume(args);
function
.nodes
.insert_and_merge(args, self.timestamp, true, |old| {
old.unwrap_or_else(|| Value {
#[cfg(debug_assertions)]
tag: function.schema.output.name(),
bits: self.unionfind.make_set(),
})
});
}
}
stack.truncate(new_len);
Expand Down
5 changes: 0 additions & 5 deletions src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,6 @@ impl Function {
res
}

/// Mark the given inputs as subsumed.
pub fn subsume(&mut self, inputs: &[Value]) {
self.nodes.get_mut(inputs).unwrap().subsumed = true;
}

/// Return a column index that contains (a superset of) the offsets for the
/// given column. This method can return nothing if the indexes available
/// contain too many irrelevant offsets.
Expand Down
7 changes: 0 additions & 7 deletions src/function/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ impl Table {
Some(&self.vals[off].1)
}

pub(crate) fn get_mut(&mut self, inputs: &[Value]) -> Option<&mut TupleOutput> {
let hash: u64 = hash_values(inputs);
let &TableOffset { off, .. } = self.table.find(hash, self.search_for(hash, inputs))?;
debug_assert!(self.vals[off].0.live());
Some(&mut self.vals[off].1)
}

/// Insert the given data into the table at the given timestamp. Return the
/// previous value, if there was one.
pub(crate) fn insert(&mut self, inputs: &[Value], out: Value, ts: u32) -> Option<Value> {
Expand Down
30 changes: 27 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,9 @@ fn test_subsume() {
}

#[test]
fn test_subsume_primitive() {
// Test that we can subsume a primitive
fn test_subsume_custom() {
// Test that we can't subsume a custom function
// Only relations and constructors are allowed to be subsumed

let mut egraph = EGraph::default();
let res = egraph.parse_and_run_program(
Expand All @@ -362,6 +363,29 @@ fn test_subsume_primitive() {
(subsume (one))
"#,
);
assert!(res.is_err());
}

#[test]
fn test_subsume_ok() {
let mut egraph = EGraph::default();
let res = egraph.parse_and_run_program(
None,
r#"
(sort E)
(constructor one () E)
(constructor two () E)
(one)
(subsume (one))
;; subsuming a non-existent tuple
(subsume (two))

(relation R (i64))
(R 1)
(subsume (R 1))
(subsume (R 2))
"#,
);
assert!(res.is_ok());
saulshanabrook marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -432,7 +456,7 @@ fn test_serialize_subsume_status() {
None,
egglog::SerializedNode::Function {
name: ("a").into(),
offset: 0,
offset: 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two a functions values now? They are equivalent right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new implementation conceptually makes a new entry to the database with the new timestamp and marks the old entry as stale.

The old implementation directly modifies the old entry, which is bad because the database is supposed to be append-only I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried to do it like delete? If that makes sense? Which does modify a row? Not saying it's right just curious what you think.

Copy link
Collaborator Author

@yihozhang yihozhang Feb 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think remove is doing the following:

  • Delete the entry from the offset table
  • Set the actual tuple in the (append-only) log as stale (self.vals[*off].0.stale_at = ts)

insert_and_merge (which is the safe way of updating the table) does the following:

  • Mark the old tuple in the log as stale
  • Append the new tuple to the log
  • Update the offset table entry to point to the new tuple

While the old subsume implementation:

  • Find the old tuple according to the offset table
  • Update its subsumed flag

So I think it is unsafe (while remove and insert_and_merge are safe)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this and looking into it so closely!!

},
);
let b_id = egraph.to_node_id(
Expand Down