Skip to content

Commit 225c3e5

Browse files
committed
fix: field name validation before processing static schema
1 parent c87b93c commit 225c3e5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/static_schema.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
2323
use std::str;
2424

2525
use arrow_schema::{DataType, Field, Schema, TimeUnit};
26-
use std::{collections::HashMap, sync::Arc};
26+
use std::{collections::{HashMap,HashSet} , sync::Arc};
2727
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2828
pub struct StaticSchema {
2929
fields: Vec<SchemaFields>,
@@ -87,7 +87,12 @@ pub fn convert_static_schema_to_arrow_schema(
8787
}
8888
}
8989
}
90+
91+
let mut existing_field_names: HashSet<String> = HashSet::new();
92+
9093
for mut field in static_schema.fields {
94+
95+
validate_field_names(&field.name, &mut existing_field_names)?;
9196
if !time_partition.is_empty() && field.name == time_partition {
9297
time_partition_exists = true;
9398
field.data_type = "datetime".to_string();
@@ -139,6 +144,7 @@ pub fn convert_static_schema_to_arrow_schema(
139144
fn add_parseable_fields_to_static_schema(
140145
parsed_schema: ParsedSchema,
141146
) -> Result<Arc<Schema>, AnyError> {
147+
142148
let mut schema: Vec<Arc<Field>> = Vec::new();
143149
for field in parsed_schema.fields.iter() {
144150
let field = Field::new(field.name.clone(), field.data_type.clone(), field.nullable);
@@ -176,3 +182,16 @@ fn default_dict_id() -> i64 {
176182
fn default_dict_is_ordered() -> bool {
177183
false
178184
}
185+
186+
fn validate_field_names(field_name: &str, existing_fields: &mut HashSet<String>) -> Result<(), AnyError> {
187+
188+
if field_name.is_empty() {
189+
return Err(anyhow!("field names should not be empty"));
190+
}
191+
192+
if !existing_fields.insert(field_name.to_string()) {
193+
return Err(anyhow!("duplicate field name: {}", field_name));
194+
}
195+
196+
Ok(())
197+
}

0 commit comments

Comments
 (0)