Skip to content

Commit

Permalink
first draft of an datafusion logical to optd logical plan conversion …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
SarveshOO7 committed Jan 28, 2025
1 parent 635692f commit 2a40cf5
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 20 deletions.
51 changes: 44 additions & 7 deletions infra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use datafusion::execution::SessionStateBuilder;
use datafusion::logical_expr::{Explain, LogicalPlan, PlanType, TableSource, ToStringifiedPlan};

Check warning on line 17 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L17

warning: unused imports: `Explain`, `PlanType`, `TableSource`, and `ToStringifiedPlan` --> infra/src/lib.rs:17:32 | 17 | use datafusion::logical_expr::{Explain, LogicalPlan, PlanType, TableSource, ToStringifiedPlan}; | ^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
Raw output
infra/src/lib.rs:17:32:w:warning: unused imports: `Explain`, `PlanType`, `TableSource`, and `ToStringifiedPlan`
  --> infra/src/lib.rs:17:32
   |
17 | use datafusion::logical_expr::{Explain, LogicalPlan, PlanType, TableSource, ToStringifiedPlan};
   |                                ^^^^^^^               ^^^^^^^^  ^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^


__END__
use datafusion::physical_plan::ExecutionPlan;
use datafusion::physical_planner::{DefaultPhysicalPlanner, PhysicalPlanner};
use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion::prelude::{Expr, SessionConfig, SessionContext};

/// TODO make distinction between relational groups and scalar groups.
#[repr(transparent)]
Expand All @@ -29,7 +29,11 @@ pub struct GroupId(u64);
pub struct ExprId(u64);

mod types;
use types::plan::logical_plan::LogicalPlan as OptDLogicalPlan;
use types::operator::logical::{
LogicalFilterOperator, LogicalJoinOperator, LogicalOperator, LogicalScanOperator,
};
use types::operator::ScalarOperator;
use types::plan::logical_plan::{LogicalLink, LogicalPlan as OptDLogicalPlan, ScalarLink};

Check warning on line 36 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L36

warning: unused import: `LogicalPlan as OptDLogicalPlan` --> infra/src/lib.rs:36:46 | 36 | use types::plan::logical_plan::{LogicalLink, LogicalPlan as OptDLogicalPlan, ScalarLink}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Raw output
infra/src/lib.rs:36:46:w:warning: unused import: `LogicalPlan as OptDLogicalPlan`
  --> infra/src/lib.rs:36:46
   |
36 | use types::plan::logical_plan::{LogicalLink, LogicalPlan as OptDLogicalPlan, ScalarLink};
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


__END__

struct OptdOptimizer {}

Expand All @@ -38,14 +42,47 @@ pub struct OptdQueryPlanner {
}

impl OptdQueryPlanner {

Check warning on line 44 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L44

warning: associated functions `convert_into_optd_scalar` and `convert_into_optd_logical` are never used --> infra/src/lib.rs:45:8 | 44 | impl OptdQueryPlanner { | --------------------- associated functions in this implementation 45 | fn convert_into_optd_scalar(predicate_expr: Expr) -> Arc<ScalarOperator<ScalarLink>> { | ^^^^^^^^^^^^^^^^^^^^^^^^ ... 50 | fn convert_into_optd_logical(plan_node: Arc<LogicalPlan>) -> Arc<LogicalOperator<LogicalLink>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
Raw output
infra/src/lib.rs:44:1:w:warning: associated functions `convert_into_optd_scalar` and `convert_into_optd_logical` are never used
  --> infra/src/lib.rs:45:8
   |
44 | impl OptdQueryPlanner {
   | --------------------- associated functions in this implementation
45 |     fn convert_into_optd_scalar(predicate_expr: Expr) -> Arc<ScalarOperator<ScalarLink>> {
   |        ^^^^^^^^^^^^^^^^^^^^^^^^
...
50 |     fn convert_into_optd_logical(plan_node: Arc<LogicalPlan>) -> Arc<LogicalOperator<LogicalLink>> {
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default


__END__
fn convert_into_optd_logical(plan_node: LogicalPlan) -> OptDLogicalPlan {
match plan_node {
LogicalPlan::Filter(filter) => todo!(),
LogicalPlan::Join(join) => todo!(),
LogicalPlan::TableScan(table_scan) => todo!(),
fn convert_into_optd_scalar(predicate_expr: Expr) -> Arc<ScalarOperator<ScalarLink>> {

Check warning on line 45 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L45

warning: unused variable: `predicate_expr` --> infra/src/lib.rs:45:33 | 45 | fn convert_into_optd_scalar(predicate_expr: Expr) -> Arc<ScalarOperator<ScalarLink>> { | ^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_predicate_expr`
Raw output
infra/src/lib.rs:45:33:w:warning: unused variable: `predicate_expr`
  --> infra/src/lib.rs:45:33
   |
45 |     fn convert_into_optd_scalar(predicate_expr: Expr) -> Arc<ScalarOperator<ScalarLink>> {
   |                                 ^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_predicate_expr`


__END__
// TODO: Implement the conversion logic here
Arc::new(ScalarOperator::new())
}

fn convert_into_optd_logical(plan_node: Arc<LogicalPlan>) -> Arc<LogicalOperator<LogicalLink>> {
match &*plan_node {
LogicalPlan::Filter(filter) => {
Arc::new(LogicalOperator::Filter(LogicalFilterOperator {
child: LogicalLink::LogicalNode(Self::convert_into_optd_logical(
filter.input.clone(),
)),
predicate: LogicalLink::ScalarNode(Self::convert_into_optd_scalar(
filter.predicate.clone(),
)),
}))
}

LogicalPlan::Join(join) => Arc::new(LogicalOperator::Join(
(LogicalJoinOperator {

Check warning on line 64 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L64

warning: consider removing unnecessary double parentheses --> infra/src/lib.rs:64:17 | 64 | / (LogicalJoinOperator { 65 | | join_type: (), 66 | | left: LogicalLink::LogicalNode(Self::convert_into_optd_logical( 67 | | join.left.clone(), ... | 72 | | condition: LogicalLink::ScalarNode(Arc::new(todo!())), 73 | | }), | |__________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens = note: `#[warn(clippy::double_parens)]` on by default
Raw output
infra/src/lib.rs:64:17:w:warning: consider removing unnecessary double parentheses
  --> infra/src/lib.rs:64:17
   |
64 | /                 (LogicalJoinOperator {
65 | |                     join_type: (),
66 | |                     left: LogicalLink::LogicalNode(Self::convert_into_optd_logical(
67 | |                         join.left.clone(),
...  |
72 | |                     condition: LogicalLink::ScalarNode(Arc::new(todo!())),
73 | |                 }),
   | |__________________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
   = note: `#[warn(clippy::double_parens)]` on by default


__END__

Check warning on line 64 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L64

warning: unnecessary parentheses around function argument --> infra/src/lib.rs:64:17 | 64 | (LogicalJoinOperator { | ^ ... 73 | }), | ^ | = note: `#[warn(unused_parens)]` on by default help: remove these parentheses | 64 ~ LogicalJoinOperator { 65 | join_type: (), ... 72 | condition: LogicalLink::ScalarNode(Arc::new(todo!())), 73 ~ }, |
Raw output
infra/src/lib.rs:64:17:w:warning: unnecessary parentheses around function argument
  --> infra/src/lib.rs:64:17
   |
64 |                 (LogicalJoinOperator {
   |                 ^
...
73 |                 }),
   |                  ^
   |
   = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
   |
64 ~                 LogicalJoinOperator {
65 |                     join_type: (),
...
72 |                     condition: LogicalLink::ScalarNode(Arc::new(todo!())),
73 ~                 },
   |


__END__
join_type: (),
left: LogicalLink::LogicalNode(Self::convert_into_optd_logical(
join.left.clone(),
)),
right: LogicalLink::LogicalNode(Self::convert_into_optd_logical(
join.right.clone(),
)),
condition: LogicalLink::ScalarNode(Arc::new(todo!())),

Check warning on line 72 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L72

warning: unreachable call --> infra/src/lib.rs:72:56 | 72 | condition: LogicalLink::ScalarNode(Arc::new(todo!())), | ^^^^^^^^ ------- any code following this expression is unreachable | | | unreachable call | = note: `#[warn(unreachable_code)]` on by default
Raw output
infra/src/lib.rs:72:56:w:warning: unreachable call
  --> infra/src/lib.rs:72:56
   |
72 |                     condition: LogicalLink::ScalarNode(Arc::new(todo!())),
   |                                                        ^^^^^^^^ ------- any code following this expression is unreachable
   |                                                        |
   |                                                        unreachable call
   |
   = note: `#[warn(unreachable_code)]` on by default


__END__
}),
)),

LogicalPlan::TableScan(table_scan) => Arc::new(LogicalOperator::Scan(
(LogicalScanOperator {

Check warning on line 77 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L77

warning: consider removing unnecessary double parentheses --> infra/src/lib.rs:77:17 | 77 | / (LogicalScanOperator { 78 | | table_name: table_scan.table_name.to_quoted_string(), 79 | | predicate: None, // TODO fix this: there are multiple predicates in the scan but our IR only accepts one 80 | | }), | |__________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens
Raw output
infra/src/lib.rs:77:17:w:warning: consider removing unnecessary double parentheses
  --> infra/src/lib.rs:77:17
   |
77 | /                 (LogicalScanOperator {
78 | |                     table_name: table_scan.table_name.to_quoted_string(),
79 | |                     predicate: None, // TODO fix this: there are multiple predicates in the scan but our IR only accepts one
80 | |                 }),
   | |__________________^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_parens


__END__

Check warning on line 77 in infra/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/lib.rs#L77

warning: unnecessary parentheses around function argument --> infra/src/lib.rs:77:17 | 77 | (LogicalScanOperator { | ^ ... 80 | }), | ^ | help: remove these parentheses | 77 ~ LogicalScanOperator { 78 | table_name: table_scan.table_name.to_quoted_string(), 79 | predicate: None, // TODO fix this: there are multiple predicates in the scan but our IR only accepts one 80 ~ }, |
Raw output
infra/src/lib.rs:77:17:w:warning: unnecessary parentheses around function argument
  --> infra/src/lib.rs:77:17
   |
77 |                 (LogicalScanOperator {
   |                 ^
...
80 |                 }),
   |                  ^
   |
help: remove these parentheses
   |
77 ~                 LogicalScanOperator {
78 |                     table_name: table_scan.table_name.to_quoted_string(),
79 |                     predicate: None, // TODO fix this: there are multiple predicates in the scan but our IR only accepts one
80 ~                 },
   |


__END__
table_name: table_scan.table_name.to_quoted_string(),
predicate: None, // TODO fix this: there are multiple predicates in the scan but our IR only accepts one
}),
)),
_ => panic!("OptD does not support this type of query yet"),
}
}

async fn create_physical_plan_inner(
&self,
logical_plan: &LogicalPlan,
Expand Down
12 changes: 4 additions & 8 deletions infra/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use datafusion::physical_plan::ExecutionPlanProperties;
use datafusion::physical_plan::Partitioning;
use datafusion::prelude::SessionConfig;
use futures::StreamExt;
use std::{io, time::SystemTime};
use infra::create_df_context;
use std::{io, time::SystemTime};

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -16,13 +16,9 @@ async fn main() -> Result<()> {

let session_config = SessionConfig::from_env()?.with_information_schema(true);

let ctx = crate::create_df_context(
Some(session_config.clone()),
Some(rt_config.clone()),
None
)
.await
.unwrap();
let ctx = crate::create_df_context(Some(session_config.clone()), Some(rt_config.clone()), None)
.await
.unwrap();

// Create a DataFrame with the input query
let queries = io::read_to_string(io::stdin())?;
Expand Down
4 changes: 3 additions & 1 deletion infra/src/types/memo/rule.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::Memo;
use crate::{
types::expression::{relational::logical::LogicalExpr, Expr},
types::plan::{partial_logical_plan::PartialLogicalPlan, partial_physical_plan::PartialPhysicalPlan},
types::plan::{
partial_logical_plan::PartialLogicalPlan, partial_physical_plan::PartialPhysicalPlan,
},
};

#[trait_variant::make(Send)]
Expand Down
2 changes: 1 addition & 1 deletion infra/src/types/operator/logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum LogicalOperator<Link> {
/// TODO Add docs.
pub struct LogicalScanOperator<Link> {

Check warning on line 26 in infra/src/types/operator/logical.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/types/operator/logical.rs#L26

warning: struct `LogicalScanOperator` is never constructed --> infra/src/types/operator/logical.rs:26:12 | 26 | pub struct LogicalScanOperator<Link> { | ^^^^^^^^^^^^^^^^^^^
Raw output
infra/src/types/operator/logical.rs:26:12:w:warning: struct `LogicalScanOperator` is never constructed
  --> infra/src/types/operator/logical.rs:26:12
   |
26 | pub struct LogicalScanOperator<Link> {
   |            ^^^^^^^^^^^^^^^^^^^


__END__
pub table_name: String,
pub predicate: Link,
pub predicate: Option<Link>,
}

/// TODO Add docs.
Expand Down
11 changes: 11 additions & 0 deletions infra/src/types/operator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{marker::PhantomData, sync::Arc};

Check warning on line 1 in infra/src/types/operator/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/types/operator/mod.rs#L1

warning: unused import: `sync::Arc` --> infra/src/types/operator/mod.rs:1:32 | 1 | use std::{marker::PhantomData, sync::Arc}; | ^^^^^^^^^
Raw output
infra/src/types/operator/mod.rs:1:32:w:warning: unused import: `sync::Arc`
 --> infra/src/types/operator/mod.rs:1:32
  |
1 | use std::{marker::PhantomData, sync::Arc};
  |                                ^^^^^^^^^


__END__

use super::plan::logical_plan::ScalarLink;

pub mod logical;
pub mod physical;

Expand All @@ -10,3 +12,12 @@ pub mod physical;
pub struct ScalarOperator<Link> {

Check warning on line 12 in infra/src/types/operator/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/types/operator/mod.rs#L12

warning: struct `ScalarOperator` is never constructed --> infra/src/types/operator/mod.rs:12:12 | 12 | pub struct ScalarOperator<Link> { | ^^^^^^^^^^^^^^
Raw output
infra/src/types/operator/mod.rs:12:12:w:warning: struct `ScalarOperator` is never constructed
  --> infra/src/types/operator/mod.rs:12:12
   |
12 | pub struct ScalarOperator<Link> {
   |            ^^^^^^^^^^^^^^


__END__
_phantom: PhantomData<Link>,
}

impl ScalarOperator<ScalarLink> {

Check warning on line 16 in infra/src/types/operator/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] infra/src/types/operator/mod.rs#L16

warning: associated function `new` is never used --> infra/src/types/operator/mod.rs:18:12 | 16 | impl ScalarOperator<ScalarLink> { | ------------------------------- associated function in this implementation 17 | // Add a public constructor 18 | pub fn new() -> Self { | ^^^
Raw output
infra/src/types/operator/mod.rs:16:1:w:warning: associated function `new` is never used
  --> infra/src/types/operator/mod.rs:18:12
   |
16 | impl ScalarOperator<ScalarLink> {
   | ------------------------------- associated function in this implementation
17 |     // Add a public constructor
18 |     pub fn new() -> Self {
   |            ^^^


__END__
// Add a public constructor
pub fn new() -> Self {
ScalarOperator {
_phantom: std::marker::PhantomData,
}
}
}
4 changes: 3 additions & 1 deletion infra/src/types/plan/partial_physical_plan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::types::operator::{logical::LogicalOperator, physical::PhysicalOperator, ScalarOperator};
use crate::types::operator::{
logical::LogicalOperator, physical::PhysicalOperator, ScalarOperator,
};
use crate::GroupId;
use std::sync::Arc;

Expand Down
7 changes: 5 additions & 2 deletions infra/src/types/plan/physical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use crate::types::operator::{
ScalarOperator,
};
use datafusion::{
common::{arrow::datatypes::Schema, JoinType}, datasource::physical_plan::{CsvExecBuilder, FileScanConfig}, execution::object_store::ObjectStoreUrl, physical_plan::{
common::{arrow::datatypes::Schema, JoinType},
datasource::physical_plan::{CsvExecBuilder, FileScanConfig},
execution::object_store::ObjectStoreUrl,
physical_plan::{
expressions::NoOp,
filter::FilterExec,
joins::{HashJoinExec, PartitionMode},
ExecutionPlan,
}
},
};
use std::sync::Arc;

Expand Down

0 comments on commit 2a40cf5

Please sign in to comment.