Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Schema::all_fields to flattened_fields #6001

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arrow-flight/tests/flight_sql_client_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ impl FlightSqlService for FlightSqlServiceImpl {
.try_collect::<Vec<_>>()
.await?;

for (left, right) in parameters[0].schema().all_fields().iter().zip(vec![
for (left, right) in parameters[0].schema().flattened_fields().iter().zip(vec![
Field::new("$1", DataType::Utf8, false),
Field::new("$2", DataType::Int64, true),
]) {
Expand Down
2 changes: 1 addition & 1 deletion arrow-ipc/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ impl IpcDataGenerator {
write_options: &IpcWriteOptions,
) -> Result<(Vec<EncodedData>, EncodedData), ArrowError> {
let schema = batch.schema();
let mut encoded_dictionaries = Vec::with_capacity(schema.all_fields().len());
let mut encoded_dictionaries = Vec::with_capacity(schema.flattened_fields().len());

let mut dict_id = dictionary_tracker.dict_ids.clone().into_iter();

Expand Down
2 changes: 1 addition & 1 deletion arrow-json/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ impl ReaderBuilder {

let decoder = make_decoder(data_type, self.coerce_primitive, self.strict_mode, nullable)?;

let num_fields = self.schema.all_fields().len();
let num_fields = self.schema.flattened_fields().len();

Ok(Decoder {
decoder,
Expand Down
44 changes: 43 additions & 1 deletion arrow-schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,53 @@ impl Schema {
}

/// Returns a vector with references to all fields (including nested fields)
lewiszlw marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Example
///
/// ```
/// use std::sync::Arc;
/// use arrow_schema::{DataType, Field, Fields, Schema};
///
/// let f1 = Arc::new(Field::new("a", DataType::Boolean, false));
///
/// let f2_inner = Arc::new(Field::new("b_inner", DataType::Int8, false));
/// let f2 = Arc::new(Field::new("b", DataType::List(f2_inner.clone()), false));
///
/// let f3_inner1 = Arc::new(Field::new("c_inner1", DataType::Int8, false));
/// let f3_inner2 = Arc::new(Field::new("c_inner2", DataType::Int8, false));
/// let f3 = Arc::new(Field::new(
/// "c",
/// DataType::Struct(vec![f3_inner1.clone(), f3_inner2.clone()].into()),
/// false
/// ));
///
/// let mut schema = Schema::new(vec![
/// f1.clone(), f2.clone(), f3.clone()
/// ]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/// assert_eq!(
/// schema.flattened_fields(),
/// vec![
/// f1.as_ref(),
/// f2.as_ref(),
/// f2_inner.as_ref(),
/// f3.as_ref(),
/// f3_inner1.as_ref(),
/// f3_inner2.as_ref()
/// ]
/// );
/// ```
#[inline]
lewiszlw marked this conversation as resolved.
Show resolved Hide resolved
pub fn all_fields(&self) -> Vec<&Field> {
pub fn flattened_fields(&self) -> Vec<&Field> {
self.fields.iter().flat_map(|f| f.fields()).collect()
}

/// Returns a vector with references to all fields (including nested fields)
#[deprecated(since = "52.1.0", note = "Use `flattened_fields` instead")]
alamb marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn all_fields(&self) -> Vec<&Field> {
self.flattened_fields()
}

/// Returns an immutable reference of a specific [`Field`] instance selected using an
/// offset within the internal `fields` vector.
///
Expand Down
2 changes: 1 addition & 1 deletion parquet/src/arrow/async_reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ mod tests {
#[tokio::test]
async fn test_parquet_record_batch_stream_schema() {
fn get_all_field_names(schema: &Schema) -> Vec<&String> {
schema.all_fields().iter().map(|f| f.name()).collect()
schema.flattened_fields().iter().map(|f| f.name()).collect()
}

// ParquetRecordBatchReaderBuilder::schema differs from
Expand Down
Loading