From 8576d34423a784d578da1674f23f491c9443ff37 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Fri, 5 Dec 2025 10:45:09 -0300 Subject: codecrafters submit [skip ci] --- src/main.zig | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index 6e1f547..ba98b50 100755 --- a/src/main.zig +++ b/src/main.zig @@ -122,7 +122,20 @@ pub fn main() !void { try column_indices.append(allocator, idx); } - try schema.readTableRowsMultiColumnWhere(allocator, &file, page_size, rootpage, column_indices.items, where_column_idx, where_value, stdout); + // Try to use index scan if WHERE clause exists + if (where_column != null and where_value != null) { + // Try index scan first + schema.readTableRowsWithIndex(allocator, &file, page_size, table_name, rootpage, column_indices.items, where_column.?, where_value.?, stdout) catch |err| { + if (err == error.NoIndexFound) { + // Fall back to table scan + try schema.readTableRowsMultiColumnWhere(allocator, &file, page_size, rootpage, column_indices.items, where_column_idx, where_value, stdout); + } else { + return err; + } + }; + } else { + try schema.readTableRowsMultiColumnWhere(allocator, &file, page_size, rootpage, column_indices.items, where_column_idx, where_value, stdout); + } } } } else { -- cgit v1.2.3