From 0b241c52278ca90c05d41bbf04835dfcf08b7d0c Mon Sep 17 00:00:00 2001 From: Satya Date: Mon, 25 May 2026 18:29:43 -0400 Subject: [PATCH] feat(queryer): support polymorphic inherited queries with IN clause and SQL notices --- fixtures/queryer.json | 2 +- src/queryer/compiler.rs | 20 ++++++++++++++++++-- src/queryer/mod.rs | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/fixtures/queryer.json b/fixtures/queryer.json index ce551f5..0fe8601 100644 --- a/fixtures/queryer.json +++ b/fixtures/queryer.json @@ -2432,7 +2432,7 @@ " JOIN agreego.entity entity_2 ON entity_2.id = order_1.id", " WHERE", " NOT entity_2.archived", - " AND order_1.counterparty_type = 'organization'", + " AND order_1.counterparty_type IN ('bot', 'organization', 'person')", "))))" ] ] diff --git a/src/queryer/compiler.rs b/src/queryer/compiler.rs index b0d9f8c..756e364 100644 --- a/src/queryer/compiler.rs +++ b/src/queryer/compiler.rs @@ -603,7 +603,7 @@ impl<'a> Compiler<'a> { if let Some(type_name) = bound_type_name { // Ensure this type actually exists - if self.db.types.contains_key(&type_name) { + if let Some(type_def) = self.db.types.get(&type_name) { if let Some(relation) = self.db.relations.get(&edge.constraint) { let mut poly_col = None; let mut table_to_alias = ""; @@ -621,7 +621,23 @@ impl<'a> Compiler<'a> { .get(table_to_alias) .or_else(|| type_aliases.get(&node.parent_alias)) { - where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name)); + // DEBUG: See what variations we have for this type + #[cfg(not(test))] + pgrx::notice!("JSPG_POLY_BOUNDS: type={}, col={}, variations_len={}, variations={:?}", type_name, col, type_def.variations.len(), type_def.variations); + + // Use IN clause with all variations to support inherited types. + // e.g., "asset" has variations ["asset", "property", "unit", ...] + // so target_type IN ('asset','property','unit') instead of = 'asset' + 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(", ") + )); + } else { + where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name)); + } } } } diff --git a/src/queryer/mod.rs b/src/queryer/mod.rs index 114ff12..54ff8e3 100644 --- a/src/queryer/mod.rs +++ b/src/queryer/mod.rs @@ -50,6 +50,10 @@ impl Queryer { Err(drop) => return drop, }; + // DEBUG: Emit compiled SQL as a NOTICE for inspection + #[cfg(not(test))] + pgrx::notice!("JSPG_SQL[{}]: {}", schema_id, sql); + // 3. Execute via Database Executor self.execute_sql(schema_id, &sql, args) }