doki23 commented on a change in pull request #2054:
URL: https://github.com/apache/arrow-datafusion/pull/2054#discussion_r836967428



##########
File path: datafusion/src/logical_plan/builder.rs
##########
@@ -1162,6 +1164,117 @@ pub(crate) fn expand_qualified_wildcard(
     expand_wildcard(&qualifier_schema, plan)
 }
 
+/// 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)
+            .and_then(|_| check_any_invalid_expr(expr, input.schema())),
+
+        LogicalPlan::Filter(Filter {
+            predicate: expr,
+            input,
+        }) => check_plan_invalid(input)
+            .and_then(|_| check_invalid_expr(expr, input.schema())),
+
+        LogicalPlan::Aggregate(Aggregate {
+            input,
+            group_expr,
+            aggr_expr,
+            ..
+        }) => {
+            let schema = input.schema();
+            check_plan_invalid(input)
+                .and_then(|_| check_any_invalid_expr(group_expr, schema))
+                .and_then(|_| check_any_invalid_expr(aggr_expr, schema))
+        }
+
+        LogicalPlan::Join(Join { left, right, .. })
+        | LogicalPlan::CrossJoin(CrossJoin { left, right, .. }) => {
+            check_plan_invalid(left).and_then(|_| 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().try_for_each(check_plan_invalid)
+        }
+
+        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(()),
+    }
+}
+
+/// find first error in the exprs
+fn check_any_invalid_expr(exprs: &[Expr], schema: &DFSchemaRef) -> Result<()> {
+    exprs.iter().try_for_each(|e| check_invalid_expr(e, schema))
+}
+
+/// do some checks for exprs in a logical plan
+fn check_invalid_expr(expr: &Expr, schema: &DFSchemaRef) -> Result<()> {

Review comment:
       Yes, this check is incomplete, it only does some simple check now.
   `A = 5 OR (NOT "foo")` can be checked if add an arm of match:
   ```
   Expr::BinaryExpr {left, op, right} => check_type_matched(left, op, right)
   ```
   But I agree that visitor pattern is better.




-- 
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]


Reply via email to