Skip to content

Commit 777bbae

Browse files
committed
logic to overriding known timestamp fields which were inferred as string fields
1 parent b8a3f53 commit 777bbae

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

server/src/event/format.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,48 @@ pub fn get_existing_fields(
162162
existing_fields
163163
}
164164

165+
pub fn get_existing_timestamp_fields(
166+
existing_schema: &HashMap<String, Arc<Field>>,
167+
) -> Vec<Arc<Field>> {
168+
let mut timestamp_fields = Vec::new();
169+
170+
for field in existing_schema.values() {
171+
if let DataType::Timestamp(TimeUnit::Millisecond, None) = field.data_type() {
172+
timestamp_fields.push(field.clone());
173+
}
174+
}
175+
176+
timestamp_fields
177+
}
178+
179+
pub fn override_timestamp_fields(
180+
inferred_schema: Arc<Schema>,
181+
existing_timestamp_fields: &[Arc<Field>],
182+
) -> Arc<Schema> {
183+
let timestamp_field_names: Vec<&str> = existing_timestamp_fields
184+
.iter()
185+
.map(|field| field.name().as_str())
186+
.collect();
187+
188+
let updated_fields: Vec<Arc<Field>> = inferred_schema
189+
.fields()
190+
.iter()
191+
.map(|field| {
192+
if timestamp_field_names.contains(&field.name().as_str()) {
193+
Arc::new(Field::new(
194+
field.name(),
195+
DataType::Timestamp(TimeUnit::Millisecond, None),
196+
field.is_nullable(),
197+
))
198+
} else {
199+
field.clone()
200+
}
201+
})
202+
.collect();
203+
204+
Arc::new(Schema::new(updated_fields))
205+
}
206+
165207
pub fn update_field_type_in_schema(
166208
inferred_schema: Arc<Schema>,
167209
existing_schema: Option<&HashMap<String, Arc<Field>>>,
@@ -172,6 +214,9 @@ pub fn update_field_type_in_schema(
172214

173215
if let Some(existing_schema) = existing_schema {
174216
let existing_fields = get_existing_fields(inferred_schema.clone(), Some(existing_schema));
217+
let existing_timestamp_fields = get_existing_timestamp_fields(existing_schema);
218+
// overriding known timestamp fields which were inferred as string fields
219+
updated_schema = override_timestamp_fields(updated_schema, &existing_timestamp_fields);
175220
let existing_field_names: Vec<String> = existing_fields
176221
.iter()
177222
.map(|field| field.name().clone())

0 commit comments

Comments
 (0)