Skip to content

Commit ec2fc6f

Browse files
Dandandanthinkharderdev
authored andcommitted
Fix bug in simplify expressions (apache#214) (apache#7699)
Co-authored-by: Dan Harris <[email protected]>
1 parent 773e950 commit ec2fc6f

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ impl SimplifyExpressions {
6565
) -> Result<LogicalPlan> {
6666
let schema = if !plan.inputs().is_empty() {
6767
DFSchemaRef::new(merge_schema(plan.inputs()))
68-
} else if let LogicalPlan::TableScan(_) = plan {
68+
} else if let LogicalPlan::TableScan(scan) = plan {
6969
// When predicates are pushed into a table scan, there needs to be
7070
// a schema to resolve the fields against.
71-
Arc::clone(plan.schema())
71+
Arc::new(DFSchema::try_from_qualified_schema(
72+
&scan.table_name,
73+
&scan.source.schema(),
74+
)?)
7275
} else {
7376
Arc::new(DFSchema::empty())
7477
};
@@ -111,7 +114,7 @@ mod tests {
111114
use crate::simplify_expressions::utils::for_test::{
112115
cast_to_int64_expr, now_expr, to_timestamp_expr,
113116
};
114-
use crate::test::test_table_scan_with_name;
117+
use crate::test::{assert_fields_eq, test_table_scan_with_name};
115118

116119
use super::*;
117120
use arrow::datatypes::{DataType, Field, Schema};
@@ -174,6 +177,48 @@ mod tests {
174177
Ok(())
175178
}
176179

180+
#[test]
181+
fn test_simplify_table_full_filter_in_scan() -> Result<()> {
182+
let fields = vec![
183+
Field::new("a", DataType::UInt32, false),
184+
Field::new("b", DataType::UInt32, false),
185+
Field::new("c", DataType::UInt32, false),
186+
];
187+
188+
let schema = Schema::new(fields);
189+
190+
let table_scan = table_scan_with_filters(
191+
Some("test"),
192+
&schema,
193+
Some(vec![0]),
194+
vec![col("b").is_not_null()],
195+
)?
196+
.build()?;
197+
assert_eq!(1, table_scan.schema().fields().len());
198+
assert_fields_eq(&table_scan, vec!["a"]);
199+
200+
let expected = "TableScan: test projection=[a], full_filters=[Boolean(true) AS b IS NOT NULL]";
201+
202+
assert_optimized_plan_eq(&table_scan, expected)
203+
}
204+
205+
#[test]
206+
fn test_simplify_filter_pushdown() -> Result<()> {
207+
let table_scan = test_table_scan();
208+
let plan = LogicalPlanBuilder::from(table_scan)
209+
.project(vec![col("a")])?
210+
.filter(and(col("b").gt(lit(1)), col("b").gt(lit(1))))?
211+
.build()?;
212+
213+
assert_optimized_plan_eq(
214+
&plan,
215+
"\
216+
Filter: test.b > Int32(1)\
217+
\n Projection: test.a\
218+
\n TableScan: test",
219+
)
220+
}
221+
177222
#[test]
178223
fn test_simplify_optimized_plan() -> Result<()> {
179224
let table_scan = test_table_scan();

0 commit comments

Comments
 (0)