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 ListBuilder::append_value (#3949) #3954

Merged
merged 2 commits into from
Mar 28, 2023

Conversation

tustvold
Copy link
Contributor

Which issue does this PR close?

Part of #3949
Relates to #1841

Rationale for this change

The current ListBuilder API involves appending values to the ListBuilder::values() and then finishing the slot with ListBuilder::append. Whilst flexible, this API is rather verbose. Now that all the builders implement Extend it is possible to provide a more ergonomic API.

What changes are included in this PR?

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 26, 2023
T: Extend<Option<V>>,
I: IntoIterator<Item = Option<V>>,
{
self.extend(std::iter::once(Some(i)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least in some basic experiments LLVM seems to be able to optimise this as one would hope

https://rust.godbolt.org/z/zcxzqzf59

/// # use arrow_array::types::Int32Type;
/// let mut builder = ListBuilder::new(Int32Builder::new());
///
/// builder.append_value([Some(1), Some(2), Some(3)]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is much easier to read than the current API

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

I agree -- this is much nicer @tustvold -- thank you

/// delimiting the result with [`Self::append`]
///
/// ```
/// # use arrow_array::builder::{Int32Builder, ListBuilder};
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this example is redundant with

/// # Example
///
/// ```
/// # use arrow_array::builder::{StringBuilder, ListBuilder};
/// # use arrow_array::ListArray;
/// // Build a 3 element array of lists:
/// //
/// // column
/// // ---------
/// // [one]
/// // []
/// // [two, three]
///
/// let mut builder = ListBuilder::new(StringBuilder::new());
/// // [one]
/// builder.values().append_value("one");
/// builder.append(true);
/// // []
/// builder.append(true);
/// // [two, three]
/// builder.values().append_value("two");
/// builder.values().append_value("three");
/// builder.append(true);
///
/// // Create an array
/// let list_array: ListArray = builder.finish();
/// ```

self.extend(std::iter::once(Some(i)))
}

/// Append a null to this [`GenericListBuilder`]
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend adding a link to append_value for an example

Suggested change
/// Append a null to this [`GenericListBuilder`]
/// Append a null to this [`GenericListBuilder`]
///
/// See [`Self::append_value`] for an example use.


/// Appends an optional value into this [`GenericListBuilder`]
///
/// If `Some` calls [`Self::append_value`] otherwise calls [`Self::append_null`]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// If `Some` calls [`Self::append_value`] otherwise calls [`Self::append_null`]
/// If `Some` calls [`Self::append_value`] otherwise calls [`Self::append_null`]
///
/// See [`Self::append_value`] for an example use.

@tustvold tustvold merged commit eb36d37 into apache:master Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants