Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

fix(clinical): retain crosswalk rows with large string columns

Rudy Celekli5981b9fc1c3f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d864085..2cfff83b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@ All notable changes to Open Ontologies are documented here.
## [Unreleased]
+### Fixed
+
+- Load crosswalk Parquet string columns with either Utf8 or LargeUtf8 offsets, including mixed schemas, so valid rows are not silently discarded.
+
### Added
- **A named profile hands a client one job's worth of the tool surface** (#263).
diff --git a/src/clinical.rs b/src/clinical.rs
index 11891ffb..5b9d7e93 100644
--- a/src/clinical.rs
+++ b/src/clinical.rs
@@ -13,6 +13,33 @@ pub struct CrosswalkRow {
pub target_label: String,
}
+// Parquet Arrow metadata may retain either string offset width.
+// Keep unsupported columns and null value handling consistent with the loader.
+enum CrosswalkStrings<'a> {
+ Utf8(&'a arrow::array::StringArray),
+ LargeUtf8(&'a arrow::array::LargeStringArray),
+}
+
+impl<'a> CrosswalkStrings<'a> {
+ fn new(array: &'a dyn arrow::array::Array) -> Option<Self> {
+ if let Some(strings) = array.as_any().downcast_ref::<arrow::array::StringArray>() {
+ Some(Self::Utf8(strings))
+ } else {
+ array
+ .as_any()
+ .downcast_ref::<arrow::array::LargeStringArray>()
+ .map(Self::LargeUtf8)
+ }
+ }
+
+ fn value(&self, index: usize) -> &str {
+ match self {
+ Self::Utf8(strings) => strings.value(index),
+ Self::LargeUtf8(strings) => strings.value(index),
+ }
+ }
+}
+
/// Clinical crosswalks backed by a Parquet file.
pub struct ClinicalCrosswalks {
rows: Vec<CrosswalkRow>,
@@ -21,7 +48,6 @@ pub struct ClinicalCrosswalks {
impl ClinicalCrosswalks {
/// Load crosswalk data from a Parquet file.
pub fn load(path: &str) -> anyhow::Result<Self> {
- use arrow::array::StringArray;
use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
use std::fs::File;
@@ -33,19 +59,19 @@ impl ClinicalCrosswalks {
for batch in reader {
let batch = batch?;
let source_code = batch.column_by_name("source_code")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let source_system = batch.column_by_name("source_system")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let target_code = batch.column_by_name("target_code")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let target_system = batch.column_by_name("target_system")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let relation = batch.column_by_name("relation")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let source_label = batch.column_by_name("source_label")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
let target_label = batch.column_by_name("target_label")
- .and_then(|c| c.as_any().downcast_ref::<StringArray>());
+ .and_then(|c| CrosswalkStrings::new(c.as_ref()));
if let (Some(sc), Some(ss), Some(tc), Some(ts), Some(rel), Some(sl), Some(tl)) =
(source_code, source_system, target_code, target_system, relation, source_label, target_label)
diff --git a/tests/clinical_string_columns_test.rs b/tests/clinical_string_columns_test.rs
new file mode 100644
index 00000000..a4210d5c
--- /dev/null
+++ b/tests/clinical_string_columns_test.rs
@@ -0,0 +1,163 @@
+mod common;
+
+use arrow::array::{ArrayRef, Int32Array, LargeStringArray, StringArray};
+use arrow::datatypes::{Field, Schema};
+use arrow::record_batch::RecordBatch;
+use open_ontologies::clinical::ClinicalCrosswalks;
+use parquet::arrow::ArrowWriter;
+use std::path::Path;
+use std::process::Command;
+use std::sync::Arc;
+
+const COLUMNS: [(&str, [&str; 2]); 7] = [
+ ("source_code", ["SAMPLE1", "SAMPLE_OTHER"]),
+ ("source_system", ["ICD10", "ICD10"]),
+ ("target_code", ["SAMPLE2", "SAMPLE_OTHER_TARGET"]),
+ ("target_system", ["SNOMED", "SNOMED"]),
+ ("relation", ["exactMatch", "closeMatch"]),
+ ("source_label", ["Sample source 商品", "Other source"]),
+ ("target_label", ["Sample target", "Other target"]),
+];
+
+fn columns(large: [bool; 7]) -> Vec<(&'static str, ArrayRef)> {
+ COLUMNS
+ .iter()
+ .zip(large)
+ .map(|((name, values), large)| {
+ let array: ArrayRef = if large {
+ Arc::new(LargeStringArray::from(values.to_vec()))
+ } else {
+ Arc::new(StringArray::from(values.to_vec()))
+ };
+ (*name, array)
+ })
+ .collect()
+}
+
+fn write_crosswalk(path: &Path, columns: Vec<(&str, ArrayRef)>) {
+ let _gate = common::exec_gate();
+ let schema = Arc::new(Schema::new(
+ columns
+ .iter()
+ .map(|(name, array)| Field::new(*name, array.data_type().clone(), true))
+ .collect::<Vec<_>>(),
+ ));
+ let batch = RecordBatch::try_new(
+ schema.clone(),
+ columns.into_iter().map(|(_, array)| array).collect(),
+ )
+ .unwrap();
+ let file = std::fs::File::create(path).unwrap();
+ let mut writer = ArrowWriter::try_new(file, schema, None).unwrap();
+ writer.write(&batch).unwrap();
+ writer.close().unwrap();
+}
+
+fn assert_sample(crosswalk: &ClinicalCrosswalks) {
+ let rows = crosswalk.lookup("SAMPLE1", "ICD10");
+ assert_eq!(rows.len(), 1);
+ let row = rows[0];
+ assert_eq!(row.source_code, "SAMPLE1");
+ assert_eq!(row.source_system, "ICD10");
+ assert_eq!(row.target_code, "SAMPLE2");
+ assert_eq!(row.target_system, "SNOMED");
+ assert_eq!(row.relation, "exactMatch");
+ assert_eq!(row.source_label, "Sample source 商品");
+ assert_eq!(row.target_label, "Sample target");
+ assert_eq!(crosswalk.lookup("SAMPLE_OTHER", "ICD10").len(), 1);
+ assert!(crosswalk.lookup("SAMPLE1", "SNOMED").is_empty());
+ let matches = crosswalk.search_label("Sample source 商品");
+ assert_eq!(matches[0]["source_code"], "SAMPLE1");
+ assert_eq!(matches[0]["similarity"], 1.0);
+}
+
+#[test]
+fn crosswalk_loads_both_string_widths_and_mixed_columns() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("crosswalks.parquet");
+ for large in [
+ [false; 7],
+ [true; 7],
+ [true, false, true, false, true, false, true],
+ ] {
+ write_crosswalk(&path, columns(large));
+ let crosswalk = ClinicalCrosswalks::load(path.to_str().unwrap()).unwrap();
+ assert_sample(&crosswalk);
+ }
+}
+
+#[test]
+fn unsupported_or_missing_columns_and_null_labels_keep_existing_behavior() {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("crosswalks.parquet");
+ let mut missing = columns([false; 7]);
+ missing.pop();
+ write_crosswalk(&path, missing);
+ assert!(
+ ClinicalCrosswalks::load(path.to_str().unwrap())
+ .unwrap()
+ .lookup("SAMPLE1", "ICD10")
+ .is_empty()
+ );
+
+ let mut unsupported = columns([true; 7]);
+ unsupported[0].1 = Arc::new(Int32Array::from(vec![1, 2]));
+ write_crosswalk(&path, unsupported);
+ assert!(
+ ClinicalCrosswalks::load(path.to_str().unwrap())
+ .unwrap()
+ .lookup("SAMPLE1", "ICD10")
+ .is_empty()
+ );
+
+ for large in [false, true] {
+ let mut nullable = columns([large; 7]);
+ nullable[5].1 = if large {
+ Arc::new(LargeStringArray::from(vec![None, Some("Other source")]))
+ } else {
+ Arc::new(StringArray::from(vec![None, Some("Other source")]))
+ };
+ write_crosswalk(&path, nullable);
+ let crosswalk = ClinicalCrosswalks::load(path.to_str().unwrap()).unwrap();
+ let rows = crosswalk.lookup("SAMPLE1", "ICD10");
+ assert_eq!(rows.len(), 1);
+ assert_eq!(rows[0].source_label, "");
+ }
+}
+
+#[test]
+fn crosswalk_cli_returns_identical_mappings_for_each_string_schema() {
+ let directory = tempfile::tempdir().unwrap();
+ std::fs::create_dir(directory.path().join("data")).unwrap();
+ let path = directory.path().join("data/crosswalks.parquet");
+ let mut outputs = Vec::new();
+ for large in [
+ [false; 7],
+ [true; 7],
+ [true, false, true, false, true, false, true],
+ ] {
+ write_crosswalk(&path, columns(large));
+ let output = {
+ let _gate = common::exec_gate();
+ Command::new(env!("CARGO_BIN_EXE_open-ontologies"))
+ .current_dir(directory.path())
+ .args(["--no-connect", "--data-dir"])
+ .arg(directory.path().join("state"))
+ .args(["crosswalk", "SAMPLE1", "--system", "ICD10"])
+ .output()
+ .unwrap()
+ };
+ assert!(
+ output.status.success(),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ let value: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
+ assert_eq!(value["mappings"].as_array().unwrap().len(), 1);
+ assert_eq!(value["mappings"][0]["target_code"], "SAMPLE2");
+ assert_eq!(value["mappings"][0]["source_label"], "Sample source 商品");
+ outputs.push(value);
+ }
+ assert_eq!(outputs[0], outputs[1]);
+ assert_eq!(outputs[0], outputs[2]);
+}