Infers a columnar schema from a batch of ILP lines. Scans all lines to discover tag keys and field keys, then builds a schema: timestamp + tag columns (Symbol) + field columns (typed).
(lines: &[IlpLine<'_>])
| 19 | /// Scans all lines to discover tag keys and field keys, then builds |
| 20 | /// a schema: timestamp + tag columns (Symbol) + field columns (typed). |
| 21 | pub fn infer_schema(lines: &[IlpLine<'_>]) -> ColumnarSchema { |
| 22 | let mut tag_keys: Vec<String> = Vec::new(); |
| 23 | let mut field_keys: Vec<(String, ColumnType)> = Vec::new(); |
| 24 | let mut seen_tags: std::collections::HashSet<String> = std::collections::HashSet::new(); |
| 25 | let mut seen_fields: std::collections::HashSet<String> = std::collections::HashSet::new(); |
| 26 | |
| 27 | for line in lines { |
| 28 | for &(key, _) in &line.tags { |
| 29 | if seen_tags.insert(key.to_string()) { |
| 30 | tag_keys.push(key.to_string()); |
| 31 | } |
| 32 | } |
| 33 | for &(key, ref val) in &line.fields { |
| 34 | if seen_fields.insert(key.to_string()) { |
| 35 | let col_type = match val { |
| 36 | FieldValue::Float(_) => ColumnType::Float64, |
| 37 | FieldValue::Int(_) | FieldValue::UInt(_) => ColumnType::Int64, |
| 38 | FieldValue::Str(_) => ColumnType::Symbol, |
| 39 | FieldValue::Bool(_) => ColumnType::Float64, |
| 40 | }; |
| 41 | field_keys.push((key.to_string(), col_type)); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | let mut columns = Vec::with_capacity(1 + tag_keys.len() + field_keys.len()); |
| 47 | columns.push(("timestamp".to_string(), ColumnType::Timestamp)); |
| 48 | for tag in &tag_keys { |
| 49 | columns.push((tag.clone(), ColumnType::Symbol)); |
| 50 | } |
| 51 | for (field, ty) in &field_keys { |
| 52 | columns.push((field.clone(), *ty)); |
| 53 | } |
| 54 | |
| 55 | ColumnarSchema { |
| 56 | timestamp_idx: 0, |
| 57 | codecs: vec![nodedb_codec::ColumnCodec::Auto; columns.len()], |
| 58 | columns, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// Inject the three reserved bitemporal Int64 columns (`_ts_system`, |
| 63 | /// `_ts_valid_from`, `_ts_valid_until`) at the end of the schema when |