From eb98380e577eb3a660eaed9bd83f6f9a660cd609 Mon Sep 17 00:00:00 2001 From: codecrafters-bot Date: Fri, 5 Dec 2025 03:43:32 +0000 Subject: init [skip ci] --- .codecrafters/compile.sh | 11 +++++++ .codecrafters/run.sh | 11 +++++++ .gitattributes | 1 + .gitignore | 21 ++++++++++++ README.md | 76 +++++++++++++++++++++++++++++++++++++++++++ build.zig | 34 +++++++++++++++++++ build.zig.zon | 69 +++++++++++++++++++++++++++++++++++++++ codecrafters.yml | 11 +++++++ download_sample_databases.sh | 9 +++++ sample.db | Bin 0 -> 16384 bytes src/main.zig | 37 +++++++++++++++++++++ your_program.sh | 24 ++++++++++++++ 12 files changed, 304 insertions(+) create mode 100755 .codecrafters/compile.sh create mode 100755 .codecrafters/run.sh create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 codecrafters.yml create mode 100755 download_sample_databases.sh create mode 100644 sample.db create mode 100755 src/main.zig create mode 100755 your_program.sh diff --git a/.codecrafters/compile.sh b/.codecrafters/compile.sh new file mode 100755 index 0000000..5782342 --- /dev/null +++ b/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +zig build diff --git a/.codecrafters/run.sh b/.codecrafters/run.sh new file mode 100755 index 0000000..18ffaf9 --- /dev/null +++ b/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec "$(dirname "$0")"/zig-out/bin/main "$@" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7e2b058 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Database files used for testing +*.db + +# Zig build artifacts +main +zig-cache/ +.zig-cache/ +zig-out/ + +# Compiled binary output +bin/ +!bin/.gitkeep + +# Ignore any .o or .h files generated during build +*.o +*.h + +# Ignore OS and editor specific files +**/.DS_Store +*.swp +*~ diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa522e0 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +[![progress-banner](https://backend.codecrafters.io/progress/sqlite/700381c7-3674-4e7c-b625-78230791b566)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Zig solutions to the +["Build Your Own SQLite" Challenge](https://codecrafters.io/challenges/sqlite). + +In this challenge, you'll build a barebones SQLite implementation that supports +basic SQL queries like `SELECT`. Along the way we'll learn about +[SQLite's file format](https://www.sqlite.org/fileformat.html), how indexed data +is +[stored in B-trees](https://jvns.ca/blog/2014/10/02/how-does-sqlite-work-part-2-btrees/) +and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your SQLite implementation is in `src/main.zig`. Study and +uncomment the relevant code, and push your changes to pass the first stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `zig (0.15)` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `src/main.zig`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. + +# Sample Databases + +To make it easy to test queries locally, we've added a sample database in the +root of this repository: `sample.db`. + +This contains two tables: `apples` & `oranges`. You can use this to test your +implementation for the first 6 stages. + +You can explore this database by running queries against it like this: + +```sh +$ sqlite3 sample.db "select id, name from apples" +1|Granny Smith +2|Fuji +3|Honeycrisp +4|Golden Delicious +``` + +There are two other databases that you can use: + +1. `superheroes.db`: + - This is a small version of the test database used in the table-scan stage. + - It contains one table: `superheroes`. + - It is ~1MB in size. +1. `companies.db`: + - This is a small version of the test database used in the index-scan stage. + - It contains one table: `companies`, and one index: `idx_companies_country` + - It is ~7MB in size. + +These aren't included in the repository because they're large in size. You can +download them by running this script: + +```sh +./download_sample_databases.sh +``` + +If the script doesn't work for some reason, you can download the databases +directly from +[codecrafters-io/sample-sqlite-databases](https://github.com/codecrafters-io/sample-sqlite-databases). diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..0eccf2a --- /dev/null +++ b/build.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +// Learn more about this file here: https://ziglang.org/learn/build-system +pub fn build(b: *std.Build) void { + const exe = b.addExecutable(.{ + .name = "main", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = b.graph.host, + }), + }); + + // This declares intent for the executable to be installed into the + // standard location when the user invokes the "install" step (the default + // step when running `zig build`). + b.installArtifact(exe); + + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); + + // This creates a build step. It will be visible in the `zig build --help` menu, + // and can be selected like this: `zig build run` + // This will evaluate the `run` step rather than the default, which is "install". + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + + // This allows the user to pass arguments to the application in the build + // command itself, like this: `zig build run -- arg1 arg2 etc` + if (b.args) |args| { + run_cmd.addArgs(args); + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..8891635 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,69 @@ +.{ + .name = .codecrafters_sqlite, + .fingerprint = 0x558026c9dcdd8a05, + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + .minimum_zig_version = "0.15.1", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..31d465e --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Zig version used to run your code +# on Codecrafters. +# +# Available versions: zig-0.15.2 +buildpack: zig-0.15.2 diff --git a/download_sample_databases.sh b/download_sample_databases.sh new file mode 100755 index 0000000..03e0573 --- /dev/null +++ b/download_sample_databases.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo "Downloading superheroes.db: ~1MB (used in stage 7)" +curl -Lo superheroes.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/superheroes.db + +echo "Downloading companies.db: ~7MB (used in stage 8)" +curl -Lo companies.db https://raw.githubusercontent.com/codecrafters-io/sample-sqlite-databases/master/companies.db + +echo "Sample databases downloaded." diff --git a/sample.db b/sample.db new file mode 100644 index 0000000..687673e Binary files /dev/null and b/sample.db differ diff --git a/src/main.zig b/src/main.zig new file mode 100755 index 0000000..eb8124b --- /dev/null +++ b/src/main.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +var stdout_buffer: [1024]u8 = undefined; +var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); +const stdout = &stdout_writer.interface; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + if (args.len < 3) { + std.debug.print("Usage: {s} \n", .{args[0]}); + std.process.exit(1); + } + + const database_file_path: []const u8 = args[1]; + const command: []const u8 = args[2]; + + if (std.mem.eql(u8, command, ".dbinfo")) { + var file = try std.fs.cwd().openFile(database_file_path, .{}); + defer file.close(); + + // You can use print statements as follows for debugging, they'll be visible when running tests. + std.debug.print("Logs from your program will appear here!\n", .{}); + + // TODO: Uncomment the code below to pass the first stage + // var buf: [2]u8 = undefined; + // _ = try file.seekTo(16); + // _ = try file.read(&buf); + // const page_size = std.mem.readInt(u16, &buf, .big); + // try stdout.print("database page size: {}\n", .{page_size}); + // try stdout.flush(); + } +} diff --git a/your_program.sh b/your_program.sh new file mode 100755 index 0000000..9691601 --- /dev/null +++ b/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + zig build +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec "$(dirname "$0")"/zig-out/bin/main "$@" -- cgit v1.2.3