Skip to content

Commit ce6051f

Browse files
committed
move to private utils
1 parent 24469a7 commit ce6051f

File tree

4 files changed

+9
-64
lines changed

4 files changed

+9
-64
lines changed

datafusion/functions-aggregate-common/src/utils.rs

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use arrow::array::{ArrayRef, ArrowNativeTypeOp, RecordBatch};
18+
use arrow::array::{ArrayRef, ArrowNativeTypeOp};
1919
use arrow::compute::SortOptions;
2020
use arrow::datatypes::{
21-
ArrowNativeType, DataType, DecimalType, Field, FieldRef, Schema, ToByteSlice,
22-
};
23-
use datafusion_common::{
24-
exec_err, internal_datafusion_err, internal_err, plan_err, DataFusionError, Result,
25-
ScalarValue,
21+
ArrowNativeType, DataType, DecimalType, Field, FieldRef, ToByteSlice,
2622
};
23+
use datafusion_common::{exec_err, internal_datafusion_err, Result};
2724
use datafusion_expr_common::accumulator::Accumulator;
28-
use datafusion_expr_common::columnar_value::ColumnarValue;
29-
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
3025
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
3126
use std::sync::Arc;
3227

@@ -166,55 +161,3 @@ impl<T: DecimalType> DecimalAverager<T> {
166161
}
167162
}
168163
}
169-
170-
/// Evaluates a physical expression to extract its scalar value.
171-
///
172-
/// This is used to extract constant values from expressions (like percentile parameters)
173-
/// by evaluating them against an empty record batch.
174-
pub fn get_percentile_scalar_value(expr: &Arc<dyn PhysicalExpr>) -> Result<ScalarValue> {
175-
let empty_schema = Arc::new(Schema::empty());
176-
let batch = RecordBatch::new_empty(Arc::clone(&empty_schema));
177-
if let ColumnarValue::Scalar(s) = expr.evaluate(&batch)? {
178-
Ok(s)
179-
} else {
180-
internal_err!("Didn't expect ColumnarValue::Array")
181-
}
182-
}
183-
184-
/// Validates that a percentile expression is a literal float value between 0.0 and 1.0.
185-
///
186-
/// Used by both `percentile_cont` and `approx_percentile_cont` to validate their
187-
/// percentile parameters.
188-
pub fn validate_percentile_expr(
189-
expr: &Arc<dyn PhysicalExpr>,
190-
fn_name: &str,
191-
) -> Result<f64> {
192-
let scalar_value = get_percentile_scalar_value(expr).map_err(|_e| {
193-
DataFusionError::Plan(format!(
194-
"Percentile value for '{fn_name}' must be a literal"
195-
))
196-
})?;
197-
198-
let percentile = match scalar_value {
199-
ScalarValue::Float32(Some(value)) => {
200-
value as f64
201-
}
202-
ScalarValue::Float64(Some(value)) => {
203-
value
204-
}
205-
sv => {
206-
return plan_err!(
207-
"Percentile value for '{fn_name}' must be Float32 or Float64 literal (got data type {})",
208-
sv.data_type()
209-
)
210-
}
211-
};
212-
213-
// Ensure the percentile is between 0 and 1.
214-
if !(0.0..=1.0).contains(&percentile) {
215-
return plan_err!(
216-
"Percentile value must be between 0.0 and 1.0 inclusive, {percentile} is invalid"
217-
);
218-
}
219-
Ok(percentile)
220-
}

datafusion/functions-aggregate/src/approx_percentile_cont.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ use datafusion_expr::{
4545
use datafusion_functions_aggregate_common::tdigest::{
4646
TDigest, TryIntoF64, DEFAULT_MAX_SIZE,
4747
};
48-
use datafusion_functions_aggregate_common::utils::{
49-
get_percentile_scalar_value, validate_percentile_expr,
50-
};
5148
use datafusion_macros::user_doc;
5249
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
5350

51+
use crate::utils::{get_percentile_scalar_value, validate_percentile_expr};
52+
5453
create_func!(ApproxPercentileCont, approx_percentile_cont_udaf);
5554

5655
/// Computes the approximate percentile continuous of a set of numbers

datafusion/functions-aggregate/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub mod sum;
8989
pub mod variance;
9090

9191
pub mod planner;
92+
mod utils;
9293

9394
use crate::approx_percentile_cont::approx_percentile_cont_udaf;
9495
use crate::approx_percentile_cont_with_weight::approx_percentile_cont_with_weight_udaf;

datafusion/functions-aggregate/src/percentile_cont.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ use datafusion_expr::{
4848
use datafusion_expr::{EmitTo, GroupsAccumulator};
4949
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::accumulate::accumulate;
5050
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls::filtered_null_mask;
51-
use datafusion_functions_aggregate_common::utils::{validate_percentile_expr, Hashable};
51+
use datafusion_functions_aggregate_common::utils::Hashable;
5252
use datafusion_macros::user_doc;
5353

54+
use crate::utils::validate_percentile_expr;
55+
5456
/// Precision multiplier for linear interpolation calculations.
5557
///
5658
/// This value of 1,000,000 was chosen to balance precision with overflow safety:

0 commit comments

Comments
 (0)