SQLite Forum

Virtual tables query closes cursor too early when no rows returned
Login
Summary:
When joining two vtables that result in zero rows, the cursor is closed such that calling code can't get the schema of the result.

Sqlite Version: 3.30.1
python 3.7
apsw version 3.30.1-r3

Example:
tblA:
id   val1  val2
---  ----  ----
2    3     27

tblB:
id   x     y
---  ----  ----
3    23.4  98.3

SQL:
   SELECT a.id, a.val1, a.val2, b.x, b.y
   FROM tblA a
   JOIN tblB b ON a.id = b.id

When tblA and tblB are implemented as virtual tables, the cursor is closed before the return from cursor.execute(). This causes an exception when the calling code uses cursor.description to get the schema.  If tblA and tblB are sqlite tables, the cursor is not closed before the return from execute() and the caller can get the schema.

Moreover, if we change the sql to something with the same net result:
   SELECT a.id, a.val1, a.val2, b.x, b.y
   FROM tblA a
   LEFT JOIN tblB b ON a.id = b.id
   WHERE b.id is not null
the virtual tables approach does not close the cursor prematurely. (So I have a work around, at the cost of a performance hit, since the query plan changes.)

Notes, cuz I figure you'll ask:
+ I have checked that this is not caused by an exception in my vtbl implementation. 
+ I don't have example vtbl code for this because I'm not allowed to export it. Still working on a contrived example.