milenkovicm commented on code in PR #1752:
URL: 
https://github.com/apache/datafusion-ballista/pull/1752#discussion_r3301062383


##########
ballista/scheduler/src/state/aqe/mod.rs:
##########
@@ -122,8 +121,8 @@ pub(crate) struct AdaptiveExecutionGraph {
     session_config: Arc<SessionConfig>,
     /// Logical plan as a human-readable string, captured at submission time.
     logical_plan: Option<String>,
-    /// Physical plan, captured at submission time.
-    physical_plan: Arc<dyn ExecutionPlan>,
+    // /// Physical plan, captured at submission time.

Review Comment:
   To be removed 



##########
ballista/scheduler/src/state/aqe/optimizer_rule/join_selection.rs:
##########
@@ -0,0 +1,644 @@
+// 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.
+
+//! Functional tests for [`CoalescePartitionsRule`]: drive a query through
+//! `AdaptivePlanner`, finalize the upstream stage with synthetic per-partition
+//! byte stats, and snapshot the displayed plan tree so the rule's effect on
+//! the leaf `ExchangeExec` is visible at the `coalesce=K of M` field.
+//!
+//! Each test uses small synthetic byte sizes paired with a small
+//! `coalesce_target_partition_bytes` so the bin-pack outcome is hand-traceable
+//! against `split_size_list_by_target_size`.
+
+use ballista_core::config::BallistaConfig;
+use datafusion::{
+    catalog::memory::DataSourceExec,
+    common::tree_node::{Transformed, TreeNode},
+    error::DataFusionError,
+    physical_optimizer::PhysicalOptimizerRule,
+    physical_plan::{
+        ExecutionPlan,
+        joins::{HashJoinExec, PartitionMode, SortMergeJoinExec},
+    },
+};
+use std::sync::{Arc, atomic::AtomicUsize};
+
+use crate::state::aqe::execution_plan::{
+    DynamicJoinSelectionExec, ExchangeExec, JoinSelectionAction,
+};
+
+#[derive(Debug, Default)]
+pub struct DelayJoinSelectionRule {}
+
+impl PhysicalOptimizerRule for DelayJoinSelectionRule {
+    fn optimize(
+        &self,
+        plan: Arc<dyn ExecutionPlan>,
+        config: &datafusion::config::ConfigOptions,
+    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
+        let bc = config
+            .extensions
+            .get::<BallistaConfig>()
+            .cloned()
+            .unwrap_or_default();
+        if bc.adaptive_join_enabled() {
+            let result = plan.transform_up(|node| {
+                if let Some(hj) = node.as_any().downcast_ref::<HashJoinExec>() 
{
+                    let dynamic = 
DynamicJoinSelectionExec::from_hash_join(hj)?;
+                    Ok(Transformed::yes(dynamic as Arc<dyn ExecutionPlan>))
+                } else if let Some(smj) =
+                    node.as_any().downcast_ref::<SortMergeJoinExec>()
+                {
+                    let dynamic = 
DynamicJoinSelectionExec::from_sort_join(smj)?;
+                    Ok(Transformed::yes(dynamic as Arc<dyn ExecutionPlan>))
+                } else {
+                    Ok(Transformed::no(node))
+                }
+            })?;
+            Ok(result.data)
+        } else {
+            return Ok(plan);
+        }
+    }
+
+    fn name(&self) -> &str {
+        "DelayJoinSelectionRule"
+    }
+
+    fn schema_check(&self) -> bool {
+        true
+    }
+}
+
+#[derive(Debug, Default)]
+pub struct SelectJoinRule {
+    plan_id_generator: Arc<AtomicUsize>,
+}
+
+impl SelectJoinRule {
+    pub(crate) fn new(plan_id_generator: Arc<AtomicUsize>) -> Self {
+        SelectJoinRule { plan_id_generator }
+    }
+
+    fn plan_id(&self) -> usize {
+        self.plan_id_generator
+            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
+    }
+
+    pub fn supports_swap_join_order(
+        left: &dyn ExecutionPlan,
+        right: &dyn ExecutionPlan,
+    ) -> datafusion::common::Result<bool> {
+        // Get the left and right table's total bytes
+        // If both the left and right tables contain total_byte_size 
statistics,
+        // use `total_byte_size` to determine `should_swap_join_order`, else 
use `num_rows`
+        let left_stats = left.partition_statistics(None)?;
+        let right_stats = right.partition_statistics(None)?;
+        // First compare `total_byte_size` of left and right side,
+        // if information in this field is insufficient fallback to the 
`num_rows`
+        match (
+            left_stats.total_byte_size.get_value(),
+            right_stats.total_byte_size.get_value(),
+        ) {
+            (Some(l), Some(r)) => Ok(l > r),
+            _ => match (
+                left_stats.num_rows.get_value(),
+                right_stats.num_rows.get_value(),
+            ) {
+                (Some(l), Some(r)) => Ok(l > r),
+                _ => Ok(false),
+            },
+        }
+    }
+}
+
+impl PhysicalOptimizerRule for SelectJoinRule {
+    fn optimize(
+        &self,
+        plan: Arc<dyn ExecutionPlan>,
+        config: &datafusion::config::ConfigOptions,
+    ) -> datafusion::error::Result<Arc<dyn ExecutionPlan>> {
+        let result = plan.transform_up(|node| {

Review Comment:
   Should not get triggered if disabled in configuration 



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to