Revert "queryer: NULL-tolerant bound for optional single-family forward relations"

This reverts commit 94477b677d.
This commit is contained in:
2026-06-19 15:34:24 -04:00
parent 98a9719509
commit 7f666e0ece
3 changed files with 11 additions and 121 deletions

View File

@ -70,17 +70,6 @@
} }
} }
} }
},
{
"name": "get_oneof_counterparty_orders",
"schemas": {
"get_oneof_counterparty_orders.response": {
"type": "array",
"items": {
"type": "oneof.order"
}
}
}
} }
], ],
"enums": [], "enums": [],
@ -764,21 +753,6 @@
} }
} }
}, },
"oneof.order": {
"type": "order",
"properties": {
"counterparty": {
"oneOf": [
{
"type": "person"
},
{
"type": "widget"
}
]
}
}
},
"light.order": { "light.order": {
"type": "order", "type": "order",
"properties": { "properties": {
@ -2379,7 +2353,7 @@
} }
}, },
{ {
"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)", "description": "Order select with nested polymorphic counterparty family relation",
"action": "query", "action": "query",
"schema_id": "get_counterparty_orders.response", "schema_id": "get_counterparty_orders.response",
"expect": { "expect": {
@ -2458,69 +2432,7 @@
" JOIN agreego.order order_2 ON order_2.id = entity_1.id", " JOIN agreego.order order_2 ON order_2.id = entity_1.id",
" WHERE", " WHERE",
" NOT entity_1.archived", " 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",
"))))" "))))"
] ]
] ]

View File

@ -574,7 +574,7 @@ impl<'a> Compiler<'a> {
fn compile_polymorphic_bounds( fn compile_polymorphic_bounds(
&self, &self,
r#type: &crate::database::r#type::Type, _type: &crate::database::r#type::Type,
type_aliases: &std::collections::HashMap<String, String>, type_aliases: &std::collections::HashMap<String, String>,
node: &Node, node: &Node,
where_clauses: &mut Vec<String>, where_clauses: &mut Vec<String>,
@ -607,21 +607,10 @@ impl<'a> Compiler<'a> {
if let Some(relation) = self.db.relations.get(&edge.constraint) { if let Some(relation) = self.db.relations.get(&edge.constraint) {
let mut poly_col = None; let mut poly_col = None;
let mut table_to_alias = ""; 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 { if edge.forward && relation.source_columns.len() > 1 {
poly_col = Some(&relation.source_columns[1]); // e.g., target_type poly_col = Some(&relation.source_columns[1]); // e.g., target_type
table_to_alias = &relation.source_type; // e.g., relationship table_to_alias = &relation.source_type; // e.g., relationship
null_tolerant = !r#type.relationship;
} else if !edge.forward && relation.destination_columns.len() > 1 { } else if !edge.forward && relation.destination_columns.len() > 1 {
poly_col = Some(&relation.destination_columns[1]); // e.g., source_type poly_col = Some(&relation.destination_columns[1]); // e.g., source_type
table_to_alias = &relation.destination_type; // e.g., relationship table_to_alias = &relation.destination_type; // e.g., relationship
@ -632,25 +621,20 @@ impl<'a> Compiler<'a> {
.get(table_to_alias) .get(table_to_alias)
.or_else(|| type_aliases.get(&node.parent_alias)) .or_else(|| type_aliases.get(&node.parent_alias))
{ {
let predicate = if type_def.variations.len() > 1 { if type_def.variations.len() > 1 {
let quoted: Vec<String> = type_def let quoted: Vec<String> = type_def
.variations .variations
.iter() .iter()
.map(|v| format!("'{}'", v)) .map(|v| format!("'{}'", v))
.collect(); .collect();
format!("{}.{} IN ({})", alias, col, quoted.join(", ")) where_clauses.push(format!(
"{}.{} IN ({})",
alias,
col,
quoted.join(", ")
));
} else { } else {
format!("{}.{} = '{}'", alias, col, type_name) where_clauses.push(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);
} }
} }
} }

View File

@ -1517,12 +1517,6 @@ fn test_queryer_0_15() {
crate::tests::runner::run_test_case(&path, 0, 15).unwrap(); crate::tests::runner::run_test_case(&path, 0, 15).unwrap();
} }
#[test]
fn test_queryer_0_16() {
let path = format!("{}/fixtures/queryer.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 16).unwrap();
}
#[test] #[test]
fn test_polymorphism_0_0() { fn test_polymorphism_0_0() {
let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR")); let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR"));