From 0bac3912918b01aca02f7441a0298e3b4c601f47 Mon Sep 17 00:00:00 2001 From: trinity-1686a Date: Thu, 28 Nov 2024 19:39:13 +0100 Subject: [PATCH 1/2] add support for counting non integer in aggregation --- src/aggregation/agg_req_with_accessor.rs | 22 ++++++++++++++++++---- src/aggregation/metric/stats.rs | 20 +++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/aggregation/agg_req_with_accessor.rs b/src/aggregation/agg_req_with_accessor.rs index 986d2e1d09..5b5bfb6d77 100644 --- a/src/aggregation/agg_req_with_accessor.rs +++ b/src/aggregation/agg_req_with_accessor.rs @@ -271,10 +271,6 @@ impl AggregationWithAccessor { field: ref field_name, .. }) - | Count(CountAggregation { - field: ref field_name, - .. - }) | Max(MaxAggregation { field: ref field_name, .. @@ -299,6 +295,24 @@ impl AggregationWithAccessor { get_ff_reader(reader, field_name, Some(get_numeric_or_date_column_types()))?; add_agg_with_accessor(&agg, accessor, column_type, &mut res)?; } + Count(CountAggregation { + field: ref field_name, + .. + }) => { + let allowed_column_types = [ + ColumnType::I64, + ColumnType::U64, + ColumnType::F64, + ColumnType::Str, + ColumnType::DateTime, + ColumnType::Bool, + ColumnType::IpAddr, + // ColumnType::Bytes Unsupported + ]; + let (accessor, column_type) = + get_ff_reader(reader, field_name, Some(&allowed_column_types))?; + add_agg_with_accessor(&agg, accessor, column_type, &mut res)?; + } Percentiles(ref percentiles) => { let (accessor, column_type) = get_ff_reader( reader, diff --git a/src/aggregation/metric/stats.rs b/src/aggregation/metric/stats.rs index 8b8be39998..8c6e3b036c 100644 --- a/src/aggregation/metric/stats.rs +++ b/src/aggregation/metric/stats.rs @@ -220,9 +220,23 @@ impl SegmentStatsCollector { .column_block_accessor .fetch_block(docs, &agg_accessor.accessor); } - for val in agg_accessor.column_block_accessor.iter_vals() { - let val1 = f64_from_fastfield_u64(val, &self.field_type); - self.stats.collect(val1); + if [ + ColumnType::I64, + ColumnType::U64, + ColumnType::F64, + ColumnType::DateTime, + ] + .contains(&self.field_type) + { + for val in agg_accessor.column_block_accessor.iter_vals() { + let val1 = f64_from_fastfield_u64(val, &self.field_type); + self.stats.collect(val1); + } + } else { + for _val in agg_accessor.column_block_accessor.iter_vals() { + // we ignore the value and simply record that we got something + self.stats.collect(0.0); + } } } } From 32b6e9711b12a05bdff77fb174374ba909f586a6 Mon Sep 17 00:00:00 2001 From: trinity Pointard Date: Fri, 13 Dec 2024 16:06:24 +0100 Subject: [PATCH 2/2] add tests --- src/aggregation/metric/stats.rs | 12 ++++++++++++ src/aggregation/mod.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/aggregation/metric/stats.rs b/src/aggregation/metric/stats.rs index 8c6e3b036c..99d2ddceb4 100644 --- a/src/aggregation/metric/stats.rs +++ b/src/aggregation/metric/stats.rs @@ -449,6 +449,11 @@ mod tests { "field": "score", }, }, + "count_str": { + "value_count": { + "field": "text", + }, + }, "range": range_agg })) .unwrap(); @@ -514,6 +519,13 @@ mod tests { }) ); + assert_eq!( + res["count_str"], + json!({ + "value": 7.0, + }) + ); + Ok(()) } diff --git a/src/aggregation/mod.rs b/src/aggregation/mod.rs index 7b1f21b296..4bfe9a6d24 100644 --- a/src/aggregation/mod.rs +++ b/src/aggregation/mod.rs @@ -578,7 +578,7 @@ mod tests { .set_indexing_options( TextFieldIndexing::default().set_index_option(IndexRecordOption::WithFreqs), ) - .set_fast(None) + .set_fast(Some("raw")) .set_stored(); let text_field = schema_builder.add_text_field("text", text_fieldtype); let date_field = schema_builder.add_date_field("date", FAST);