I have an SQLIte3 database which has a BLOB field which encodes three integer values with a total bit length of 112 bits (64+16+32). I can perform comparisons and use the MAX() to find the maximum value of this field for some parameters of the query such as grouping. However, because everything is encoded as a BLOB, I can’t index the individual integer values, and as a result if I need to search on only one of these three values a full table scan is needed, producing poor performance.
To solve this, I want to split the BLOB field out into three separate INTEGER columns.
CREATE TABLE test(col BLOB,...)
==>
CREATE TABLE test(col1 INTEGER, col2 INTEGER, col3 INTEGER, ...);
This makes comparison operations more complicated, because now instead of saying:
A.col > B.col
I now have to use something like:
CASE
WHEN A.col1 > B.col1 THEN 1
WHEN A.col1 < B.col1 THEN -1
WHEN A.col2 > B.col2 THEN 1
WHEN A.col2 < B.col2 THEN -1
WHEN A.col3 > B.col3 THEN 1
WHEN A.col3 < B.col3 THEN -1
ELSE 0
END
However, it’s not clear to me how to transform a query that uses MIN or MAX:
MAX(col)
==>
???
As the total size of the integers exceeds SQLite’s maximum integer size, I can’t do something tricky like try to combine them into a larger integer.
I thought about maybe casting INTEGER to BLOB, but it’s not clear if this is possible for multiple integers, i.e. could I cast such that some hypothetical SQL like the following pseudo-SQL:
CAST(col1 AS INT64,col2 AS INT16,col3 AS INT32 AS BLOB)
produced a binary structure where the first 64 bits contains the value of col1, the next 16 contains the value of col2, and the final 32 bits contained the value of col3 so that the MAX function would work correctly to find the row with the maximum of the combined value? If not, how can I calculate the MIN and MAX of an “aggregate” of multiple columns where each column forms part of the entire value of those columns in a hierarchical fashion?