-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[analytics-engine] Per-shard bucket oversampling for TopK aggregation queries #21775
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
abd7312
feat(analytics-engine): Add index setting and Sort.perPartition for TopK
sandeshkr419 4a142b5
feat(analytics-engine): Add reduce_eval scalar UDF for opaque aggrega…
sandeshkr419 aac4142
feat(analytics-engine): Wire reduce_eval in Java (SqlFunction + YAML …
sandeshkr419 d54d271
feat(analytics-engine): Add OpenSearchTopKRewriter (post-CBO Sort ins…
sandeshkr419 e770031
test(analytics-engine): Add ShardBucketOversamplingIT
sandeshkr419 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
sandbox/plugins/analytics-backend-datafusion/rust/src/udf/reduce_eval.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| //! `reduce_eval(agg_name, state)` — evaluates an opaque aggregate's partial state | ||
| //! to produce a sortable scalar. Used by the TopK rewriter's reduce Project. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use datafusion::arrow::array::{Array, ArrayRef, BinaryArray, UInt64Array}; | ||
| use datafusion::arrow::datatypes::{DataType, Field}; | ||
| use datafusion::common::{DataFusionError, Result, ScalarValue}; | ||
| use datafusion::execution::context::SessionContext; | ||
| use datafusion::functions_aggregate::approx_distinct::approx_distinct_udaf; | ||
| use datafusion::logical_expr::function::AccumulatorArgs; | ||
| use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, Signature, Volatility}; | ||
| use datafusion::physical_expr::expressions::Column; | ||
|
|
||
| pub fn register_all(ctx: &SessionContext) { | ||
| ctx.register_udf(ScalarUDF::new_from_impl(ReduceEvalUdf)); | ||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| struct ReduceEvalUdf; | ||
|
|
||
| impl ScalarUDFImpl for ReduceEvalUdf { | ||
| fn as_any(&self) -> &dyn std::any::Any { self } | ||
| fn name(&self) -> &str { "reduce_eval" } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| static SIG: std::sync::LazyLock<Signature> = std::sync::LazyLock::new(|| { | ||
| Signature::any(2, Volatility::Immutable) | ||
| }); | ||
| &SIG | ||
| } | ||
|
|
||
| fn return_type(&self, _args: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::UInt64) | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let agg_name = match &args.args[0] { | ||
| ColumnarValue::Scalar(ScalarValue::Utf8(Some(s))) => s.clone(), | ||
| _ => return Err(DataFusionError::Execution("reduce_eval: first arg must be literal agg name".into())), | ||
| }; | ||
| let state_col = match &args.args[1] { | ||
| ColumnarValue::Array(a) => a.clone(), | ||
| ColumnarValue::Scalar(s) => s.to_array_of_size(args.number_rows)?, | ||
| }; | ||
|
|
||
| match agg_name.as_str() { | ||
| "approx_distinct" => eval_approx_distinct(&state_col), | ||
| other => Err(DataFusionError::Execution(format!("reduce_eval: unsupported aggregate '{other}'"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn eval_approx_distinct(state_col: &ArrayRef) -> Result<ColumnarValue> { | ||
| let binary = state_col.as_any().downcast_ref::<BinaryArray>() | ||
| .ok_or_else(|| DataFusionError::Execution("reduce_eval(approx_distinct): expected Binary state".into()))?; | ||
|
|
||
| let field: Arc<Field> = Arc::new(Field::new("x", DataType::Int64, true)); | ||
| let schema = Arc::new(datafusion::arrow::datatypes::Schema::new(vec![field.as_ref().clone()])); | ||
| let expr: Arc<dyn datafusion::physical_plan::PhysicalExpr> = Arc::new(Column::new("x", 0)); | ||
| let ret_field: Arc<Field> = Arc::new(Field::new("r", DataType::UInt64, true)); | ||
|
|
||
| let mut results = Vec::with_capacity(binary.len()); | ||
| for i in 0..binary.len() { | ||
| if binary.is_null(i) { | ||
| results.push(0u64); | ||
| continue; | ||
| } | ||
| let mut acc = approx_distinct_udaf().accumulator(AccumulatorArgs { | ||
| return_field: ret_field.clone(), | ||
| schema: &schema, | ||
| ignore_nulls: false, | ||
| order_bys: &[], | ||
| name: "x", | ||
| is_distinct: false, | ||
| exprs: &[expr.clone()], | ||
| expr_fields: &[field.clone()], | ||
| is_reversed: false, | ||
| })?; | ||
| let state_array: ArrayRef = Arc::new(BinaryArray::from(vec![binary.value(i)])); | ||
| acc.merge_batch(&[state_array])?; | ||
| match acc.evaluate()? { | ||
| ScalarValue::UInt64(Some(v)) => results.push(v), | ||
| _ => results.push(0), | ||
| } | ||
| } | ||
| Ok(ColumnarValue::Array(Arc::new(UInt64Array::from(results)))) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use datafusion::arrow::array::BinaryArray; | ||
| use datafusion::functions_aggregate::approx_distinct::approx_distinct_udaf; | ||
| use datafusion::logical_expr::Accumulator; | ||
|
|
||
| #[test] | ||
| fn test_reduce_eval_approx_distinct() { | ||
| // Build an HLL state by updating an accumulator | ||
| let field: Arc<Field> = Arc::new(Field::new("x", DataType::Int64, true)); | ||
| let schema = Arc::new(datafusion::arrow::datatypes::Schema::new(vec![field.as_ref().clone()])); | ||
| let expr: Arc<dyn datafusion::physical_plan::PhysicalExpr> = Arc::new(Column::new("x", 0)); | ||
| let ret_field: Arc<Field> = Arc::new(Field::new("r", DataType::UInt64, true)); | ||
| let mut acc = approx_distinct_udaf().accumulator(AccumulatorArgs { | ||
| return_field: ret_field.clone(), schema: &schema, ignore_nulls: false, | ||
| order_bys: &[], name: "x", is_distinct: false, | ||
| exprs: &[expr.clone()], expr_fields: &[field.clone()], is_reversed: false, | ||
| }).unwrap(); | ||
|
|
||
| // Feed some values | ||
| let values: ArrayRef = Arc::new(datafusion::arrow::array::Int64Array::from(vec![1, 2, 3, 4, 5])); | ||
| acc.update_batch(&[values]).unwrap(); | ||
| let state = acc.state().unwrap(); | ||
|
|
||
| // Extract the Binary state | ||
| let state_array = state[0].to_array_of_size(1).unwrap(); | ||
| let binary = state_array.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
|
|
||
| // Run reduce_eval | ||
| let result = eval_approx_distinct(&(Arc::new(binary.clone()) as ArrayRef)).unwrap(); | ||
| match result { | ||
| ColumnarValue::Array(arr) => { | ||
| let uint_arr = arr.as_any().downcast_ref::<UInt64Array>().unwrap(); | ||
| assert_eq!(uint_arr.value(0), 5); // 5 distinct values | ||
| } | ||
| _ => panic!("expected Array"), | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.