Skip to content

Commit 1cb7c18

Browse files
committed
test(gin): add before/after test for GIN index usage
Verify that containment queries use Seq Scan before index creation and switch to using the GIN index after creation. Exports explain_query and assert_uses_seq_scan helpers to support this test pattern.
1 parent b5e840f commit 1cb7c18

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tests/sqlx/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ pub mod selectors;
1111

1212
pub use assertions::QueryAssertion;
1313
pub use helpers::{
14-
analyze_table, assert_uses_index, create_jsonb_gin_index, get_encrypted_term,
15-
get_ore_encrypted, get_ore_encrypted_as_jsonb, get_ste_vec_encrypted,
16-
get_ste_vec_selector_term, get_ste_vec_term_by_id,
14+
analyze_table, assert_uses_index, assert_uses_seq_scan, create_jsonb_gin_index,
15+
explain_query, get_encrypted_term, get_ore_encrypted, get_ore_encrypted_as_jsonb,
16+
get_ste_vec_encrypted, get_ste_vec_selector_term, get_ste_vec_term_by_id,
1717
};
1818
pub use index_types as IndexTypes;
1919
pub use selectors::Selectors;

tests/sqlx/tests/containment_with_index_tests.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
//! Uses the ste_vec_vast table (500 rows) from migration 005_install_ste_vec_vast_data.sql
1010
1111
use anyhow::Result;
12-
use eql_tests::{analyze_table, assert_uses_index, create_jsonb_gin_index, get_ste_vec_encrypted};
12+
use eql_tests::{
13+
analyze_table, assert_uses_index, assert_uses_seq_scan, create_jsonb_gin_index,
14+
explain_query, get_ste_vec_encrypted,
15+
};
1316
use sqlx::PgPool;
1417

1518
// Constants for ste_vec_vast table testing
@@ -181,3 +184,38 @@ async fn jsonb_array_count_with_index(pool: PgPool) -> Result<()> {
181184

182185
Ok(())
183186
}
187+
188+
189+
#[sqlx::test]
190+
async fn jsonb_containment_uses_seq_scan_without_index(pool: PgPool) -> Result<()> {
191+
// Test: Verify sequential scan is used BEFORE GIN index is created
192+
//
193+
// This test demonstrates that the GIN index actually makes a difference:
194+
// 1. Without index: query uses Seq Scan
195+
// 2. After creating index: query uses Index Scan
196+
//
197+
// This is a "before and after" test to prove the index improves query execution.
198+
199+
// Run ANALYZE first to ensure planner has accurate statistics
200+
analyze_table(&pool, STE_VEC_VAST_TABLE).await?;
201+
202+
let id = 1;
203+
let row = get_ste_vec_encrypted(&pool, STE_VEC_VAST_TABLE, id).await?;
204+
205+
let sql = format!(
206+
"SELECT 1 FROM {} WHERE eql_v2.jsonb_array(e) @> eql_v2.jsonb_array('{}'::eql_v2_encrypted) LIMIT 1",
207+
STE_VEC_VAST_TABLE, row
208+
);
209+
210+
// BEFORE: Without index, should use Seq Scan
211+
let explain_before = explain_query(&pool, &sql).await?;
212+
assert_uses_seq_scan(&explain_before);
213+
214+
// Create the GIN index
215+
setup_ste_vec_vast_gin_index(&pool).await?;
216+
217+
// AFTER: With index, should use the GIN index
218+
assert_uses_index(&pool, &sql, STE_VEC_VAST_GIN_INDEX).await?;
219+
220+
Ok(())
221+
}

0 commit comments

Comments
 (0)