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:
@ -574,7 +574,7 @@ impl<'a> Compiler<'a> {
|
||||
|
||||
fn compile_polymorphic_bounds(
|
||||
&self,
|
||||
_type: &crate::database::r#type::Type,
|
||||
r#type: &crate::database::r#type::Type,
|
||||
type_aliases: &std::collections::HashMap<String, String>,
|
||||
node: &Node,
|
||||
where_clauses: &mut Vec<String>,
|
||||
@ -607,10 +607,21 @@ impl<'a> Compiler<'a> {
|
||||
if let Some(relation) = self.db.relations.get(&edge.constraint) {
|
||||
let mut poly_col = None;
|
||||
let mut table_to_alias = "";
|
||||
// An OPTIONAL forward polymorphic relation on a real entity (e.g.
|
||||
// invoice.counterparty, order.counterparty) keeps its discriminator column on the
|
||||
// entity row itself; the auto-generated type bound then sits in that row's WHERE
|
||||
// and would wrongly drop the row when the relation is absent (NULL discriminator).
|
||||
// Such a row must still be returned (with the property resolving to null), so the
|
||||
// bound is made NULL-tolerant below. This does NOT apply to edge entities
|
||||
// (relationship == true): there the same source_type/target_type columns
|
||||
// partition typed sub-collections (phone_numbers vs email_addresses), so the
|
||||
// bound must stay exact — a NULL endpoint genuinely belongs to no partition.
|
||||
let mut null_tolerant = false;
|
||||
|
||||
if edge.forward && relation.source_columns.len() > 1 {
|
||||
poly_col = Some(&relation.source_columns[1]); // e.g., target_type
|
||||
table_to_alias = &relation.source_type; // e.g., relationship
|
||||
null_tolerant = !r#type.relationship;
|
||||
} else if !edge.forward && relation.destination_columns.len() > 1 {
|
||||
poly_col = Some(&relation.destination_columns[1]); // e.g., source_type
|
||||
table_to_alias = &relation.destination_type; // e.g., relationship
|
||||
@ -621,20 +632,25 @@ impl<'a> Compiler<'a> {
|
||||
.get(table_to_alias)
|
||||
.or_else(|| type_aliases.get(&node.parent_alias))
|
||||
{
|
||||
if type_def.variations.len() > 1 {
|
||||
let predicate = if type_def.variations.len() > 1 {
|
||||
let quoted: Vec<String> = type_def
|
||||
.variations
|
||||
.iter()
|
||||
.map(|v| format!("'{}'", v))
|
||||
.collect();
|
||||
where_clauses.push(format!(
|
||||
"{}.{} IN ({})",
|
||||
alias,
|
||||
col,
|
||||
quoted.join(", ")
|
||||
));
|
||||
format!("{}.{} IN ({})", alias, col, quoted.join(", "))
|
||||
} else {
|
||||
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));
|
||||
format!("{}.{} = '{}'", alias, col, type_name)
|
||||
};
|
||||
|
||||
// NULL-tolerant for parent-level (forward FK) filters: an OPTIONAL
|
||||
// polymorphic relation that is absent (NULL discriminator) must NOT exclude
|
||||
// the parent — the inner CASE resolves it to NULL. (e.g. an invoice with no
|
||||
// counterparty yet must still be returned, with counterparty = null.)
|
||||
if null_tolerant {
|
||||
where_clauses.push(format!("({} OR {}.{} IS NULL)", predicate, alias, col));
|
||||
} else {
|
||||
where_clauses.push(predicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user