jackwener commented on a change in pull request #2054:
URL: https://github.com/apache/arrow-datafusion/pull/2054#discussion_r833998638
##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -916,7 +916,11 @@ impl LogicalPlanBuilder {
/// Build the plan
pub fn build(&self) -> Result<LogicalPlan> {
- Ok(self.plan.clone())
+ if let Some(err) = check_plan_invalid(&self.plan) {
+ Err(err)
+ } else {
+ Ok(self.plan.clone())
+ }
Review comment:
```suggestion
check_plan_invalid(&self.plan)?;
Ok(self.plan.clone())
```
##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -1090,6 +1094,101 @@ pub(crate) fn expand_wildcard(
}
}
+/// check whether the logical plan we are building is valid
+fn check_plan_invalid(plan: &LogicalPlan) -> Option<DataFusionError> {
+ match plan {
+ LogicalPlan::Projection(Projection { expr, input, .. })
+ | LogicalPlan::Sort(Sort { expr, input })
+ | LogicalPlan::Window(Window {
+ window_expr: expr,
+ input,
+ ..
+ }) => check_plan_invalid(input)
+ .or_else(|| check_any_invalid_expr(expr, input.schema())),
+
+ LogicalPlan::Filter(Filter {
+ predicate: expr,
+ input,
+ }) => {
+ check_plan_invalid(input).or_else(|| check_invalid_expr(expr,
input.schema()))
+ }
+
+ LogicalPlan::Aggregate(Aggregate {
+ input,
+ group_expr,
+ aggr_expr,
+ ..
+ }) => {
+ let schema = input.schema();
+ check_plan_invalid(input)
+ .or_else(|| check_any_invalid_expr(group_expr, schema))
+ .or_else(|| check_any_invalid_expr(aggr_expr, schema))
+ }
+
+ LogicalPlan::Join(Join { left, right, .. })
+ | LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => {
+ check_plan_invalid(left).or_else(|| check_plan_invalid(right))
+ }
+
+ LogicalPlan::Repartition(Repartition { input, .. })
+ | LogicalPlan::Limit(Limit { input, .. })
+ | LogicalPlan::Explain(Explain { plan: input, .. })
+ | LogicalPlan::Analyze(Analyze { input, .. }) =>
check_plan_invalid(input),
+
+ LogicalPlan::Union(Union { inputs, .. }) => {
+ inputs.iter().filter_map(check_plan_invalid).next()
+ }
+
+ LogicalPlan::TableScan(TableScan {
+ table_name: _,
+ source,
+ projection,
+ projected_schema,
+ filters,
+ limit: _,
+ }) => {
+ if let Some(projection) = projection {
+ if projection.len() > projected_schema.fields().len() {
+ return Some(DataFusionError::Plan(
+ "projection contains columns that doesnt belong to
projected schema"
+ .to_owned(),
+ ));
+ }
+ }
+ let schema = &source.schema().to_dfschema_ref().ok()?;
+ check_any_invalid_expr(filters, schema)
+ }
+
+ _ => None,
+ }
+}
+
+/// find first error in the exprs
+fn check_any_invalid_expr(
+ exprs: &[Expr],
+ schema: &DFSchemaRef,
+) -> Option<DataFusionError> {
+ exprs
+ .iter()
+ .filter_map(|e| check_invalid_expr(e, schema))
+ .next()
+}
Review comment:
```suggestion
/// check whether the logical plan we are building is valid
fn check_plan_invalid(plan: &LogicalPlan) -> Result<()> {
match plan {
LogicalPlan::Projection(Projection { expr, input, .. })
| LogicalPlan::Sort(Sort { expr, input })
| LogicalPlan::Window(Window {
window_expr: expr,
input,
..
}) => check_plan_invalid(input)
.or_else(|_| check_any_invalid_expr(expr, input.schema())),
LogicalPlan::Filter(Filter {
predicate: expr,
input,
}) => check_plan_invalid(input)
.or_else(|_| check_invalid_expr(expr, input.schema())),
LogicalPlan::Aggregate(Aggregate {
input,
group_expr,
aggr_expr,
..
}) => {
let schema = input.schema();
check_plan_invalid(input)
.or_else(|_| check_any_invalid_expr(group_expr, schema))
.or_else(|_| check_any_invalid_expr(aggr_expr, schema))
}
LogicalPlan::Join(Join { left, right, .. })
| LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => {
check_plan_invalid(left).or_else(|_| check_plan_invalid(right))
}
LogicalPlan::Repartition(Repartition { input, .. })
| LogicalPlan::Limit(Limit { input, .. })
| LogicalPlan::Explain(Explain { plan: input, .. })
| LogicalPlan::Analyze(Analyze { input, .. }) =>
check_plan_invalid(input),
LogicalPlan::Union(Union { inputs, .. }) => {
inputs.iter().map(check_plan_invalid).collect()
}
LogicalPlan::TableScan(TableScan {
table_name: _,
source,
projection,
projected_schema,
filters,
limit: _,
}) => {
if let Some(projection) = projection {
if projection.len() > projected_schema.fields().len() {
return Err(DataFusionError::Plan(
"projection contains columns that doesnt belong to
projected schema"
.to_owned(),
));
}
}
let schema = &source.schema().to_dfschema_ref()?;
check_any_invalid_expr(filters, schema)
}
_ => Ok(()),
}
}
```
##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -1090,6 +1094,101 @@ pub(crate) fn expand_wildcard(
}
}
+/// check whether the logical plan we are building is valid
+fn check_plan_invalid(plan: &LogicalPlan) -> Option<DataFusionError> {
+ match plan {
+ LogicalPlan::Projection(Projection { expr, input, .. })
+ | LogicalPlan::Sort(Sort { expr, input })
+ | LogicalPlan::Window(Window {
+ window_expr: expr,
+ input,
+ ..
+ }) => check_plan_invalid(input)
+ .or_else(|| check_any_invalid_expr(expr, input.schema())),
+
+ LogicalPlan::Filter(Filter {
+ predicate: expr,
+ input,
+ }) => {
+ check_plan_invalid(input).or_else(|| check_invalid_expr(expr,
input.schema()))
+ }
+
+ LogicalPlan::Aggregate(Aggregate {
+ input,
+ group_expr,
+ aggr_expr,
+ ..
+ }) => {
+ let schema = input.schema();
+ check_plan_invalid(input)
+ .or_else(|| check_any_invalid_expr(group_expr, schema))
+ .or_else(|| check_any_invalid_expr(aggr_expr, schema))
+ }
+
+ LogicalPlan::Join(Join { left, right, .. })
+ | LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => {
+ check_plan_invalid(left).or_else(|| check_plan_invalid(right))
+ }
+
+ LogicalPlan::Repartition(Repartition { input, .. })
+ | LogicalPlan::Limit(Limit { input, .. })
+ | LogicalPlan::Explain(Explain { plan: input, .. })
+ | LogicalPlan::Analyze(Analyze { input, .. }) =>
check_plan_invalid(input),
+
+ LogicalPlan::Union(Union { inputs, .. }) => {
+ inputs.iter().filter_map(check_plan_invalid).next()
+ }
+
+ LogicalPlan::TableScan(TableScan {
+ table_name: _,
+ source,
+ projection,
+ projected_schema,
+ filters,
+ limit: _,
+ }) => {
+ if let Some(projection) = projection {
+ if projection.len() > projected_schema.fields().len() {
+ return Some(DataFusionError::Plan(
+ "projection contains columns that doesnt belong to
projected schema"
+ .to_owned(),
+ ));
+ }
+ }
+ let schema = &source.schema().to_dfschema_ref().ok()?;
+ check_any_invalid_expr(filters, schema)
+ }
+
+ _ => None,
+ }
+}
+
+/// find first error in the exprs
+fn check_any_invalid_expr(
+ exprs: &[Expr],
+ schema: &DFSchemaRef,
+) -> Option<DataFusionError> {
+ exprs
+ .iter()
+ .filter_map(|e| check_invalid_expr(e, schema))
+ .next()
+}
+
+/// do some checks for exprs in a logical plan
+fn check_invalid_expr(expr: &Expr, schema: &DFSchemaRef) ->
Option<DataFusionError> {
+ match expr {
+ Expr::Not(bool_expr) if bool_expr.get_type(schema).ok()? !=
DataType::Boolean => {
+ Some(DataFusionError::Plan(
+ "Not expression's type must be a boolean".to_owned(),
+ ))
+ }
+ Expr::Wildcard => Some(DataFusionError::Plan(
+ "Wildcard expressions are not valid in a logical query
plan".to_owned(),
+ )),
+ _ => None,
+ }
+}
Review comment:
```suggestion
/// find first error in the exprs
fn check_any_invalid_expr(exprs: &[Expr], schema: &DFSchemaRef) ->
Result<()> {
exprs
.iter()
.map(|e| check_invalid_expr(e, schema))
.collect()
}
/// do some checks for exprs in a logical plan
fn check_invalid_expr(expr: &Expr, schema: &DFSchemaRef) -> Result<()> {
match expr {
Expr::Not(bool_expr) if bool_expr.get_type(schema)? !=
DataType::Boolean => Err(
DataFusionError::Plan("Not expression's type must be a
boolean".to_owned()),
),
Expr::Wildcard => Err(DataFusionError::Plan(
"Wildcard expressions are not valid in a logical query
plan".to_owned(),
)),
_ => Ok(()),
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]