-
Notifications
You must be signed in to change notification settings - Fork 4.2k
ARROW-4464: [Rust] [DataFusion] Add support for LIMIT #3669
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eac5a24
Add support for Limit
dbc639f
Make limit an usize and avoid evaluting the limit expression
e93df93
Remove redundant variable
c78ae2c
Use the previous batch's schema for Limit
2ed488c
Merge remote-tracking branch 'upstream/master' into arrow-4464
facc5c2
Add Limit case to ProjectionPushDown
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,182 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Execution of a limit (predicate) | ||
|
|
||
| use std::cell::RefCell; | ||
| use std::rc::Rc; | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::array::*; | ||
| use arrow::datatypes::{DataType, Schema}; | ||
| use arrow::record_batch::RecordBatch; | ||
|
|
||
| use super::error::{ExecutionError, Result}; | ||
| use super::relation::Relation; | ||
|
|
||
| pub struct LimitRelation { | ||
| input: Rc<RefCell<Relation>>, | ||
| schema: Arc<Schema>, | ||
| limit: usize, | ||
| num_consumed_rows: usize, | ||
| } | ||
|
|
||
| impl LimitRelation { | ||
| pub fn new(input: Rc<RefCell<Relation>>, limit: usize, schema: Arc<Schema>) -> Self { | ||
| Self { | ||
| input, | ||
| schema, | ||
| limit, | ||
| num_consumed_rows: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Relation for LimitRelation { | ||
| fn next(&mut self) -> Result<Option<RecordBatch>> { | ||
| match self.input.borrow_mut().next()? { | ||
| Some(batch) => { | ||
| let capacity = self.limit - self.num_consumed_rows; | ||
|
|
||
| if capacity <= 0 { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| if batch.num_rows() >= capacity { | ||
| let limited_columns: Result<Vec<ArrayRef>> = (0..batch.num_columns()) | ||
| .map(|i| limit(batch.column(i).as_ref(), capacity)) | ||
| .collect(); | ||
|
|
||
| let limited_batch: RecordBatch = | ||
| RecordBatch::new(Arc::new(Schema::empty()), limited_columns?); | ||
| self.num_consumed_rows += capacity; | ||
|
|
||
| Ok(Some(limited_batch)) | ||
| } else { | ||
| self.num_consumed_rows += batch.num_rows(); | ||
| Ok(Some(batch)) | ||
| } | ||
| } | ||
| None => Ok(None), | ||
| } | ||
| } | ||
|
|
||
| fn schema(&self) -> &Arc<Schema> { | ||
| &self.schema | ||
| } | ||
| } | ||
|
|
||
| //TODO: move into Arrow array_ops | ||
| fn limit(a: &Array, num_rows_to_read: usize) -> Result<ArrayRef> { | ||
| //TODO use macros | ||
| match a.data_type() { | ||
| DataType::UInt8 => { | ||
| let b = a.as_any().downcast_ref::<UInt8Array>().unwrap(); | ||
| let mut builder = UInt8Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::UInt16 => { | ||
| let b = a.as_any().downcast_ref::<UInt16Array>().unwrap(); | ||
| let mut builder = UInt16Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::UInt32 => { | ||
| let b = a.as_any().downcast_ref::<UInt32Array>().unwrap(); | ||
| let mut builder = UInt32Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::UInt64 => { | ||
| let b = a.as_any().downcast_ref::<UInt64Array>().unwrap(); | ||
| let mut builder = UInt64Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Int8 => { | ||
| let b = a.as_any().downcast_ref::<Int8Array>().unwrap(); | ||
| let mut builder = Int8Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Int16 => { | ||
| let b = a.as_any().downcast_ref::<Int16Array>().unwrap(); | ||
| let mut builder = Int16Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Int32 => { | ||
| let b = a.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
| let mut builder = Int32Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Int64 => { | ||
| let b = a.as_any().downcast_ref::<Int64Array>().unwrap(); | ||
| let mut builder = Int64Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Float32 => { | ||
| let b = a.as_any().downcast_ref::<Float32Array>().unwrap(); | ||
| let mut builder = Float32Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Float64 => { | ||
| let b = a.as_any().downcast_ref::<Float64Array>().unwrap(); | ||
| let mut builder = Float64Array::builder(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| builder.append_value(b.value(i as usize))?; | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| DataType::Utf8 => { | ||
| //TODO: this is inefficient and we should improve the Arrow impl to help make this more concise | ||
| let b = a.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
| let mut values: Vec<String> = Vec::with_capacity(num_rows_to_read as usize); | ||
| for i in 0..num_rows_to_read { | ||
| values.push(b.get_string(i as usize)); | ||
| } | ||
| let tmp: Vec<&str> = values.iter().map(|s| s.as_str()).collect(); | ||
| Ok(Arc::new(BinaryArray::from(tmp))) | ||
| } | ||
| other => Err(ExecutionError::ExecutionError(format!( | ||
| "filter not supported for {:?}", | ||
| other | ||
| ))), | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new batch needs the same schema as the original, not an empty schema
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once this is fixed, I'm ready to approve. Thanks @ntrinquier !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed that makes more sense. filter is also creating the new batch with an empty schema, so I guess this would have to be changed there too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, good catch, yes I'll file a bug for that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andygrove I created it: https://issues.apache.org/jira/browse/ARROW-4721