Skip to content

Commit db9dbbe

Browse files
authored
feat(cost-model): migrate pred types (#39)
1 parent 9ba03e6 commit db9dbbe

File tree

16 files changed

+734
-14
lines changed

16 files changed

+734
-14
lines changed

optd-cost-model/src/common/nodes.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::sync::Arc;
1+
use core::fmt;
2+
use std::{fmt::Display, sync::Arc};
23

34
use arrow_schema::DataType;
45

@@ -24,6 +25,12 @@ pub enum JoinType {
2425
RightAnti,
2526
}
2627

28+
impl Display for JoinType {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
write!(f, "{:?}", self)
31+
}
32+
}
33+
2734
/// TODO: documentation
2835
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
2936
pub enum PhysicalNodeType {
@@ -49,8 +56,7 @@ impl std::fmt::Display for PhysicalNodeType {
4956
pub enum PredicateType {
5057
List,
5158
Constant(ConstantType),
52-
AttributeRef,
53-
ExternAttributeRef,
59+
AttrIndex,
5460
UnOp(UnOpType),
5561
BinOp(BinOpType),
5662
LogOp(LogOpType),
@@ -77,7 +83,7 @@ pub struct PredicateNode {
7783
/// A generic predicate node type
7884
pub typ: PredicateType,
7985
/// Child predicate nodes, always materialized
80-
pub children: Vec<PredicateNode>,
86+
pub children: Vec<ArcPredicateNode>,
8187
/// Data associated with the predicate, if any
8288
pub data: Option<Value>,
8389
}
@@ -94,3 +100,28 @@ impl std::fmt::Display for PredicateNode {
94100
write!(f, ")")
95101
}
96102
}
103+
104+
impl PredicateNode {
105+
pub fn child(&self, idx: usize) -> ArcPredicateNode {
106+
self.children[idx].clone()
107+
}
108+
109+
pub fn unwrap_data(&self) -> Value {
110+
self.data.clone().unwrap()
111+
}
112+
}
113+
pub trait ReprPredicateNode: 'static + Clone {
114+
fn into_pred_node(self) -> ArcPredicateNode;
115+
116+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self>;
117+
}
118+
119+
impl ReprPredicateNode for ArcPredicateNode {
120+
fn into_pred_node(self) -> ArcPredicateNode {
121+
self
122+
}
123+
124+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
125+
Some(pred_node)
126+
}
127+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::common::{
2+
nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode},
3+
values::Value,
4+
};
5+
6+
/// [`AttributeIndexPred`] represents the position of an attribute in a schema or
7+
/// [`GroupAttrRefs`].
8+
///
9+
/// The `data` field holds the index of the attribute in the schema or [`GroupAttrRefs`].
10+
#[derive(Clone, Debug)]
11+
pub struct AttrIndexPred(pub ArcPredicateNode);
12+
13+
impl AttrIndexPred {
14+
pub fn new(attr_idx: u64) -> AttrIndexPred {
15+
AttrIndexPred(
16+
PredicateNode {
17+
typ: PredicateType::AttrIndex,
18+
children: vec![],
19+
data: Some(Value::UInt64(attr_idx)),
20+
}
21+
.into(),
22+
)
23+
}
24+
25+
/// Gets the attribute index.
26+
pub fn attr_index(&self) -> u64 {
27+
self.0.data.as_ref().unwrap().as_u64()
28+
}
29+
}
30+
31+
impl ReprPredicateNode for AttrIndexPred {
32+
fn into_pred_node(self) -> ArcPredicateNode {
33+
self.0
34+
}
35+
36+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
37+
if pred_node.typ != PredicateType::AttrIndex {
38+
return None;
39+
}
40+
Some(Self(pred_node))
41+
}
42+
}

optd-cost-model/src/common/predicates/bin_op_pred.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::common::nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode};
2+
13
/// TODO: documentation
24
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
35
pub enum BinOpType {
@@ -38,3 +40,48 @@ impl BinOpType {
3840
)
3941
}
4042
}
43+
44+
#[derive(Clone, Debug)]
45+
pub struct BinOpPred(pub ArcPredicateNode);
46+
47+
impl BinOpPred {
48+
pub fn new(left: ArcPredicateNode, right: ArcPredicateNode, op_type: BinOpType) -> Self {
49+
BinOpPred(
50+
PredicateNode {
51+
typ: PredicateType::BinOp(op_type),
52+
children: vec![left, right],
53+
data: None,
54+
}
55+
.into(),
56+
)
57+
}
58+
59+
pub fn left_child(&self) -> ArcPredicateNode {
60+
self.0.child(0)
61+
}
62+
63+
pub fn right_child(&self) -> ArcPredicateNode {
64+
self.0.child(1)
65+
}
66+
67+
pub fn op_type(&self) -> BinOpType {
68+
if let PredicateType::BinOp(op_type) = self.0.typ {
69+
op_type
70+
} else {
71+
panic!("not a bin op")
72+
}
73+
}
74+
}
75+
76+
impl ReprPredicateNode for BinOpPred {
77+
fn into_pred_node(self) -> ArcPredicateNode {
78+
self.0
79+
}
80+
81+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
82+
if !matches!(pred_node.typ, PredicateType::BinOp(_)) {
83+
return None;
84+
}
85+
Some(Self(pred_node))
86+
}
87+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use arrow_schema::DataType;
2+
3+
use crate::common::nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode};
4+
5+
use super::data_type_pred::DataTypePred;
6+
7+
/// [`CastPred`] casts a column from one data type to another.
8+
///
9+
/// A [`CastPred`] has two children:
10+
/// 1. The original data to cast
11+
/// 2. The target data type to cast to
12+
#[derive(Clone, Debug)]
13+
pub struct CastPred(pub ArcPredicateNode);
14+
15+
impl CastPred {
16+
pub fn new(child: ArcPredicateNode, cast_to: DataType) -> Self {
17+
CastPred(
18+
PredicateNode {
19+
typ: PredicateType::Cast,
20+
children: vec![child, DataTypePred::new(cast_to).into_pred_node()],
21+
data: None,
22+
}
23+
.into(),
24+
)
25+
}
26+
27+
pub fn child(&self) -> ArcPredicateNode {
28+
self.0.child(0)
29+
}
30+
31+
pub fn cast_to(&self) -> DataType {
32+
DataTypePred::from_pred_node(self.0.child(1))
33+
.unwrap()
34+
.data_type()
35+
}
36+
}
37+
38+
impl ReprPredicateNode for CastPred {
39+
fn into_pred_node(self) -> ArcPredicateNode {
40+
self.0
41+
}
42+
43+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
44+
if !matches!(pred_node.typ, PredicateType::Cast) {
45+
return None;
46+
}
47+
Some(Self(pred_node))
48+
}
49+
}

0 commit comments

Comments
 (0)