Skip to content

Add "end to end parquet reading test" for WASM #15362

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions datafusion/wasmtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@ getrandom = { version = "0.2.8", features = ["js"] }
wasm-bindgen = "0.2.99"

[dev-dependencies]
insta = { workspace = true }
object_store = { workspace = true }
tokio = { workspace = true }
url = { workspace = true }
wasm-bindgen-test = "0.3.49"
71 changes: 61 additions & 10 deletions datafusion/wasmtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn basic_parse() {
#[cfg(test)]
mod test {
use super::*;
use datafusion::execution::options::ParquetReadOptions;
use datafusion::{
arrow::{
array::{ArrayRef, Int32Array, RecordBatch, StringArray},
Expand All @@ -90,12 +91,16 @@ mod test {
datasource::MemTable,
execution::context::SessionContext,
};
use datafusion_common::test_util::batches_to_string;
use datafusion_execution::{
config::SessionConfig, disk_manager::DiskManagerConfig,
runtime_env::RuntimeEnvBuilder,
};
use datafusion_physical_plan::collect;
use datafusion_sql::parser::DFParser;
use insta::assert_snapshot;
use object_store::{memory::InMemory, path::Path, ObjectStore};
use url::Url;
use wasm_bindgen_test::wasm_bindgen_test;

wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
Expand All @@ -115,6 +120,22 @@ mod test {
let session_config = SessionConfig::new().with_target_partitions(1);
Arc::new(SessionContext::new_with_config_rt(session_config, rt))
}

fn create_test_data() -> (Arc<Schema>, RecordBatch) {
let schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Int32, false),
Field::new("value", DataType::Utf8, false),
]));

let data: Vec<ArrayRef> = vec![
Arc::new(Int32Array::from(vec![1, 2, 3])),
Arc::new(StringArray::from(vec!["a", "b", "c"])),
];

let batch = RecordBatch::try_new(schema.clone(), data).unwrap();
(schema, batch)
}

#[wasm_bindgen_test(unsupported = tokio::test)]
async fn basic_execute() {
let sql = "SELECT 2 + 2;";
Expand Down Expand Up @@ -185,26 +206,56 @@ mod test {

#[wasm_bindgen_test(unsupported = tokio::test)]
async fn test_parquet_write() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems to me that this test is now redundant with "test_read_and_write" but looks good to me

let schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Int32, false),
Field::new("value", DataType::Utf8, false),
]));
let (schema, batch) = create_test_data();
let mut buffer = Vec::new();
let mut writer = datafusion::parquet::arrow::ArrowWriter::try_new(
&mut buffer,
schema.clone(),
None,
)
.unwrap();

let data: Vec<ArrayRef> = vec![
Arc::new(Int32Array::from(vec![1])),
Arc::new(StringArray::from(vec!["a"])),
];
writer.write(&batch).unwrap();
writer.close().unwrap();
}

let batch = RecordBatch::try_new(schema.clone(), data).unwrap();
#[wasm_bindgen_test(unsupported = tokio::test)]
async fn test_parquet_read_and_write() {
let (schema, batch) = create_test_data();
let mut buffer = Vec::new();
let mut writer = datafusion::parquet::arrow::ArrowWriter::try_new(
&mut buffer,
schema.clone(),
None,
)
.unwrap();

writer.write(&batch).unwrap();
writer.close().unwrap();

let session_ctx = SessionContext::new();
let store = InMemory::new();

let path = Path::from("a.parquet");
store.put(&path, buffer.into()).await.unwrap();

let url = Url::parse("memory://").unwrap();
session_ctx.register_object_store(&url, Arc::new(store));

let df = session_ctx
Copy link
Contributor

Choose a reason for hiding this comment

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

love it

.read_parquet("memory:///", ParquetReadOptions::new())
.await
.unwrap();

let result = df.collect().await.unwrap();

assert_snapshot!(batches_to_string(&result), @r"
+----+-------+
| id | value |
+----+-------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+-------+
");
}
}