Skip to content

Commit 585ae80

Browse files
authored
fix: keep added columns after existing schema order (#316)
1 parent 01896d8 commit 585ae80

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

src/catalog/table.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ impl TableCatalog {
116116
if self.column_idxs.contains_key(col.name()) {
117117
return Err(DatabaseError::DuplicateColumn(col.name().to_string()));
118118
}
119-
let col_id = generator.generate().unwrap();
119+
let max_existing_id = self.columns.keys().max().copied();
120+
let mut col_id = generator.generate().unwrap();
121+
while max_existing_id.is_some_and(|max_id| col_id <= max_id) {
122+
col_id = generator.generate().unwrap();
123+
}
120124

121125
col.summary_mut().relation = ColumnRelation::Table {
122126
column_id: col_id,
@@ -292,6 +296,7 @@ mod tests {
292296
use super::*;
293297
use crate::catalog::ColumnDesc;
294298
use crate::types::LogicalType;
299+
use ulid::Generator;
295300

296301
#[test]
297302
// | a (Int32) | b (Bool) |
@@ -328,4 +333,50 @@ mod tests {
328333
assert_eq!(column_catalog.name(), "b");
329334
assert_eq!(*column_catalog.datatype(), LogicalType::Boolean,);
330335
}
336+
337+
#[test]
338+
fn test_add_column_generates_id_after_existing_columns() {
339+
for _ in 0..256 {
340+
let mut table_catalog = TableCatalog::new(
341+
"test".to_string().into(),
342+
vec![
343+
ColumnCatalog::new(
344+
"id".into(),
345+
false,
346+
ColumnDesc::new(LogicalType::Integer, None, false, None).unwrap(),
347+
),
348+
ColumnCatalog::new(
349+
"name".into(),
350+
true,
351+
ColumnDesc::new(
352+
LogicalType::Varchar(None, sqlparser::ast::CharLengthUnits::Characters),
353+
None,
354+
false,
355+
None,
356+
)
357+
.unwrap(),
358+
),
359+
],
360+
)
361+
.unwrap();
362+
let max_existing_id = table_catalog
363+
.columns()
364+
.filter_map(|column| column.id())
365+
.max()
366+
.unwrap();
367+
let mut generator = Generator::new();
368+
let new_id = table_catalog
369+
.add_column(
370+
ColumnCatalog::new(
371+
"age".into(),
372+
true,
373+
ColumnDesc::new(LogicalType::Integer, None, false, None).unwrap(),
374+
),
375+
&mut generator,
376+
)
377+
.unwrap();
378+
379+
assert!(new_id > max_existing_id);
380+
}
381+
}
331382
}

0 commit comments

Comments
 (0)