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 3 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 7 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 = { version = "52.0.0", features = ["prettyprint"], optional = true }
arrow = { version = "52.0.0", features = ["prettyprint"] }
itertools = "0"
num-traits = { version = "0.2", optional = true }
paste = "1.0"
Expand All @@ -35,5 +35,9 @@ tiledb-utils = { workspace = true }

[features]
default = []
proptest-strategies = ["dep:num-traits", "dep:proptest", "dep:proptest-derive", "dep:tiledb-test-utils"]
arrow = ["dep:arrow"]
proptest-strategies = [
"dep:num-traits",
"dep:proptest",
"dep:proptest-derive",
"dep:tiledb-test-utils",
]
154 changes: 154 additions & 0 deletions tiledb/api/examples/multi_range_subarray_arrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use std::path::PathBuf;
use std::sync::Arc;

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

use tiledb::array::{
Array, ArrayType, AttributeData, CellOrder, DimensionData, DomainData,
SchemaData, TileOrder,
};
use tiledb::context::Context;
use tiledb::error::Error as TileDBError;
use tiledb::query_arrow::{QueryBuilder, QueryLayout, QueryStatus, QueryType};
use tiledb::Result as TileDBResult;
use tiledb::{Datatype, Factory};

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, tiledb::array::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()
.map_err(|e| TileDBError::Other(format!("{e}")))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like an easy fix but this shouldn't be necessary to map the error from the tiledb API to turn it into a tiledb result.

It's not something to add to this PR but I tend to think we should go back to tiledb-rs and re-do our errors in the style that we've been doing in tables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just pushed a commit that cleans all this up. The first few examples I ignored it as orthogonal but then got annoyed at it. All I've done is pushed a impl From<MyError> for TileDBError though so the end result is the same by formatting into an Other variant. But the code is cleaner.

Beyond that, I totes agree that the error handling needs to be overhauled now that we've got a better handle on how things fit together.


let status = query
.submit()
.map_err(|e| TileDBError::Other(format!("{e}")))?;

if !matches!(status, QueryStatus::Completed) {
return Err(TileDBError::Other("Make this better.".to_string()));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems easy to miss this without something easy to search like TODO or FIXME or todo!()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, fixed now. I was just hurrying to get the examples updated.

}

let buffers = query
.buffers()
.map_err(|e| TileDBError::Other(format!("{e}")))?;

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 =
tiledb::Array::open(ctx, ARRAY_URI, tiledb::array::Mode::Write)?;

let mut query = QueryBuilder::new(array, QueryType::Write)
.with_layout(QueryLayout::RowMajor)
.start_fields()
.field_with_buffer("a", data)
.end_fields()
.build()
.map_err(|e| TileDBError::Other(format!("{e}")))?;

let (_, _) = query
.submit()
.and_then(|_| query.finalize())
.map_err(|e| TileDBError::Other(format!("{e}")))?;

Ok(())
}
Loading
Loading