Skip to content

Commit 0cd81c1

Browse files
committed
Add test for conflicting commits
Signed-off-by: DerGut <jannik.steinmann@gmx.de>
1 parent 68f7c71 commit 0cd81c1

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

crates/catalog/memory/src/catalog.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ mod tests {
414414
use std::vec;
415415

416416
use iceberg::io::FileIOBuilder;
417-
use iceberg::spec::{NestedField, PartitionSpec, PrimitiveType, Schema, SortOrder, Type};
417+
use iceberg::spec::{
418+
NestedField, NullOrder, PartitionSpec, PrimitiveType, Schema, SortOrder, Type,
419+
};
418420
use iceberg::transaction::Transaction;
419421
use regex::Regex;
420422
use tempfile::TempDir;
@@ -480,6 +482,14 @@ mod tests {
480482
}
481483
}
482484

485+
async fn create_table_with_namespace<C: Catalog>(catalog: &C) -> Table {
486+
let namespace_ident = NamespaceIdent::new("abc".into());
487+
create_namespace(catalog, &namespace_ident).await;
488+
489+
let table_ident = TableIdent::new(namespace_ident, "test".to_string());
490+
create_table(catalog, &table_ident).await
491+
}
492+
483493
fn assert_table_eq(table: &Table, expected_table_ident: &TableIdent, expected_schema: &Schema) {
484494
assert_eq!(table.identifier(), expected_table_ident);
485495

@@ -1812,10 +1822,7 @@ mod tests {
18121822
async fn test_update_table() {
18131823
let catalog = new_memory_catalog();
18141824

1815-
let namespace_ident = NamespaceIdent::new("a".into());
1816-
create_namespace(&catalog, &namespace_ident).await;
1817-
let table_ident = TableIdent::new(namespace_ident, "test".to_string());
1818-
let table = create_table(&catalog, &table_ident).await;
1825+
let table = create_table_with_namespace(&catalog).await;
18191826

18201827
// Assert the table doesn't contain the update yet
18211828
assert!(!table.metadata().properties().contains_key("key"));
@@ -1842,15 +1849,48 @@ mod tests {
18421849
);
18431850
}
18441851

1852+
#[tokio::test]
1853+
async fn test_update_table_fails_if_commit_conflicts() {
1854+
let catalog = new_memory_catalog();
1855+
let base_table = create_table_with_namespace(&catalog).await;
1856+
1857+
// Update the table by adding a new sort order.
1858+
let _sorted_table = Transaction::new(&base_table)
1859+
.replace_sort_order()
1860+
.asc("foo", NullOrder::First)
1861+
.unwrap()
1862+
.apply()
1863+
.unwrap()
1864+
.commit(&catalog)
1865+
.await
1866+
.unwrap();
1867+
1868+
// Try to update the -now old- table again with a different sort order.
1869+
let err = Transaction::new(&base_table)
1870+
.replace_sort_order()
1871+
.desc("foo", NullOrder::Last)
1872+
.unwrap()
1873+
.apply()
1874+
.unwrap()
1875+
.commit(&catalog)
1876+
.await
1877+
.unwrap_err();
1878+
1879+
// The second transaction should fail because it didn't take the new update
1880+
// into account.
1881+
assert_eq!(err.kind(), ErrorKind::Unexpected);
1882+
assert!(err.message().to_lowercase().contains("conflict"));
1883+
}
1884+
18451885
#[tokio::test]
18461886
async fn test_update_table_fails_if_table_doesnt_exist() {
18471887
let catalog = new_memory_catalog();
18481888

18491889
let namespace_ident = NamespaceIdent::new("a".into());
18501890
create_namespace(&catalog, &namespace_ident).await;
1851-
let table_ident = TableIdent::new(namespace_ident, "test".to_string());
18521891

18531892
// This table is not known to the catalog.
1893+
let table_ident = TableIdent::new(namespace_ident, "test".to_string());
18541894
let table = build_table(table_ident);
18551895

18561896
let err = Transaction::new(&table)

0 commit comments

Comments
 (0)