datafusion_functions/math/
pi.rsuse std::any::Any;
use std::sync::OnceLock;
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::Float64;
use datafusion_common::{not_impl_err, Result, ScalarValue};
use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
use datafusion_expr::sort_properties::{ExprProperties, SortProperties};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
#[derive(Debug)]
pub struct PiFunc {
signature: Signature,
}
impl Default for PiFunc {
fn default() -> Self {
PiFunc::new()
}
}
impl PiFunc {
pub fn new() -> Self {
Self {
signature: Signature::exact(vec![], Volatility::Immutable),
}
}
}
impl ScalarUDFImpl for PiFunc {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"pi"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(Float64)
}
fn invoke(&self, _args: &[ColumnarValue]) -> Result<ColumnarValue> {
not_impl_err!("{} function does not accept arguments", self.name())
}
fn invoke_no_args(&self, _number_rows: usize) -> Result<ColumnarValue> {
Ok(ColumnarValue::Scalar(ScalarValue::Float64(Some(
std::f64::consts::PI,
))))
}
fn output_ordering(&self, _input: &[ExprProperties]) -> Result<SortProperties> {
Ok(SortProperties::Singleton)
}
fn documentation(&self) -> Option<&Documentation> {
Some(get_pi_doc())
}
}
static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
fn get_pi_doc() -> &'static Documentation {
DOCUMENTATION.get_or_init(|| {
Documentation::builder()
.with_doc_section(DOC_SECTION_MATH)
.with_description("Returns an approximate value of π.")
.with_syntax_example("pi()")
.build()
.unwrap()
})
}