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

refactor: project by name #282

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn main() {
.scan((Bound::Included(&name), Bound::Excluded(&upper)))
.await
// tonbo supports pushing down projection
.projection(vec![1])
.projection(&["email"])
.take()
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion bindings/js/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Transaction {
>(high.as_ref())
},
))
.projection(projection);
.projection_with_index(projection);

if let Some(limit) = limit {
scan = scan.limit(limit);
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Transaction {
if let Some(limit) = limit {
scan = scan.limit(limit);
}
scan = scan.projection(projection);
scan = scan.projection_with_index(projection);
let stream = scan.take().await.map_err(DbError::from)?;

let stream = ScanStream::new(stream);
Expand Down
2 changes: 1 addition & 1 deletion examples/datafusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl ExecutionPlan for MusicExec {
scan = scan.limit(limit);
}
if let Some(projection) = projection {
scan = scan.projection(projection.clone());
scan = scan.projection_with_index(projection.clone());
}
let mut scan = scan.package(8192).await.map_err(|err| DataFusionError::Internal(err.to_string()))?;

Expand Down
2 changes: 1 addition & 1 deletion examples/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main() {
let mut scan = txn
.scan((Bound::Included(&name), Bound::Excluded(&upper)))
// tonbo supports pushing down projection
.projection(vec![1, 3])
.projection(&["email", "bytes"])
// push down limitation
.limit(1)
.take()
Expand Down
42 changes: 35 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
//! let mut scan = txn
//! .scan((Bound::Included(&name), Bound::Excluded(&upper)))
//! // tonbo supports pushing down projection
//! .projection(vec![1])
//! .projection(&["email"])
//! .take()
//! .await
//! .unwrap();
Expand Down Expand Up @@ -754,7 +754,35 @@ where
}

/// fields in projection Record by field indices
pub fn projection(self, mut projection: Vec<usize>) -> Self {
pub fn projection(self, projection: &[&str]) -> Self {
let schema = self.schema.record_schema.arrow_schema();
let mut projection = projection
.iter()
.map(|name| {
schema
.index_of(name)
.unwrap_or_else(|_| panic!("unexpected field {}", name))
})
.collect::<Vec<usize>>();
let primary_key_index = self.schema.record_schema.primary_key_index();
let mut fixed_projection = vec![0, 1, primary_key_index];
fixed_projection.append(&mut projection);
fixed_projection.dedup();

let mask = ProjectionMask::roots(
&ArrowSchemaConverter::new().convert(schema).unwrap(),
fixed_projection.clone(),
);

Self {
projection: mask,
projection_indices: Some(fixed_projection),
..self
}
}

/// fields in projection Record by field indices
pub fn projection_with_index(self, mut projection: Vec<usize>) -> Self {
// skip two columns: _null and _ts
for p in &mut projection {
*p += USER_COLUMN_OFFSET;
Expand Down Expand Up @@ -1605,7 +1633,7 @@ pub(crate) mod tests {
let tx = db.transaction().await;
let mut scan = tx
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![0, 1, 2])
.projection(&["id", "age", "height"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -1794,7 +1822,7 @@ pub(crate) mod tests {
let upper = Value::new(DataType::Int64, "id".to_owned(), Arc::new(49_i64), false);
let mut scan = tx
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![0, 2, 7])
.projection(&["id", "height", "bytes"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -1996,7 +2024,7 @@ pub(crate) mod tests {
let upper = Value::new(DataType::Int64, "id".to_owned(), Arc::new(43_i64), false);
let mut scan = tx1
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![0, 1])
.projection(&["id", "age"])
.take()
.await
.unwrap();
Expand All @@ -2023,7 +2051,7 @@ pub(crate) mod tests {
let tx2 = db2.transaction().await;
let mut scan = tx2
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![0, 1])
.projection(&["id", "age"])
.take()
.await
.unwrap();
Expand All @@ -2050,7 +2078,7 @@ pub(crate) mod tests {
let tx3 = db3.transaction().await;
let mut scan = tx3
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![0, 1])
.projection(&["id", "age"])
.take()
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mod tests {

let mut stream = snapshot
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand Down
22 changes: 11 additions & 11 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ mod tests {

let mut stream = txn
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -623,7 +623,7 @@ mod tests {
{
let mut stream = txn2
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -640,7 +640,7 @@ mod tests {
{
let mut stream = txn2
.scan((Bound::Included(&lower), Bound::Excluded(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -654,7 +654,7 @@ mod tests {
{
let mut stream = txn2
.scan((Bound::Excluded(&lower), Bound::Included(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -667,7 +667,7 @@ mod tests {
{
let mut stream = txn2
.scan((Bound::Excluded(&lower), Bound::Excluded(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -684,7 +684,7 @@ mod tests {
{
let mut stream = txn3
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -700,7 +700,7 @@ mod tests {
{
let mut stream = txn3
.scan((Bound::Included(&lower), Bound::Excluded(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -713,7 +713,7 @@ mod tests {
{
let mut stream = txn3
.scan((Bound::Excluded(&lower), Bound::Included(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -726,7 +726,7 @@ mod tests {
{
let mut stream = txn3
.scan((Bound::Excluded(&lower), Bound::Excluded(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand All @@ -736,7 +736,7 @@ mod tests {
{
let mut stream = txn3
.scan((Bound::Unbounded, Bound::Excluded(&upper)))
.projection(vec![1])
.projection(&["vu32"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -876,7 +876,7 @@ mod tests {
{
let mut scan = txn
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![0, 1, 2])
.projection(&["id", "age", "height"])
.take()
.await
.unwrap();
Expand Down
6 changes: 3 additions & 3 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod tests {
let upper = Value::new(DataType::Int64, "id".to_owned(), Arc::new(47_i64), false);
let mut scan = txn
.scan((Bound::Included(&lower), Bound::Included(&upper)))
.projection(vec![0, 2, 4])
.projection(&["id", "name", "bytes"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -258,7 +258,7 @@ mod tests {
let tx = db.transaction().await;
let mut scan = tx
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![0, 1, 2])
.projection(&["id", "age", "name"])
.take()
.await
.unwrap();
Expand Down Expand Up @@ -340,7 +340,7 @@ mod tests {

let mut scan = tx
.scan((Bound::Unbounded, Bound::Unbounded))
.projection(vec![0, 1, 2])
.projection(&["id", "age", "name"])
.take()
.await
.unwrap();
Expand Down
Loading