Skip to content

Commit

Permalink
Improve tuple api
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 5, 2024
1 parent 911d42a commit 05f685d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bustubx/src/expression/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ExprTrait for ColumnExpr {
}

fn evaluate(&self, tuple: &Tuple) -> BustubxResult<ScalarValue> {
Ok(tuple.get_value_by_col_name(&tuple.schema, &self.name))
tuple.value_by_name(&self.name).cloned()
}

fn to_column(&self, input_schema: &Schema) -> BustubxResult<Column> {
Expand Down
38 changes: 12 additions & 26 deletions bustubx/src/storage/tuple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::catalog::{ColumnRef, SchemaRef};

Check warning on line 1 in bustubx/src/storage/tuple.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `ColumnRef`
use crate::common::TransactionId;
use crate::{catalog::Schema, common::ScalarValue, BustubxResult};
use crate::{catalog::Schema, common::ScalarValue, BustubxError, BustubxResult};
use std::sync::Arc;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -68,39 +68,25 @@ impl Tuple {
bytes
}

pub fn get_value_by_col_id(&self, schema: &Schema, column_index: usize) -> ScalarValue {
let column = schema
.column_with_index(column_index)
.expect("column not found");

self.get_value_by_col(column)
}
pub fn get_value_by_col_name(&self, schema: &Schema, column_name: &String) -> ScalarValue {
let column = schema
.column_with_name(column_name)
.expect("column not found");

self.get_value_by_col(column)
pub fn value(&self, index: usize) -> BustubxResult<&ScalarValue> {
self.data.get(index).ok_or(BustubxError::Internal(format!(
"Not found column data at {} in tuple: {:?}",
index, self
)))
}

pub fn get_value_by_col(&self, column: ColumnRef) -> ScalarValue {
let (idx, col) = self
.schema
.columns
.iter()
.enumerate()
.find(|c| c.1 == &column)
.unwrap();
self.data.get(idx).unwrap().clone()
pub fn value_by_name(&self, name: &str) -> BustubxResult<&ScalarValue> {
let idx = self.schema.index_of(name)?;
self.value(idx)
}

// TODO 比较索引key大小
pub fn compare(&self, other: &Self, schema: &Schema) -> std::cmp::Ordering {
let column_count = schema.column_count();
for column_index in 0..column_count {
let compare_res = self
.get_value_by_col_id(schema, column_index)
.compare(&other.get_value_by_col_id(schema, column_index));
.value(column_index)
.unwrap()
.compare(&other.value(column_index).unwrap());
if compare_res == std::cmp::Ordering::Equal {
continue;
}
Expand Down

0 comments on commit 05f685d

Please sign in to comment.