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"));