Skip to content

Commit 2df01db

Browse files
committed
Simplfy exprs
1 parent 169b13e commit 2df01db

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

datafusion-examples/examples/simple_udtf.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ use datafusion::arrow::record_batch::RecordBatch;
2323
use datafusion::datasource::function::TableFunctionImpl;
2424
use datafusion::datasource::TableProvider;
2525
use datafusion::error::Result;
26-
use datafusion::execution::context::SessionState;
26+
use datafusion::execution::context::{ExecutionProps, SessionState};
2727
use datafusion::physical_plan::memory::MemoryExec;
28-
use datafusion::physical_plan::{ExecutionPlan};
28+
use datafusion::physical_plan::ExecutionPlan;
2929
use datafusion::prelude::SessionContext;
3030
use datafusion_common::{plan_err, DataFusionError, ScalarValue};
3131
use datafusion_expr::{Expr, TableType};
32+
use datafusion_optimizer::simplify_expressions::{ExprSimplifier, SimplifyContext};
3233
use std::fs::File;
3334
use std::io::Seek;
3435
use 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

Comments
 (0)