|
| 1 | +//! |
| 2 | +//! Simple schema mappings to help turn defined schemas into Arrow schemas |
| 3 | +
|
| 4 | +use arrow_schema::DataType; |
| 5 | +use serde::Deserialize; |
| 6 | +use tracing::log::*; |
| 7 | + |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +/// Supported schema definition types for the configuration of hotdog |
| 11 | +#[derive(Clone, Debug, Deserialize, PartialEq)] |
| 12 | +#[serde(rename_all = "lowercase")] |
| 13 | +pub enum FieldType { |
| 14 | + String, |
| 15 | + Struct, |
| 16 | + Long, |
| 17 | + Integer, |
| 18 | + Timestamp, |
| 19 | + Float, |
| 20 | + Boolean, |
| 21 | +} |
| 22 | + |
| 23 | +/// Helpful converter for [arrow_schema::Schema] conversion |
| 24 | +impl Into<DataType> for &FieldType { |
| 25 | + fn into(self) -> DataType { |
| 26 | + match self { |
| 27 | + FieldType::String => DataType::Utf8, |
| 28 | + FieldType::Boolean => DataType::Boolean, |
| 29 | + FieldType::Integer => DataType::Int32, |
| 30 | + FieldType::Long => DataType::Int64, |
| 31 | + FieldType::Float => DataType::Float64, |
| 32 | + FieldType::Timestamp => DataType::Timestamp(arrow_schema::TimeUnit::Millisecond, None), |
| 33 | + unknown => { |
| 34 | + warn!( |
| 35 | + "Asked to convert a schema type which cannot be automatically converted: {unknown:?}" |
| 36 | + ); |
| 37 | + DataType::Null |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// A field in the schema, which supports recursion. The name of the field is expected to be known |
| 44 | +/// by the container of the field |
| 45 | +#[derive(Clone, Debug, Deserialize)] |
| 46 | +pub struct Field { |
| 47 | + r#type: FieldType, |
| 48 | + fields: Option<HashMap<String, Field>>, |
| 49 | +} |
| 50 | + |
| 51 | +impl Field { |
| 52 | + /// Retrieve the given [FieldType] |
| 53 | + pub fn get_type(&self) -> &FieldType { |
| 54 | + &self.r#type |
| 55 | + } |
| 56 | + |
| 57 | + /// Retrieve the nested fields should they exist |
| 58 | + pub fn fields(&self) -> Option<&HashMap<String, Field>> { |
| 59 | + self.fields.as_ref() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Convert the given set of fields to an [arrow_schema::Schema] for use elsewher einside of hotdog |
| 64 | +pub fn into_arrow_schema( |
| 65 | + fields: &HashMap<String, Field>, |
| 66 | +) -> Result<arrow_schema::Schema, arrow_schema::ArrowError> { |
| 67 | + let mut arrow_fields = vec![]; |
| 68 | + |
| 69 | + for (name, field) in fields { |
| 70 | + if field.get_type() == &FieldType::Struct { |
| 71 | + if let Some(fields) = field.fields.as_ref() { |
| 72 | + let struct_fields = into_arrow_schema(fields)?; |
| 73 | + arrow_fields.push(arrow_schema::Field::new( |
| 74 | + name, |
| 75 | + DataType::Struct(struct_fields.fields), |
| 76 | + true, |
| 77 | + )); |
| 78 | + } else { |
| 79 | + error!( |
| 80 | + "I don't know how to handle structs without fields! Calling it null :shrug:" |
| 81 | + ); |
| 82 | + arrow_fields.push(arrow_schema::Field::new(name, DataType::Null, true)); |
| 83 | + } |
| 84 | + } else { |
| 85 | + arrow_fields.push(arrow_schema::Field::new( |
| 86 | + name, |
| 87 | + field.get_type().into(), |
| 88 | + true, |
| 89 | + )); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Ok(arrow_schema::Schema::new(arrow_fields)) |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_deser_simple_field() { |
| 102 | + let buf = r#" |
| 103 | +--- |
| 104 | +version: |
| 105 | + type: string |
| 106 | +"#; |
| 107 | + let fields: HashMap<String, Field> = |
| 108 | + serde_yaml::from_str(buf).expect("Failed to deserialize a simple string field"); |
| 109 | + |
| 110 | + if let Some(field) = fields.get("version") { |
| 111 | + assert_eq!(field.get_type(), &FieldType::String); |
| 112 | + } else { |
| 113 | + panic!("Failed to get a version field"); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_deser_nested_map() { |
| 119 | + let buf = r#" |
| 120 | +--- |
| 121 | +meta: |
| 122 | + type: struct |
| 123 | + fields: |
| 124 | + version: |
| 125 | + type: string |
| 126 | +"#; |
| 127 | + let fields: HashMap<String, Field> = |
| 128 | + serde_yaml::from_str(buf).expect("Failed to deserialize a nested field"); |
| 129 | + |
| 130 | + if let Some(field) = fields.get("meta") { |
| 131 | + assert_eq!(field.get_type(), &FieldType::Struct); |
| 132 | + |
| 133 | + let nested = field |
| 134 | + .fields |
| 135 | + .as_ref() |
| 136 | + .expect("Failed to get the nested fields"); |
| 137 | + assert_eq!( |
| 138 | + nested.get("version").unwrap().get_type(), |
| 139 | + &FieldType::String |
| 140 | + ); |
| 141 | + } else { |
| 142 | + panic!("Failed to get a timestamp field"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /// The whole point of this schema definition is to get to an arrow schema definition! |
| 147 | + #[test] |
| 148 | + fn test_convert_simple_to_arrow() { |
| 149 | + let buf = r#" |
| 150 | +--- |
| 151 | +version: |
| 152 | + type: string |
| 153 | +"#; |
| 154 | + let fields: HashMap<String, Field> = |
| 155 | + serde_yaml::from_str(buf).expect("Failed to deserialize a nested field"); |
| 156 | + |
| 157 | + let arr: arrow_schema::Schema = |
| 158 | + into_arrow_schema(&fields).expect("This is a valid arrow schema"); |
| 159 | + assert_ne!( |
| 160 | + arr.fields.len(), |
| 161 | + 0, |
| 162 | + "The converted schema should have one field" |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_convert_nested_to_arrow() { |
| 168 | + let buf = r#" |
| 169 | +--- |
| 170 | +meta: |
| 171 | + type: struct |
| 172 | + fields: |
| 173 | + version: |
| 174 | + type: string |
| 175 | +"#; |
| 176 | + let fields: HashMap<String, Field> = |
| 177 | + serde_yaml::from_str(buf).expect("Failed to deserialize a nested field"); |
| 178 | + |
| 179 | + let arr: arrow_schema::Schema = |
| 180 | + into_arrow_schema(&fields).expect("This is a valid arrow schema"); |
| 181 | + |
| 182 | + let (_size, field) = arr |
| 183 | + .fields |
| 184 | + .find("meta") |
| 185 | + .expect("Failed to find the meta field"); |
| 186 | + |
| 187 | + let struct_fields = arrow_schema::Fields::from(vec![arrow_schema::Field::new( |
| 188 | + "version", |
| 189 | + DataType::Utf8, |
| 190 | + true, |
| 191 | + )]); |
| 192 | + let struct_type = DataType::Struct(struct_fields); |
| 193 | + |
| 194 | + assert_eq!(field.data_type(), &struct_type); |
| 195 | + } |
| 196 | +} |
0 commit comments