queryer: NULL-tolerant bound for optional single-family forward relations
An OPTIONAL forward polymorphic relation declared via `family` (e.g.
order.counterparty / invoice.counterparty) had its auto-generated type
bound `{alias}.{disc} IN (variations)` emitted into the PARENT row's
WHERE. When the relation is absent (NULL discriminator) the parent row
was wrongly excluded — a counterparty-less order/invoice returned
nothing instead of the row with `counterparty: null`.
Fix: in `compile_polymorphic_bounds`, make the forward-FK bound
NULL-tolerant (`(… IN (…) OR …_type IS NULL)`), gated on
`!r#type.relationship`:
- real entities get NULL-tolerance (the relation is an optional
attribute; an absent one must not drop the row — the inner CASE
already resolves it to NULL);
- edge entities (`relationship == true`, e.g. `contact`) keep the
bound EXACT, because there source_type/target_type *partition*
typed sub-collections (phone_numbers vs email_addresses) and a NULL
endpoint belongs to no partition.
Note: the `oneOf` path was already correct — it emits no parent bound
(resolves via CASE … ELSE NULL), so cross-family optional relations
already hydrate NULL-safely. Added a fixture case documenting that.
Tests (fixtures/queryer.json): case 15 (entity → NULL-tolerant),
cases 3/5/10 (contact edges → exact, unchanged), new case 16
(oneOf cross-family → no bound). Full suite green.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -70,6 +70,17 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "get_oneof_counterparty_orders",
|
||||
"schemas": {
|
||||
"get_oneof_counterparty_orders.response": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "oneof.order"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"enums": [],
|
||||
@ -753,6 +764,21 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"oneof.order": {
|
||||
"type": "order",
|
||||
"properties": {
|
||||
"counterparty": {
|
||||
"oneOf": [
|
||||
{
|
||||
"type": "person"
|
||||
},
|
||||
{
|
||||
"type": "widget"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"light.order": {
|
||||
"type": "order",
|
||||
"properties": {
|
||||
@ -2353,7 +2379,7 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Order select with nested polymorphic counterparty family relation",
|
||||
"description": "Order select with nested polymorphic counterparty family relation; the optional forward FK bound is NULL-tolerant so orders without a counterparty are still returned (contrast the contact-array cases, where the edge discriminator stays exact)",
|
||||
"action": "query",
|
||||
"schema_id": "get_counterparty_orders.response",
|
||||
"expect": {
|
||||
@ -2432,7 +2458,69 @@
|
||||
" JOIN agreego.order order_2 ON order_2.id = entity_1.id",
|
||||
" WHERE",
|
||||
" NOT entity_1.archived",
|
||||
" AND order_2.counterparty_type IN ('bot', 'organization', 'person')",
|
||||
" AND (order_2.counterparty_type IN ('bot', 'organization', 'person')",
|
||||
" OR order_2.counterparty_type IS NULL)",
|
||||
"))))"
|
||||
]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "Order select with optional cross-family oneOf counterparty (person | widget); the forward FK bound is NULL-tolerant so orders without a counterparty are still returned",
|
||||
"action": "query",
|
||||
"schema_id": "get_oneof_counterparty_orders.response",
|
||||
"expect": {
|
||||
"success": true,
|
||||
"sql": [
|
||||
[
|
||||
"((SELECT jsonb_strip_nulls((",
|
||||
" SELECT COALESCE(jsonb_agg(jsonb_build_object(",
|
||||
" 'id', order_2.id,",
|
||||
" 'type', order_2.type,",
|
||||
" 'archived', entity_1.archived,",
|
||||
" 'created_at', entity_1.created_at,",
|
||||
" 'total', order_2.total,",
|
||||
" 'customer_id', order_2.customer_id,",
|
||||
" 'counterparty', CASE",
|
||||
" WHEN order_2.counterparty_type = 'person' THEN ((",
|
||||
" SELECT jsonb_build_object(",
|
||||
" 'id', entity_3.id,",
|
||||
" 'type', entity_3.type,",
|
||||
" 'archived', entity_3.archived,",
|
||||
" 'created_at', entity_3.created_at,",
|
||||
" 'name', organization_4.name,",
|
||||
" 'first_name', person_5.first_name,",
|
||||
" 'last_name', person_5.last_name,",
|
||||
" 'age', person_5.age",
|
||||
" )",
|
||||
" FROM agreego.entity entity_3",
|
||||
" JOIN agreego.organization organization_4 ON organization_4.id = entity_3.id",
|
||||
" JOIN agreego.person person_5 ON person_5.id = organization_4.id",
|
||||
" WHERE",
|
||||
" NOT entity_3.archived",
|
||||
" AND order_2.counterparty_id = entity_3.id",
|
||||
" ))",
|
||||
" WHEN order_2.counterparty_type = 'widget' THEN ((",
|
||||
" SELECT jsonb_build_object(",
|
||||
" 'id', entity_6.id,",
|
||||
" 'type', entity_6.type,",
|
||||
" 'archived', entity_6.archived,",
|
||||
" 'created_at', entity_6.created_at,",
|
||||
" 'kind', widget_7.kind",
|
||||
" )",
|
||||
" FROM agreego.entity entity_6",
|
||||
" JOIN agreego.widget widget_7 ON widget_7.id = entity_6.id",
|
||||
" WHERE",
|
||||
" NOT entity_6.archived",
|
||||
" AND order_2.counterparty_id = entity_6.id",
|
||||
" ))",
|
||||
" ELSE NULL",
|
||||
" END",
|
||||
" )), '[]'::jsonb)",
|
||||
" FROM agreego.entity entity_1",
|
||||
" JOIN agreego.order order_2 ON order_2.id = entity_1.id",
|
||||
" WHERE",
|
||||
" NOT entity_1.archived",
|
||||
"))))"
|
||||
]
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user