|
| 1 | +//! Breaking changes: |
| 2 | +//! * `Blob` is encoded as `Binary` instead of `List[u8]` |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use arrow::{ |
| 6 | + array::{ |
| 7 | + Array, ArrayRef, AsArray as _, ListArray, RecordBatch, RecordBatchOptions, UInt8Array, |
| 8 | + }, |
| 9 | + datatypes::{DataType, Field, FieldRef, Schema}, |
| 10 | +}; |
| 11 | + |
| 12 | +use re_log::ResultExt as _; |
| 13 | + |
| 14 | +pub struct Migration; |
| 15 | + |
| 16 | +impl super::Migration for Migration { |
| 17 | + const SOURCE_VERSION: semver::Version = semver::Version::new(0, 1, 1); |
| 18 | + const TARGET_VERSION: semver::Version = semver::Version::new(0, 1, 2); |
| 19 | + |
| 20 | + fn migrate(batch: RecordBatch) -> RecordBatch { |
| 21 | + migrate_blobs(batch) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Change datatype from `List[u8]` to `Binary` for blobs |
| 26 | +fn migrate_blobs(batch: RecordBatch) -> RecordBatch { |
| 27 | + re_tracing::profile_function!(); |
| 28 | + |
| 29 | + /// Is this a `List<List<u8>>` ? |
| 30 | + fn is_list_list_u8(datatype: &DataType) -> bool { |
| 31 | + if let DataType::List(list_field) = datatype |
| 32 | + && let DataType::List(innermost_field) = list_field.data_type() |
| 33 | + { |
| 34 | + innermost_field.data_type() == &DataType::UInt8 |
| 35 | + } else { |
| 36 | + false |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + fn is_blob_field(field: &Field) -> bool { |
| 41 | + let components_with_blobs = [ |
| 42 | + "rerun.components.Blob", |
| 43 | + "rerun.components.ImageBuffer", |
| 44 | + "rerun.components.VideoSample", |
| 45 | + ]; |
| 46 | + |
| 47 | + if let Some(component_type) = field.metadata().get("rerun:component_type") |
| 48 | + && components_with_blobs.contains(&component_type.as_str()) |
| 49 | + { |
| 50 | + is_list_list_u8(field.data_type()) |
| 51 | + } else { |
| 52 | + false |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let needs_migration = batch |
| 57 | + .schema() |
| 58 | + .fields() |
| 59 | + .iter() |
| 60 | + .any(|field| is_blob_field(field)); |
| 61 | + |
| 62 | + if !needs_migration { |
| 63 | + return batch; |
| 64 | + } |
| 65 | + |
| 66 | + let num_columns = batch.num_columns(); |
| 67 | + let mut fields: Vec<FieldRef> = Vec::with_capacity(num_columns); |
| 68 | + let mut columns: Vec<ArrayRef> = Vec::with_capacity(num_columns); |
| 69 | + |
| 70 | + for (field, array) in itertools::izip!(batch.schema().fields(), batch.columns()) { |
| 71 | + if is_blob_field(field) { |
| 72 | + if let Some(new_array) = convert_list_list_u8_to_list_binary(array.as_ref()) { |
| 73 | + let new_field = Field::new( |
| 74 | + field.name(), |
| 75 | + new_array.data_type().clone(), |
| 76 | + field.is_nullable(), |
| 77 | + ) |
| 78 | + .with_metadata(field.metadata().clone()); |
| 79 | + |
| 80 | + fields.push(new_field.into()); |
| 81 | + columns.push(Arc::new(new_array)); |
| 82 | + |
| 83 | + re_log::debug_once!( |
| 84 | + "Changed datatype of '{}' from List[u8] to Binary", |
| 85 | + field.name() |
| 86 | + ); |
| 87 | + continue; |
| 88 | + } else { |
| 89 | + re_log::warn_once!("Failed to convert {} to Binary", field.name()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + fields.push(field.clone()); |
| 94 | + columns.push(array.clone()); |
| 95 | + } |
| 96 | + |
| 97 | + let schema = Arc::new(Schema::new_with_metadata( |
| 98 | + fields, |
| 99 | + batch.schema().metadata.clone(), |
| 100 | + )); |
| 101 | + |
| 102 | + RecordBatch::try_new_with_options( |
| 103 | + schema.clone(), |
| 104 | + columns, |
| 105 | + &RecordBatchOptions::default().with_row_count(Some(batch.num_rows())), |
| 106 | + ) |
| 107 | + .ok_or_log_error() |
| 108 | + .unwrap_or_else(|| RecordBatch::new_empty(schema)) |
| 109 | +} |
| 110 | + |
| 111 | +/// `List[List[u8]]` -> `List[Binary]` |
| 112 | +fn convert_list_list_u8_to_list_binary(list_array: &dyn Array) -> Option<ListArray> { |
| 113 | + re_tracing::profile_function!(); |
| 114 | + |
| 115 | + // The outer `List[List[u8]]` |
| 116 | + let list_array = list_array.as_list_opt()?; |
| 117 | + |
| 118 | + // The inner List[u8] array |
| 119 | + let inner_list_array: &ListArray = list_array.values().as_list_opt()?; |
| 120 | + |
| 121 | + // The underlying u8 values |
| 122 | + let u8_array: &UInt8Array = inner_list_array.values().as_primitive_opt()?; |
| 123 | + |
| 124 | + // We consistently use 64-bit offsets for binary data in order to keep our backwards-compatibility checks simpler. |
| 125 | + // Create the binary array reusing existing buffers |
| 126 | + let binary_array = arrow::array::LargeBinaryArray::try_new( |
| 127 | + arrow::buffer::OffsetBuffer::new( |
| 128 | + inner_list_array |
| 129 | + .offsets() |
| 130 | + .iter() |
| 131 | + .map(|&o| o as i64) |
| 132 | + .collect(), |
| 133 | + ), |
| 134 | + u8_array.values().clone().into_inner(), |
| 135 | + inner_list_array.nulls().cloned(), |
| 136 | + ) |
| 137 | + .ok()?; |
| 138 | + |
| 139 | + // Create the outer list array with binary inner type |
| 140 | + let outer_list = ListArray::try_new( |
| 141 | + Arc::new(Field::new("item", DataType::LargeBinary, true)), |
| 142 | + list_array.offsets().clone(), |
| 143 | + Arc::new(binary_array), |
| 144 | + list_array.nulls().cloned(), |
| 145 | + ) |
| 146 | + .ok()?; |
| 147 | + |
| 148 | + debug_assert_eq!(list_array.len(), outer_list.len()); |
| 149 | + |
| 150 | + Some(outer_list) |
| 151 | +} |
0 commit comments