On Thu, 11 May 2023, at 13:34, Peter Maivald wrote:
> Perhaps it's a project specific choice to use column names as a source
> for type information;
What I meant was that it is project specific what they mean - eg one project may want to map to the decimal type, while another wants to map to a different type.
One thing we didn't discuss is combining columns. For example a complex number in Python could be represented as two columns in SQLite and then combined back into a single complex coming back out.
> Due to SQLite it is the *only* source
> for type information for queries that do arithmetic in the column name
> part of the select:
Python types that are compatible with 64 bit signed integers in SQLite are a way smaller subset of possible types. Is that all that needs to be solved?
> SELECT sum(col1) AS sum_DEC$2 FROM ...
> SELECT col1,col2,col1+col2 AS sum_DEC$2 FROM ...
I also tested using CAST eg
SELECT CAST(col1+col2 AS COMPLEX)
Sadly the "COMPLEX" bit does not come back out in Cursor.description just Nones.
>> A shortcut would be to pickle values going into SQLite, and then unpickle all blobs coming out, which would retain full fidelity of the values.
>
> This shortcut costs you quite a few things. You can't easily use any
> other front end than python.
True.
> You can't use arithmetic on the columns in
> SQL, comparison operators, GROUP BY maybee ...
You could do all of those by functions implemented in Python so you'd have to
SELECT MYADD(col1, col2) .... WHERE MYCMP(col3, col4)>0 GROUP BY MYGROUPER(col5)
> I'd believe that it also
> bloats the database both in terms of storage space and access speed.
Indeed. For example pickling 3+4j is 57 bytes.
SQLite has put a lot of effort into JSON support recently so it is an alternative to pickle and portable to more platforms, plus can be used with builtin functions if you extract relevant parts and that is workable with your types. The same value as JSON is only 30 bytes:
{"type":"complex","i":3,"j":4}
The upcoming release allows using JSON5 which means the double quotes around the keys can be omitted saving another 6 bytes.
Roger