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

Support with_projection and with_columns in ipc and parquet reader in eager mode #1751

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions polars/polars-io/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ impl<R: Read + Seek> IpcReader<R> {
self
}

/// Columns to select/ project
pub fn with_columns(mut self, columns: Option<Vec<String>>) -> Self {
self.columns = columns;
self
}

/// Set the reader's column projection. This counts from 0, meaning that
/// `vec![0, 4]` would select the 1st and 5th column.
pub fn with_projection(mut self, projection: Option<Vec<usize>>) -> Self {
self.projection = projection;
self
Expand Down Expand Up @@ -161,6 +164,9 @@ where
let i = schema.index_of(column)?;
prj.push(i)
}

// Ipc reader panics if the projection is not in increasing order, so sorting is the safer way.
prj.sort();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, we can remove duplicates, but It seems that it would be more helpful to return an error if there is a duplicate.

It's not good to get an error in select C, B, but wouldn't it be better to get an error in select B, B?

Copy link
Member

Choose a reason for hiding this comment

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

I prefer returning an error indeed. The API expects unique columns.

self.projection = Some(prj)
}

Expand Down Expand Up @@ -219,6 +225,8 @@ where
#[cfg(test)]
mod test {
use crate::prelude::*;
use polars_core::prelude::*;
use polars_core::df;
use std::io::Cursor;

#[test]
Expand All @@ -235,4 +243,32 @@ mod test {
let df_read = IpcReader::new(buf).finish().unwrap();
assert!(df.frame_equal(&df_read));
}

#[test]
fn test_read_ipc_with_projection() {
let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
let df = df!("a" => [1, 2, 3], "b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();

IpcWriter::new(&mut buf).finish(&df).expect("ipc writer");
buf.set_position(0);

let expected = df!("b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();
let df_read = IpcReader::new(buf).with_projection(Some(vec![1, 2])).finish().unwrap();
assert_eq!(df_read.shape(), (3, 2));
df_read.frame_equal(&expected);
}

#[test]
fn test_read_ipc_with_columns() {
let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
let df = df!("a" => [1, 2, 3], "b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();

IpcWriter::new(&mut buf).finish(&df).expect("ipc writer");
buf.set_position(0);

let expected = df!("b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();
let df_read = IpcReader::new(buf).with_columns(Some(vec!["c".to_string(), "b".to_string()])).finish().unwrap();
assert_eq!(df_read.shape(), (3, 2));
df_read.frame_equal(&expected);
}
}
32 changes: 32 additions & 0 deletions polars/polars-io/src/parquet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ where
self
}

/// Columns to select/ project
pub fn with_columns(mut self, columns: Option<Vec<String>>) -> Self {
self.columns = columns;
self
}

/// Set the reader's column projection. This counts from 0, meaning that
/// `vec![0, 4]` would select the 1st and 5th column.
pub fn with_projection(mut self, projection: Option<Vec<usize>>) -> Self {
self.projection = projection;
self
Expand Down Expand Up @@ -282,6 +285,7 @@ mod test {
use crate::prelude::*;
use polars_core::{df, prelude::*};
use std::fs::File;
use std::io::Cursor;

#[test]
fn test_parquet() {
Expand Down Expand Up @@ -315,4 +319,32 @@ mod test {
assert!(read.frame_equal_missing(&df));
Ok(())
}

#[test]
fn test_read_parquet_with_projection() {
let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
let df = df!("a" => [1, 2, 3], "b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();

ParquetWriter::new(&mut buf).finish(&df).expect("parquet writer");
buf.set_position(0);

let expected = df!("b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();
let df_read = ParquetReader::new(buf).with_projection(Some(vec![1, 2])).finish().unwrap();
assert_eq!(df_read.shape(), (3, 2));
df_read.frame_equal(&expected);
}

#[test]
fn test_read_parquet_with_columns() {
let mut buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
let df = df!("a" => [1, 2, 3], "b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();

ParquetWriter::new(&mut buf).finish(&df).expect("parquet writer");
buf.set_position(0);

let expected = df!("b" => [2, 3, 4], "c" => [3, 4, 5]).unwrap();
let df_read = ParquetReader::new(buf).with_columns(Some(vec!["c".to_string(), "b".to_string()])).finish().unwrap();
assert_eq!(df_read.shape(), (3, 2));
df_read.frame_equal(&expected);
}
}