Skip to content

Commit 23a6206

Browse files
committed
test DetectDepthRange corner cases
1 parent 83e1d6c commit 23a6206

2 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/genomicsqlite.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ static gri_depth_range_t DetectDepthRange(sqlite3 *dbconn, const string &schema_
517517
//
518518
// We'd like to write simply SELECT MIN(_gri_bin), MAX(_gri_bin) ... and trust SQLite to plan
519519
// an efficient skip-scan of the GRI on (_gri_rid, _gri_bin, ...). Unfortunately it doesn't do
520-
// that, so instead we write some convoluted SQL that forces the efficient plan.
520+
// that, so instead we write convoluted SQL that forces the efficient plan.
521521
//
522522
// This consists of --
523523
// (i) recursive CTE to find the set of relevant _gri_rid (because even
@@ -544,7 +544,7 @@ static gri_depth_range_t DetectDepthRange(sqlite3 *dbconn, const string &schema_
544544
tbl_gri +
545545
" WHERE _gri_rid = __rid AND _gri_bin >= 0 ORDER BY _gri_rid DESC, _gri_bin DESC LIMIT 1)\n"
546546
"FROM __distinct";
547-
_DBG << endl << query << endl;
547+
//_DBG << endl << query << endl;
548548
shared_ptr<sqlite3_stmt> stmt;
549549
{
550550
sqlite3_stmt *pStmt = nullptr;

test/test_gri.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,85 @@ def test_indexing():
110110
genomicsqlite.genomic_range_rowids_sql(con, "nonexistent_table")
111111

112112

113+
def test_depth_detection():
114+
# test corner cases for the bit of genomic_range_rowids() which detects the depth range
115+
116+
con = sqlite3.connect(":memory:")
117+
con.executescript("CREATE TABLE features(rid INTEGER, beg INTEGER, end INTEGER)")
118+
con.executescript(
119+
genomicsqlite.create_genomic_range_index_sql(con, "features", "rid", "beg", "end")
120+
)
121+
122+
def fanout(query):
123+
return sum(
124+
1
125+
for expl in con.execute("EXPLAIN QUERY PLAN " + query, (None, None, None))
126+
if "((_gri_rid,_gri_bin)>(?,?) AND (_gri_rid,_gri_bin)<(?,?))" in expl[3]
127+
)
128+
129+
assert fanout(genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]) == 9
130+
131+
con.executescript(
132+
"INSERT INTO features VALUES(NULL, NULL, NULL); INSERT INTO features VALUES(NULL, 0, 10000000000)"
133+
)
134+
assert fanout(genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]) == 9
135+
assert not list(
136+
con.execute(genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1], (None, 123, 456))
137+
)
138+
139+
con.executescript("INSERT INTO features VALUES(42, 1048568, 1048584)")
140+
query = genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]
141+
print("\n" + query)
142+
assert " / 4096)" in query # level 6
143+
assert not list(
144+
con.execute(
145+
genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1], (42, None, 1048584)
146+
)
147+
)
148+
149+
assert fanout(query) == 1
150+
151+
con.executescript(
152+
"""
153+
INSERT INTO features VALUES(44, 1048568, 1048584);
154+
INSERT INTO features VALUES(44, 0, 64000)
155+
"""
156+
)
157+
query = genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]
158+
print("\n" + query)
159+
assert " / 65536)" in query # level 5
160+
assert " / 4096)" in query # level 6
161+
assert fanout(query) == 2
162+
163+
con.executescript(
164+
"""
165+
INSERT INTO features VALUES(43, NULL, 10000000000);
166+
INSERT INTO features VALUES(44, 0, NULL)
167+
"""
168+
)
169+
assert fanout(genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]) == 2
170+
171+
con.executescript(
172+
"""
173+
INSERT INTO features VALUES(43, 0, 10000000000);
174+
INSERT INTO features VALUES(43, 32, 48)
175+
"""
176+
)
177+
query = genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]
178+
print("\n" + query)
179+
assert " / 16)" not in query
180+
assert fanout(query) == 8
181+
182+
con.executescript(
183+
"""
184+
INSERT INTO features VALUES(43, 0, 10000000000);
185+
INSERT INTO features VALUES(43, 32, 47)
186+
"""
187+
)
188+
query = genomicsqlite.genomic_range_rowids_sql(con, "features")[1:-1]
189+
assert fanout(query) == 9
190+
191+
113192
def test_refseq():
114193
con = sqlite3.connect(":memory:")
115194

0 commit comments

Comments
 (0)