Skip to content

Commit

Permalink
Fix update
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Feb 29, 2024
1 parent fefef00 commit ab23aff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
19 changes: 15 additions & 4 deletions bustubx/src/execution/physical_plan/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::common::{ScalarValue, TableReference};
use crate::execution::{ExecutionContext, VolcanoExecutor};
use crate::expression::{Expr, ExprTrait};
use crate::storage::{TableIterator, EMPTY_TUPLE};
use crate::{BustubxResult, Tuple};
use crate::{BustubxError, BustubxResult, Tuple};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Mutex;

#[derive(Debug)]
pub struct PhysicalUpdate {
Expand All @@ -15,6 +16,7 @@ pub struct PhysicalUpdate {
pub selection: Option<Expr>,

update_rows: AtomicU32,
table_iterator: Mutex<Option<TableIterator>>,
}

impl PhysicalUpdate {
Expand All @@ -30,20 +32,28 @@ impl PhysicalUpdate {
assignments,
selection,
update_rows: AtomicU32::new(0),
table_iterator: Mutex::new(None),
}
}
}

impl VolcanoExecutor for PhysicalUpdate {
fn init(&self, _context: &mut ExecutionContext) -> BustubxResult<()> {
fn init(&self, context: &mut ExecutionContext) -> BustubxResult<()> {
self.update_rows.store(0, Ordering::SeqCst);
let table_heap = context.catalog.table_heap(&self.table)?;
*self.table_iterator.lock().unwrap() = Some(TableIterator::new(table_heap.clone(), ..));
Ok(())
}

fn next(&self, context: &mut ExecutionContext) -> BustubxResult<Option<Tuple>> {
// TODO may scan index
let Some(table_iterator) = &mut *self.table_iterator.lock().unwrap() else {
return Err(BustubxError::Execution(
"table iterator not created".to_string(),
));
};
let table_heap = context.catalog.table_heap(&self.table)?;
let mut table_iterator = TableIterator::new(table_heap.clone(), ..);

loop {
if let Some((rid, mut tuple)) = table_iterator.next()? {
if let Some(selection) = &self.selection {
Expand All @@ -53,8 +63,9 @@ impl VolcanoExecutor for PhysicalUpdate {
}
// update tuple data
for (col_name, value_expr) in self.assignments.iter() {
let new_value = value_expr.evaluate(&EMPTY_TUPLE)?;
let index = tuple.schema.index_of(None, &col_name)?;
let col_datatype = tuple.schema.columns[index].data_type;
let new_value = value_expr.evaluate(&EMPTY_TUPLE)?.cast_to(&col_datatype)?;
tuple.data[index] = new_value;
}
table_heap.update_tuple(rid, tuple)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ insert into t1 values (1, 2)
statement ok
update t1 set b = 3 where a = 1;

# query II
# select * from t1
# ----
# 1 3
query II
select * from t1
----
1 3

0 comments on commit ab23aff

Please sign in to comment.