Skip to content

Support Creating Non-Nullable Lists with ListBuilder #5330

Closed

Description

@dvic

Describe the bug
When I try to write a non-null List<non-null i32> to a parquet file, I get an error. Might be related to #385.

To Reproduce

use std::{error::Error, fs::File, sync::Arc};

use ::arrow::record_batch::RecordBatch;
use arrow::{
    array::{Int32Builder, ListBuilder},
    datatypes::{DataType, Field, Schema},
};

use parquet::arrow::arrow_writer::ArrowWriter;

pub fn main() -> Result<(), Box<dyn Error>> {
    let mut builder = ListBuilder::new(Int32Builder::new());

    builder.append_value([Some(1), Some(2), Some(3)]);

    let array = builder.finish();

    let inner_nullable = false;

    let schema = Arc::new(Schema::new(vec![Field::new_list(
        "col_a",
        Field::new_list_field(DataType::Int32, inner_nullable),
        false,
    )]));

    let batch = RecordBatch::try_new(schema, vec![Arc::new(array)])?;

    let buf = File::create("test.parquet")?;
    let mut writer = ArrowWriter::try_new(buf, batch.schema(), None)?;
    writer.write(&batch)?;
    writer.close()?;

    Ok(())
}

Expected behavior
I expect no errors, but instead I get:

Error: InvalidArgumentError("column types must match schema types, 

expected
List(Field { name: \"item\", data_type: Int32, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} })

but found 
List(Field { name: \"item\", data_type: Int32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} })

at column index 0")

Activity

  1. added
    bug
    on Jan 24, 2024
  2. tustvold commented on Jan 24, 2024

    @tustvold
    Contributor

    This is actually a known limitation of ListBuilder, where it can only produce nullable lists. I think there is already an issue about this, but I can't find it.

    You will need to use the ListArray constructors, potentially with the components produced by ListBuilder

    Edit: the feature is actually pretty straightforward so bashed it out in #5331

  3. added
    enhancement
    and removed
    bug
    on Jan 24, 2024
  4. changed the title [-]Cannot write parquet file with list of non-nullable item[/-] [+]Support Creating Non-Nullable Lists with ListBuilder[/+] on Jan 24, 2024
  5. dvic commented on Jan 25, 2024

    @dvic
    ContributorAuthor

    Edit: the feature is actually pretty straightforward so bashed it out in #5331

    Thanks! I actually just wanted to propose something like this :) (I saw the todo line at https://docs.rs/arrow-array/50.0.0/src/arrow_array/builder/generic_list_builder.rs.html#313)

  6. added
    arrow
    on Mar 1, 2024
  7. tustvold commented on Mar 1, 2024

    @tustvold
    Contributor

    label_issue.py automatically added labels {'arrow'} from #5331

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrowenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions