@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55use eql_domains:: { Domain , DomainFamily , Role , Term } ;
66
77use crate :: context:: { domain_name, is_ord_capable, query_domain_name} ;
8- use crate :: operator_surface:: OPERATORS ;
8+ use crate :: operator_surface:: { Operator , OperatorSignature , TypeSlot , OPERATORS } ;
99
1010/// REQUIRE edge for the v3 schema file — pulled in by every generated file.
1111const V3_SCHEMA : & str = "src/v3/schema.sql" ;
@@ -248,18 +248,67 @@ fn query_types_path(family_name: &str) -> String {
248248 scalar_path ( family_name, & format ! ( "query_{family_name}_types.sql" ) )
249249}
250250
251+ /// The family's term-bearing storage domains OTHER than `domain` — the operands
252+ /// that must never be compared with `domain`'s query twin. Storage-only domains
253+ /// (no terms, no operators) are left out: every pair costs two schema objects
254+ /// (see [`cross_pair_operators`]).
255+ fn cross_pair_domains ( spec : & DomainFamily , domain : & Domain ) -> Vec < & ' static Domain > {
256+ spec. domains
257+ . iter ( )
258+ . filter ( |d| d. is_scalar ( ) && d. name != domain. name && !d. terms . is_empty ( ) )
259+ . collect ( )
260+ }
261+
262+ /// The operators (with their `(domain, domain)` signature) blocked for a
263+ /// `(sibling storage domain, this domain's query twin)` pair: those BOTH domains
264+ /// advertise. A query twin of a different domain is a DIFFERENT index family, so
265+ /// there is no sound comparison to offer — the pair raises rather than flatten to
266+ /// native `jsonb <op> jsonb`.
267+ ///
268+ /// The intersection, not the union, is deliberate. Each blocked pair costs a
269+ /// function and an operator, and the uninstaller takes a lock per object against
270+ /// a stock budget it already approaches (docs/upgrading/v3.0.md, U-009), so the
271+ /// set is held to the operators a caller can reasonably believe both sides
272+ /// support. The remaining unblocked cases name an operator one side does not
273+ /// support at all, which is a second mistake on top of the wrong query domain.
274+ fn cross_pair_operators (
275+ domain : & Domain ,
276+ sibling : & Domain ,
277+ ) -> Vec < ( & ' static Operator , OperatorSignature ) > {
278+ let ours = Term :: operators_for_terms ( domain. terms ) ;
279+ let theirs = Term :: operators_for_terms ( sibling. terms ) ;
280+ OPERATORS
281+ . iter ( )
282+ . filter ( |op| ours. contains ( & op. symbol . as_str ( ) ) && theirs. contains ( & op. symbol . as_str ( ) ) )
283+ . filter_map ( |op| {
284+ op. signatures
285+ . iter ( )
286+ . find ( |s| s. left == TypeSlot :: Domain && s. right == TypeSlot :: Domain )
287+ . map ( |s| ( op, * s) )
288+ } )
289+ . collect ( )
290+ }
291+
251292/// Body for a term-bearing domain's query_<name>_functions.sql: the
252293/// query-operand extractor OVERLOADS (the same extractors, on
253294/// `eql_v3.query_<name>`) plus the comparison WRAPPERS binding the storage
254295/// domain to its query twin — for the domain's SUPPORTED operators only, in
255296/// both directions. Reuses the same `functions.sql` template as the storage
256297/// surface; a query operand carries the same terms, so each wrapper compares
257298/// `extractor(a)` to `extractor(b)` with no ciphertext cast.
258- pub fn render_query_functions_file ( family_name : & str , domain : & Domain ) -> String {
299+ ///
300+ /// `siblings` are the family's OTHER scalar storage domains (see
301+ /// [`cross_pair_domains`]). Each gets exact-signature blockers against this
302+ /// query twin in both directions — see [`cross_pair_operators`].
303+ pub fn render_query_functions_file (
304+ family_name : & str ,
305+ domain : & Domain ,
306+ siblings : & [ & Domain ] ,
307+ ) -> String {
259308 use crate :: consts:: sql_str;
260309 use crate :: context:: {
261- domain_name, environment, extractor_entry, query_domain_name , wrapper_entry ,
262- FunctionsContext ,
310+ domain_name, environment, extractor_entry, mismatch_blocker_entry , query_domain_name ,
311+ wrapper_entry , FunctionsContext , SqlParam ,
263312 } ;
264313 let name = domain. full_name ( family_name) ;
265314 let query_name = domain. query_name ( family_name) ;
@@ -277,14 +326,12 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
277326 // for supported operators only (a query operand is never sent for a blocked
278327 // operator). `is_supported(op) ⟹ term_for_operator is Some`.
279328 //
280- // Query twins deliberately emit NO blockers (unlike the storage surface). A
281- // query operand only ever appears as the RHS of `col <op> operand`, and for
282- // an unsupported `<op>` that predicate resolves against the STORAGE domain's
283- // `(<storage>, jsonb)` blocker (the query operand degrades to its `jsonb`
284- // base), which raises — so the realistic path is already protected. The only
285- // unblocked cases are nonsensical `operand <op> operand` / `operand <op>
286- // jsonb`, which no caller writes; blocking them would mean emitting the full
287- // blocker matrix against every query twin for zero real-world coverage.
329+ // Query twins emit no blockers against their OWN storage domain or `jsonb`:
330+ // a query operand only ever appears as the RHS of `col <op> operand`, and
331+ // for an unsupported `<op>` that predicate resolves against the STORAGE
332+ // domain's `(<storage>, jsonb)` blocker, which raises. The pairs that are
333+ // NOT covered that way are a DIFFERENT domain's operand against this twin,
334+ // handled by the cross-pair blockers below.
288335 for op in OPERATORS {
289336 if !supported. contains ( & op. symbol . as_str ( ) ) {
290337 continue ;
@@ -307,6 +354,35 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
307354 ) ) ;
308355 }
309356
357+ // Cross-pair blockers: a DIFFERENT domain of this family against this query
358+ // twin. Both are domains over `jsonb`, so without an exact signature
359+ // PostgreSQL flattens the pair to native `jsonb <op> jsonb` and answers
360+ // silently (`eql_v3_text_ord = query_text_eq` is false for equal values).
361+ for sibling in siblings {
362+ let sibling_dom = domain_name ( & sibling. full_name ( family_name) ) ;
363+ for ( op, sig) in cross_pair_operators ( domain, sibling) {
364+ for ( left, right) in [
365+ ( sibling_dom. clone ( ) , query_dom. clone ( ) ) ,
366+ ( query_dom. clone ( ) , sibling_dom. clone ( ) ) ,
367+ ] {
368+ entries. push ( mismatch_blocker_entry (
369+ op,
370+ [
371+ SqlParam {
372+ name : "a" ,
373+ ty : left,
374+ } ,
375+ SqlParam {
376+ name : arg_b_name ( op. symbol . as_str ( ) ) ,
377+ ty : right,
378+ } ,
379+ ] ,
380+ & sig. render ( & query_dom) . returns ,
381+ ) ) ;
382+ }
383+ }
384+ }
385+
310386 let ctx = FunctionsContext {
311387 requires : vec ! [
312388 V3_SCHEMA . to_string( ) ,
@@ -330,7 +406,14 @@ pub fn render_query_functions_file(family_name: &str, domain: &Domain) -> String
330406/// `CREATE OPERATOR` binding `(storage_domain, query_<name>)` for every
331407/// supported operator, plus its `(query_<name>, storage_domain)` commutator, so
332408/// `col <op> $1::eql_v3.query_<name>` resolves to the query wrapper.
333- pub fn render_query_operators_file ( family_name : & str , domain : & Domain ) -> String {
409+ ///
410+ /// `siblings` as for [`render_query_functions_file`]: each binds a blocker for
411+ /// the `(sibling, query)` pair and its commutator.
412+ pub fn render_query_operators_file (
413+ family_name : & str ,
414+ domain : & Domain ,
415+ siblings : & [ & Domain ] ,
416+ ) -> String {
334417 use crate :: context:: {
335418 domain_name, environment, operator_entry, query_domain_name, OperatorsContext ,
336419 } ;
@@ -348,7 +431,13 @@ pub fn render_query_operators_file(family_name: &str, domain: &Domain) -> String
348431 operators. push ( operator_entry ( op, & storage_dom, & query_dom, true ) ) ;
349432 operators. push ( operator_entry ( op, & query_dom, & storage_dom, true ) ) ;
350433 }
351-
434+ for sibling in siblings {
435+ let sibling_dom = domain_name ( & sibling. full_name ( family_name) ) ;
436+ for ( op, _) in cross_pair_operators ( domain, sibling) {
437+ operators. push ( operator_entry ( op, & sibling_dom, & query_dom, false ) ) ;
438+ operators. push ( operator_entry ( op, & query_dom, & sibling_dom, false ) ) ;
439+ }
440+ }
352441 let ctx = OperatorsContext {
353442 requires : vec ! [
354443 V3_SCHEMA . to_string( ) ,
@@ -947,11 +1036,11 @@ pub fn render_type(spec: &DomainFamily, out_dir: &Path) -> Vec<(PathBuf, String)
9471036 let query_name = d. query_name ( family_name) ;
9481037 rendered. push ( (
9491038 out_dir. join ( format ! ( "{query_name}_functions.sql" ) ) ,
950- render_query_functions_file ( family_name, d) ,
1039+ render_query_functions_file ( family_name, d, & cross_pair_domains ( spec , d ) ) ,
9511040 ) ) ;
9521041 rendered. push ( (
9531042 out_dir. join ( format ! ( "{query_name}_operators.sql" ) ) ,
954- render_query_operators_file ( family_name, d) ,
1043+ render_query_operators_file ( family_name, d, & cross_pair_domains ( spec , d ) ) ,
9551044 ) ) ;
9561045 }
9571046 if let Some ( agg) = render_aggregates_file ( family_name, d) {
@@ -1127,6 +1216,51 @@ pub fn clean_all(out_root: &Path) -> Result<Vec<PathBuf>, WriteError> {
11271216
11281217#[ cfg( test) ]
11291218mod tests {
1219+ /// A query twin carries blockers for every OTHER term-bearing
1220+ /// domain of its family (both directions, only operators both sides
1221+ /// advertise) and none for the family's own storage domain or storage-only
1222+ /// domains, so a mismatched pair raises instead of flattening to `jsonb`.
1223+ #[ test]
1224+ fn query_twin_blocks_other_domains_of_its_family ( ) {
1225+ let text = eql_domains:: CATALOG
1226+ . iter ( )
1227+ . find ( |f| f. name == "text" )
1228+ . expect ( "text family" ) ;
1229+ let eq = text. domains . iter ( ) . find ( |d| d. name == "eq" ) . unwrap ( ) ;
1230+ let ord = text. domains . iter ( ) . find ( |d| d. name == "ord" ) . unwrap ( ) ;
1231+ let siblings = cross_pair_domains ( text, eq) ;
1232+ assert ! ( siblings
1233+ . iter( )
1234+ . all( |d| d. name != "eq" && !d. terms. is_empty( ) ) ) ;
1235+ assert ! ( siblings. iter( ) . any( |d| d. name == "ord" ) ) ;
1236+
1237+ let sql = render_query_functions_file ( "text" , eq, & siblings) ;
1238+ for ( l, r) in [
1239+ ( "public.eql_v3_text_ord" , "eql_v3.query_text_eq" ) ,
1240+ ( "eql_v3.query_text_eq" , "public.eql_v3_text_ord" ) ,
1241+ ] {
1242+ assert ! (
1243+ sql. contains( & format!( "eql_v3_internal.eq(a {l}, b {r})" ) ) ,
1244+ "missing {l} = {r} blocker"
1245+ ) ;
1246+ }
1247+ // query_text_eq advertises only `=`/`<>`: no ordering blocker.
1248+ assert ! ( !sql. contains( "eql_v3_internal.lt(a public.eql_v3_text_ord" ) ) ;
1249+ // Never the own twin, never the storage-only base domain.
1250+ assert ! ( !sql. contains( "eql_v3_internal.eq(a public.eql_v3_text_eq" ) ) ;
1251+ assert ! ( !sql. contains( "eql_v3_internal.eq(a public.eql_v3_text," ) ) ;
1252+
1253+ let ops = render_query_operators_file ( "text" , eq, & siblings) ;
1254+ assert ! ( ops. contains(
1255+ "FUNCTION = eql_v3_internal.eq,\n LEFTARG = public.eql_v3_text_ord, RIGHTARG = eql_v3.query_text_eq\n "
1256+ ) ) ;
1257+
1258+ // The ordering twin gets ordering blockers against `search`.
1259+ let ord_sql = render_query_functions_file ( "text" , ord, & cross_pair_domains ( text, ord) ) ;
1260+ assert ! ( ord_sql
1261+ . contains( "eql_v3_internal.lt(a public.eql_v3_text_search, b eql_v3.query_text_ord)" ) ) ;
1262+ }
1263+
11301264 use super :: * ;
11311265 use eql_domains:: CATALOG ;
11321266
@@ -1220,7 +1354,7 @@ mod tests {
12201354 let s = spec ( "integer" ) ;
12211355 let d = domain ( s, "ord_ore" ) ;
12221356 let storage = render_functions_file ( "integer" , d) ;
1223- let query = render_query_functions_file ( "integer" , d) ;
1357+ let query = render_query_functions_file ( "integer" , d, & cross_pair_domains ( s , d ) ) ;
12241358 for op in [ "=" , "<>" , "<" , "<=" , ">" , ">=" ] {
12251359 let body = format ! (
12261360 "SELECT eql_v3.ord_term_ore(a) OPERATOR(public.{op}) eql_v3.ord_term_ore(b) $$;"
0 commit comments