Skip to content

Commit 4795d13

Browse files
committed
Complete partial implementation of filter
1 parent 5049ed8 commit 4795d13

File tree

5 files changed

+502
-3
lines changed

5 files changed

+502
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::common::{
2+
nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode},
3+
values::Value,
4+
};
5+
6+
use super::list_pred::ListPred;
7+
8+
#[derive(Clone, Debug)]
9+
pub struct InListPred(pub ArcPredicateNode);
10+
11+
impl InListPred {
12+
pub fn new(child: ArcPredicateNode, list: ListPred, negated: bool) -> Self {
13+
InListPred(
14+
PredicateNode {
15+
typ: PredicateType::InList,
16+
children: vec![child, list.into_pred_node()],
17+
data: Some(Value::Bool(negated)),
18+
}
19+
.into(),
20+
)
21+
}
22+
23+
pub fn child(&self) -> ArcPredicateNode {
24+
self.0.child(0)
25+
}
26+
27+
pub fn list(&self) -> ListPred {
28+
ListPred::from_pred_node(self.0.child(1)).unwrap()
29+
}
30+
31+
/// `true` for `NOT IN`.
32+
pub fn negated(&self) -> bool {
33+
self.0.data.as_ref().unwrap().as_bool()
34+
}
35+
}
36+
37+
impl ReprPredicateNode for InListPred {
38+
fn into_pred_node(self) -> ArcPredicateNode {
39+
self.0
40+
}
41+
42+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
43+
if !matches!(pred_node.typ, PredicateType::InList) {
44+
return None;
45+
}
46+
Some(Self(pred_node))
47+
}
48+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::sync::Arc;
2+
3+
use crate::common::{
4+
nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode},
5+
values::Value,
6+
};
7+
8+
#[derive(Clone, Debug)]
9+
pub struct LikePred(pub ArcPredicateNode);
10+
11+
impl LikePred {
12+
pub fn new(
13+
negated: bool,
14+
case_insensitive: bool,
15+
child: ArcPredicateNode,
16+
pattern: ArcPredicateNode,
17+
) -> Self {
18+
// TODO: support multiple values in data.
19+
let negated = if negated { 1 } else { 0 };
20+
let case_insensitive = if case_insensitive { 1 } else { 0 };
21+
LikePred(
22+
PredicateNode {
23+
typ: PredicateType::Like,
24+
children: vec![child.into_pred_node(), pattern.into_pred_node()],
25+
data: Some(Value::Serialized(Arc::new([negated, case_insensitive]))),
26+
}
27+
.into(),
28+
)
29+
}
30+
31+
pub fn child(&self) -> ArcPredicateNode {
32+
self.0.child(0)
33+
}
34+
35+
pub fn pattern(&self) -> ArcPredicateNode {
36+
self.0.child(1)
37+
}
38+
39+
/// `true` for `NOT LIKE`.
40+
pub fn negated(&self) -> bool {
41+
match self.0.data.as_ref().unwrap() {
42+
Value::Serialized(data) => data[0] != 0,
43+
_ => panic!("not a serialized value"),
44+
}
45+
}
46+
47+
pub fn case_insensitive(&self) -> bool {
48+
match self.0.data.as_ref().unwrap() {
49+
Value::Serialized(data) => data[1] != 0,
50+
_ => panic!("not a serialized value"),
51+
}
52+
}
53+
}
54+
55+
impl ReprPredicateNode for LikePred {
56+
fn into_pred_node(self) -> ArcPredicateNode {
57+
self.0
58+
}
59+
60+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
61+
if !matches!(pred_node.typ, PredicateType::Like) {
62+
return None;
63+
}
64+
Some(Self(pred_node))
65+
}
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::common::nodes::{ArcPredicateNode, PredicateNode, PredicateType, ReprPredicateNode};
2+
3+
#[derive(Clone, Debug)]
4+
pub struct ListPred(pub ArcPredicateNode);
5+
6+
impl ListPred {
7+
pub fn new(preds: Vec<ArcPredicateNode>) -> Self {
8+
ListPred(
9+
PredicateNode {
10+
typ: PredicateType::List,
11+
children: preds,
12+
data: None,
13+
}
14+
.into(),
15+
)
16+
}
17+
18+
/// Gets number of expressions in the list
19+
pub fn len(&self) -> usize {
20+
self.0.children.len()
21+
}
22+
23+
pub fn is_empty(&self) -> bool {
24+
self.0.children.is_empty()
25+
}
26+
27+
pub fn child(&self, idx: usize) -> ArcPredicateNode {
28+
self.0.child(idx)
29+
}
30+
31+
pub fn to_vec(&self) -> Vec<ArcPredicateNode> {
32+
self.0.children.clone()
33+
}
34+
}
35+
36+
impl ReprPredicateNode for ListPred {
37+
fn into_pred_node(self) -> ArcPredicateNode {
38+
self.0
39+
}
40+
41+
fn from_pred_node(pred_node: ArcPredicateNode) -> Option<Self> {
42+
if pred_node.typ != PredicateType::List {
43+
return None;
44+
}
45+
Some(Self(pred_node))
46+
}
47+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ pub mod cast_pred;
44
pub mod constant_pred;
55
pub mod data_type_pred;
66
pub mod func_pred;
7+
pub mod in_list_pred;
8+
pub mod like_pred;
9+
pub mod list_pred;
710
pub mod log_op_pred;
811
pub mod sort_order_pred;
912
pub mod un_op_pred;

0 commit comments

Comments
 (0)