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 NullBuffer and BooleanBuffer From conversions #4380

Merged
merged 2 commits into from
Jun 8, 2023

Conversation

tustvold
Copy link
Contributor

@tustvold tustvold commented Jun 7, 2023

Which issue does this PR close?

Closes #.

Rationale for this change

Follow up to #4379 and #4338

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 Jun 7, 2023
/// // [1, 2, 3, 4]
/// let array = Int32Array::new(vec![1, 2, 3, 4].into(), None);
/// // [1, null, 3, 4]
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is unfortunate that arrow went with the name null buffer, and then made true mean not null. But hey ho

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree -- it should be called the ValidBuffer or else have defined 1 to mean null. As you say 🤷

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW could this be

Suggested change
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
/// let nulls = NullBuffer::from(&[true, false, true, true]);

And potentially avoid the allocation? I see you added that From impl 🤔

Copy link
Member

Choose a reason for hiding this comment

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

For From<&[bool]>, looks like it still does allocation.

Copy link
Contributor Author

@tustvold tustvold Jun 8, 2023

Choose a reason for hiding this comment

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

It would eliminate the allocation for the Vec, although the compiler might be smart enough to do that anyway, will update.

Edit this actually causes issues because it infers the type as &[bool; 4], will leave as vec

}
}

impl From<&[bool]> for NullBuffer {
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 debated adding custom conversion that would count the nulls, but I wasn't entirely convinced it would be faster - as the bit counting is very fast, so I left it for now. We can easily optimise later

}
}

impl From<&[bool]> for NullBuffer {
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 debated adding custom conversion that would count the nulls, but I wasn't entirely convinced it would be faster - as the bit counting is very fast, so I left it for now. We can easily optimise later

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.

Looks like a nice improvement to me

/// // [1, 2, 3, 4]
/// let array = Int32Array::new(vec![1, 2, 3, 4].into(), None);
/// // [1, null, 3, 4]
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree -- it should be called the ValidBuffer or else have defined 1 to mean null. As you say 🤷

Comment on lines +302 to +303
///
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

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

Giving a little context about when to use this constructor I think might help

Suggested change
///
/// ```
///
/// # Example
///
/// Creating a [`PrimitiveArray`] directly from a [`ScalarBuffer`] and [`NullBuffer`] ls generally
/// the most performant approach and avoids any additional allocations.
///
/// ```

Copy link
Member

Choose a reason for hiding this comment

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

+1

arrow-buffer/src/buffer/boolean.rs Show resolved Hide resolved
/// // [1, 2, 3, 4]
/// let array = Int32Array::new(vec![1, 2, 3, 4].into(), None);
/// // [1, null, 3, 4]
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW could this be

Suggested change
/// let nulls = NullBuffer::from(vec![true, false, true, true]);
/// let nulls = NullBuffer::from(&[true, false, true, true]);

And potentially avoid the allocation? I see you added that From impl 🤔

@tustvold tustvold merged commit 7fa82d6 into apache:master Jun 8, 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.

3 participants