← All Exercises

Bitmap Index with AND Queries

intermediatejavascript~15min

Background

Bitmap indexes turn conjunctive filters into bitwise AND, which is why analytics warehouses love them for low-cardinality columns like department or status.

Problem

For every field in the query, build a bitmap over all rows where position i is one exactly when row i’s field equals the query value (compared as strings). AND the bitmaps together and output the bitmaps keyed as field=value plus the indexes of rows where every bitmap is one.

Input

A JSON object {"rows": object[], "query": object} where query fields exist in the rows.

Output

A JSON object {"bitmaps": object, "matchIndexes": number[]} with one bitmap per query field and matches in row order.

Sample tests

Sample 1

Input: {"rows":[{"dept":"eng","senior":true},{"dept":"sales","senior":false},{"dept":"eng","senior":false},{"dept":"eng","senior":true}],"query":{"dept":"eng","senior":true}}
Expected: {"bitmaps":{"dept=eng":[1,0,1,1],"senior=true":[1,0,0,1]},"matchIndexes":[0,3]}

Sample 2

Input: {"rows":[{"region":"eu"},{"region":"us"},{"region":"eu"}],"query":{"region":"apac"}}
Expected: {"bitmaps":{"region=apac":[0,0,0]},"matchIndexes":[]}

Your solution

Learning objective

Translate conjunctive filters into per-value bitmaps and bitwise conjunction.