Wednesday, 30 July 2025

Sorted Inverted Index in Apache Pinot

A Sorted Inverted Index is a specialized version of the inverted index that maintains both the inverted mapping (value to document IDs) and a sorted order of the values within each index. This index is ideal for scenarios where you want to perform quick lookups as well as efficient range queries or ordering on the indexed column.

 

The key difference between a standard Bitmap Inverted Index and a Sorted Inverted Index is the ordering of the values. In a Sorted Inverted Index, the document IDs are maintained in a sorted order for each indexed value, which helps to optimize range queries.

 

How Does a Sorted Inverted Index Work?

Let’s consider the dataset with documents that have a Category and Price columns.

 

Document Id

Category

Price

d1

Electronics   

1000

d2

Furniture

700

d3

Electronics   

1200

d4

Clothing 

500

 

When creating a Sorted Inverted Index for the Category and Price columns, Pinot does the following:

 

·      Category Column: Maps each unique category to a list of document IDs that contain that category. These lists are sorted based on document ID or another internal ordering.

 

·      Price Column: Maps each unique price to a list of document IDs that contain that price. These lists are sorted based on price values, enabling efficient range queries.

 

Example of a Sorted Inverted Index

Let’s see how Pinot would create a Sorted Inverted Index for the Category and Price columns:

 

Category Column (Sorted Inverted Index):

Category

Document IDs (Sorted)

Electronics   

d1, d3

Furniture

d2

Clothing 

d4

 

In the Sorted Inverted Index, the document IDs for Electronics (d1 and d3) are already sorted in ascending order. Similarly, the other categories (Furniture and Clothing) also have their document IDs sorted.

 

Price Column (Sorted Inverted Index):

Price

Document IDs (Sorted)

500

d4

700

d2

1000

d1

1200

d3

 

Here, the document IDs are sorted according to the price values. For example, Price = 500 maps to document d4, and Price = 1200 maps to document d3, both stored in a sorted list.

 

Querying with Sorted Inverted Index

The Sorted Inverted Index improves query performance not only for point queries (looking for exact matches) but also for range queries and ordering operations.

 

Query 1: Find documents with Category = 'Electronics'

SELECT * FROM Documents WHERE Category = 'Electronics';

How Pinot Retrieves Data?

·      Looks up Category = 'Electronics' in the index, which maps to d1, d3.

·      Retrieves documents d1 and d3 directly.

 

 Query 2: Find documents with Price BETWEEN 700 AND 1200

 

SELECT * FROM Documents WHERE Price BETWEEN 700 AND 1200;

How Pinot Retrieves Data?

·      Looks up Price values in the index. The list for Price values between 700 and 1200 includes d1, d2, d3.

·      Pinot quickly retrieves documents d1, d2, and d3.

·      The index's sorted order makes it easy to identify which documents lie within the specified range.

 

Query 3: Find documents sorted by Price in descending order?

SELECT * FROM Documents ORDER BY Price DESC;

 

How Pinot Retrieves Data?

The index for Price is already sorted. Pinot can quickly return the documents in descending order by simply reversing the sorted list.

 

Advantages of Sorted Inverted Index

·      Efficient Range Queries: The sorted order of values makes range queries very efficient. You don’t need to scan the entire dataset to find values that fall within a given range.

 

·      Fast Sorting: Queries that require sorting (e.g., ORDER BY clauses) can take advantage of the sorted order in the index, eliminating the need for a full sort operation.

 

·      Lower Disk I/O: By maintaining a sorted list of document IDs for each indexed value, Pinot can avoid performing expensive disk scans when retrieving documents.

 

When to Use Sorted Inverted Index

·      Range Queries: Use the Sorted Inverted Index for columns that are frequently queried with range filters (e.g., BETWEEN, >=, <=).

 

·      Ordering Queries: If you often need to retrieve data sorted by a particular column, the Sorted Inverted Index is perfect for optimizing these queries.

 

·      High Cardinality Columns: For columns with high cardinality (many unique values), such as Price, Timestamp, etc., a Sorted Inverted Index can offer significant performance improvements.

 

In summary, the Sorted Inverted Index in Apache Pinot is an excellent tool for optimizing queries on columns that require fast lookups, range queries, and sorting operations. By maintaining sorted lists of document IDs for each unique value, it ensures that queries run efficiently without the need for full scans or expensive sorting operations.

 

 

 

Previous                                                    Next                                                    Home

Bitmap Inverted Index in Apache Pinot

In modern data processing systems, fast query performance is crucial, especially when handling large datasets. One of the key ways to achieve fast filtering and retrieval of data is by using indexes. Apache Pinot, a distributed columnar store designed for real-time analytics, uses several types of indexes to enhance query speed. One of the most effective types of indexes is the Bitmap Inverted Index.

 

What is a Bitmap Inverted Index?

A Bitmap Inverted Index is an efficient data structure used to index a column with low cardinality. It stores a bitmap (or list of document IDs) for each unique value in a column. Instead of storing a separate record for each row, a bitmap or list of row IDs is used to identify which rows contain the specific value. This enables extremely fast lookups when filtering data.

 

Unlike traditional row-based indexes, where each index entry maps to specific values, a bitmap index maps each unique value to a list of documents (rows) where the value occurs.

 

How Does Bitmap Inverted Index Work?

Let's consider a simple dataset of documents with a Category and Price column.

 

Document ID   

Category 

Price

d1

Electronics   

1000

d2

Furniture

700

d3

Electronics   

1200

d4

Clothing 

500

 

Now, the Bitmap Inverted Index for the Category and Price columns would look like this:

 

Category Column Index:

Category

Document IDs

Electronics   

d1, d3

Furniture

d2

Clothing 

d4

 

Price Column Index:

Price

Document IDs

1000

d1

700

d2

1200

d3

500

d4

 

How Queries Work with Bitmap Inverted Index?

When a query is executed, Apache Pinot can quickly access the Bitmap Inverted Index to filter documents.

 

For example:

 

Query 1: Find documents with Category = 'Electronics'

SELECT * FROM Documents WHERE Category = 'Electronics';

How Pinot Retrieves Data?

·      Looks up the Category = 'Electronics' entry in the index, which maps to d1, d3.

·      Retrieves documents d1 and d3 directly.

 

Query 2: Find documents with Price = 700

SELECT * FROM Documents WHERE Price = 700;

How Pinot Retrieves Data?

·      Looks up the Price = 700 entry in the index, which maps to d2.

·      Retrieves document d2 directly.

 

Advantages of Bitmap Inverted Index

·      Efficient for Low Cardinality Columns: Bitmap indexes are particularly useful when columns contain a limited number of unique values (such as Category, Price, etc.).

·      Fast Filtering: Bitmap indexes allow for quick filtering by directly referencing document IDs where the value exists.

·      Space Efficient: Instead of storing values repeatedly, the index stores only the document IDs where the value appears. This leads to smaller storage requirements.

·      Multi-condition Queries: Bitmap indexes can easily combine results from multiple conditions (using logical operations like AND, OR), making them excellent for complex filtering.

 

When to Use Bitmap Inverted Index?

·      Low Cardinality Columns: When a column has a small number of unique values (e.g., Category, Gender, Region), Bitmap Inverted Index can be highly efficient.

·      Frequent Filters: If certain columns are frequently used for filtering in queries, such as in analytics dashboards, using a Bitmap Inverted Index will greatly improve performance.

·      Real-time Analytics: Bitmap Inverted Indexes are ideal for real-time analytics systems where quick response times are essential, especially for filtering large volumes of data.

 

 

In summary, Bitmap Inverted Indexes in Apache Pinot offer a powerful way to optimize query performance, especially for columns with low cardinality. By mapping unique values to document IDs, Bitmap Inverted Indexes allow fast, space-efficient filtering that can greatly enhance the performance of real-time analytical queries.

 

 

 

Previous                                                    Next                                                    Home

Tuesday, 29 July 2025

Understanding Forward Indexing in Apache Pinot

Apache Pinot is a real-time distributed OLAP datastore designed for low-latency analytics. Efficient indexing is a critical component that allows Pinot to provide fast query performance. Among the various indexing techniques available, forward indexing plays an important role.

This blog post helps you to understand what indexing is, what forward indexing means, and the different types of forward indexing supported in Pinot.

 

1. Introduction to Indexing

Indexing is a technique used in databases to speed up data retrieval operations. Instead of scanning the entire dataset for every query, indexes allow databases to quickly locate relevant rows, improving query performance significantly.

 

1.1 Why is Indexing Important?

·      Reduces query latency by avoiding full-table scans

·      Enhances performance for filtering, aggregations, and sorting operations

·      Helps optimize storage and memory usage

 

Example

Imagine you have a massive dataset containing 100 million customer transactions in an e-commerce platform. Each transaction record contains the following fields:

 

Transaction ID 

Customer ID

Product Category 

Amount

Timestamp

TXN001 

CUST123

Electronics

$299 

2024-03-01 10:00:00

TXN002

CUST456

Apparel

$79

2024-03-01 10:05:12

...

...

...

...

...

TXN100M

CUST789

Home Decor 

$150 

2025-03-30 15:45:30

 

1.2 Problem Without Indexing

Now, let’s say you want to run a query like:

SELECT * FROM transactions WHERE Customer_ID = 'CUST456';

Without indexing, the database must scan all 100 million records to find the matching transactions. This is called a full-table scan, which is extremely slow and inefficient, potentially taking minutes to return results.

 

1.3 How Indexing Helps here?

Indexing works like a table of contents or a dictionary lookup that helps the database to find the relevant records quickly. Suppose we create an index on Customer_ID, it will store a sorted mapping of Customer_ID to the corresponding row locations, allowing queries to be executed in milliseconds instead of minutes.

CREATE INDEX idx_customer_id ON transactions(Customer_ID);

Now, when we run the same query:

SELECT * FROM transactions WHERE Customer_ID = 'CUST456';

Instead of scanning all 100 million records, the database directly looks up CUST456 in the index and retrieves the relevant rows instantly.

 

2. Forward Indexing

 

2.1 What is Forward Indexing in Pinot?

In Apache Pinot, a Forward Index is the default indexing mechanism that stores column values row-wise. This allows Pinot to efficiently retrieve entire rows during query execution.

 

Apache Pinot supports three types of forward indexes:

 

·      Dictionary-Encoded Forward Index: Assigns a unique ID to each distinct column value, creating a dictionary that maps IDs to actual values. The forward index stores bit-compressed IDs instead of raw values, reducing storage and improving query performance.

 

·      Sorted Forward Index: Uses dictionary encoding but also maintains a sorted order of values. Each unique value maps to a range of document IDs (start and end positions), optimizing lookups and scans.

 

·      Raw Value Forward Index: Stores actual column values directly without encoding or compression. This approach is useful when dictionary encoding isn’t efficient, such as for high-cardinality columns.

 

2.2 Dictionary-Encoded Forward Index in Apache Pinot

A Dictionary-Encoded Forward Index is a storage optimization technique where each unique value in a column is assigned a unique ID, and instead of storing the actual values, only the encoded IDs are stored.

 

This technique reduces storage costs and improves query performance by using integer-based lookups instead of scanning large text or numeric values directly.

 

How Dictionary Encoding Works (Step-by-Step Explanation)?

Let’s take an example where we have a Product Table in Pinot:

 

Row ID 

Product Name 

Category

1

Laptop

Electronics

2

Smartphone 

Electronics

3

Keyboard 

Accessories

4

Laptop 

Electronics

5

Smartphone 

Electronics

 

Step 1: Create a Dictionary for Each Column

Pinot assigns a unique dictionary ID for each unique value in a column:

 

Dictionary for Product Name

Dictionary ID

Product Name

0

Laptop

1

Smartphone

2

Keyboard

 

Dictionary for Category

Dictionary ID

Category

0

Electronics

1

Accessories

 

Step 2: Replace Column Values with Encoded IDs

Instead of storing actual values, Pinot stores only the dictionary IDs:

 

Row Id

Product Name (Dictionary ID)   

Category (Dictionary ID)

1

0

0

2

1

0

3

2

1

4

0

0

5

1

0

 

Now, instead of scanning long text values like "Laptop", Pinot only scans small integers (0, 1, 2), which are much faster to process and take less storage.

 

Advantages of Dictionary-Encoded Forward Index

·      Storage Efficiency: Text and high-cardinality numeric values are stored as small integer IDs, reducing disk space usage.

·      Faster Query Execution: Since numbers are smaller than text, scans, filtering, and lookups are much faster.

·      Optimized Filtering (WHERE conditions): Pinot can quickly scan integer values instead of large text, making filtering operations (WHERE Product = 'Laptop') more efficient.

·      Improved Compression: Since dictionary IDs have fewer unique values, they are highly compressible, further reducing storage costs.

 

Disadvantages (When Not to Use It?)

·      Not Ideal for High-Cardinality Data: If a column has too many unique values (e.g., timestamps, UUIDs, user IDs), the dictionary grows large and loses its storage advantage.

·      Extra Lookup Step: When querying, Pinot must first look up the dictionary to get actual values, adding a slight overhead.

 

Example Query Execution in Pinot

Let’s say we want to query:

SELECT COUNT(*) 
FROM ProductTable 
WHERE Product_Name = 'Laptop';

How Pinot Processes This Query?

·      Instead of scanning "Laptop" directly, Pinot looks up the dictionary ID (0).

·      Pinot scans the compressed forward index for occurrences of 0 in the Product Name column.

·      It returns the count faster than scanning the raw text values.

 

2.3 Sorted Forward Index

A Sorted Forward Index is an optimization technique in Apache Pinot where a column is stored in a pre-sorted order, allowing for faster lookups, range queries, and filtering operations.

 

Unlike a standard dictionary-encoded forward index, which assigns unique IDs but does not impose any order, a sorted forward index keeps dictionary-encoded values in sorted order, enabling binary search and efficient range scans.

 

How Sorted Forward Index Works (Step-by-Step Explanation)?

Let’s take an example of a Transaction Table in Pinot:

 

Transaction ID 

Customer ID

Amount

T1

C101

1000

T2

C102

700

T3

C101

50

T4

C103

1200

T5

C102

500

If we apply Sorted Forward Index on the Customer ID column, Pinot will store Customer IDs in sorted order instead of how they were inserted.

 

Step 1: Sort the Customer ID Column

Pinot sorts the values in the Customer ID column and remembers their positions.

 

Original Order

Sorted Order (Sorted in Index)

C101

C101 (Position 0)

C102

C101 (Position 1)

C101

C102 (Position 2)

C103

C102 (Position 3)

C102

C103 (Position 4)

 

Stored like below.

Customer Id

Start Position

End Position

C101

0

1

C102

2

3

C103

4

4

 

Now, when Pinot searches for C101, it can quickly find the start and end positions instead of scanning everything.

 

Step 2: Faster Queries Using Sorted Index

 

Let's say we run the query:

SELECT COUNT(*) FROM Transactions WHERE CustomerID = 'C101';

 

How Pinot Uses Sorted Forward Index?

It knows C101 starts at position 0 and ends at position 1 (because it's sorted). Instead of scanning all rows, it directly jumps to those positions. This makes lookups much faster!

 

Advantages Of Sorted Forward Index(When to Use It?)

·      Fast filtering (WHERE CustomerID = 'C101')

·      Great for range queries (WHERE Amount BETWEEN 500 AND 1000)

·      Less storage needed compared to raw indexes

 

Disadvantages (When Not to Use It?)

·      Not good for high-cardinality columns (e.g., timestamps)

·      Expensive updates since sorting needs to be maintained

 

 

2.4 Raw Value Forward Index

A Raw Value Forward Index is the simplest type of forward index in Apache Pinot. It stores the actual column values as they were ingested without any transformation, sorting, or encoding.

 

Unlike dictionary-encoded or sorted forward indexes, a raw forward index keeps every value in its original row order, making it ideal for high-cardinality columns.

 

Let's say we have this Transactions Table:

Transaction ID

Customer ID   

Amount

T1

C101

1000

T2

C103

700

T3

C101

1200

T4

C102

500

 

With a Raw Value Forward Index, Pinot stores the column’s values exactly as they were inserted.

 

How Data Is Stored (Raw Format)

Customer ID Column Storage (Row-Wise)

 

Position 0: C101

Position 1: C103

Position 2: C101

Position 3: C102

 

Amount Column Storage (Row-Wise)

 

Position 0: 1000

Position 1: 700

Position 2: 1200

Position 3: 500

 

Each column’s values are stored as they are, keeping their original row order. There’s no sorting or dictionary compression applied.

 

How Queries Work with Raw Forward Index?

 

Query 1: Find all transactions for CustomerID = 'C101'

SELECT * FROM Transactions WHERE CustomerID = 'C101';

 

How Pinot Retrieves Data?

·      It scans the entire "Customer ID" column.

·      Finds C101 at positions 0 and 2.

·      Fetches the corresponding rows.

 

In this case, Full column scan is required, which is slower compared to Sorted Forward Index.

 

Query 2: Find the total sales amount

SELECT SUM(Amount) FROM Transactions;

 

How Pinot Retrieves Data?

·      Reads the "Amount" column directly.

·      Adds up 1000 + 700 + 1200 + 500.

·      Returns the result.

 

Since values are stored as-is, Pinot can read them without extra lookup steps (unlike dictionary encoding). This makes aggregations faster.

 

Use when:

·      The column has high-cardinality values (e.g., timestamps, user IDs).

·      You perform many aggregations (SUM, AVG, MAX, MIN).

 

Avoid when:

·      You frequently filter or search on the column (WHERE CustomerID = 'C101'), because full scans are needed.

·      The column has low-cardinality values, where dictionary-encoded or sorted forward indexes are more efficient.

 

In summary,

·      If you need efficient filtering on a low-cardinality column, go with Dictionary-Encoded or Sorted Forward Index.

·      If you frequently aggregate data or work with high-cardinality columns, choose Raw Value Forward Index.

·      Sorted Forward Index is used for Fast filtering, range queries. Avoid this for High-cardinality columns.

 

 

 

Previous                                                    Next                                                    Home