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

refactor: correct the implementation of all_schemas() #5236

Merged
merged 2 commits into from
Feb 12, 2023
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
84 changes: 38 additions & 46 deletions datafusion/expr/src/logical_plan/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,62 +174,54 @@ impl LogicalPlan {
}
}

/// Get a vector of references to all schemas in every node of the logical plan
/// Get all meaningful schemas of a plan and its children plan.
pub fn all_schemas(&self) -> Vec<&DFSchemaRef> {
match self {
LogicalPlan::TableScan(TableScan {
projected_schema, ..
}) => vec![projected_schema],
LogicalPlan::Window(Window { input, schema, .. })
| LogicalPlan::Projection(Projection { input, schema, .. })
| LogicalPlan::Aggregate(Aggregate { input, schema, .. })
| LogicalPlan::Unnest(Unnest { input, schema, .. }) => {
let mut schemas = input.all_schemas();
schemas.insert(0, schema);
// return self and children schemas
LogicalPlan::Window(_)
| LogicalPlan::Projection(_)
| LogicalPlan::Aggregate(_)
| LogicalPlan::Unnest(_)
| LogicalPlan::Join(_)
| LogicalPlan::CrossJoin(_) => {
let mut schemas = vec![self.schema()];
self.inputs().iter().for_each(|input| {
schemas.push(input.schema());
});
schemas
}
LogicalPlan::Join(Join {
left,
right,
schema,
..
})
| LogicalPlan::CrossJoin(CrossJoin {
left,
right,
schema,
}) => {
let mut schemas = left.all_schemas();
schemas.extend(right.all_schemas());
schemas.insert(0, schema);
schemas
// just return self.schema()
LogicalPlan::Explain(_)
| LogicalPlan::Analyze(_)
| LogicalPlan::EmptyRelation(_)
| LogicalPlan::CreateExternalTable(_)
| LogicalPlan::CreateCatalogSchema(_)
| LogicalPlan::CreateCatalog(_)
| LogicalPlan::Dml(_)
| LogicalPlan::Values(_)
| LogicalPlan::SubqueryAlias(_)
| LogicalPlan::Union(_)
| LogicalPlan::TableScan(_) => {
vec![self.schema()]
}
LogicalPlan::Subquery(Subquery { subquery, .. }) => subquery.all_schemas(),
LogicalPlan::Extension(extension) => vec![extension.node.schema()],
LogicalPlan::Explain(Explain { schema, .. })
| LogicalPlan::Analyze(Analyze { schema, .. })
| LogicalPlan::EmptyRelation(EmptyRelation { schema, .. })
| LogicalPlan::CreateExternalTable(CreateExternalTable { schema, .. })
| LogicalPlan::CreateCatalogSchema(CreateCatalogSchema { schema, .. })
| LogicalPlan::CreateCatalog(CreateCatalog { schema, .. })
| LogicalPlan::Values(Values { schema, .. })
| LogicalPlan::SubqueryAlias(SubqueryAlias { schema, .. })
| LogicalPlan::Union(Union { schema, .. }) => {
vec![schema]
// return children schemas
LogicalPlan::Limit(_)
| LogicalPlan::Subquery(_)
| LogicalPlan::Extension(_)
| LogicalPlan::Repartition(_)
| LogicalPlan::Sort(_)
| LogicalPlan::CreateMemoryTable(_)
| LogicalPlan::CreateView(_)
| LogicalPlan::Filter(_)
| LogicalPlan::Distinct(_)
| LogicalPlan::Prepare(_) => {
self.inputs().iter().map(|p| p.schema()).collect()
}
LogicalPlan::Limit(Limit { input, .. })
| LogicalPlan::Repartition(Repartition { input, .. })
| LogicalPlan::Sort(Sort { input, .. })
| LogicalPlan::CreateMemoryTable(CreateMemoryTable { input, .. })
| LogicalPlan::CreateView(CreateView { input, .. })
| LogicalPlan::Filter(Filter { input, .. })
| LogicalPlan::Distinct(Distinct { input, .. })
| LogicalPlan::Prepare(Prepare { input, .. }) => input.all_schemas(),
// return empty
LogicalPlan::DropTable(_)
| LogicalPlan::DropView(_)
| LogicalPlan::DescribeTable(_)
| LogicalPlan::SetVariable(_) => vec![],
LogicalPlan::Dml(DmlStatement { table_schema, .. }) => vec![table_schema],
}
}

Expand Down