Skip to content

Commit a8f92c3

Browse files
committed
Finish all tests for filter
1 parent ebab829 commit a8f92c3

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

optd-cost-model/src/cost/filter/in_list.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,88 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
6565
}
6666
}
6767
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use std::collections::HashMap;
72+
73+
use crate::{
74+
common::{types::TableId, values::Value},
75+
cost_model::tests::*,
76+
stats::{utilities::counter::Counter, MostCommonValues},
77+
};
78+
79+
#[tokio::test]
80+
async fn test_in_list() {
81+
let mut mcvs_counts = HashMap::new();
82+
mcvs_counts.insert(vec![Some(Value::Int32(1))], 8);
83+
mcvs_counts.insert(vec![Some(Value::Int32(2))], 2);
84+
let mcvs_total_count = 10;
85+
let per_attribute_stats = TestPerAttributeStats::new(
86+
MostCommonValues::Counter(Counter::new_from_existing(mcvs_counts, mcvs_total_count)),
87+
2,
88+
0.0,
89+
None,
90+
);
91+
let table_id = TableId(0);
92+
let cost_model = create_cost_model_mock_storage(
93+
vec![table_id],
94+
vec![per_attribute_stats],
95+
vec![None],
96+
HashMap::new(),
97+
);
98+
99+
assert_approx_eq::assert_approx_eq!(
100+
cost_model
101+
.get_in_list_selectivity(&in_list(table_id, 0, vec![Value::Int32(1)], false))
102+
.await
103+
.unwrap(),
104+
0.8
105+
);
106+
assert_approx_eq::assert_approx_eq!(
107+
cost_model
108+
.get_in_list_selectivity(&in_list(
109+
table_id,
110+
0,
111+
vec![Value::Int32(1), Value::Int32(2)],
112+
false
113+
))
114+
.await
115+
.unwrap(),
116+
1.0
117+
);
118+
assert_approx_eq::assert_approx_eq!(
119+
cost_model
120+
.get_in_list_selectivity(&in_list(table_id, 0, vec![Value::Int32(3)], false))
121+
.await
122+
.unwrap(),
123+
0.0
124+
);
125+
assert_approx_eq::assert_approx_eq!(
126+
cost_model
127+
.get_in_list_selectivity(&in_list(table_id, 0, vec![Value::Int32(1)], true))
128+
.await
129+
.unwrap(),
130+
0.2
131+
);
132+
assert_approx_eq::assert_approx_eq!(
133+
cost_model
134+
.get_in_list_selectivity(&in_list(
135+
table_id,
136+
0,
137+
vec![Value::Int32(1), Value::Int32(2)],
138+
true
139+
))
140+
.await
141+
.unwrap(),
142+
0.0
143+
);
144+
assert_approx_eq::assert_approx_eq!(
145+
cost_model
146+
.get_in_list_selectivity(&in_list(table_id, 0, vec![Value::Int32(3)], true))
147+
.await
148+
.unwrap(),
149+
1.0
150+
);
151+
}
152+
}

optd-cost-model/src/cost/filter/like.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,97 @@ impl<S: CostModelStorageManager> CostModelImpl<S> {
9999
}
100100
}
101101
}
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use std::collections::HashMap;
106+
107+
use crate::{
108+
common::{types::TableId, values::Value},
109+
cost_model::tests::*,
110+
stats::{
111+
utilities::counter::Counter, MostCommonValues, FIXED_CHAR_SEL_FACTOR,
112+
FULL_WILDCARD_SEL_FACTOR,
113+
},
114+
};
115+
116+
#[tokio::test]
117+
async fn test_like_no_nulls() {
118+
let mut mcvs_counts = HashMap::new();
119+
mcvs_counts.insert(vec![Some(Value::String("abcd".into()))], 1);
120+
mcvs_counts.insert(vec![Some(Value::String("abc".into()))], 1);
121+
let mcvs_total_count = 10;
122+
let per_attribute_stats = TestPerAttributeStats::new(
123+
MostCommonValues::Counter(Counter::new_from_existing(mcvs_counts, mcvs_total_count)),
124+
2,
125+
0.0,
126+
None,
127+
);
128+
let table_id = TableId(0);
129+
let cost_model = create_cost_model_mock_storage(
130+
vec![table_id],
131+
vec![per_attribute_stats],
132+
vec![None],
133+
HashMap::new(),
134+
);
135+
136+
assert_approx_eq::assert_approx_eq!(
137+
cost_model
138+
.get_like_selectivity(&like(table_id, 0, "%abcd%", false))
139+
.await
140+
.unwrap(),
141+
0.1 + FULL_WILDCARD_SEL_FACTOR.powi(2) * FIXED_CHAR_SEL_FACTOR.powi(4)
142+
);
143+
assert_approx_eq::assert_approx_eq!(
144+
cost_model
145+
.get_like_selectivity(&like(table_id, 0, "%abc%", false))
146+
.await
147+
.unwrap(),
148+
0.1 + 0.1 + FULL_WILDCARD_SEL_FACTOR.powi(2) * FIXED_CHAR_SEL_FACTOR.powi(3)
149+
);
150+
assert_approx_eq::assert_approx_eq!(
151+
cost_model
152+
.get_like_selectivity(&like(table_id, 0, "%abc%", true))
153+
.await
154+
.unwrap(),
155+
1.0 - (0.1 + 0.1 + FULL_WILDCARD_SEL_FACTOR.powi(2) * FIXED_CHAR_SEL_FACTOR.powi(3))
156+
);
157+
}
158+
159+
#[tokio::test]
160+
async fn test_like_with_nulls() {
161+
let null_frac = 0.5;
162+
let mut mcvs_counts = HashMap::new();
163+
mcvs_counts.insert(vec![Some(Value::String("abcd".into()))], 1);
164+
let mcvs_total_count = 10;
165+
let per_attribute_stats = TestPerAttributeStats::new(
166+
MostCommonValues::Counter(Counter::new_from_existing(mcvs_counts, mcvs_total_count)),
167+
2,
168+
null_frac,
169+
None,
170+
);
171+
let table_id = TableId(0);
172+
let cost_model = create_cost_model_mock_storage(
173+
vec![table_id],
174+
vec![per_attribute_stats],
175+
vec![None],
176+
HashMap::new(),
177+
);
178+
179+
assert_approx_eq::assert_approx_eq!(
180+
cost_model
181+
.get_like_selectivity(&like(table_id, 0, "%abcd%", false))
182+
.await
183+
.unwrap(),
184+
0.1 + FULL_WILDCARD_SEL_FACTOR.powi(2) * FIXED_CHAR_SEL_FACTOR.powi(4)
185+
);
186+
assert_approx_eq::assert_approx_eq!(
187+
cost_model
188+
.get_like_selectivity(&like(table_id, 0, "%abcd%", true))
189+
.await
190+
.unwrap(),
191+
1.0 - (0.1 + FULL_WILDCARD_SEL_FACTOR.powi(2) * FIXED_CHAR_SEL_FACTOR.powi(4))
192+
- null_frac
193+
);
194+
}
195+
}

0 commit comments

Comments
 (0)