parquet/arrow/schema/complex.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::collections::HashMap;
use std::sync::Arc;
use crate::arrow::schema::primitive::convert_primitive;
use crate::arrow::{ProjectionMask, PARQUET_FIELD_ID_META_KEY};
use crate::basic::{ConvertedType, Repetition};
use crate::errors::ParquetError;
use crate::errors::Result;
use crate::schema::types::{SchemaDescriptor, Type, TypePtr};
use arrow_schema::{DataType, Field, Fields, SchemaBuilder};
fn get_repetition(t: &Type) -> Repetition {
let info = t.get_basic_info();
match info.has_repetition() {
true => info.repetition(),
false => Repetition::REQUIRED,
}
}
/// Representation of a parquet schema element, in terms of arrow schema elements
#[derive(Debug, Clone)]
pub struct ParquetField {
/// The level which represents an insertion into the current list
/// i.e. guaranteed to be > 0 for an element of list type
pub rep_level: i16,
/// The level at which this field is fully defined,
/// i.e. guaranteed to be > 0 for a nullable type or child of a
/// nullable type
pub def_level: i16,
/// Whether this field is nullable
pub nullable: bool,
/// The arrow type of the column data
///
/// Note: In certain cases the data stored in parquet may have been coerced
/// to a different type and will require conversion on read (e.g. Date64 and Interval)
pub arrow_type: DataType,
/// The type of this field
pub field_type: ParquetFieldType,
}
impl ParquetField {
/// Converts `self` into an arrow list, with its current type as the field type
///
/// This is used to convert repeated columns, into their arrow representation
fn into_list(self, name: &str) -> Self {
ParquetField {
rep_level: self.rep_level,
def_level: self.def_level,
nullable: false,
arrow_type: DataType::List(Arc::new(Field::new(name, self.arrow_type.clone(), false))),
field_type: ParquetFieldType::Group {
children: vec![self],
},
}
}
/// Returns a list of [`ParquetField`] children if this is a group type
pub fn children(&self) -> Option<&[Self]> {
match &self.field_type {
ParquetFieldType::Primitive { .. } => None,
ParquetFieldType::Group { children } => Some(children),
}
}
}
#[derive(Debug, Clone)]
pub enum ParquetFieldType {
Primitive {
/// The index of the column in parquet
col_idx: usize,
/// The type of the column in parquet
primitive_type: TypePtr,
},
Group {
children: Vec<ParquetField>,
},
}
/// Encodes the context of the parent of the field currently under consideration
struct VisitorContext {
rep_level: i16,
def_level: i16,
/// An optional [`DataType`] sourced from the embedded arrow schema
data_type: Option<DataType>,
}
impl VisitorContext {
/// Compute the resulting definition level, repetition level and nullability
/// for a child field with the given [`Repetition`]
fn levels(&self, repetition: Repetition) -> (i16, i16, bool) {
match repetition {
Repetition::OPTIONAL => (self.def_level + 1, self.rep_level, true),
Repetition::REQUIRED => (self.def_level, self.rep_level, false),
Repetition::REPEATED => (self.def_level + 1, self.rep_level + 1, false),
}
}
}
/// Walks the parquet schema in a depth-first fashion in order to map it to arrow data structures
///
/// See [Logical Types] for more information on the conversion algorithm
///
/// [Logical Types]: https://github.com/apache/parquet-format/blob/master/LogicalTypes.md
struct Visitor {
/// The column index of the next leaf column
next_col_idx: usize,
/// Mask of columns to include
mask: ProjectionMask,
}
impl Visitor {
fn visit_primitive(
&mut self,
primitive_type: &TypePtr,
context: VisitorContext,
) -> Result<Option<ParquetField>> {
let col_idx = self.next_col_idx;
self.next_col_idx += 1;
if !self.mask.leaf_included(col_idx) {
return Ok(None);
}
let repetition = get_repetition(primitive_type);
let (def_level, rep_level, nullable) = context.levels(repetition);
let arrow_type = convert_primitive(primitive_type, context.data_type)?;
let primitive_field = ParquetField {
rep_level,
def_level,
nullable,
arrow_type,
field_type: ParquetFieldType::Primitive {
primitive_type: primitive_type.clone(),
col_idx,
},
};
Ok(Some(match repetition {
Repetition::REPEATED => primitive_field.into_list(primitive_type.name()),
_ => primitive_field,
}))
}
fn visit_struct(
&mut self,
struct_type: &TypePtr,
context: VisitorContext,
) -> Result<Option<ParquetField>> {
// The root type will not have a repetition level
let repetition = get_repetition(struct_type);
let (def_level, rep_level, nullable) = context.levels(repetition);
let parquet_fields = struct_type.get_fields();
// Extract the arrow fields
let arrow_fields = match &context.data_type {
Some(DataType::Struct(fields)) => {
if fields.len() != parquet_fields.len() {
return Err(arrow_err!(
"incompatible arrow schema, expected {} struct fields got {}",
parquet_fields.len(),
fields.len()
));
}
Some(fields)
}
Some(d) => {
return Err(arrow_err!(
"incompatible arrow schema, expected struct got {}",
d
))
}
None => None,
};
let mut child_fields = SchemaBuilder::with_capacity(parquet_fields.len());
let mut children = Vec::with_capacity(parquet_fields.len());
// Perform a DFS of children
for (idx, parquet_field) in parquet_fields.iter().enumerate() {
let data_type = match arrow_fields {
Some(fields) => {
let field = &fields[idx];
if field.name() != parquet_field.name() {
return Err(arrow_err!(
"incompatible arrow schema, expected field named {} got {}",
parquet_field.name(),
field.name()
));
}
Some(field.data_type().clone())
}
None => None,
};
let arrow_field = arrow_fields.map(|x| &*x[idx]);
let child_ctx = VisitorContext {
rep_level,
def_level,
data_type,
};
if let Some(child) = self.dispatch(parquet_field, child_ctx)? {
// The child type returned may be different from what is encoded in the arrow
// schema in the event of a mismatch or a projection
child_fields.push(convert_field(parquet_field, &child, arrow_field));
children.push(child);
}
}
if children.is_empty() {
return Ok(None);
}
let struct_field = ParquetField {
rep_level,
def_level,
nullable,
arrow_type: DataType::Struct(child_fields.finish().fields),
field_type: ParquetFieldType::Group { children },
};
Ok(Some(match repetition {
Repetition::REPEATED => struct_field.into_list(struct_type.name()),
_ => struct_field,
}))
}
fn visit_map(
&mut self,
map_type: &TypePtr,
context: VisitorContext,
) -> Result<Option<ParquetField>> {
let rep_level = context.rep_level + 1;
let (def_level, nullable) = match get_repetition(map_type) {
Repetition::REQUIRED => (context.def_level + 1, false),
Repetition::OPTIONAL => (context.def_level + 2, true),
Repetition::REPEATED => return Err(arrow_err!("Map cannot be repeated")),
};
if map_type.get_fields().len() != 1 {
return Err(arrow_err!(
"Map field must have exactly one key_value child, found {}",
map_type.get_fields().len()
));
}
// Add map entry (key_value) to context
let map_key_value = &map_type.get_fields()[0];
if map_key_value.get_basic_info().repetition() != Repetition::REPEATED {
return Err(arrow_err!("Child of map field must be repeated"));
}
if map_key_value.get_fields().len() != 2 {
// According to the specification the values are optional (#1642)
return Err(arrow_err!(
"Child of map field must have two children, found {}",
map_key_value.get_fields().len()
));
}
// Get key and value, and create context for each
let map_key = &map_key_value.get_fields()[0];
let map_value = &map_key_value.get_fields()[1];
match map_key.get_basic_info().repetition() {
Repetition::REPEATED => {
return Err(arrow_err!("Map keys cannot be repeated"));
}
Repetition::REQUIRED | Repetition::OPTIONAL => {
// Relaxed check for having repetition REQUIRED as there exists
// parquet writers and files that do not conform to this standard.
// This allows us to consume a broader range of existing files even
// if they are out of spec.
}
}
if map_value.get_basic_info().repetition() == Repetition::REPEATED {
return Err(arrow_err!("Map values cannot be repeated"));
}
// Extract the arrow fields
let (arrow_map, arrow_key, arrow_value, sorted) = match &context.data_type {
Some(DataType::Map(field, sorted)) => match field.data_type() {
DataType::Struct(fields) => {
if fields.len() != 2 {
return Err(arrow_err!(
"Map data type should contain struct with two children, got {}",
fields.len()
));
}
(Some(field), Some(&*fields[0]), Some(&*fields[1]), *sorted)
}
d => {
return Err(arrow_err!("Map data type should contain struct got {}", d));
}
},
Some(d) => {
return Err(arrow_err!(
"incompatible arrow schema, expected map got {}",
d
))
}
None => (None, None, None, false),
};
let maybe_key = {
let context = VisitorContext {
rep_level,
def_level,
data_type: arrow_key.map(|x| x.data_type().clone()),
};
self.dispatch(map_key, context)?
};
let maybe_value = {
let context = VisitorContext {
rep_level,
def_level,
data_type: arrow_value.map(|x| x.data_type().clone()),
};
self.dispatch(map_value, context)?
};
// Need both columns to be projected
match (maybe_key, maybe_value) {
(Some(key), Some(value)) => {
let key_field = Arc::new(
convert_field(map_key, &key, arrow_key)
// The key is always non-nullable (#5630)
.with_nullable(false),
);
let value_field = Arc::new(convert_field(map_value, &value, arrow_value));
let field_metadata = match arrow_map {
Some(field) => field.metadata().clone(),
_ => HashMap::default(),
};
let map_field = Field::new_struct(
map_key_value.name(),
[key_field, value_field],
false, // The inner map field is always non-nullable (#1697)
)
.with_metadata(field_metadata);
Ok(Some(ParquetField {
rep_level,
def_level,
nullable,
arrow_type: DataType::Map(Arc::new(map_field), sorted),
field_type: ParquetFieldType::Group {
children: vec![key, value],
},
}))
}
_ => Ok(None),
}
}
fn visit_list(
&mut self,
list_type: &TypePtr,
context: VisitorContext,
) -> Result<Option<ParquetField>> {
if list_type.is_primitive() {
return Err(arrow_err!(
"{:?} is a list type and can't be processed as primitive.",
list_type
));
}
let fields = list_type.get_fields();
if fields.len() != 1 {
return Err(arrow_err!(
"list type must have a single child, found {}",
fields.len()
));
}
let repeated_field = &fields[0];
if get_repetition(repeated_field) != Repetition::REPEATED {
return Err(arrow_err!("List child must be repeated"));
}
// If the list is nullable
let (def_level, nullable) = match list_type.get_basic_info().repetition() {
Repetition::REQUIRED => (context.def_level, false),
Repetition::OPTIONAL => (context.def_level + 1, true),
Repetition::REPEATED => return Err(arrow_err!("List type cannot be repeated")),
};
let arrow_field = match &context.data_type {
Some(DataType::List(f)) => Some(f.as_ref()),
Some(DataType::LargeList(f)) => Some(f.as_ref()),
Some(DataType::FixedSizeList(f, _)) => Some(f.as_ref()),
Some(d) => {
return Err(arrow_err!(
"incompatible arrow schema, expected list got {}",
d
))
}
None => None,
};
if repeated_field.is_primitive() {
// If the repeated field is not a group, then its type is the element type and elements are required.
//
// required/optional group my_list (LIST) {
// repeated int32 element;
// }
//
let context = VisitorContext {
rep_level: context.rep_level,
def_level,
data_type: arrow_field.map(|f| f.data_type().clone()),
};
return match self.visit_primitive(repeated_field, context) {
Ok(Some(mut field)) => {
// visit_primitive will infer a non-nullable list, update if necessary
field.nullable = nullable;
Ok(Some(field))
}
r => r,
};
}
let items = repeated_field.get_fields();
if items.len() != 1
|| repeated_field.name() == "array"
|| repeated_field.name() == format!("{}_tuple", list_type.name())
{
// If the repeated field is a group with multiple fields, then its type is the element type and elements are required.
//
// If the repeated field is a group with one field and is named either array or uses the LIST-annotated group's name
// with _tuple appended then the repeated type is the element type and elements are required.
let context = VisitorContext {
rep_level: context.rep_level,
def_level,
data_type: arrow_field.map(|f| f.data_type().clone()),
};
return match self.visit_struct(repeated_field, context) {
Ok(Some(mut field)) => {
field.nullable = nullable;
Ok(Some(field))
}
r => r,
};
}
// Regular list handling logic
let item_type = &items[0];
let rep_level = context.rep_level + 1;
let def_level = def_level + 1;
let new_context = VisitorContext {
def_level,
rep_level,
data_type: arrow_field.map(|f| f.data_type().clone()),
};
match self.dispatch(item_type, new_context) {
Ok(Some(item)) => {
let item_field = Arc::new(convert_field(item_type, &item, arrow_field));
// Use arrow type as hint for index size
let arrow_type = match context.data_type {
Some(DataType::LargeList(_)) => DataType::LargeList(item_field),
Some(DataType::FixedSizeList(_, len)) => {
DataType::FixedSizeList(item_field, len)
}
_ => DataType::List(item_field),
};
Ok(Some(ParquetField {
rep_level,
def_level,
nullable,
arrow_type,
field_type: ParquetFieldType::Group {
children: vec![item],
},
}))
}
r => r,
}
}
fn dispatch(
&mut self,
cur_type: &TypePtr,
context: VisitorContext,
) -> Result<Option<ParquetField>> {
if cur_type.is_primitive() {
self.visit_primitive(cur_type, context)
} else {
match cur_type.get_basic_info().converted_type() {
ConvertedType::LIST => self.visit_list(cur_type, context),
ConvertedType::MAP | ConvertedType::MAP_KEY_VALUE => {
self.visit_map(cur_type, context)
}
_ => self.visit_struct(cur_type, context),
}
}
}
}
/// Computes the [`Field`] for a child column
///
/// The resulting [`Field`] will have the type dictated by `field`, a name
/// dictated by the `parquet_type`, and any metadata from `arrow_hint`
fn convert_field(parquet_type: &Type, field: &ParquetField, arrow_hint: Option<&Field>) -> Field {
let name = parquet_type.name();
let data_type = field.arrow_type.clone();
let nullable = field.nullable;
match arrow_hint {
Some(hint) => {
// If the inferred type is a dictionary, preserve dictionary metadata
let field = match (&data_type, hint.dict_id(), hint.dict_is_ordered()) {
(DataType::Dictionary(_, _), Some(id), Some(ordered)) => {
Field::new_dict(name, data_type, nullable, id, ordered)
}
_ => Field::new(name, data_type, nullable),
};
field.with_metadata(hint.metadata().clone())
}
None => {
let mut ret = Field::new(name, data_type, nullable);
let basic_info = parquet_type.get_basic_info();
if basic_info.has_id() {
let mut meta = HashMap::with_capacity(1);
meta.insert(
PARQUET_FIELD_ID_META_KEY.to_string(),
basic_info.id().to_string(),
);
ret.set_metadata(meta);
}
ret
}
}
}
/// Computes the [`ParquetField`] for the provided [`SchemaDescriptor`] with `leaf_columns` listing
/// the indexes of leaf columns to project, and `embedded_arrow_schema` the optional
/// [`Fields`] embedded in the parquet metadata
///
/// Note: This does not support out of order column projection
pub fn convert_schema(
schema: &SchemaDescriptor,
mask: ProjectionMask,
embedded_arrow_schema: Option<&Fields>,
) -> Result<Option<ParquetField>> {
let mut visitor = Visitor {
next_col_idx: 0,
mask,
};
let context = VisitorContext {
rep_level: 0,
def_level: 0,
data_type: embedded_arrow_schema.map(|fields| DataType::Struct(fields.clone())),
};
visitor.dispatch(&schema.root_schema_ptr(), context)
}
/// Computes the [`ParquetField`] for the provided `parquet_type`
pub fn convert_type(parquet_type: &TypePtr) -> Result<ParquetField> {
let mut visitor = Visitor {
next_col_idx: 0,
mask: ProjectionMask::all(),
};
let context = VisitorContext {
rep_level: 0,
def_level: 0,
data_type: None,
};
Ok(visitor.dispatch(parquet_type, context)?.unwrap())
}