From 4289f7d698cc22c302d941aca61af346572adc4a Mon Sep 17 00:00:00 2001 From: Linwei Zhang Date: Thu, 29 Feb 2024 23:55:44 +0800 Subject: [PATCH] Support update varchar --- bustubx/src/storage/page/table_page.rs | 27 ++++++++++++++++++++++---- tests/sqllogictest/slt/update.slt | 8 ++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/bustubx/src/storage/page/table_page.rs b/bustubx/src/storage/page/table_page.rs index 0fe7aad..1ed0fed 100644 --- a/bustubx/src/storage/page/table_page.rs +++ b/bustubx/src/storage/page/table_page.rs @@ -130,11 +130,13 @@ impl TablePage { // Get the offset for the next tuple insertion. let tuple_offset = self.next_tuple_offset(tuple)?; let tuple_id = self.header.num_tuples; + let tuple_bytes = TupleCodec::encode(tuple); + debug_assert!(tuple_bytes.len() < u16::MAX as usize); // Store tuple information including offset, length, and metadata. self.header.tuple_infos.push(TupleInfo { offset: tuple_offset as u16, - size: TupleCodec::encode(tuple).len() as u16, + size: tuple_bytes.len() as u16, meta: *meta, }); @@ -147,8 +149,7 @@ impl TablePage { } // Copy the tuple's data into the appropriate position within the page's data buffer. - self.data[tuple_offset..tuple_offset + TupleCodec::encode(tuple).len()] - .copy_from_slice(&TupleCodec::encode(tuple)); + self.data[tuple_offset..tuple_offset + tuple_bytes.len()].copy_from_slice(&tuple_bytes); Ok(tuple_id) } @@ -181,7 +182,25 @@ impl TablePage { self.data[offset..(offset + size)].copy_from_slice(&tuple_bytes); } else { // need move other tuples - todo!() + let mut full_tuples = vec![]; + for info in self.header.tuple_infos.iter() { + full_tuples.push(( + info.meta, + TupleCodec::decode( + &self.data[info.offset as usize..(info.offset + info.size) as usize], + self.schema.clone(), + )? + .0, + )); + } + full_tuples[slot_num as usize].1 = tuple; + + let mut new_page = TablePage::new(self.schema.clone(), self.header.next_page_id); + for (meta, tuple) in full_tuples.iter() { + new_page.insert_tuple(meta, tuple)?; + } + self.header = new_page.header; + self.data = new_page.data; } Ok(()) } diff --git a/tests/sqllogictest/slt/update.slt b/tests/sqllogictest/slt/update.slt index dcae621..798c1bb 100644 --- a/tests/sqllogictest/slt/update.slt +++ b/tests/sqllogictest/slt/update.slt @@ -1,13 +1,13 @@ statement ok -create table t1 (a tinyint, b integer) +create table t1 (a tinyint, b integer, c varchar) statement ok -insert into t1 values (1, 2) +insert into t1 values (1, 2, 'x') statement ok -update t1 set b = 3 where a = 1; +update t1 set b = 3, c = 'xx' where a = 1; query II select * from t1 ---- -1 3 \ No newline at end of file +1 3 xx \ No newline at end of file