@@ -23,12 +23,13 @@ use datafusion::arrow::record_batch::RecordBatch;
2323use datafusion:: datasource:: function:: TableFunctionImpl ;
2424use datafusion:: datasource:: TableProvider ;
2525use datafusion:: error:: Result ;
26- use datafusion:: execution:: context:: SessionState ;
26+ use datafusion:: execution:: context:: { ExecutionProps , SessionState } ;
2727use datafusion:: physical_plan:: memory:: MemoryExec ;
28- use datafusion:: physical_plan:: { ExecutionPlan } ;
28+ use datafusion:: physical_plan:: ExecutionPlan ;
2929use datafusion:: prelude:: SessionContext ;
3030use datafusion_common:: { plan_err, DataFusionError , ScalarValue } ;
3131use datafusion_expr:: { Expr , TableType } ;
32+ use datafusion_optimizer:: simplify_expressions:: { ExprSimplifier , SimplifyContext } ;
3233use std:: fs:: File ;
3334use std:: io:: Seek ;
3435use std:: path:: Path ;
@@ -51,9 +52,9 @@ async fn main() -> Result<()> {
5152 let testdata = datafusion:: test_util:: arrow_test_data ( ) ;
5253 let csv_file = format ! ( "{testdata}/csv/aggregate_test_100.csv" ) ;
5354
54- // Pass 2 arguments, read csv with at most 2 rows
55+ // Pass 2 arguments, read csv with at most 2 rows (simplify logic makes 1+1 --> 2)
5556 let df = ctx
56- . sql ( format ! ( "SELECT * FROM read_csv('{csv_file}', 2 );" ) . as_str ( ) )
57+ . sql ( format ! ( "SELECT * FROM read_csv('{csv_file}', 1 + 1 );" ) . as_str ( ) )
5758 . await ?;
5859 df. show ( ) . await ?;
5960
@@ -135,8 +136,13 @@ impl TableFunctionImpl for LocalCsvTableFunc {
135136 let limit = exprs
136137 . get ( 1 )
137138 . map ( |expr| {
139+ // try to simpify the expression, so 1+2 becomes 3, for example
140+ let execution_props = ExecutionProps :: new ( ) ;
141+ let info = SimplifyContext :: new ( & execution_props) ;
142+ let expr = ExprSimplifier :: new ( info) . simplify ( expr. clone ( ) ) ?;
143+
138144 if let Expr :: Literal ( ScalarValue :: Int64 ( Some ( limit) ) ) = expr {
139- Ok ( * limit as usize )
145+ Ok ( limit as usize )
140146 } else {
141147 plan_err ! ( "Limit must be an integer" )
142148 }
0 commit comments