Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
xudong963 committed Sep 4, 2024
1 parent e8d85a4 commit d49a135
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/common/exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ serde = { workspace = true }
serde_json = { workspace = true }
tantivy = { workspace = true }
thiserror = { workspace = true }
tokio = "1.39.2"
tonic = { workspace = true }

[package.metadata.cargo-machete]
Expand Down
2 changes: 1 addition & 1 deletion src/query/sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ serde = { workspace = true }
sha2 = { workspace = true }
simsearch = "0.2"
time = "0.3.14"
url = "2.3.1"
tokio = "1.39.2"
url = "2.3.1"

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::time::Duration;

use databend_common_base::base::tokio::time::Instant;
use databend_common_catalog::table_context::TableContext;
use databend_common_exception::ErrorCode;
use databend_common_exception::Result;

use crate::optimizer::dynamic_sample::filter_selectivity_sample::filter_selectivity_sample;
Expand Down Expand Up @@ -80,9 +79,10 @@ pub async fn dynamic_sample(
join_selectivity_sample(ctx, metadata, s_expr, sample_executor).await
}
RelOperator::Scan(_) => s_expr.plan().derive_stats(&RelExpr::with_s_expr(s_expr)),
_ => Err(ErrorCode::Unimplemented(format!(
"derive_cardinality_by_sample for {:?} is not supported yet",
s_expr.plan()
))),
// Todo: add more operators here, and support more query patterns.
_ => {
let rel_expr = RelExpr::with_s_expr(s_expr);
rel_expr.derive_cardinality()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ pub async fn filter_selectivity_sample(

// Calculate sample size (0.2% of total data)
let sample_size = (num_rows as f64 * 0.002).ceil();

scan.sample = Some(Sample {
sample_level: SampleLevel::ROW,
sample_conf: SampleConfig::RowsNum(sample_size),
});

let new_child = SExpr::create_leaf(Arc::new(RelOperator::Scan(scan)));
let mut new_s_expr = s_expr.replace_children(vec![Arc::new(new_child)]);
let collect_statistics_optimizer =
CollectStatisticsOptimizer::new(ctx.clone(), metadata.clone());
new_s_expr = collect_statistics_optimizer.run(&new_s_expr).await?;
let mut new_s_expr = s_expr.clone();
// If the table is too small, we don't need to sample.
if sample_size >= 10.0 {
scan.sample = Some(Sample {
sample_level: SampleLevel::ROW,
sample_conf: SampleConfig::RowsNum(sample_size),
});
let new_child = SExpr::create_leaf(Arc::new(RelOperator::Scan(scan)));
new_s_expr = s_expr.replace_children(vec![Arc::new(new_child)]);
let collect_statistics_optimizer =
CollectStatisticsOptimizer::new(ctx.clone(), metadata.clone());
new_s_expr = collect_statistics_optimizer.run(&new_s_expr).await?;
}

new_s_expr = SExpr::create_unary(
Arc::new(create_count_aggregate(AggregateMode::Partial).into()),
Expand All @@ -91,12 +93,12 @@ pub async fn filter_selectivity_sample(
if let Some(count) = block.get_last_column().as_number() {
if let Some(number_scalar) = count.index(0) {
// Compute and return selectivity
let selectivity = number_scalar.to_f64().to_f64().unwrap() / sample_size as f64;
let selectivity = number_scalar.to_f64().to_f64().unwrap() / sample_size;
let mut statistics = child_rel_expr.derive_cardinality()?.statistics.clone();
let mut sb = SelectivityEstimator::new(&mut statistics, HashSet::new());
sb.update_other_statistic_by_selectivity(selectivity);
let stat_info = Arc::new(StatInfo {
cardinality: selectivity * num_rows as f64,
cardinality: (selectivity * num_rows as f64).ceil(),
statistics,
});
*s_expr.stat_info.lock().unwrap() = Some(stat_info.clone());
Expand Down
1 change: 1 addition & 0 deletions src/query/sql/src/planner/optimizer/dynamic_sample/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#[allow(clippy::module_inception)]
mod dynamic_sample;
mod filter_selectivity_sample;
mod join_selectivity_sample;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SingleToInnerOptimizer {
let mut children = Vec::with_capacity(s_expr.arity());
for child in s_expr.children() {
let new_child = Self::single_to_inner(child)?;
if !new_child.eq(&child) {
if !new_child.eq(child) {
children_changed = true;
}
children.push(Arc::new(new_child));
Expand Down
Loading

0 comments on commit d49a135

Please sign in to comment.