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

DRAFT: experiment of #9637 #9658

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions datafusion/common/src/dfschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,19 @@ impl DFSchema {
})
}

pub fn equivalent_names_and_types_v2(&self, fields: &Vec<DFField>) -> bool {
if self.fields().len() != fields.len() {
return false;
}
let self_fields = self.fields().iter();
let other_fields = fields.iter();
self_fields.zip(other_fields).all(|(f1, f2)| {
f1.qualifier() == f2.qualifier()
&& f1.name() == f2.name()
&& Self::datatype_is_semantically_equal(f1.data_type(), f2.data_type())
})
}

/// Checks if two [`DataType`]s are logically equal. This is a notably weaker constraint
/// than datatype_is_semantically_equal in that a Dictionary<K,V> type is logically
/// equal to a plain V type, but not semantically equal. Dictionary<K1, V1> is also
Expand Down
1 change: 1 addition & 0 deletions datafusion/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod file_options;
pub mod format;
pub mod hash_utils;
pub mod instant;
pub mod optimize_node;
pub mod parsers;
pub mod rounding;
pub mod scalar;
Expand Down
64 changes: 64 additions & 0 deletions datafusion/common/src/optimize_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::DataFusionError;

#[derive(Debug, PartialEq, Eq)]
pub enum OptimizedState {
Yes,
No,
Fail,
}

#[derive(Debug)]
pub struct Optimized<T, E = DataFusionError> {
pub optimzied_data: Option<T>,
// Used to store the original data if optimized successfully
pub original_data: T,
pub optimized_state: OptimizedState,
// Used to store the error if optimized failed, so we can early return but preserve the original data
pub error: Option<E>,
}

impl<T, E> Optimized<T, E> {
pub fn yes(optimzied_data: T, original_data: T) -> Self {
Self {
optimzied_data: Some(optimzied_data),
original_data,
optimized_state: OptimizedState::Yes,
error: None,
}
}

pub fn no(original_data: T) -> Self {
Self {
optimzied_data: None,
original_data,
optimized_state: OptimizedState::No,
error: None,
}
}

pub fn fail(original_data: T, e: E) -> Self {
Self {
optimzied_data: None,
original_data,
optimized_state: OptimizedState::Fail,
error: Some(e),
}
}
}
4 changes: 4 additions & 0 deletions datafusion/common/src/tree_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ impl<T> Transformed<T> {
}
}

pub fn is_transformed(&self) -> bool {
self.transformed
}

/// Wrapper for transformed data with [`TreeNodeRecursion::Continue`] statement.
pub fn yes(data: T) -> Self {
Self::new(data, true, TreeNodeRecursion::Continue)
Expand Down
49 changes: 35 additions & 14 deletions datafusion/core/src/execution/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1875,16 +1875,29 @@ impl SessionState {
stringified_plans
.push(analyzed_plan.to_stringified(PlanType::FinalAnalyzedLogicalPlan));

// optimize the child plan, capturing the output of each optimizer
let optimized_plan = self.optimizer.optimize(
&analyzed_plan,
self,
|optimized_plan, optimizer| {
let optimizer_name = optimizer.name().to_string();
let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };
stringified_plans.push(optimized_plan.to_stringified(plan_type));
},
);
let optimized_plan = if self.options().optimizer.skip_failed_rules {
self.optimizer.optimize(
&analyzed_plan,
self,
|optimized_plan, optimizer| {
let optimizer_name = optimizer.name().to_string();
let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };
stringified_plans.push(optimized_plan.to_stringified(plan_type));
},
)
} else {
// optimize the child plan, capturing the output of each optimizer
self.optimizer.optimize_owned(
analyzed_plan,
self,
|optimized_plan, optimizer| {
let optimizer_name = optimizer.name().to_string();
let plan_type = PlanType::OptimizedLogicalPlan { optimizer_name };
stringified_plans.push(optimized_plan.to_stringified(plan_type));
},
)
};

let (plan, logical_optimization_succeeded) = match optimized_plan {
Ok(plan) => (Arc::new(plan), true),
Err(DataFusionError::Context(optimizer_name, err)) => {
Expand All @@ -1904,10 +1917,18 @@ impl SessionState {
logical_optimization_succeeded,
}))
} else {
let analyzed_plan =
self.analyzer
.execute_and_check(plan, self.options(), |_, _| {})?;
self.optimizer.optimize(&analyzed_plan, self, |_, _| {})
if self.options().optimizer.skip_failed_rules {
let analyzed_plan =
self.analyzer
.execute_and_check(plan, self.options(), |_, _| {})?;
self.optimizer.optimize(&analyzed_plan, self, |_, _| {})
} else {
let analyzed_plan =
self.analyzer
.execute_and_check(plan, self.options(), |_, _| {})?;
self.optimizer
.optimize_owned(analyzed_plan, self, |_, _| {})
}
}
}

Expand Down
Loading
Loading