Skip to content

Commit d34f159

Browse files
committed
test(v3): assert scalar scale predicates are selective
1 parent 4af413b commit d34f159

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

tests/sqlx/src/matrix.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,10 +1670,13 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)",
16701670
// Natural bare-operator form: `value {op} <lit>`. This
16711671
// is the inlinability tripwire — a broken inline flips
16721672
// it to Seq Scan.
1673-
let natural = format!(
1674-
"SELECT * FROM {table} WHERE value {op} '{lit}'::jsonb{cast}",
1673+
let natural_predicate = format!(
1674+
"value {op} '{lit}'::jsonb{cast}",
16751675
op = $op, cast = rhs_cast,
16761676
);
1677+
let natural = format!(
1678+
"SELECT * FROM {table} WHERE {natural_predicate}",
1679+
);
16771680
$crate::matrix::assert_index_scan_uses(
16781681
&mut *tx, &natural, index,
16791682
&format!(
@@ -1683,6 +1686,17 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)",
16831686
extractor = extractor, using = $using,
16841687
),
16851688
).await?;
1689+
let matched: i64 = sqlx::query_scalar(&format!(
1690+
"SELECT count(*) FROM {table} WHERE {natural_predicate}",
1691+
))
1692+
.fetch_one(&mut *tx)
1693+
.await?;
1694+
assert_eq!(
1695+
matched, 1,
1696+
"scale: natural-form `{op}` (rhs {cast:?}) must match exactly \
1697+
one row",
1698+
op = $op, cast = rhs_cast,
1699+
);
16861700

16871701
// Explicit extractor form: `{extractor}(value) {op}
16881702
// {extractor}(<lit>)`. Complements the natural form;
@@ -1700,11 +1714,13 @@ SELECT $1::jsonb::{d} FROM generate_series(1, 5000)",
17001714
// signature pins the domain), so skipping it here loses
17011715
// no coverage.
17021716
if !rhs_cast.is_empty() {
1703-
let extracted = format!(
1704-
"SELECT * FROM {table} \
1705-
WHERE {extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})",
1717+
let extracted_predicate = format!(
1718+
"{extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})",
17061719
extractor = extractor, op = $op, cast = rhs_cast,
17071720
);
1721+
let extracted = format!(
1722+
"SELECT * FROM {table} WHERE {extracted_predicate}",
1723+
);
17081724
$crate::matrix::assert_index_scan_uses(
17091725
&mut *tx, &extracted, index,
17101726
&format!(
@@ -1714,6 +1730,17 @@ WHERE {extractor}(value) {op} {extractor}('{lit}'::jsonb{cast})",
17141730
extractor = extractor, using = $using,
17151731
),
17161732
).await?;
1733+
let matched: i64 = sqlx::query_scalar(&format!(
1734+
"SELECT count(*) FROM {table} WHERE {extracted_predicate}",
1735+
))
1736+
.fetch_one(&mut *tx)
1737+
.await?;
1738+
assert_eq!(
1739+
matched, 1,
1740+
"scale: extractor-form `{op}` (rhs {cast:?}) must match \
1741+
exactly one row",
1742+
op = $op, cast = rhs_cast,
1743+
);
17171744
}
17181745
}
17191746
}

0 commit comments

Comments
 (0)