Skip to content

ScalarValue::try_from_array holds onto input array for Struct, Lists types #15205

Open

Description

@Dandandan

Describe the bug

ScalarValue::try_from_array are used in areas such as accumulators.
Currently arrowslice is used for Struct/Union/List types, causing accumulators holding onto the original arrays (rather than a single value), and thus higher memory usage.

To Reproduce

Create an (struct / list type) array of size n and use ScalarValue::try_from_array to convert it into a ScalarValue.

Expected behavior

Value is copied into a new Array or there would be needing efficient way of representing the scalar value.
Memory usage should be corresponding to a single value rather than the full input array.

Additional context

No response

Activity

  1. changed the title [-]`ScalarValue::try_from_array` holds onto input array for Struct, Union, Lists types[/-] [+]`ScalarValue::try_from_array` holds onto input array for Struct, Lists types[/+] on Mar 13, 2025
  2. chenkovsky commented on Mar 16, 2025

    @chenkovsky
    Contributor

    take

  3. chenkovsky commented on Mar 18, 2025

    @chenkovsky
    Contributor

    Hi, @Dandandan .

    it seems that there's no direct way to deep copy array in arrow?
    so I tested the following code. it seems that it works.

    let a = array.slice(index, 1);
    
    // copy
    let data = a.to_data();
    let mut copy = MutableArrayData::new(vec![&data], true, data.len());
    copy.extend(0, 0, data.len());
    let a = make_array(copy.freeze());
    
    Self::Struct(Arc::new(a.as_struct().to_owned()))

    But I'm wondering whether it's better to provide a deep copy method for ScalarValue rather than modify try_from_array.
    then we can just copy the scalar value when it's needed since deep copy is expensive.

  4. bert-beyondloops commented on May 21, 2026

    @bert-beyondloops
    Contributor

    I personally would go for the fix behind the compact() functionality on ScalarValue

    See #21928

    In this way, the developer still has the option to just use the ScalarValue::try_from_array as is without any side effects of recopying memory and possible performance impact.

    If reducing memory footprint is really needed, a compact() call can be made explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingperformanceMake DataFusion faster

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions