From 9a266a531b39e7ba2623b1dff0b3e6caf2ac7e62 Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 12 Oct 2021 10:59:14 +0200 Subject: [PATCH 1/2] test correct primary key inference --- milli/src/update/index_documents/transform.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/milli/src/update/index_documents/transform.rs b/milli/src/update/index_documents/transform.rs index 8d656e50c..f4cbc8e22 100644 --- a/milli/src/update/index_documents/transform.rs +++ b/milli/src/update/index_documents/transform.rs @@ -497,6 +497,7 @@ mod test { use super::*; mod compute_primary_key { + use super::{compute_primary_key_pair, FieldsIdsMap}; #[test] @@ -537,5 +538,27 @@ mod test { assert!(result.is_err()); assert_eq!(fields_map.len(), 0); } + + } + + mod primary_key_inference { + use bimap::BiHashMap; + + use crate::update::index_documents::transform::find_primary_key; + + #[test] + fn primary_key_infered_on_first_field() { + // We run the test multiple times to change the order in which the fields are iterated upon. + for _ in 1..50 { + let mut map = BiHashMap::new(); + map.insert(1, "fakeId".to_string()); + map.insert(2, "fakeId".to_string()); + map.insert(3, "fakeId".to_string()); + map.insert(4, "fakeId".to_string()); + map.insert(0, "realId".to_string()); + + assert_eq!(find_primary_key(&map), Some("realId")); + } + } } } From 86ead92ed5155db84a4ea401a09e025eec9715d2 Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 12 Oct 2021 11:14:12 +0200 Subject: [PATCH 2/2] infer primary key on sorted fields --- milli/src/update/index_documents/transform.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/milli/src/update/index_documents/transform.rs b/milli/src/update/index_documents/transform.rs index f4cbc8e22..c0c88abed 100644 --- a/milli/src/update/index_documents/transform.rs +++ b/milli/src/update/index_documents/transform.rs @@ -77,7 +77,9 @@ fn create_fields_mapping( fn find_primary_key(index: &bimap::BiHashMap) -> Option<&str> { index - .right_values() + .iter() + .sorted_by_key(|(k, _)| *k) + .map(|(_, v)| v) .find(|v| v.to_lowercase().contains(DEFAULT_PRIMARY_KEY_NAME)) .map(String::as_str) } @@ -497,7 +499,6 @@ mod test { use super::*; mod compute_primary_key { - use super::{compute_primary_key_pair, FieldsIdsMap}; #[test] @@ -538,7 +539,6 @@ mod test { assert!(result.is_err()); assert_eq!(fields_map.len(), 0); } - } mod primary_key_inference {