4

I have an sqlite table when each table has a field which I use to hold some bitmask enum.

I would like to perform a query which will return me all results and group all enums into one field:

Services: TEXT name,INTEGER service,INTEGER mask

SELECT service,XXX FROM Services GROUP BY service

Basically, I would like XXX to be the result of bitmask OR (|) of all masks per that service.

Data:

'a1',1,1
'a2',1,2,
'a3',1,4,
'a4',1,8,

'b1',2,1,
'b2',2,3,
'b3',2,2

So as a result, I would like to get the following rows:

1,15 (1|2|4|8)
2,3 (1|3|2)

Thanks

EDIT: In my original question, I've forgot to mention that each mask is not a single bit but can be a mask of multiple bits (I've changed the second example to reflect that).

3
  • I think in your example you meant the results to show 2,3 as it's a (1|2|2) based on your example
    – tawman
    Jan 27, 2012 at 15:52
  • you are right - thanks. I've fixed that. Jan 30, 2012 at 23:33
  • Do you know all the possible bitmap mask values ahead of time that you will use? i.e. 1, 2, 4, ... 4096?
    – tawman
    Jan 31, 2012 at 5:22

2 Answers 2

7

SQLite supports custom aggregate functions; depending on your set up you could register a custom function to do this pretty easily. Using the C API:

void bitwise_or_step(sqlite3_context *context, int argc, sqlite3_value** argv)
{
    int *buffer = (int *)sqlite3_aggregate_context(context, sizeof(int));
    int x = sqlite3_value_int(argv[0]);
    *buffer |= x;
}

void bitwise_or_final(sqlite3_context *context)
{
    int *buffer = (int *)sqlite3_aggregate_context(context, sizeof(int));
    sqlite3_result_int(context, *buffer);
}

sqlite3_create_function_v2(db, "BITWISE_OR", 1, SQLITE_ANY, NULL,
    NULL, bitwise_or_step, bitwise_or_final, NULL);

Then within your SQL, you should be able to do:

SELECT service,BITWISE_OR(mask) FROM Services GROUP BY service

If you're using PHP, you can define a custom aggregate function from PHP, too.

0
3

A bitmask OR (|) operation of integers in SQL is simple matter of summing up unique power of 2 values for each key:

todd$ sqlite3 ex1
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table Services(Name varchar(100), Service int, mask int);
sqlite> insert into Services values("a1", 1, 1);
sqlite> insert into Services values("a2", 1, 2);
sqlite> insert into Services values("a3", 1, 4);
sqlite> insert into Services values("a4", 1, 8);
sqlite> insert into Services values("b1", 2, 1);
sqlite> insert into Services values("b2", 2, 3);
sqlite> insert into Services values("b3", 2, 2);
sqlite> select * from Services;
a1|1|1
a2|1|2
a3|1|4
a4|1|8
b1|2|1
b2|2|3
b3|2|2

EDIT: When you know the bitmap domain, you can bitand the value into its parts and sum:

sqlite> select Service, max(mask&1) + max(mask&2) +  max(mask&4) + max(mask&8) from Services group by Service;
1|15
2|3
sqlite> 

You can extend the max(mask&bit) logic for all know powers of 2 you are storing in the bitmap mask.

4
  • thanks, but when my mask is 7 for example, this solution won't work Jan 30, 2012 at 23:30
  • @Gilad 7 is not a power of 2 so I am not following your logic. The assumption based on your dataset is that each row would contain a single bitmap value per row. You can still use a SQL only approach when a row has a combined bitmap, but it is more elaborate with bitand checking et al. Need a better sample data set to illustrate.
    – tawman
    Jan 31, 2012 at 3:37
  • thank you for the example. your solution can indeed work with SQL only, but in my application I've preferred to register a custom function (as per @benzado suggestion) since eventually it makes the SQL query shorter and more readable. Jan 31, 2012 at 16:38
  • @Gilad no worries; the SQL only approach works for other database as well since I used the concept in an Oracle solution years ago for applying a security model with roles being the bitmap.
    – tawman
    Jan 31, 2012 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.