From d34f159b88198bfd8c0413c105e34b450f786208 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Fri, 19 Jun 2026 23:29:37 +1000 Subject: [PATCH] test(v3): assert scalar scale predicates are selective --- tests/sqlx/src/matrix.rs | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/tests/sqlx/src/matrix.rs b/tests/sqlx/src/matrix.rs index 09c3b75e..eae042a9 100644 --- a/tests/sqlx/src/matrix.rs +++ b/tests/sqlx/src/matrix.rs @@ -1670,10 +1670,13 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)", // Natural bare-operator form: `value {op} `. This // is the inlinability tripwire — a broken inline flips // it to Seq Scan. - let natural = format!( - "SELECT * FROM {table} WHERE value {op} '{lit}'::jsonb{cast}", + let natural_predicate = format!( + "value {op} '{lit}'::jsonb{cast}", op = $op, cast = rhs_cast, ); + let natural = format!( + "SELECT * FROM {table} WHERE {natural_predicate}", + ); $crate::matrix::assert_index_scan_uses( &mut *tx, &natural, index, &format!( @@ -1683,6 +1686,17 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)", extractor = extractor, using = $using, ), ).await?; + let matched: i64 = sqlx::query_scalar(&format!( + "SELECT count(*) FROM {table} WHERE {natural_predicate}", + )) + .fetch_one(&mut *tx) + .await?; + assert_eq!( + matched, 1, + "scale: natural-form `{op}` (rhs {cast:?}) must match exactly \ +one row", + op = $op, cast = rhs_cast, + ); // Explicit extractor form: `{extractor}(value) {op} // {extractor}()`. Complements the natural form; @@ -1700,11 +1714,13 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)", // signature pins the domain), so skipping it here loses // no coverage. if !rhs_cast.is_empty() { - let extracted = format!( - "SELECT * FROM {table} \ -WHERE {extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})", + let extracted_predicate = format!( + "{extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})", extractor = extractor, op = $op, cast = rhs_cast, ); + let extracted = format!( + "SELECT * FROM {table} WHERE {extracted_predicate}", + ); $crate::matrix::assert_index_scan_uses( &mut *tx, &extracted, index, &format!( @@ -1714,6 +1730,17 @@ WHERE {extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})", extractor = extractor, using = $using, ), ).await?; + let matched: i64 = sqlx::query_scalar(&format!( + "SELECT count(*) FROM {table} WHERE {extracted_predicate}", + )) + .fetch_one(&mut *tx) + .await?; + assert_eq!( + matched, 1, + "scale: extractor-form `{op}` (rhs {cast:?}) must match \ +exactly one row", + op = $op, cast = rhs_cast, + ); } } }