-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Limit struct to use usize for offset and add new constructor…
… method
- Loading branch information
Showing
9 changed files
with
145 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::fmt::Display; | ||
|
||
use arrow::datatypes::SchemaRef; | ||
|
||
use crate::logical::plan::LogicalPlan; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Limit { | ||
pub input: Box<LogicalPlan>, | ||
pub fetch: usize, | ||
pub offset: usize, | ||
} | ||
|
||
impl Display for Limit { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Limit: fetch={}, offset={}", self.fetch, self.offset) | ||
} | ||
} | ||
|
||
impl Limit { | ||
pub fn new(input: LogicalPlan, fetch: usize, offset: usize) -> Self { | ||
Self { | ||
input: Box::new(input), | ||
fetch, | ||
offset, | ||
} | ||
} | ||
|
||
pub fn schema(&self) -> SchemaRef { | ||
self.input.schema() | ||
} | ||
|
||
pub fn children(&self) -> Option<Vec<&LogicalPlan>> { | ||
self.input.children() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use arrow::array::RecordBatch; | ||
use arrow::datatypes::SchemaRef; | ||
|
||
use crate::error::Result; | ||
use crate::physical::plan::PhysicalPlan; | ||
|
||
use std::sync::Arc; | ||
|
||
pub struct Limit { | ||
pub input: Arc<dyn PhysicalPlan>, | ||
pub fetch: usize, | ||
pub offset: usize, | ||
} | ||
|
||
impl Limit { | ||
pub fn new(input: Arc<dyn PhysicalPlan>, fetch: usize, offset: Option<usize>) -> Self { | ||
Self { | ||
input, | ||
fetch, | ||
offset: offset.unwrap_or_default(), | ||
} | ||
} | ||
} | ||
|
||
impl PhysicalPlan for Limit { | ||
fn schema(&self) -> SchemaRef { | ||
self.input.schema() | ||
} | ||
|
||
fn execute(&self) -> Result<Vec<RecordBatch>> { | ||
Ok(self | ||
.input | ||
.execute()? | ||
.into_iter() | ||
.map(|batch| batch.slice(self.offset, self.fetch)) | ||
.collect()) | ||
} | ||
|
||
fn children(&self) -> Option<Vec<Arc<dyn PhysicalPlan>>> { | ||
self.input.children() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{build_table_scan, physical::plan::PhysicalPlan, test_utils::assert_batch_eq}; | ||
|
||
use super::Limit; | ||
|
||
#[test] | ||
fn test_limit() { | ||
let input = build_table_scan!( | ||
("a", Int32Type, DataType::Int32, vec![1, 2, 3, 4]), | ||
("b", Float64Type, DataType::Float64, vec![1.0, 2.0, 3.0, 4.0]), | ||
("c", UInt64Type, DataType::UInt64, vec![1, 2, 3, 4]), | ||
); | ||
|
||
let limit = Limit::new(input, 3, Some(1)); | ||
|
||
let reuslts = limit.execute().unwrap(); | ||
|
||
assert_batch_eq( | ||
&reuslts, | ||
vec![ | ||
"+---+-----+---+", | ||
"| a | b | c |", | ||
"+---+-----+---+", | ||
"| 2 | 2.0 | 2 |", | ||
"| 3 | 3.0 | 3 |", | ||
"| 4 | 4.0 | 4 |", | ||
"+---+-----+---+", | ||
], | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters