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

Replace buffer management with Arrow buffers #173

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions tiledb/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ path = "src/lib.rs"

[dependencies]
anyhow = { workspace = true }
arrow = { workspace = true, optional = true }
arrow = { workspace = true }
cells = { workspace = true, features = ["proptest-strategies"], optional = true }
itertools = { workspace = true }
num-traits = { workspace = true, optional = true }
Expand All @@ -18,7 +18,7 @@ proptest = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
thiserror = { workspace = true }
tiledb-common = { workspace = true }
tiledb-common = { workspace = true, features = ["arrow"] }
tiledb-pod = { workspace = true, optional = true, features = ["serde"] }
tiledb-sys = { workspace = true }

Expand All @@ -36,7 +36,7 @@ tiledb-sys-cfg = { workspace = true }

[features]
default = []
arrow = ["dep:arrow", "dep:serde", "dep:serde_json", "tiledb-common/arrow", "tiledb-common/serde", "tiledb-pod/serde"]
arrow = ["dep:serde", "dep:serde_json", "tiledb-common/arrow", "tiledb-common/serde", "tiledb-pod/serde"]
pod = ["dep:tiledb-pod"]
proptest-strategies = ["dep:cells", "dep:proptest"]
serde = ["dep:serde", "dep:serde_json", "dep:tiledb-pod"]
Expand All @@ -45,6 +45,18 @@ serde = ["dep:serde", "dep:serde_json", "dep:tiledb-pod"]
name = "fragment_info"
required-features = ["serde"]

[[example]]
name = "multi_range_subarray_arrow"
required-features = ["pod"]

[[example]]
name = "quickstart_sparse_string_arrow"
required-features = ["pod"]

[[example]]
name = "reading_incomplete_arrow"
required-features = ["pod"]

[[example]]
name = "using_tiledb_stats"
required-features = ["serde"]
139 changes: 139 additions & 0 deletions tiledb/api/examples/multi_range_subarray_arrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use std::path::PathBuf;
use std::sync::Arc;

use arrow::array as aa;
use itertools::izip;

use tiledb_api::array::Array;
use tiledb_api::context::Context;
use tiledb_api::query_arrow::{QueryBuilder, QueryLayout, QueryType};
use tiledb_api::{Factory, Result as TileDBResult};
use tiledb_common::array::{ArrayType, CellOrder, Mode, TileOrder};
use tiledb_common::Datatype;
use tiledb_pod::array::{AttributeData, DimensionData, DomainData, SchemaData};

const ARRAY_URI: &str = "multi_range_slicing";

/// This example creates a 4x4 dense array with the contents:
///
/// Col: 1 2 3 4
/// Row: 1 1 2 3 4
/// 2 5 6 7 8
/// 3 9 10 11 12
/// 4 13 14 15 16
///
/// The query run restricts rows to [1, 2, 4] and returns all columns which
/// should produce these rows:
///
/// Row Col Value
/// 1 1 1
/// 1 2 2
/// 1 3 3
/// 1 4 4
/// 2 1 5
/// 2 2 6
/// 2 3 7
/// 2 4 8
/// 4 1 13
/// 4 2 14
/// 4 3 15
/// 4 4 16
fn main() -> TileDBResult<()> {
if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
let _ = std::env::set_current_dir(
PathBuf::from(manifest_dir).join("examples").join("output"),
);
}

let ctx = Context::new()?;
if Array::exists(&ctx, ARRAY_URI)? {
Array::delete(&ctx, ARRAY_URI)?;
}

create_array(&ctx)?;
write_array(&ctx)?;

let array = Array::open(&ctx, ARRAY_URI, Mode::Read)?;
let mut query = QueryBuilder::new(array, QueryType::Read)
.with_layout(QueryLayout::RowMajor)
.start_fields()
.field("rows")
.field("cols")
.field("a")
.end_fields()
.start_subarray()
.add_range("rows", &[1, 2])
.add_range("rows", &[4, 4])
.add_range("cols", &[1, 4])
.end_subarray()
.build()?;

let status = query.submit()?;
assert!(status.is_complete());

let buffers = query.buffers()?;

let rows = buffers.get::<aa::Int32Array>("rows").unwrap();
let cols = buffers.get::<aa::Int32Array>("cols").unwrap();
let attr = buffers.get::<aa::Int32Array>("a").unwrap();

for (r, c, a) in izip!(rows.values(), cols.values(), attr.values()) {
println!("{} {} {}", r, c, a);
}

Ok(())
}

fn create_array(ctx: &Context) -> TileDBResult<()> {
let schema = SchemaData {
array_type: ArrayType::Dense,
domain: DomainData {
dimension: vec![
DimensionData {
name: "rows".to_owned(),
datatype: Datatype::Int32,
constraints: ([1i32, 4], 4i32).into(),
filters: None,
},
DimensionData {
name: "cols".to_owned(),
datatype: Datatype::Int32,
constraints: ([1i32, 4], 4i32).into(),
filters: None,
},
],
},
attributes: vec![AttributeData {
name: "a".to_owned(),
datatype: Datatype::Int32,
..Default::default()
}],
tile_order: Some(TileOrder::RowMajor),
cell_order: Some(CellOrder::RowMajor),

..Default::default()
};

let schema = schema.create(ctx)?;
Array::create(ctx, ARRAY_URI, schema)?;
Ok(())
}

fn write_array(ctx: &Context) -> TileDBResult<()> {
let data = Arc::new(aa::Int32Array::from(vec![
1i32, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
]));

let array = Array::open(ctx, ARRAY_URI, Mode::Write)?;

let mut query = QueryBuilder::new(array, QueryType::Write)
.with_layout(QueryLayout::RowMajor)
.start_fields()
.field_with_buffer("a", data)
.end_fields()
.build()?;

let (_, _) = query.submit().and_then(|_| query.finalize())?;

Ok(())
}
Loading
Loading