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

DictionaryArray::values() clones the underlying ArrayRef #313

Closed
tustvold opened this issue May 17, 2021 · 0 comments · Fixed by #314
Closed

DictionaryArray::values() clones the underlying ArrayRef #313

tustvold opened this issue May 17, 2021 · 0 comments · Fixed by #314
Labels
api-change Changes to the arrow API arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog

Comments

@tustvold
Copy link
Contributor

The current implementation of DictionaryArray::values() is

/// Returns a reference to the dictionary values array
pub fn values(&self) -> ArrayRef {
    self.values.clone()
}

Aside from a redundant atomic increment on many codepaths, this prevents borrows from propagating correctly, making it challenging to borrow values out of a DictionaryArray.

For example consider a code snippet that wishes to return the first value from a DictionaryArray

fn test(array: &DictionaryArray<Int32Type>) -> &str {
    array.values().as_any().downcast_ref::<StringArray>().unwrap().value(0)
}

Currently this won't compile because the value returned by values() is a stack local temporary. Given one of the motivators of using dictionaries is to avoid redundant copying of the values, this seems unfortunate.

If instead the signature was

/// Returns a reference to the dictionary values array
pub fn values(&self) -> &ArrayRef {
    &self.values
}

The test function above would compile.

Another possibility would be to create a new method, e.g. values_ref(&self) but this seems like a bit of a hack, and returning &ArrayRef would make DictionaryArray consistent with RecordBatch::column.

@tustvold tustvold added the enhancement Any new improvement worthy of a entry in the changelog label May 17, 2021
tustvold added a commit to tustvold/arrow-rs that referenced this issue May 17, 2021
Signed-off-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
tustvold added a commit to tustvold/arrow-rs that referenced this issue May 17, 2021
Signed-off-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
alamb pushed a commit that referenced this issue May 21, 2021
Signed-off-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
alamb pushed a commit to alamb/arrow-rs that referenced this issue May 22, 2021
)

Signed-off-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
alamb pushed a commit to alamb/arrow-rs that referenced this issue May 22, 2021
)

Signed-off-by: Raphael Taylor-Davies <r.taylordavies@googlemail.com>
@alamb alamb added api-change Changes to the arrow API arrow Changes to the arrow crate labels Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change Changes to the arrow API arrow Changes to the arrow crate enhancement Any new improvement worthy of a entry in the changelog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants