MCPcopy Create free account
hub / github.com/apache/datafusion / simplify

Method simplify

datafusion/functions/src/math/power.rs:568–604  ·  view source on GitHub ↗

Simplify the `power` function by the relevant rules: 1. Power(a, 0) ===> 1 2. Power(a, 1) ===> a 3. Power(a, Log(a, b)) ===> b

(
        &self,
        args: Vec<Expr>,
        info: &SimplifyContext,
    )

Source from the content-addressed store, hash-verified

566 /// 2. Power(a, 1) ===> a
567 /// 3. Power(a, Log(a, b)) ===> b
568 fn simplify(
569 &self,
570 args: Vec<Expr>,
571 info: &SimplifyContext,
572 ) -> Result<ExprSimplifyResult> {
573 let [base, exponent] = take_function_args("power", args)?;
574 let base_type = info.get_data_type(&base)?;
575 let exponent_type = info.get_data_type(&exponent)?;
576
577 // Null propagation
578 if base_type.is_null() || exponent_type.is_null() {
579 let return_type = self.return_type(&[base_type, exponent_type])?;
580 return Ok(ExprSimplifyResult::Simplified(lit(
581 ScalarValue::Null.cast_to(&return_type)?
582 )));
583 }
584
585 match exponent {
586 Expr::Literal(value, _)
587 if value == ScalarValue::new_zero(&exponent_type)? =>
588 {
589 Ok(ExprSimplifyResult::Simplified(lit(ScalarValue::new_one(
590 &base_type,
591 )?)))
592 }
593 Expr::Literal(value, _) if value == ScalarValue::new_one(&exponent_type)? => {
594 Ok(ExprSimplifyResult::Simplified(base))
595 }
596 Expr::ScalarFunction(ScalarFunction { func, mut args })
597 if is_log(&func) && args.len() == 2 && base == args[0] =>
598 {
599 let b = args.pop().unwrap(); // length checked above
600 Ok(ExprSimplifyResult::Simplified(b))
601 }
602 _ => Ok(ExprSimplifyResult::Original(vec![base, exponent])),
603 }
604 }
605
606 fn documentation(&self) -> Option<&Documentation> {
607 self.doc()

Callers

nothing calls this directly

Calls 9

take_function_argsFunction · 0.85
is_logFunction · 0.85
get_data_typeMethod · 0.80
litFunction · 0.50
is_nullMethod · 0.45
return_typeMethod · 0.45
cast_toMethod · 0.45
lenMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected