Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 12b64d1

Browse files
committed
remove into function in datafusion-repr, add schema conversion in bridge repo instead
Signed-off-by: AveryQi115 <[email protected]>
1 parent 45974d2 commit 12b64d1

File tree

5 files changed

+24
-37
lines changed

5 files changed

+24
-37
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

optd-datafusion-bridge/src/from_optd.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashMap, sync::Arc};
33
use anyhow::{bail, Context, Result};
44
use async_recursion::async_recursion;
55
use datafusion::{
6-
arrow::datatypes::{Schema, SchemaRef},
6+
arrow::datatypes::{DataType, Field, Schema, SchemaRef},
77
datasource::source_as_provider,
88
logical_expr::Operator,
99
physical_expr,
@@ -33,6 +33,27 @@ use optd_datafusion_repr::{
3333

3434
use crate::{physical_collector::CollectorExec, OptdPlanContext};
3535

36+
// TODO: current DataType and ConstantType are not 1 to 1 mapping
37+
// optd schema stores constantType from data type in catalog.get
38+
// for decimal128, the precision is lost
39+
fn from_optd_schema(optd_schema: &OptdSchema) -> Schema {
40+
let match_type = |typ: &ConstantType| match typ {
41+
ConstantType::Any => unimplemented!(),
42+
ConstantType::Bool => DataType::Boolean,
43+
ConstantType::Int => DataType::Int64,
44+
ConstantType::Date => DataType::Date32,
45+
ConstantType::Decimal => DataType::Float64,
46+
ConstantType::Utf8String => DataType::Utf8,
47+
};
48+
let fields: Vec<_> = optd_schema
49+
.0
50+
.iter()
51+
.enumerate()
52+
.map(|(i, typ)| Field::new(&format!("c{}", i), match_type(typ), false))
53+
.collect();
54+
Schema::new(fields)
55+
}
56+
3657
impl OptdPlanContext<'_> {
3758
#[async_recursion]
3859
async fn from_optd_table_scan(
@@ -449,7 +470,7 @@ impl OptdPlanContext<'_> {
449470
}
450471
OptRelNodeTyp::PhysicalEmptyRelation => {
451472
let physical_node = PhysicalEmptyRelation::from_rel_node(rel_node).unwrap();
452-
let datafusion_schema: Schema = schema.into();
473+
let datafusion_schema: Schema = from_optd_schema(&schema);
453474
Ok(Arc::new(datafusion::physical_plan::empty::EmptyExec::new(
454475
physical_node.produce_one_row(),
455476
Arc::new(datafusion_schema),
@@ -461,7 +482,6 @@ impl OptdPlanContext<'_> {
461482
}
462483

463484
pub async fn from_optd(&mut self, root_rel: OptRelNodeRef) -> Result<Arc<dyn ExecutionPlan>> {
464-
println!("{}", root_rel);
465485
self.from_optd_plan_node(PlanNode::from_rel_node(root_rel).unwrap())
466486
.await
467487
}

optd-datafusion-repr/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
anyhow = "1"
10-
arrow-schema = "*"
11-
datafusion = "32.0.0"
1210
num-traits = "0.2"
1311
num-derive = "0.2"
1412
tracing = "0.1"

optd-datafusion-repr/src/cost/base_cost.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl CostModel<OptRelNodeTyp> for OptCostModel {
106106
.unwrap_or(1) as f64;
107107
Self::cost(row_cnt, 0.0, row_cnt)
108108
}
109-
OptRelNodeTyp::PhysicalEmptyRelation => Self::cost(1.0, 0.01, 0.0),
109+
OptRelNodeTyp::PhysicalEmptyRelation => Self::cost(0.5, 0.01, 0.0),
110110
OptRelNodeTyp::PhysicalFilter => {
111111
let (row_cnt, _, _) = Self::cost_tuple(&children[0]);
112112
let (_, compute_cost, _) = Self::cost_tuple(&children[1]);

optd-datafusion-repr/src/properties/schema.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
use std::{
2-
collections::HashMap,
3-
sync::{Arc, Mutex},
4-
};
5-
61
use optd_core::property::PropertyBuilder;
72

83
use crate::plan_nodes::{ConstantType, OptRelNodeTyp};
9-
use datafusion::arrow::datatypes::{DataType, Field, Schema as DatafusionSchema};
104

115
#[derive(Clone, Debug)]
126
pub struct Schema(pub Vec<ConstantType>);
@@ -18,29 +12,6 @@ impl Schema {
1812
}
1913
}
2014

21-
impl Into<DatafusionSchema> for Schema {
22-
// TODO: current DataType and ConstantType are not 1 to 1 mapping
23-
// optd schema stores constantType from data type in catalog.get
24-
// for decimal128, the precision is lost
25-
fn into(self) -> DatafusionSchema {
26-
let match_type = |typ: &ConstantType| match typ {
27-
ConstantType::Any => unimplemented!(),
28-
ConstantType::Bool => DataType::Boolean,
29-
ConstantType::Int => DataType::Int64,
30-
ConstantType::Date => DataType::Date32,
31-
ConstantType::Decimal => DataType::Float64,
32-
ConstantType::Utf8String => DataType::Utf8,
33-
};
34-
let fields: Vec<_> = self
35-
.0
36-
.iter()
37-
.enumerate()
38-
.map(|(i, typ)| Field::new(&format!("c{}", i), match_type(typ), false))
39-
.collect();
40-
DatafusionSchema::new(fields)
41-
}
42-
}
43-
4415
pub trait Catalog: Send + Sync + 'static {
4516
fn get(&self, name: &str) -> Schema;
4617
}

0 commit comments

Comments
 (0)