Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
add primitive testing
Browse files Browse the repository at this point in the history
  • Loading branch information
connortsui20 committed Feb 3, 2024
1 parent 9a2edad commit 089caa4
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
15 changes: 15 additions & 0 deletions type_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "type_test"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1"
arrow = "50"
datafusion = "35"
substrait = "0.24"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
rayon = "1"
63 changes: 63 additions & 0 deletions type_test/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use arrow::{
array::{Array, BooleanArray, Int32Array},
datatypes::{DataType, Field, Schema},
record_batch::RecordBatch,
};
use std::sync::Arc;

fn main() {
let schema = Arc::new(make_schema());
let columns = make_test_columns();
let rb = RecordBatch::try_new(schema, columns).unwrap();
println!("Record Batch Before Filter:\n{:?}", rb);
let rb = filter_record_batch(rb);
println!("Record Batch After Filter:\n{:?}", rb);
}

fn make_schema() -> Schema {
let field_a = Field::new("a", DataType::Int32, false);
let field_b = Field::new("b", DataType::Boolean, false);

Schema::new(vec![field_a, field_b])
}

fn make_test_columns() -> Vec<Arc<dyn Array>> {
let column_b = BooleanArray::from(vec![true, false, true, true, false]);
let column_a = Int32Array::from(vec![1, 2, 3, 4, 5]);
vec![Arc::new(column_a), Arc::new(column_b)]
}

fn filter_record_batch(rb: RecordBatch) -> RecordBatch {
// Filter on the int being even
let column_a = rb.column(0).clone();
let column_b = rb.column(1).clone();

assert_eq!(*column_a.data_type(), DataType::Int32);
assert_eq!(*column_b.data_type(), DataType::Boolean);

let array_a = column_a.into_data();
let buf_a = array_a.buffer::<i32>(0);

let buf_b: BooleanArray = column_b.into_data().into();

let mut out_vec_a = Vec::with_capacity(5);
let mut out_vec_b = Vec::with_capacity(5);

(0..5).for_each(|i| {
if buf_a[i] % 2 == 0 {
out_vec_a.push(buf_a[i]);
out_vec_b.push(buf_b.value(i));
}
});

let out_schema = rb.schema();

let out_column_a = Int32Array::from(out_vec_a);
let out_column_b = BooleanArray::from(out_vec_b);

RecordBatch::try_new(
out_schema,
vec![Arc::new(out_column_a), Arc::new(out_column_b)],
)
.unwrap()
}

0 comments on commit 089caa4

Please sign in to comment.