Skip to content

Commit 3a098b4

Browse files
authored
duckdb: forbid reading files with duplicate struct field names (#8917)
Resolves: #8710
1 parent 3816cb3 commit 3a098b4

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎vortex-duckdb/src/convert/dtype.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use vortex_geo::extension::MultiPolygon;
6565
use vortex_geo::extension::Point;
6666
use vortex_geo::extension::Polygon;
6767
use vortex_geo::extension::WellKnownBinary;
68+
use vortex_utils::aliases::hash_set::HashSet;
6869

6970
use crate::cpp::DUCKDB_TYPE;
7071
use crate::duckdb::LogicalType;
@@ -325,10 +326,14 @@ impl TryFrom<&StructFields> for LogicalType {
325326
.map(|field_dtype| LogicalType::try_from(&field_dtype))
326327
.collect::<Result<_, _>>()?;
327328

329+
let mut name_set = HashSet::new();
328330
let child_names: Vec<CString> = struct_type
329331
.names()
330332
.iter()
331333
.map(|field_name| {
334+
if name_set.replace(field_name.as_ref()).is_some() {
335+
vortex_bail!("Duplicate field '{field_name}'");
336+
}
332337
CString::new(field_name.as_ref())
333338
.map_err(|e| vortex_err!("Invalid field name '{field_name}': {e}"))
334339
})

‎vortex-duckdb/src/e2e_test/vortex_scan_test.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ffi::CStr;
77
use std::path::Path;
88
use std::slice;
99
use std::str::FromStr;
10+
use std::sync::Arc;
1011

1112
use anyhow::Result;
1213
use geo_types::LineString;
@@ -37,6 +38,7 @@ use vortex::dtype::PType;
3738
use vortex::encodings::fastlanes::RLEData;
3839
use vortex::file::WriteOptionsSessionExt;
3940
use vortex::io::runtime::BlockingRuntime;
41+
use vortex::layout::layouts::flat::writer::FlatLayoutStrategy;
4042
use vortex::scalar::PValue;
4143
use vortex::scalar::Scalar;
4244
use vortex_array::arrays::ExtensionArray;
@@ -1112,3 +1114,35 @@ fn test_vortex_scan_fixed_size_list_length_projection() {
11121114
assert_eq!(lengths, vec![4i64; 6], "{func}(int_lists) mismatch");
11131115
}
11141116
}
1117+
1118+
/// Vortex allows duplicate struct names but duckdb doesn't. Ensure we can't
1119+
/// read a file if names are not unique
1120+
#[test]
1121+
fn test_duplicate_struct_fields() {
1122+
let array = StructArray::try_from_iter([
1123+
("a", buffer![1i32, 2, 3].into_array()),
1124+
("a", buffer![10i64, 20, 30].into_array()),
1125+
])
1126+
.unwrap();
1127+
let array = StructArray::try_from_iter([("s", array)])
1128+
.unwrap()
1129+
.into_array();
1130+
let path = create_temp_file();
1131+
RUNTIME.block_on(async {
1132+
let mut file = async_fs::File::create(&path).await.unwrap();
1133+
SESSION
1134+
.write_options()
1135+
.with_strategy(Arc::new(FlatLayoutStrategy::default()))
1136+
.write(&mut file, array.to_array_stream())
1137+
.await
1138+
.unwrap()
1139+
});
1140+
let conn = database_connection();
1141+
let path = path.path().to_string_lossy();
1142+
1143+
assert!(conn.query(&format!("SELECT s FROM '{path}'")).is_err());
1144+
assert!(
1145+
conn.query(&format!("SELECT string_agg(s::VARCHAR, '') FROM '{path}'"))
1146+
.is_err()
1147+
);
1148+
}

0 commit comments

Comments
 (0)