From 94477b677d5beb3aa6a0432e58770f1e4ef1361a Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 10:22:29 -0400 Subject: [PATCH 1/6] queryer: NULL-tolerant bound for optional single-family forward relations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- fixtures/queryer.json | 92 ++++++++++++++++++++++++++++++++++++++++- src/queryer/compiler.rs | 34 +++++++++++---- src/tests/fixtures.rs | 6 +++ 3 files changed, 121 insertions(+), 11 deletions(-) diff --git a/fixtures/queryer.json b/fixtures/queryer.json index e09ed07..73cec19 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -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", "))))" ] ] diff --git a/src/queryer/compiler.rs b/src/queryer/compiler.rs index 9108454..378e5e3 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -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, node: &Node, where_clauses: &mut Vec, @@ -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 = 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); } } } diff --git a/src/tests/fixtures.rs b/src/tests/fixtures.rs index a7c4136..739849a 100644 --- a/src/tests/fixtures.rs +++ b/src/tests/fixtures.rs @@ -1517,6 +1517,12 @@ fn test_queryer_0_15() { 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] fn test_polymorphism_0_0() { let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR")); From c97e5d75b302ee710638e579fa58092e281bce29 Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 11:03:13 -0400 Subject: [PATCH 2/6] queryer: don't emit a parent type-bound for reified-relationship properties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- fixtures/queryer.json | 72 +++++++++++++++++++++++++++++++++++++++++ src/queryer/compiler.rs | 11 +++++++ src/tests/fixtures.rs | 6 ++++ 3 files changed, 89 insertions(+) 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")); From 0509995589a63b05e3e0a17086cab881b6893e6b Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 11:08:52 -0400 Subject: [PATCH 3/6] version: 1.0.161 --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index ad18496..15bcce9 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.0.160 +1.0.161 From 98a9719509462e96855d9e423a663b0e8a1a1ba9 Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 15:34:23 -0400 Subject: [PATCH 4/6] Revert "queryer: don't emit a parent type-bound for reified-relationship properties" This reverts commit c97e5d75b302ee710638e579fa58092e281bce29. --- fixtures/queryer.json | 72 ----------------------------------------- src/queryer/compiler.rs | 11 ------- src/tests/fixtures.rs | 6 ---- 3 files changed, 89 deletions(-) diff --git a/fixtures/queryer.json b/fixtures/queryer.json index f41bb95..73cec19 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -430,19 +430,6 @@ } } }, - "single.person": { - "type": "person", - "properties": { - "primary_contact": { - "type": "contact", - "properties": { - "target": { - "type": "email_address" - } - } - } - } - }, "full.person": { "type": "person", "properties": { @@ -2538,65 +2525,6 @@ ] ] } - }, - { - "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 90ca094..378e5e3 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -604,17 +604,6 @@ 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 3f44e74..739849a 100644 --- a/src/tests/fixtures.rs +++ b/src/tests/fixtures.rs @@ -1523,12 +1523,6 @@ 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")); From 7f666e0ece8ddeef4a757c60a9ab6ec57b6fc115 Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 15:34:24 -0400 Subject: [PATCH 5/6] Revert "queryer: NULL-tolerant bound for optional single-family forward relations" This reverts commit 94477b677d5beb3aa6a0432e58770f1e4ef1361a. --- fixtures/queryer.json | 92 +---------------------------------------- src/queryer/compiler.rs | 34 ++++----------- src/tests/fixtures.rs | 6 --- 3 files changed, 11 insertions(+), 121 deletions(-) diff --git a/fixtures/queryer.json b/fixtures/queryer.json index 73cec19..e09ed07 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -70,17 +70,6 @@ } } } - }, - { - "name": "get_oneof_counterparty_orders", - "schemas": { - "get_oneof_counterparty_orders.response": { - "type": "array", - "items": { - "type": "oneof.order" - } - } - } } ], "enums": [], @@ -764,21 +753,6 @@ } } }, - "oneof.order": { - "type": "order", - "properties": { - "counterparty": { - "oneOf": [ - { - "type": "person" - }, - { - "type": "widget" - } - ] - } - } - }, "light.order": { "type": "order", "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", "schema_id": "get_counterparty_orders.response", "expect": { @@ -2458,69 +2432,7 @@ " 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')", - " 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", + " AND order_2.counterparty_type IN ('bot', 'organization', 'person')", "))))" ] ] diff --git a/src/queryer/compiler.rs b/src/queryer/compiler.rs index 378e5e3..9108454 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -574,7 +574,7 @@ impl<'a> Compiler<'a> { fn compile_polymorphic_bounds( &self, - r#type: &crate::database::r#type::Type, + _type: &crate::database::r#type::Type, type_aliases: &std::collections::HashMap, node: &Node, where_clauses: &mut Vec, @@ -607,21 +607,10 @@ 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 @@ -632,25 +621,20 @@ impl<'a> Compiler<'a> { .get(table_to_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 = type_def .variations .iter() .map(|v| format!("'{}'", v)) .collect(); - format!("{}.{} IN ({})", alias, col, quoted.join(", ")) + where_clauses.push(format!( + "{}.{} IN ({})", + alias, + col, + quoted.join(", ") + )); } else { - 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); + where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name)); } } } diff --git a/src/tests/fixtures.rs b/src/tests/fixtures.rs index 739849a..a7c4136 100644 --- a/src/tests/fixtures.rs +++ b/src/tests/fixtures.rs @@ -1517,12 +1517,6 @@ fn test_queryer_0_15() { 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] fn test_polymorphism_0_0() { let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR")); From 10e388421da8dce8cfbfe05b74577f8993aafbb5 Mon Sep 17 00:00:00 2001 From: Satya Date: Fri, 19 Jun 2026 15:35:01 -0400 Subject: [PATCH 6/6] version: 1.0.162 --- version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version b/version index 15bcce9..b74bd0d 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.0.161 +1.0.162