Apache DataFusion SQL Query Engine
ARROW-8261: [Rust-DataFusion] Made limit accept integers and no longer accept expressions.
Jorge C. Leitao90c86781bb68
diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rsindex 1cb3c5f577..1974eca57c 100644--- a/rust/datafusion/src/execution/context.rs+++ b/rust/datafusion/src/execution/context.rs@@ -423,44 +423,15 @@ impl ExecutionContext {Ok(Arc::new(SortExec::try_new(sort_expr, input)?))}- LogicalPlan::Limit { input, expr, .. } => {+ LogicalPlan::Limit { input, n, .. } => {let input = self.create_physical_plan(input, batch_size)?;let input_schema = input.as_ref().schema().clone();- match expr {- &Expr::Literal(ref scalar_value) => {- let limit: usize = match scalar_value {- ScalarValue::Int8(limit) if *limit >= 0 => {- Ok(*limit as usize)- }- ScalarValue::Int16(limit) if *limit >= 0 => {- Ok(*limit as usize)- }- ScalarValue::Int32(limit) if *limit >= 0 => {- Ok(*limit as usize)- }- ScalarValue::Int64(limit) if *limit >= 0 => {- Ok(*limit as usize)- }- ScalarValue::UInt8(limit) => Ok(*limit as usize),- ScalarValue::UInt16(limit) => Ok(*limit as usize),- ScalarValue::UInt32(limit) => Ok(*limit as usize),- ScalarValue::UInt64(limit) => Ok(*limit as usize),- _ => Err(ExecutionError::ExecutionError(- "Limit only supports non-negative integer literals"- .to_string(),- )),- }?;- Ok(Arc::new(LimitExec::new(- input_schema.clone(),- input.partitions()?,- limit,- )))- }- _ => Err(ExecutionError::ExecutionError(- "Limit only supports non-negative integer literals".to_string(),- )),- }+ Ok(Arc::new(LimitExec::new(+ input_schema.clone(),+ input.partitions()?,+ *n,+ )))}_ => Err(ExecutionError::General("Unsupported logical plan variant".to_string(),diff --git a/rust/datafusion/src/execution/table_impl.rs b/rust/datafusion/src/execution/table_impl.rsindex facb83fecf..8f798bdf4d 100644--- a/rust/datafusion/src/execution/table_impl.rs+++ b/rust/datafusion/src/execution/table_impl.rs@@ -23,8 +23,8 @@ use crate::arrow::datatypes::DataType;use crate::arrow::record_batch::RecordBatch;use crate::error::{ExecutionError, Result};use crate::execution::context::ExecutionContext;+use crate::logicalplan::LogicalPlanBuilder;use crate::logicalplan::{Expr, LogicalPlan};-use crate::logicalplan::{LogicalPlanBuilder, ScalarValue};use crate::table::*;use arrow::datatypes::Schema;@@ -83,10 +83,8 @@ impl Table for TableImpl {}/// Limit the number of rows- fn limit(&self, n: u32) -> Result<Arc<dyn Table>> {- let plan = LogicalPlanBuilder::from(&self.plan)- .limit(Expr::Literal(ScalarValue::UInt32(n)))?- .build()?;+ fn limit(&self, n: usize) -> Result<Arc<dyn Table>> {+ let plan = LogicalPlanBuilder::from(&self.plan).limit(n)?.build()?;Ok(Arc::new(TableImpl::new(&plan)))}diff --git a/rust/datafusion/src/logicalplan.rs b/rust/datafusion/src/logicalplan.rsindex 3384b93a40..fe711eeadc 100644--- a/rust/datafusion/src/logicalplan.rs+++ b/rust/datafusion/src/logicalplan.rs@@ -586,8 +586,8 @@ pub enum LogicalPlan {},/// Represents the maximum number of records to returnLimit {- /// The expression- expr: Expr,+ /// The limit+ n: usize,/// The logical planinput: Box<LogicalPlan>,/// The schema description@@ -713,11 +713,9 @@ impl LogicalPlan {input.fmt_with_indent(f, indent + 1)}LogicalPlan::Limit {- ref input,- ref expr,- ..+ ref input, ref n, ..} => {- write!(f, "Limit: {:?}", expr)?;+ write!(f, "Limit: {}", n)?;input.fmt_with_indent(f, indent + 1)}LogicalPlan::CreateExternalTable { ref name, .. } => {@@ -916,9 +914,9 @@ impl LogicalPlanBuilder {}/// Apply a limit- pub fn limit(&self, expr: Expr) -> Result<Self> {+ pub fn limit(&self, n: usize) -> Result<Self> {Ok(Self::from(&LogicalPlan::Limit {- expr,+ n,input: Box::new(self.plan.clone()),schema: self.plan.schema().clone(),}))diff --git a/rust/datafusion/src/optimizer/projection_push_down.rs b/rust/datafusion/src/optimizer/projection_push_down.rsindex cb2c3fdd79..8fc203e602 100644--- a/rust/datafusion/src/optimizer/projection_push_down.rs+++ b/rust/datafusion/src/optimizer/projection_push_down.rs@@ -203,18 +203,11 @@ impl ProjectionPushDown {projected_schema: Box::new(projected_schema),})}- LogicalPlan::Limit { expr, input, .. } => {- // Note that limit expressions are scalar values so there is no need to- // rewrite them but we do need to optimize the input to the limit plan- LogicalPlanBuilder::from(&self.optimize_plan(- &input,- accum,- mapping,- has_projection,- )?)- .limit(expr.clone())?- .build()- }+ LogicalPlan::Limit { n, input, .. } => LogicalPlanBuilder::from(+ &self.optimize_plan(&input, accum, mapping, has_projection)?,+ )+ .limit(*n)?+ .build(),LogicalPlan::CreateExternalTable {schema,name,@@ -477,12 +470,12 @@ mod tests {let plan = LogicalPlanBuilder::from(&table_scan).project(vec![Column(2), Column(0)])?- .limit(Expr::Literal(ScalarValue::UInt32(5)))?+ .limit(5)?.build()?;assert_fields_eq(&plan, vec!["c", "a"]);- let expected = "Limit: UInt32(5)\+ let expected = "Limit: 5\\n Projection: #1, #0\\n TableScan: test projection=Some([0, 2])";diff --git a/rust/datafusion/src/sql/planner.rs b/rust/datafusion/src/sql/planner.rsindex 5c9570638b..d6bd296574 100644--- a/rust/datafusion/src/sql/planner.rs+++ b/rust/datafusion/src/sql/planner.rs@@ -215,16 +215,14 @@ impl<S: SchemaProvider> SqlToRel<S> {) -> Result<LogicalPlan> {match *limit {Some(ref limit_expr) => {- let limit_rex = match self.sql_to_rex(&limit_expr, &input.schema())? {- Expr::Literal(ScalarValue::Int64(n)) => {- Ok(Expr::Literal(ScalarValue::UInt32(n as u32)))- }+ let n = match self.sql_to_rex(&limit_expr, &input.schema())? {+ Expr::Literal(ScalarValue::Int64(n)) => Ok(n as usize),_ => Err(ExecutionError::General("Unexpected expression for LIMIT clause".to_string(),)),}?;- LogicalPlanBuilder::from(&input).limit(limit_rex)?.build()+ LogicalPlanBuilder::from(&input).limit(n)?.build()}_ => Ok(input.clone()),}diff --git a/rust/datafusion/src/table.rs b/rust/datafusion/src/table.rsindex 1c1e982a32..bd04f0738e 100644--- a/rust/datafusion/src/table.rs+++ b/rust/datafusion/src/table.rs@@ -44,7 +44,7 @@ pub trait Table {) -> Result<Arc<dyn Table>>;/// limit the number of rows- fn limit(&self, n: u32) -> Result<Arc<dyn Table>>;+ fn limit(&self, n: usize) -> Result<Arc<dyn Table>>;/// Return the logical planfn to_logical_plan(&self) -> LogicalPlan;