queryer: don't emit a parent type-bound for reified-relationship properties
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) <noreply@anthropic.com>
This commit is contained in:
@ -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",
|
||||
"))))"
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user