Skip to content
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

Fix output schema generated by CommonSubExprEliminate #3726

Merged
merged 4 commits into from
Oct 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 149 additions & 34 deletions datafusion/optimizer/src/common_subexpr_eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use crate::{OptimizerConfig, OptimizerRule};
use arrow::datatypes::DataType;
use datafusion_common::{DFField, DFSchema, DataFusionError, Result};
use datafusion_common::{DFField, DFSchema, DFSchemaRef, DataFusionError, Result};
use datafusion_expr::{
col,
expr_rewriter::{ExprRewritable, ExprRewriter, RewriteRecursion},
Expand Down Expand Up @@ -94,7 +94,10 @@ fn optimize(
schema,
alias,
}) => {
let arrays = to_arrays(expr, input, &mut expr_set)?;
let input_schema = Arc::clone(input.schema());
let all_schemas: Vec<DFSchemaRef> =
plan.all_schemas().into_iter().cloned().collect();
let arrays = to_arrays(expr, input_schema, all_schemas, &mut expr_set)?;

let (mut new_expr, new_input) = rewrite_expr(
&[expr],
Expand All @@ -112,22 +115,18 @@ fn optimize(
)?))
}
LogicalPlan::Filter(Filter { predicate, input }) => {
let schema = plan.schema().as_ref().clone();
let data_type = if let Ok(data_type) = predicate.get_type(&schema) {
data_type
} else {
// predicate type could not be resolved in schema, fall back to all schemas
let schemas = plan.all_schemas();
let all_schema =
schemas.into_iter().fold(DFSchema::empty(), |mut lhs, rhs| {
lhs.merge(rhs);
lhs
});
predicate.get_type(&all_schema)?
};
let input_schema = Arc::clone(input.schema());
let all_schemas: Vec<DFSchemaRef> =
plan.all_schemas().into_iter().cloned().collect();

let mut id_array = vec![];
expr_to_identifier(predicate, &mut expr_set, &mut id_array, data_type)?;
expr_to_identifier(
predicate,
&mut expr_set,
&mut id_array,
input_schema,
all_schemas,
)?;

let (mut new_expr, new_input) = rewrite_expr(
&[&[predicate.clone()]],
Expand All @@ -153,7 +152,11 @@ fn optimize(
window_expr,
schema,
}) => {
let arrays = to_arrays(window_expr, input, &mut expr_set)?;
let input_schema = Arc::clone(input.schema());
let all_schemas: Vec<DFSchemaRef> =
plan.all_schemas().into_iter().cloned().collect();
let arrays =
to_arrays(window_expr, input_schema, all_schemas, &mut expr_set)?;

let (mut new_expr, new_input) = rewrite_expr(
&[window_expr],
Expand All @@ -175,8 +178,17 @@ fn optimize(
input,
schema,
}) => {
let group_arrays = to_arrays(group_expr, input, &mut expr_set)?;
let aggr_arrays = to_arrays(aggr_expr, input, &mut expr_set)?;
let input_schema = Arc::clone(input.schema());
let all_schemas: Vec<DFSchemaRef> =
plan.all_schemas().into_iter().cloned().collect();
let group_arrays = to_arrays(
group_expr,
Arc::clone(&input_schema),
all_schemas.clone(),
&mut expr_set,
)?;
let aggr_arrays =
to_arrays(aggr_expr, input_schema, all_schemas, &mut expr_set)?;

let (mut new_expr, new_input) = rewrite_expr(
&[group_expr, aggr_expr],
Expand All @@ -197,7 +209,10 @@ fn optimize(
)?))
}
LogicalPlan::Sort(Sort { expr, input, fetch }) => {
let arrays = to_arrays(expr, input, &mut expr_set)?;
let input_schema = Arc::clone(input.schema());
let all_schemas: Vec<DFSchemaRef> =
plan.all_schemas().into_iter().cloned().collect();
let arrays = to_arrays(expr, input_schema, all_schemas, &mut expr_set)?;

let (mut new_expr, new_input) = rewrite_expr(
&[expr],
Expand Down Expand Up @@ -255,14 +270,20 @@ fn pop_expr(new_expr: &mut Vec<Vec<Expr>>) -> Result<Vec<Expr>> {

fn to_arrays(
expr: &[Expr],
input: &LogicalPlan,
input_schema: DFSchemaRef,
all_schemas: Vec<DFSchemaRef>,
expr_set: &mut ExprSet,
) -> Result<Vec<Vec<(usize, String)>>> {
expr.iter()
.map(|e| {
let data_type = e.get_type(input.schema())?;
let mut id_array = vec![];
expr_to_identifier(e, expr_set, &mut id_array, data_type)?;
expr_to_identifier(
e,
expr_set,
&mut id_array,
Arc::clone(&input_schema),
all_schemas.clone(),
)?;

Ok(id_array)
})
Expand Down Expand Up @@ -370,7 +391,15 @@ struct ExprIdentifierVisitor<'a> {
expr_set: &'a mut ExprSet,
/// series number (usize) and identifier.
id_array: &'a mut Vec<(usize, Identifier)>,
data_type: DataType,
/// input schema for the node that we're optimizing, so we can determine the correct datatype
/// for each subexpression
input_schema: DFSchemaRef,
/// all schemas in the logical plan, as a fall back if we cannot resolve an expression type
/// from the input schema alone
// This fallback should never be necessary as the expression datatype should always be
// resolvable from the input schema of the node that's being optimized.
// todo: This can likely be removed if we are sure it's safe to do so.
all_schemas: Vec<DFSchemaRef>,

// inner states
visit_stack: Vec<VisitRecord>,
Expand Down Expand Up @@ -448,7 +477,25 @@ impl ExpressionVisitor for ExprIdentifierVisitor<'_> {

self.id_array[idx] = (self.series_number, desc.clone());
self.visit_stack.push(VisitRecord::ExprItem(desc.clone()));
let data_type = self.data_type.clone();

let data_type = if let Ok(data_type) = expr.get_type(&self.input_schema) {
data_type
} else {
// Expression type could not be resolved in schema, fall back to all schemas.
//
// This fallback should never be necessary as the expression datatype should always be
// resolvable from the input schema of the node that's being optimized.
// todo: This else-branch can likely be removed if we are sure it's safe to do so.
let merged_schema =
self.all_schemas
.iter()
.fold(DFSchema::empty(), |mut lhs, rhs| {
lhs.merge(rhs);
lhs
});
alamb marked this conversation as resolved.
Show resolved Hide resolved
expr.get_type(&merged_schema)?
};

self.expr_set
.entry(desc)
.or_insert_with(|| (expr.clone(), 0, data_type))
Expand All @@ -462,12 +509,14 @@ fn expr_to_identifier(
expr: &Expr,
expr_set: &mut ExprSet,
id_array: &mut Vec<(usize, Identifier)>,
data_type: DataType,
input_schema: DFSchemaRef,
all_schemas: Vec<DFSchemaRef>,
) -> Result<()> {
expr.accept(ExprIdentifierVisitor {
expr_set,
id_array,
data_type,
input_schema,
all_schemas,
visit_stack: vec![],
node_count: 0,
series_number: 0,
Expand Down Expand Up @@ -577,7 +626,8 @@ fn replace_common_expr(
mod test {
use super::*;
use crate::test::*;
use datafusion_expr::logical_plan::JoinType;
use arrow::datatypes::{Field, Schema};
use datafusion_expr::logical_plan::{table_scan, JoinType};
use datafusion_expr::{
avg, binary_expr, col, lit, logical_plan::builder::LogicalPlanBuilder, sum,
Operator,
Expand All @@ -597,22 +647,36 @@ mod test {
fn id_array_visitor() -> Result<()> {
let expr = binary_expr(
binary_expr(
sum(binary_expr(col("a"), Operator::Plus, lit("1"))),
sum(binary_expr(col("a"), Operator::Plus, lit(1))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this test doesn't make sense as coercion should have happened before this pass

Operator::Minus,
avg(col("c")),
),
Operator::Multiply,
lit(2),
);

let schema = Arc::new(DFSchema::new_with_metadata(
vec![
DFField::new(None, "a", DataType::Int64, false),
DFField::new(None, "c", DataType::Int64, false),
],
Default::default(),
)?);

let mut id_array = vec![];
expr_to_identifier(&expr, &mut HashMap::new(), &mut id_array, DataType::Int64)?;
expr_to_identifier(
&expr,
&mut HashMap::new(),
&mut id_array,
Arc::clone(&schema),
vec![schema],
)?;

let expected = vec![
(9, "SUM(a + Utf8(\"1\")) - AVG(c) * Int32(2)Int32(2)SUM(a + Utf8(\"1\")) - AVG(c)AVG(c)cSUM(a + Utf8(\"1\"))a + Utf8(\"1\")Utf8(\"1\")a"),
(7, "SUM(a + Utf8(\"1\")) - AVG(c)AVG(c)cSUM(a + Utf8(\"1\"))a + Utf8(\"1\")Utf8(\"1\")a"),
(4, "SUM(a + Utf8(\"1\"))a + Utf8(\"1\")Utf8(\"1\")a"),
(3, "a + Utf8(\"1\")Utf8(\"1\")a"),
(9, "SUM(a + Int32(1)) - AVG(c) * Int32(2)Int32(2)SUM(a + Int32(1)) - AVG(c)AVG(c)cSUM(a + Int32(1))a + Int32(1)Int32(1)a"),
(7, "SUM(a + Int32(1)) - AVG(c)AVG(c)cSUM(a + Int32(1))a + Int32(1)Int32(1)a"),
(4, "SUM(a + Int32(1))a + Int32(1)Int32(1)a"),
(3, "a + Int32(1)Int32(1)a"),
(1, ""),
(2, ""),
(6, "AVG(c)c"),
Expand Down Expand Up @@ -796,4 +860,55 @@ mod test {
assert!(field_set.insert(field.qualified_name()));
}
}

#[test]
fn eliminated_subexpr_datatype() {
use datafusion_expr::cast;

let schema = Schema::new(vec![
Field::new("a", DataType::UInt64, false),
Field::new("b", DataType::UInt64, false),
Field::new("c", DataType::UInt64, false),
]);

let plan = table_scan(Some("table"), &schema, None)
.unwrap()
.filter(
cast(col("a"), DataType::Int64)
.lt(lit(1_i64))
.and(cast(col("a"), DataType::Int64).not_eq(lit(1_i64))),
)
.unwrap()
.build()
.unwrap();
let rule = CommonSubexprEliminate {};
let optimized_plan = rule.optimize(&plan, &mut OptimizerConfig::new()).unwrap();

let schema = optimized_plan.schema();
let fields_with_datatypes: Vec<_> = schema
.fields()
.iter()
.map(|field| (field.name(), field.data_type()))
.collect();
let formatted_fields_with_datatype = format!("{fields_with_datatypes:#?}");
let expected = r###"[
(
"CAST(table.a AS Int64)table.a",
Int64,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This used to be just Boolean before the fix.

),
(
"a",
UInt64,
),
(
"b",
UInt64,
),
(
"c",
UInt64,
),
]"###;
assert_eq!(expected, formatted_fields_with_datatype);
}
}