From f87b12b2aab92adfe682e4ef4876944c09f3e85c Mon Sep 17 00:00:00 2001 From: codephage <39155160+codephage2020@users.noreply.github.com> Date: Thu, 26 Jun 2025 17:27:30 +0800 Subject: [PATCH] feat: remove own pow fn (#6404) feat remove own pow fn Signed-off-by: codephage. <381510760@qq.com> --- src/common/function/src/scalars/math.rs | 3 - src/common/function/src/scalars/math/pow.rs | 120 ------------------ .../common/function/arithmetic.result | 30 ++--- 3 files changed, 15 insertions(+), 138 deletions(-) delete mode 100644 src/common/function/src/scalars/math/pow.rs diff --git a/src/common/function/src/scalars/math.rs b/src/common/function/src/scalars/math.rs index bb55f72e1c..173e2c9332 100644 --- a/src/common/function/src/scalars/math.rs +++ b/src/common/function/src/scalars/math.rs @@ -14,7 +14,6 @@ pub mod clamp; mod modulo; -mod pow; mod rate; use std::fmt; @@ -26,7 +25,6 @@ use datafusion::error::DataFusionError; use datafusion::logical_expr::Volatility; use datatypes::prelude::ConcreteDataType; use datatypes::vectors::VectorRef; -pub use pow::PowFunction; pub use rate::RateFunction; use snafu::ResultExt; @@ -39,7 +37,6 @@ pub(crate) struct MathFunction; impl MathFunction { pub fn register(registry: &FunctionRegistry) { registry.register_scalar(ModuloFunction); - registry.register_scalar(PowFunction); registry.register_scalar(RateFunction); registry.register_scalar(RangeFunction); registry.register_scalar(ClampFunction); diff --git a/src/common/function/src/scalars/math/pow.rs b/src/common/function/src/scalars/math/pow.rs deleted file mode 100644 index 171c06a694..0000000000 --- a/src/common/function/src/scalars/math/pow.rs +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright 2023 Greptime Team -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::fmt; -use std::sync::Arc; - -use common_query::error::Result; -use common_query::prelude::{Signature, Volatility}; -use datatypes::data_type::DataType; -use datatypes::prelude::ConcreteDataType; -use datatypes::types::LogicalPrimitiveType; -use datatypes::vectors::VectorRef; -use datatypes::with_match_primitive_type_id; -use num::traits::Pow; -use num_traits::AsPrimitive; - -use crate::function::{Function, FunctionContext}; -use crate::scalars::expression::{scalar_binary_op, EvalContext}; - -#[derive(Clone, Debug, Default)] -pub struct PowFunction; - -impl Function for PowFunction { - fn name(&self) -> &str { - "pow" - } - - fn return_type(&self, _input_types: &[ConcreteDataType]) -> Result { - Ok(ConcreteDataType::float64_datatype()) - } - - fn signature(&self) -> Signature { - Signature::uniform(2, ConcreteDataType::numerics(), Volatility::Immutable) - } - - fn eval(&self, _func_ctx: &FunctionContext, columns: &[VectorRef]) -> Result { - with_match_primitive_type_id!(columns[0].data_type().logical_type_id(), |$S| { - with_match_primitive_type_id!(columns[1].data_type().logical_type_id(), |$T| { - let col = scalar_binary_op::<<$S as LogicalPrimitiveType>::Native, <$T as LogicalPrimitiveType>::Native, f64, _>(&columns[0], &columns[1], scalar_pow, &mut EvalContext::default())?; - Ok(Arc::new(col)) - },{ - unreachable!() - }) - },{ - unreachable!() - }) - } -} - -#[inline] -fn scalar_pow(value: Option, base: Option, _ctx: &mut EvalContext) -> Option -where - S: AsPrimitive, - T: AsPrimitive, -{ - match (value, base) { - (Some(value), Some(base)) => Some(value.as_().pow(base.as_())), - _ => None, - } -} - -impl fmt::Display for PowFunction { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "POW") - } -} - -#[cfg(test)] -mod tests { - use common_query::prelude::TypeSignature; - use datatypes::value::Value; - use datatypes::vectors::{Float32Vector, Int8Vector}; - - use super::*; - use crate::function::FunctionContext; - #[test] - fn test_pow_function() { - let pow = PowFunction; - - assert_eq!("pow", pow.name()); - assert_eq!( - ConcreteDataType::float64_datatype(), - pow.return_type(&[]).unwrap() - ); - - assert!(matches!(pow.signature(), - Signature { - type_signature: TypeSignature::Uniform(2, valid_types), - volatility: Volatility::Immutable - } if valid_types == ConcreteDataType::numerics() - )); - - let values = vec![1.0, 2.0, 3.0]; - let bases = vec![0i8, -1i8, 3i8]; - - let args: Vec = vec![ - Arc::new(Float32Vector::from_vec(values.clone())), - Arc::new(Int8Vector::from_vec(bases.clone())), - ]; - - let vector = pow.eval(&FunctionContext::default(), &args).unwrap(); - assert_eq!(3, vector.len()); - - for i in 0..3 { - let p: f64 = (values[i] as f64).pow(bases[i] as f64); - assert!(matches!(vector.get(i), Value::Float64(v) if v == p)); - } - } -} diff --git a/tests/cases/standalone/common/function/arithmetic.result b/tests/cases/standalone/common/function/arithmetic.result index 4dd780c61b..17e668b24d 100644 --- a/tests/cases/standalone/common/function/arithmetic.result +++ b/tests/cases/standalone/common/function/arithmetic.result @@ -28,27 +28,27 @@ Error: 3001(EngineExecuteQuery), Divide by zero error SELECT POW (2, 5); -+------------------------+ -| pow(Int64(2),Int64(5)) | -+------------------------+ -| 32.0 | -+------------------------+ ++--------------------------+ +| power(Int64(2),Int64(5)) | ++--------------------------+ +| 32 | ++--------------------------+ SELECT POW (1.01, 365); -+-------------------------------+ -| pow(Float64(1.01),Int64(365)) | -+-------------------------------+ -| 37.78343433288728 | -+-------------------------------+ ++---------------------------------+ +| power(Float64(1.01),Int64(365)) | ++---------------------------------+ +| 37.78343433288728 | ++---------------------------------+ SELECT POW (0.99, 365); -+-------------------------------+ -| pow(Float64(0.99),Int64(365)) | -+-------------------------------+ -| 0.025517964452291125 | -+-------------------------------+ ++---------------------------------+ +| power(Float64(0.99),Int64(365)) | ++---------------------------------+ +| 0.025517964452291125 | ++---------------------------------+ SELECT CLAMP(10, 0, 1);