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

Add Field::new_list_field and improve DataType::new_list docs #4627

Merged
merged 7 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions arrow-schema/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,14 @@ impl DataType {
}
}

/// Create a List DataType default name is "item"
/// Create a [`DataType::List`] where each elements has the
alamb marked this conversation as resolved.
Show resolved Hide resolved
/// specified type and nullability and coventional name
alamb marked this conversation as resolved.
Show resolved Hide resolved
/// (`"item"`);
///
/// To specify field level metadata, construct the inner `Field`
/// directly via [`Field::new`] or [`Field::new_list_item`].
pub fn new_list(data_type: DataType, nullable: bool) -> Self {
DataType::List(Arc::new(Field::new("item", data_type, nullable)))
DataType::List(Arc::new(Field::new_list_item(data_type, nullable)))
}
}

Expand Down
20 changes: 19 additions & 1 deletion arrow-schema/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Hash for Field {
}

impl Field {
/// Creates a new field
/// Creates a new field with the given name, type, and nullability
pub fn new(name: impl Into<String>, data_type: DataType, nullable: bool) -> Self {
Field {
name: name.into(),
Expand All @@ -129,6 +129,24 @@ impl Field {
}
}

/// Creates a new field suitable for [`DataType::List`] and
/// [`DataType::LargeList`]
///
/// While not required, by convention the inner `Field` of these
/// types is named `"item"`
///
/// # Example
/// ```
/// # use arrow_schema::{Field, DataType};
/// assert_eq!(
/// Field::new("item", DataType::Int32, true),
/// Field::new_list_item(DataType::Int32, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

It does occur to me that this is now longer 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But it is more explict / discoverable

Maybe Field::new_list_field? or Field::new_list?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't feel strongly, I personally don't see the issue with "item" but we're going to have to agree to disagree there 😅

Copy link
Member

Choose a reason for hiding this comment

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

I guess new_list_field? As this is to create a field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea -- done in 76b1e70

/// );
/// ```
pub fn new_list_item(data_type: DataType, nullable: bool) -> Self {
Self::new("item", data_type, nullable)
}

/// Creates a new field that has additional dictionary information
pub fn new_dict(
name: impl Into<String>,
Expand Down
Loading