From c97e5d75b302ee710638e579fa58092e281bce29 Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 11:03:13 -0400 Subject: [PATCH] queryer: don't emit a parent type-bound for reified-relationship properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A SINGLE reified-relationship property (e.g. invoice.counterparty, person.primary_contact — a property whose type is a relationship subtype) is hydrated by a correlated subquery that joins the relationship table and correlates source_id/target_id = parent.id; its discriminators (the relationship subtype and the target's type CASE) are constrained INSIDE that subquery. For such a property the resolved edge is a REVERSE traversal (forward = false) over a generic relationship FK (fk_relationship_source_entity, destination = entity). compile_ polymorphic_bounds then took the `!edge.forward` branch with poly_col = destination_columns[1] = "type" and table_to_alias = "entity", which resolves to the PARENT's entity alias — emitting a bound like `entity_1.type = 'counterparty'` into the parent WHERE. No parent row has that type, so EVERY parent was dropped (e.g. get_invoice returned null for an existing invoice whose counterparty edge was absent). Array reified traversals (contacts, occupancies) never hit this: an array property has no bound_type_name. Only the single form did, and it was previously unexercised. Fix: in compile_polymorphic_bounds, skip the bound when the property's resolved type is itself a relationship (type_def.relationship) — there is no parent-row discriminator column to bound. Test: new queryer case (person.primary_contact, type=contact) asserts the parent WHERE has no spurious entity.type bound while the subquery keeps its discriminator + source_id correlation. Full suite green. Co-Authored-By: Claude Opus 4.8 (1M context) --- fixtures/queryer.json | 72 +++++++++++++++++++++++++++++++++++++++++ src/queryer/compiler.rs | 11 +++++++ src/tests/fixtures.rs | 6 ++++ 3 files changed, 89 insertions(+) diff --git a/fixtures/queryer.json b/fixtures/queryer.json index 73cec19..f41bb95 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -430,6 +430,19 @@ } } }, + "single.person": { + "type": "person", + "properties": { + "primary_contact": { + "type": "contact", + "properties": { + "target": { + "type": "email_address" + } + } + } + } + }, "full.person": { "type": "person", "properties": { @@ -2525,6 +2538,65 @@ ] ] } + }, + { + "description": "Single reified-relationship property (person.primary_contact, type=contact): discriminator + source correlation live INSIDE the subquery; the parent WHERE must NOT get a spurious entity.type bound (regression: reverse reified edges previously emitted entity_1.type = 'contact', excluding every parent)", + "action": "query", + "schema_id": "single.person", + "expect": { + "success": true, + "sql": [ + [ + "((SELECT jsonb_strip_nulls((", + " SELECT jsonb_build_object(", + " 'id', entity_1.id,", + " 'type', entity_1.type,", + " 'archived', entity_1.archived,", + " 'created_at', entity_1.created_at,", + " 'name', organization_2.name,", + " 'first_name', person_3.first_name,", + " 'last_name', person_3.last_name,", + " 'age', person_3.age,", + " 'primary_contact', (", + " SELECT jsonb_build_object(", + " 'id', entity_4.id,", + " 'type', entity_4.type,", + " 'archived', entity_4.archived,", + " 'created_at', entity_4.created_at,", + " 'is_primary', contact_6.is_primary,", + " 'target', (", + " SELECT jsonb_build_object(", + " 'id', entity_7.id,", + " 'type', entity_7.type,", + " 'archived', entity_7.archived,", + " 'created_at', entity_7.created_at,", + " 'address', email_address_8.address", + " )", + " FROM agreego.entity entity_7", + " JOIN agreego.email_address email_address_8 ON email_address_8.id = entity_7.id", + " WHERE", + " NOT entity_7.archived", + " AND relationship_5.target_id = entity_7.id", + " )", + " )", + " FROM agreego.entity entity_4", + " JOIN agreego.relationship relationship_5 ON relationship_5.id = entity_4.id", + " JOIN agreego.contact contact_6 ON contact_6.id = relationship_5.id", + " WHERE", + " NOT entity_4.archived", + " AND relationship_5.target_type = 'email_address'", + " AND relationship_5.source_id = entity_1.id", + " )", + " )", + " FROM agreego.entity entity_1", + " JOIN agreego.organization organization_2 ON organization_2.id = entity_1.id", + " JOIN agreego.person person_3 ON person_3.id = organization_2.id", + " WHERE", + " NOT entity_1.archived", + "))))" + ] + ] + } } ] } diff --git a/src/queryer/compiler.rs b/src/queryer/compiler.rs index 378e5e3..90ca094 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -604,6 +604,17 @@ impl<'a> Compiler<'a> { if let Some(type_name) = bound_type_name { // Ensure this type actually exists if let Some(type_def) = self.db.types.get(&type_name) { + // A reified-relationship property (e.g. invoice.counterparty, person.primary_contact) + // is hydrated by a correlated subquery that joins the relationship table and correlates + // source_id/target_id = parent.id; its discriminators (the relationship subtype and the + // target's type CASE) are constrained INSIDE that subquery. There is no parent-row column + // to bound here — emitting one wrongly constrains the PARENT entity's own `type` column + // (e.g. `entity_1.type = 'counterparty'`), which no parent row satisfies, dropping every + // parent. (Array reified traversals never reach this code: an array prop has no + // bound_type_name.) So skip the bound entirely for relationship-typed properties. + if type_def.relationship { + continue; + } if let Some(relation) = self.db.relations.get(&edge.constraint) { let mut poly_col = None; let mut table_to_alias = ""; diff --git a/src/tests/fixtures.rs b/src/tests/fixtures.rs index 739849a..3f44e74 100644 --- a/src/tests/fixtures.rs +++ b/src/tests/fixtures.rs @@ -1523,6 +1523,12 @@ fn test_queryer_0_16() { crate::tests::runner::run_test_case(&path, 0, 16).unwrap(); } +#[test] +fn test_queryer_0_17() { + let path = format!("{}/fixtures/queryer.json", env!("CARGO_MANIFEST_DIR")); + crate::tests::runner::run_test_case(&path, 0, 17).unwrap(); +} + #[test] fn test_polymorphism_0_0() { let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR"));