mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-31 23:14:21 +01:00
Closes #364 Covering indexes mean being able to read all the necessary data from an index instead of using the underlying table at all. This PR adds that functionality. This PR can be reviewed commit-by-commit as the first commits are enablers for the actual covering index usage functionality Example of a scan where covering index can be used: ```sql limbo> .schema CREATE TABLE t(a,b,c,d,e); CREATE INDEX abc ON t (a,b,c); limbo> explain select b+1,concat(a, c) from t; addr opcode p1 p2 p3 p4 p5 comment ---- ----------------- ---- ---- ---- ------------- -- ------- 0 Init 0 12 0 0 Start at 12 1 OpenRead 0 3 0 0 table=abc, root=3 2 Rewind 0 11 0 0 Rewind abc 3 Column 0 1 3 0 r[3]=abc.b 4 Integer 1 4 0 0 r[4]=1 5 Add 3 4 1 0 r[1]=r[3]+r[4] 6 Column 0 0 5 0 r[5]=abc.a 7 Column 0 2 6 0 r[6]=abc.c 8 Function 0 5 2 concat 0 r[2]=func(r[5..6]) 9 ResultRow 1 2 0 0 output=r[1..2] 10 Next 0 3 0 0 11 Halt 0 0 0 0 12 Transaction 0 0 0 0 write=false 13 Goto 0 1 0 0 ``` Example of a scan where it can't be used: ```sql limbo> .schema CREATE TABLE t(a,b,c,d,e); CREATE INDEX abc ON t (a,b,c); limbo> explain select a,b,c,d from t limit 5; addr opcode p1 p2 p3 p4 p5 comment ---- ----------------- ---- ---- ---- ------------- -- ------- 0 Init 0 11 0 0 Start at 11 1 OpenRead 0 2 0 0 table=t, root=2 2 Rewind 0 10 0 0 Rewind t 3 Column 0 0 4 0 r[4]=t.a 4 Column 0 1 5 0 r[5]=t.b 5 Column 0 2 6 0 r[6]=t.c 6 Column 0 3 7 0 r[7]=t.d 7 ResultRow 4 4 0 0 output=r[4..7] 8 DecrJumpZero 1 10 0 0 if (--r[1]==0) goto 10 9 Next 0 3 0 0 10 Halt 0 0 0 0 11 Transaction 0 0 0 0 write=false 12 Integer 5 1 0 0 r[1]=5 13 Integer 0 2 0 0 r[2]=0 14 OffsetLimit 1 3 2 0 if r[1]>0 then r[3]=r[1]+max(0,r[2]) else r[3]=(-1) 15 Goto 0 1 0 0 ``` Closes #1351