Read a SQLite database file directly. Start with its header, follow B-tree pages and use the data you read to answer queries.
Set up your project
Choose a starter for your language. C · Clojure · C++ · C# · Elixir · Gleam · Go · Java · JavaScript · Kotlin · PHP · Python · Ruby · Rust · Swift · TypeScript · Zig.
Use the examples in each stage to check your implementation locally. CodeCrafters operates its hosted runner separately.
- Starter templates
- Reference solutions
- Language environments
- Download the source snapshot (ZIP)
- Upstream test runner
Contents
- Print page size
- Print number of tables
- Print table names
- Count rows in a table
- Read data from a single column
- Read data from multiple columns
- Filter data with a WHERE clause
- Retrieve data using a full-table scan
- Retrieve data using an index
Core project
1. Print page size
In this stage, you’ll implement printing the page size of a SQLite database file via the .dbinfo dot command.
.dbinfo
.dbinfo is a dot command that prints metadata about a SQLite database.
The .dbinfo command is executed like this:
$ sqlite3 sample.db .dbinfo
It outputs metadata about the database file:
database page size: 4096
write format: 1
read format: 1
...
number of tables: 5
schema size: 330
data version: 1
In this stage, your .dbinfo command only needs to output the “database page size.”
Database file
The SQLite database file begins with the database header. The database page size is stored in the header, right after the magic string. It’s a 2-byte, big-endian value (read left-to-right).
// Start of file
53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 // Magic string: "SQLite format 3" + null terminator.
10 00 /* Database page size, in bytes.
Here, the page size is 4096 bytes. */
...
Tests
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db .dbinfo
Your program must print the database page size of the database file, like this:
database page size: 4096
Notes
- For more information about the SQLite database file format, see the Database File Format guide.
- Database headers use big-endian to store multi-byte fields. See the MDN article on endianness to learn more.
2. Print number of tables
In this stage, you’ll add “number of tables” to your .dbinfo command’s output.
The sqlite_schema table
To get the number of tables in a SQLite database, you need to examine the database’s sqlite_schema table. The sqlite_schema table stores the database schema.
For each table, index, view, or trigger in the database, there’s a corresponding row in sqlite_schema. The one exception is that there’s no row for the sqlite_schema table itself.
To see what sqlite_schema looks like, run this command:
$ sqlite3 sample.db "SELECT * FROM sqlite_schema;"
In this challenge, you can assume that databases only contain tables—no indexes, views, or triggers. So, each row in sqlite_schema represents a table in the database. As a result, you can get the total number of tables in the database by getting the number of rows in sqlite_schema.
Pages
A SQLite database file is made up of one or more pages. All tables, including sqlite_schema, are stored on one or more table b-tree pages.
In this challenge, you can assume that the sqlite_schema table is small enough to fit entirely on a single page. (In reality, it can sometimes span multiple pages.) In order to get the number of rows in sqlite_schema, you need to read the sqlite_schema page.
The sqlite_schema page
You’ll learn more about b-tree pages in later stages. For now, here’s what you need to know:
- The
sqlite_schemapage is always page 1, and it always begins at offset 0. The file header is a part of the page. - The
sqlite_schemapage stores the rows of thesqlite_schematable in chunks of data called “cells.” Each cell stores a single row.
So, the number of tables in the database is equal to the number of cells on the sqlite_schema page.
Cell count
You can get the number of cells on the sqlite_schema page by looking at the sqlite_schema page header. The b-tree page header contains a 2-byte big-endian value that specifies number of cells on the page. See the official documentation for more information.
Note that the page header is separate from the file header. The page header appears directly after the file header.
Tests
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db .dbinfo
Your program must print the following values:
- Database page size
- Number of tables
database page size: 4096
number of tables: 3
Notes
- You may find it useful to read through
sample.dband make sure you understand the file format, before working on a solution. To do this, you can runhexdump -C sample.db, or use a hex editor like HexEd.it.
3. Print table names
In this stage, you’ll implement the .tables dot command, which prints the names of the user tables in a SQLite database.
The sqlite_schema.tbl_name column
The names of the tables in a SQLite database are stored in the tbl_name column of the sqlite_schema table. The sqlite_schema page stores the rows of the sqlite_schema table in chunks of data called “cells.” Each cell contains a single row. You need to read all the cells and extract the value of sqlite_schema.tbl_name from each one.
Cell pointer array
To figure out where the cells are located, read the sqlite_schema page’s cell pointer array. This array specifies the offsets of every cell on the page. Here’s what you need to know:
- The array appears directly after the page header.
- The elements (offsets) are 2-byte big-endian values.
- The offsets are relative to the start of the page.
- The array size is equal to the number of cells on the page (The page header specifies the number of cells on the page).
Cell
Once you have all the offsets, you can read the cells. The type of cell on the sqlite_schema page is called a “table b-tree leaf cell.” It’s made up of three parts:
- The size of the record, in bytes (varint)
- The rowid (varint)
- The record (record format)
Cells use variable-length integers, also called “varints.” See the official documentation to learn how they work.
You can ignore the rowid—it’s not relevant to this stage.
The part you’re interested in is the record. “Record” is just another word for “row.” That’s the part that contains the sqlite_schema.tbl_name column.
Record format
Records are stored in record format:
- Header:
- Size of the header, including this value (varint)
- Serial type code for each column in the record, in order (varint)
- Body:
- The value of each column in the record, in order (format varies based on serial type code)
A “serial type code” specifies the data type and size of a column. See the official documentation for the table of all serial type codes.
Example
The following is a cell from page 1 of sample.db:
00000ec0 78 03 07 17 1b 1b 01 81 47 74 61 62 6c | x.......Gtabl|
00000ed0 65 6f 72 61 6e 67 65 73 6f 72 61 6e 67 65 73 04 |eorangesoranges.|
00000ee0 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 61 |CREATE TABLE ora|
00000ef0 6e 67 65 73 0a 28 0a 09 69 64 20 69 6e 74 65 67 |nges.(..id integ|
00000f00 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 |er primary key a|
00000f10 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 09 6e |utoincrement,..n|
00000f20 61 6d 65 20 74 65 78 74 2c 0a 09 64 65 73 63 72 |ame text,..descr|
00000f30 69 70 74 69 6f 6e 20 74 65 78 74 0a 29 |iption text.) |
Here’s an analysis of the cell:
// Size of the record (varint): 120
78
// The rowid (safe to ignore)
03
// Record header
07 // Size of record header (varint): 7
17 // Serial type for sqlite_schema.type (varint): 23
// Size of sqlite_schema.type = (23-13)/2 = 5
1b // Serial type for sqlite_schema.name (varint): 27
// Size of sqlite_schema.name = (27-13)/2 = 7
1b // Serial type for sqlite_schema.tbl_name (varint): 27
// Size of sqlite_schema.tbl_name = (27-13)/2 = 7
01 // Serial type for sqlite_schema.rootpage (varint): 1
// 8-bit twos-complement integer
81 47 // Serial type for sqlite_schema.sql (varint): 199
// Size of sqlite_schema.sql = (199-13)/2 = 93
// Record body
74 61 62 6c 65 // Value of sqlite_schema.type: "table"
6f 72 61 6e 67 65 73 // Value of sqlite_schema.name: "oranges"
6f 72 61 6e 67 65 73 // Value of sqlite_schema.tbl_name: "oranges" <---
...
Tests
Here’s how the tester will execute your program:
$ ./your_sqlite3.sh sample.db .tables
Your program must print the names of the tables in the database file:
apples oranges
Notes
- The actual
.tablescommand accepts an optional pattern argument, and also adds additional spaces between each table name, for formatting purposes. You do not need to implement either of these features for your.tablescommand. - If a cell’s payload is too large to fit on a single page, the remainder of the payload will be stored on cell payload overflow pages. You do not need to handle payload overflow in this challenge.
- The record part of a cell is called “payload,” in the official documentation.
4. Count rows in a table
Now that you’ve gotten your feet wet with the SQLite database file format, it’s time to move on to actual SQL!
In this stage, your program will need to read the count of rows from a table.
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db "SELECT COUNT(*) FROM apples"
and here’s the output it expects:
4
You’ll need to read the table’s row from the sqlite_schema table and follow the rootpage value to visit the page corresponding to the table. For now you can assume that the contents of the table are small enough to fit inside the root page. We’ll deal with tables that span multiple pages in stage 7.
Remember: You don’t need to implement a full-blown SQL parser just yet. We’ll get to that in the next stages. For now you can just split the input by ” ” and pick the last item to get the table name.
5. Read data from a single column
Now that you’re comfortable with jumping across database pages, let’s dig a little deeper and read data from rows in a table.
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db "SELECT name FROM apples"
and here’s the output it expects:
Granny Smith
Fuji
Honeycrisp
Golden Delicious
The order of rows returned doesn’t matter.
Rows are stored on disk in the Record Format, which is just an ordered sequence of values. To extract data for a single column, you’ll need to know the order of that column in the sequence. You’ll need to parse the table’s CREATE TABLE statement to do this. The CREATE TABLE statement is stored in the sqlite_schema table’s sql column.
For Python
Not interested in implementing a SQL parser from scratch? sqlparse is available as a dependency if you’d like to use it.
For Go
Not interested in implementing a SQL parser from scratch? xwb1989/sqlparser is available as a dependency if you’d like to use it.
For Rust
Not interested in implementing a SQL parser from scratch? The nom, peg and regex crates are available in Cargo.toml if you’d like to use them.
6. Read data from multiple columns
This stage is similar to the previous one, just that the tester will query for multiple columns instead of just one.
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db "SELECT name, color FROM apples"
and here’s the output it expects:
Granny Smith|Light Green
Fuji|Red
Honeycrisp|Blush Red
Golden Delicious|Yellow
Just like in the previous stage, the order of rows doesn’t matter.
7. Filter data with a WHERE clause
In this stage, you’ll support filtering records using a WHERE clause.
Here’s how the tester will execute your program:
$ ./your_program.sh sample.db "SELECT name, color FROM apples WHERE color = 'Yellow'"
and here’s the output it expects:
Golden Delicious|Yellow
For now you can assume that the contents of the table are small enough to fit inside the root page. We’ll deal with tables that span multiple pages in the next stage.
8. Retrieve data using a full-table scan
Time to play with larger amounts of data!
In this stage you’ll deal with the same syntax as before: a query with a WHERE clause. However, this time, the table you’ll be querying will be larger and it’ll span multiple pages.
Here’s how the tester will execute your program:
$ ./your_program.sh superheroes.db "SELECT id, name FROM superheroes WHERE eye_color = 'Pink Eyes'"
and here’s the output it expects:
297|Stealth (New Earth)
790|Tobias Whale (New Earth)
1085|Felicity (New Earth)
2729|Thrust (New Earth)
3289|Angora Lapin (New Earth)
3913|Matris Ater Clementia (New Earth)
The tester is going to use a sample database of superheroes that is ~1MB in size. You can download a small version of this to test locally, read the Sample Databases section in the README of your repository.
You’ll need to traverse a B-tree in this stage. If you’re unfamiliar with how B-trees work or just need a refresher, Vaidehi Joshi’s Busying Oneself With B-Trees is a good place to start. For specifics on how SQLite stores B-trees on disk, read the B-tree Pages documentation section.
9. Retrieve data using an index
In this stage, we’ll implement an index scan. Rather than reading all rows in a table and then filtering in-memory, we’ll use an index to perform a more intelligent search.
To test whether your implementation actually uses an index, the tester will use a database is ~1GB in size and expect your program to return query results in less than 3 seconds.
The test database contains a companies table with an index named idx_companies_country on the country column.
You can download a small version of this database to test locally, read the Sample Databases section in the README of your repository for details.
Here’s how the tester will execute your program:
$ ./your_program.sh companies.db "SELECT id, name FROM companies WHERE country = 'eritrea'"
and here’s the output it expects:
121311|unilink s.c.
2102438|orange asmara it solutions
5729848|zara mining share company
6634629|asmara rental
You can assume that all queries run by the tester will include country in the WHERE clause, so they can be served by the index. The tester will run multiple randomized queries and expect all of them to return results in under 3 seconds.
Source and licence
Stage material by CodeCrafters. Source snapshot · Original licence.
MIT License Copyright (c) 2022 CodeCrafters Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.