Select Tuples
Select Tuples
Description:
Retrieves tuples from a table by scanning heap pages, applying optional WHERE clause conditions, and returning matching rows. Supports full table scans and conditional filtering based on table metadata stored in the catalog.
Function:
pub fn select_tuples(
catalog: &Catalog,
db_name: &str,
table_name: &str,
selected_columns: &[String],
condition_groups: &[Vec<Condition>],
) -> io::Result<SelectResult>
Input:
catalog— Catalog containing database and table metadata.db_name— Name of the database containing the table.table_name— Name of the table to query.selected_columns— Columns to be returned. Can contain"*"for all columns.condition_groups— Parsed WHERE clause conditions.
Output:
- Returns a
SelectResultcontaining:columns— Selected column names.rows— Matching tuples.row_count— Number of tuples returned.
- Returns an
io::Errorif table access, page reads, or tuple decoding fails.
Implementation:
- Validate that the specified database exists.
- Validate that the specified table exists.
- Retrieve the table schema from the catalog.
- Open the corresponding heap file.
- Determine the total number of pages in the table.
- Sequentially scan all heap pages.
- Read each page from disk.
- Iterate through all slot entries in the page.
- Skip tuples marked as deleted.
- Deserialize tuple data into typed column values.
- Evaluate the tuple against the supplied WHERE conditions.
- If the tuple satisfies the conditions:
- Extract the requested columns.
- Add the row to the result set.
- Continue scanning until all pages are processed.
- Return the collected rows and row count.
Internal API Calls:
-
catalog.databases.get(db_name)- Retrieves database metadata.
-
database.tables.get(table_name)- Retrieves table schema metadata.
-
open_table_file(db_name, table_name)- Opens the heap file associated with the table.
-
page_count(file)- Retrieves the total number of pages in the heap file.
-
read_page(file, &mut page, page_id)- Reads a page from disk.
-
decode_tuple(&tuple_data, columns)- Converts tuple bytes into typed values.
-
matches_condition_groups_pub(...)- Evaluates WHERE clause conditions.
-
project_columns(...)- Extracts only the requested columns from a row.
Files Created/Modified:
- None.
Storage Updates:
- None.
- The operation is read-only.
Notes:
- Performs a sequential heap scan across all table pages.
- Tuples marked with
SLOT_FLAG_DELETEDare ignored. - Supports both
SELECT *and projection of specific columns. - WHERE clause filtering is evaluated during the scan.
- Does not modify heap pages, FSM pages, visibility maps, or catalog metadata.