Skip to content

Commit c933b7e

Browse files
committed
x
1 parent a0c4989 commit c933b7e

3 files changed

Lines changed: 49 additions & 34 deletions

File tree

src/query/sql/test-support/src/optimizer/mod.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ pub struct HistogramStats {
112112
pub avg_spacing: Option<f64>,
113113
}
114114

115+
impl HistogramStats {
116+
pub fn to_histogram(&self) -> Result<Histogram> {
117+
let buckets = self
118+
.buckets
119+
.iter()
120+
.map(|bucket| {
121+
HistogramBucket::try_from_bounds(
122+
bucket.lower_bound.clone(),
123+
bucket.upper_bound.clone(),
124+
bucket.num_values,
125+
bucket.num_distinct,
126+
)
127+
.map_err(|err| ErrorCode::Internal(format!("invalid histogram bucket: {err}")))
128+
})
129+
.collect::<Result<Vec<_>>>()?;
130+
131+
Histogram::try_from_buckets(self.accuracy, buckets, self.avg_spacing)
132+
.map_err(|err| ErrorCode::Internal(format!("invalid histogram: {err}")))
133+
}
134+
}
135+
115136
#[derive(Debug, Serialize, Deserialize, Clone)]
116137
pub struct HistogramBucketStats {
117138
pub lower_bound: Datum,
@@ -556,7 +577,7 @@ impl StatsApplier<'_> {
556577
{
557578
let full_name = format!("{table_name}.{column_name}");
558579
if let Some(stats) = self.histogram_stats.get(&full_name) {
559-
result.insert(*column_index, Some(histogram_from_stats(stats)?));
580+
result.insert(*column_index, Some(stats.to_histogram()?));
560581
}
561582
}
562583
}
@@ -565,25 +586,6 @@ impl StatsApplier<'_> {
565586
}
566587
}
567588

568-
pub(crate) fn histogram_from_stats(stats: &HistogramStats) -> Result<Histogram> {
569-
let buckets = stats
570-
.buckets
571-
.iter()
572-
.map(|bucket| {
573-
HistogramBucket::try_from_bounds(
574-
bucket.lower_bound.clone(),
575-
bucket.upper_bound.clone(),
576-
bucket.num_values,
577-
bucket.num_distinct,
578-
)
579-
.map_err(|err| ErrorCode::Internal(format!("invalid histogram bucket: {err}")))
580-
})
581-
.collect::<Result<Vec<_>>>()?;
582-
583-
Histogram::try_from_buckets(stats.accuracy, buckets, stats.avg_spacing)
584-
.map_err(|err| ErrorCode::Internal(format!("invalid histogram: {err}")))
585-
}
586-
587589
fn write_result<F>(mint: &mut Mint, name: &str, f: F) -> Result<()>
588590
where F: FnOnce(&mut dyn Write) -> Result<()> {
589591
let mut file = mint.new_goldenfile(name).unwrap();

src/query/sql/test-support/src/optimizer/replay.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use databend_common_statistics::Histogram;
2626
use serde::Deserialize;
2727

2828
use super::HistogramStats;
29-
use super::histogram_from_stats;
3029

3130
#[derive(Debug, Clone, Deserialize)]
3231
pub struct ReplayInput {
@@ -159,7 +158,8 @@ impl ReplayInput {
159158
return None;
160159
}
161160
column.histogram.as_ref().map(|histogram| {
162-
histogram_from_stats(histogram)
161+
histogram
162+
.to_histogram()
163163
.map(|histogram| (column.column_name.clone(), histogram))
164164
})
165165
})

src/query/sql/tests/it/optimizer/union_all.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ use databend_common_sql::plans::Operator;
3030
use databend_common_sql::plans::Plan;
3131
use databend_common_sql::plans::RelOperator;
3232
use databend_common_sql_test_support::ColumnStats;
33-
use databend_common_statistics::Datum;
33+
use databend_common_sql_test_support::HistogramStats;
3434
use databend_common_statistics::Histogram;
35-
use databend_common_statistics::HistogramBuilder;
3635

3736
use crate::framework::LiteTableContext;
3837
use crate::framework::golden::open_golden_file;
@@ -304,6 +303,12 @@ fn column_stat(json: &str) -> Result<BasicColumnStatistics> {
304303
Ok(stats.to_basic_column_statistics())
305304
}
306305

306+
fn histogram_stat(json: &str) -> Result<Histogram> {
307+
let stats: HistogramStats = serde_json::from_str(json)
308+
.map_err(|err| ErrorCode::Internal(format!("invalid histogram statistics JSON: {err}")))?;
309+
stats.to_histogram()
310+
}
311+
307312
fn cases() -> Result<Vec<UnionCase>> {
308313
Ok(vec![
309314
UnionCase {
@@ -408,11 +413,15 @@ fn cases() -> Result<Vec<UnionCase>> {
408413
)]),
409414
histograms: HashMap::from([(
410415
"k".to_string(),
411-
HistogramBuilder::from_ndv(
412-
10,
413-
100,
414-
Some((Datum::Int(0), Datum::Int(9))),
415-
2,
416+
histogram_stat(
417+
r#"{
418+
"accuracy": false,
419+
"buckets": [
420+
{"lower_bound": {"Int": 0}, "upper_bound": {"Int": 4}, "num_values": 50.0, "num_distinct": 5.0},
421+
{"lower_bound": {"Int": 5}, "upper_bound": {"Int": 9}, "num_values": 50.0, "num_distinct": 5.0}
422+
],
423+
"avg_spacing": 4.5
424+
}"#,
416425
)?,
417426
)]),
418427
},
@@ -425,11 +434,15 @@ fn cases() -> Result<Vec<UnionCase>> {
425434
)]),
426435
histograms: HashMap::from([(
427436
"k".to_string(),
428-
HistogramBuilder::from_ndv(
429-
10,
430-
100,
431-
Some((Datum::Int(5), Datum::Int(14))),
432-
2,
437+
histogram_stat(
438+
r#"{
439+
"accuracy": false,
440+
"buckets": [
441+
{"lower_bound": {"Int": 5}, "upper_bound": {"Int": 9}, "num_values": 50.0, "num_distinct": 5.0},
442+
{"lower_bound": {"Int": 10}, "upper_bound": {"Int": 14}, "num_values": 50.0, "num_distinct": 5.0}
443+
],
444+
"avg_spacing": 4.5
445+
}"#,
433446
)?,
434447
)]),
435448
},

0 commit comments

Comments
 (0)