From d2617d7c1c5e57e2783152ebd97dfeba7ea4f8e4 Mon Sep 17 00:00:00 2001 From: jonahgao Date: Sat, 7 Dec 2024 23:35:13 +0800 Subject: [PATCH 1/4] refactor: use `LazyLock` in the `user_doc` macro --- .../src/approx_distinct.rs | 2 - .../functions-aggregate/src/approx_median.rs | 2 - .../src/approx_percentile_cont.rs | 3 +- .../src/approx_percentile_cont_with_weight.rs | 3 +- .../functions-aggregate/src/array_agg.rs | 3 +- datafusion/functions-aggregate/src/average.rs | 3 +- .../functions-aggregate/src/bool_and_or.rs | 2 - .../functions-aggregate/src/correlation.rs | 3 +- datafusion/functions-aggregate/src/count.rs | 3 +- .../functions-aggregate/src/covariance.rs | 2 - .../functions-aggregate/src/first_last.rs | 3 +- .../functions-aggregate/src/grouping.rs | 2 - datafusion/functions-aggregate/src/median.rs | 3 +- datafusion/functions-aggregate/src/min_max.rs | 2 - .../functions-aggregate/src/nth_value.rs | 3 +- datafusion/functions-aggregate/src/stddev.rs | 3 +- .../functions-aggregate/src/string_agg.rs | 2 - datafusion/functions-aggregate/src/sum.rs | 2 - .../functions-aggregate/src/variance.rs | 2 - datafusion/functions/src/datetime/to_date.rs | 2 - datafusion/functions/src/math/abs.rs | 3 +- datafusion/functions/src/string/ltrim.rs | 2 - datafusion/macros/src/lib.rs | 105 ++++++++++-------- 23 files changed, 68 insertions(+), 92 deletions(-) diff --git a/datafusion/functions-aggregate/src/approx_distinct.rs b/datafusion/functions-aggregate/src/approx_distinct.rs index 74691ba740fd..1d378fff176f 100644 --- a/datafusion/functions-aggregate/src/approx_distinct.rs +++ b/datafusion/functions-aggregate/src/approx_distinct.rs @@ -31,7 +31,6 @@ use datafusion_common::ScalarValue; use datafusion_common::{ downcast_value, internal_err, not_impl_err, DataFusionError, Result, }; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::format_state_name; use datafusion_expr::{ @@ -42,7 +41,6 @@ use std::any::Any; use std::fmt::{Debug, Formatter}; use std::hash::Hash; use std::marker::PhantomData; -use std::sync::OnceLock; make_udaf_expr_and_func!( ApproxDistinct, diff --git a/datafusion/functions-aggregate/src/approx_median.rs b/datafusion/functions-aggregate/src/approx_median.rs index d4441da61292..5d174a752296 100644 --- a/datafusion/functions-aggregate/src/approx_median.rs +++ b/datafusion/functions-aggregate/src/approx_median.rs @@ -19,13 +19,11 @@ use std::any::Any; use std::fmt::Debug; -use std::sync::OnceLock; use arrow::{datatypes::DataType, datatypes::Field}; use arrow_schema::DataType::{Float64, UInt64}; use datafusion_common::{not_impl_err, plan_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::type_coercion::aggregates::NUMERICS; use datafusion_expr::utils::format_state_name; diff --git a/datafusion/functions-aggregate/src/approx_percentile_cont.rs b/datafusion/functions-aggregate/src/approx_percentile_cont.rs index 13407fecf220..61424e8f2445 100644 --- a/datafusion/functions-aggregate/src/approx_percentile_cont.rs +++ b/datafusion/functions-aggregate/src/approx_percentile_cont.rs @@ -18,7 +18,7 @@ use std::any::Any; use std::fmt::{Debug, Formatter}; use std::mem::size_of_val; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::{Array, RecordBatch}; use arrow::compute::{filter, is_not_null}; @@ -35,7 +35,6 @@ use datafusion_common::{ downcast_value, internal_err, not_impl_datafusion_err, not_impl_err, plan_err, DataFusionError, Result, ScalarValue, }; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::type_coercion::aggregates::{INTEGERS, NUMERICS}; use datafusion_expr::utils::format_state_name; diff --git a/datafusion/functions-aggregate/src/approx_percentile_cont_with_weight.rs b/datafusion/functions-aggregate/src/approx_percentile_cont_with_weight.rs index 485874aeb284..10b9b06f1f94 100644 --- a/datafusion/functions-aggregate/src/approx_percentile_cont_with_weight.rs +++ b/datafusion/functions-aggregate/src/approx_percentile_cont_with_weight.rs @@ -18,7 +18,7 @@ use std::any::Any; use std::fmt::{Debug, Formatter}; use std::mem::size_of_val; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::{ array::ArrayRef, @@ -27,7 +27,6 @@ use arrow::{ use datafusion_common::ScalarValue; use datafusion_common::{not_impl_err, plan_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::type_coercion::aggregates::NUMERICS; use datafusion_expr::Volatility::Immutable; diff --git a/datafusion/functions-aggregate/src/array_agg.rs b/datafusion/functions-aggregate/src/array_agg.rs index 98530a9fc236..b75de83f6ace 100644 --- a/datafusion/functions-aggregate/src/array_agg.rs +++ b/datafusion/functions-aggregate/src/array_agg.rs @@ -25,7 +25,6 @@ use datafusion_common::cast::as_list_array; use datafusion_common::utils::{get_row_at_idx, SingleRowListArrayBuilder}; use datafusion_common::{exec_err, ScalarValue}; use datafusion_common::{internal_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::format_state_name; use datafusion_expr::{Accumulator, Signature, Volatility}; @@ -36,7 +35,7 @@ use datafusion_macros::user_doc; use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr}; use std::collections::{HashSet, VecDeque}; use std::mem::{size_of, size_of_val}; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; make_udaf_expr_and_func!( ArrayAgg, diff --git a/datafusion/functions-aggregate/src/average.rs b/datafusion/functions-aggregate/src/average.rs index 65ca441517a0..18874f831e9d 100644 --- a/datafusion/functions-aggregate/src/average.rs +++ b/datafusion/functions-aggregate/src/average.rs @@ -42,14 +42,13 @@ use datafusion_functions_aggregate_common::aggregate::groups_accumulator::nulls: filtered_null_mask, set_nulls, }; -use datafusion_doc::DocSection; use datafusion_functions_aggregate_common::utils::DecimalAverager; use datafusion_macros::user_doc; use log::debug; use std::any::Any; use std::fmt::Debug; use std::mem::{size_of, size_of_val}; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; make_udaf_expr_and_func!( Avg, diff --git a/datafusion/functions-aggregate/src/bool_and_or.rs b/datafusion/functions-aggregate/src/bool_and_or.rs index 1b5b20f43b3e..29dfc68e0576 100644 --- a/datafusion/functions-aggregate/src/bool_and_or.rs +++ b/datafusion/functions-aggregate/src/bool_and_or.rs @@ -19,7 +19,6 @@ use std::any::Any; use std::mem::size_of_val; -use std::sync::OnceLock; use arrow::array::ArrayRef; use arrow::array::BooleanArray; @@ -38,7 +37,6 @@ use datafusion_expr::{ Signature, Volatility, }; -use datafusion_doc::DocSection; use datafusion_functions_aggregate_common::aggregate::groups_accumulator::bool_op::BooleanGroupsAccumulator; use datafusion_macros::user_doc; diff --git a/datafusion/functions-aggregate/src/correlation.rs b/datafusion/functions-aggregate/src/correlation.rs index b40555bf6c7f..a0ccdb0ae7d0 100644 --- a/datafusion/functions-aggregate/src/correlation.rs +++ b/datafusion/functions-aggregate/src/correlation.rs @@ -20,7 +20,7 @@ use std::any::Any; use std::fmt::Debug; use std::mem::size_of_val; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::compute::{and, filter, is_not_null}; use arrow::{ @@ -31,7 +31,6 @@ use arrow::{ use crate::covariance::CovarianceAccumulator; use crate::stddev::StddevAccumulator; use datafusion_common::{plan_err, Result, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::{ function::{AccumulatorArgs, StateFieldsArgs}, type_coercion::aggregates::NUMERICS, diff --git a/datafusion/functions-aggregate/src/count.rs b/datafusion/functions-aggregate/src/count.rs index 550df8cb4f7d..b4164c211c35 100644 --- a/datafusion/functions-aggregate/src/count.rs +++ b/datafusion/functions-aggregate/src/count.rs @@ -17,7 +17,6 @@ use ahash::RandomState; use datafusion_common::stats::Precision; -use datafusion_doc::DocSection; use datafusion_functions_aggregate_common::aggregate::count_distinct::BytesViewDistinctCountAccumulator; use datafusion_macros::user_doc; use datafusion_physical_expr::expressions; @@ -25,7 +24,7 @@ use std::collections::HashSet; use std::fmt::Debug; use std::mem::{size_of, size_of_val}; use std::ops::BitAnd; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::{ array::{ArrayRef, AsArray}, diff --git a/datafusion/functions-aggregate/src/covariance.rs b/datafusion/functions-aggregate/src/covariance.rs index adb546e4d906..ffbf2ceef052 100644 --- a/datafusion/functions-aggregate/src/covariance.rs +++ b/datafusion/functions-aggregate/src/covariance.rs @@ -19,7 +19,6 @@ use std::fmt::Debug; use std::mem::size_of_val; -use std::sync::OnceLock; use arrow::{ array::{ArrayRef, Float64Array, UInt64Array}, @@ -31,7 +30,6 @@ use datafusion_common::{ downcast_value, plan_err, unwrap_or_internal_err, DataFusionError, Result, ScalarValue, }; -use datafusion_doc::DocSection; use datafusion_expr::{ function::{AccumulatorArgs, StateFieldsArgs}, type_coercion::aggregates::NUMERICS, diff --git a/datafusion/functions-aggregate/src/first_last.rs b/datafusion/functions-aggregate/src/first_last.rs index f3e66edbc009..9ad55d91a68b 100644 --- a/datafusion/functions-aggregate/src/first_last.rs +++ b/datafusion/functions-aggregate/src/first_last.rs @@ -20,7 +20,7 @@ use std::any::Any; use std::fmt::Debug; use std::mem::size_of_val; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::{ArrayRef, AsArray, BooleanArray}; use arrow::compute::{self, lexsort_to_indices, take_arrays, SortColumn}; @@ -29,7 +29,6 @@ use datafusion_common::utils::{compare_rows, get_row_at_idx}; use datafusion_common::{ arrow_datafusion_err, internal_err, DataFusionError, Result, ScalarValue, }; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::{format_state_name, AggregateOrderSensitivity}; use datafusion_expr::{ diff --git a/datafusion/functions-aggregate/src/grouping.rs b/datafusion/functions-aggregate/src/grouping.rs index 36bdf68c1b0e..445774ff11e7 100644 --- a/datafusion/functions-aggregate/src/grouping.rs +++ b/datafusion/functions-aggregate/src/grouping.rs @@ -19,12 +19,10 @@ use std::any::Any; use std::fmt; -use std::sync::OnceLock; use arrow::datatypes::DataType; use arrow::datatypes::Field; use datafusion_common::{not_impl_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::function::AccumulatorArgs; use datafusion_expr::function::StateFieldsArgs; use datafusion_expr::utils::format_state_name; diff --git a/datafusion/functions-aggregate/src/median.rs b/datafusion/functions-aggregate/src/median.rs index db5fbf00165f..70f192c32ae1 100644 --- a/datafusion/functions-aggregate/src/median.rs +++ b/datafusion/functions-aggregate/src/median.rs @@ -18,7 +18,7 @@ use std::cmp::Ordering; use std::fmt::{Debug, Formatter}; use std::mem::{size_of, size_of_val}; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::{downcast_integer, ArrowNumericType}; use arrow::{ @@ -34,7 +34,6 @@ use arrow::array::ArrowNativeTypeOp; use arrow::datatypes::{ArrowNativeType, ArrowPrimitiveType}; use datafusion_common::{DataFusionError, HashSet, Result, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::function::StateFieldsArgs; use datafusion_expr::{ function::AccumulatorArgs, utils::format_state_name, Accumulator, AggregateUDFImpl, diff --git a/datafusion/functions-aggregate/src/min_max.rs b/datafusion/functions-aggregate/src/min_max.rs index acbeebaad68b..a0f7634c5fa8 100644 --- a/datafusion/functions-aggregate/src/min_max.rs +++ b/datafusion/functions-aggregate/src/min_max.rs @@ -55,7 +55,6 @@ use arrow::datatypes::{ use crate::min_max::min_max_bytes::MinMaxBytesAccumulator; use datafusion_common::ScalarValue; -use datafusion_doc::DocSection; use datafusion_expr::{ function::AccumulatorArgs, Accumulator, AggregateUDFImpl, Documentation, Signature, Volatility, @@ -65,7 +64,6 @@ use datafusion_macros::user_doc; use half::f16; use std::mem::size_of_val; use std::ops::Deref; -use std::sync::OnceLock; fn get_min_max_result_type(input_types: &[DataType]) -> Result> { // make sure that the input types only has one element. diff --git a/datafusion/functions-aggregate/src/nth_value.rs b/datafusion/functions-aggregate/src/nth_value.rs index 15b9e97516ca..8252fd6baaa3 100644 --- a/datafusion/functions-aggregate/src/nth_value.rs +++ b/datafusion/functions-aggregate/src/nth_value.rs @@ -21,14 +21,13 @@ use std::any::Any; use std::collections::VecDeque; use std::mem::{size_of, size_of_val}; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::{new_empty_array, ArrayRef, AsArray, StructArray}; use arrow_schema::{DataType, Field, Fields}; use datafusion_common::utils::{get_row_at_idx, SingleRowListArrayBuilder}; use datafusion_common::{exec_err, internal_err, not_impl_err, Result, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::format_state_name; use datafusion_expr::{ diff --git a/datafusion/functions-aggregate/src/stddev.rs b/datafusion/functions-aggregate/src/stddev.rs index 09a39e342cce..adf86a128cfb 100644 --- a/datafusion/functions-aggregate/src/stddev.rs +++ b/datafusion/functions-aggregate/src/stddev.rs @@ -20,14 +20,13 @@ use std::any::Any; use std::fmt::{Debug, Formatter}; use std::mem::align_of_val; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::Float64Array; use arrow::{array::ArrayRef, datatypes::DataType, datatypes::Field}; use datafusion_common::{internal_err, not_impl_err, Result}; use datafusion_common::{plan_err, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs}; use datafusion_expr::utils::format_state_name; use datafusion_expr::{ diff --git a/datafusion/functions-aggregate/src/string_agg.rs b/datafusion/functions-aggregate/src/string_agg.rs index 5a52bec55f15..7643b44e11d5 100644 --- a/datafusion/functions-aggregate/src/string_agg.rs +++ b/datafusion/functions-aggregate/src/string_agg.rs @@ -22,7 +22,6 @@ use arrow_schema::DataType; use datafusion_common::cast::as_generic_string_array; use datafusion_common::Result; use datafusion_common::{not_impl_err, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::function::AccumulatorArgs; use datafusion_expr::{ Accumulator, AggregateUDFImpl, Documentation, Signature, TypeSignature, Volatility, @@ -31,7 +30,6 @@ use datafusion_macros::user_doc; use datafusion_physical_expr::expressions::Literal; use std::any::Any; use std::mem::size_of_val; -use std::sync::OnceLock; make_udaf_expr_and_func!( StringAgg, diff --git a/datafusion/functions-aggregate/src/sum.rs b/datafusion/functions-aggregate/src/sum.rs index ccc6ee3cf925..6c2854f6bc24 100644 --- a/datafusion/functions-aggregate/src/sum.rs +++ b/datafusion/functions-aggregate/src/sum.rs @@ -22,7 +22,6 @@ use datafusion_expr::utils::AggregateOrderSensitivity; use std::any::Any; use std::collections::HashSet; use std::mem::{size_of, size_of_val}; -use std::sync::OnceLock; use arrow::array::Array; use arrow::array::ArrowNativeTypeOp; @@ -35,7 +34,6 @@ use arrow::datatypes::{ }; use arrow::{array::ArrayRef, datatypes::Field}; use datafusion_common::{exec_err, not_impl_err, Result, ScalarValue}; -use datafusion_doc::DocSection; use datafusion_expr::function::AccumulatorArgs; use datafusion_expr::function::StateFieldsArgs; use datafusion_expr::utils::format_state_name; diff --git a/datafusion/functions-aggregate/src/variance.rs b/datafusion/functions-aggregate/src/variance.rs index 70b10734088f..8aa7a40ce320 100644 --- a/datafusion/functions-aggregate/src/variance.rs +++ b/datafusion/functions-aggregate/src/variance.rs @@ -25,13 +25,11 @@ use arrow::{ datatypes::{DataType, Field}, }; use std::mem::{size_of, size_of_val}; -use std::sync::OnceLock; use std::{fmt::Debug, sync::Arc}; use datafusion_common::{ downcast_value, not_impl_err, plan_err, DataFusionError, Result, ScalarValue, }; -use datafusion_doc::DocSection; use datafusion_expr::{ function::{AccumulatorArgs, StateFieldsArgs}, utils::format_state_name, diff --git a/datafusion/functions/src/datetime/to_date.rs b/datafusion/functions/src/datetime/to_date.rs index e2edea843e98..091d0ba37644 100644 --- a/datafusion/functions/src/datetime/to_date.rs +++ b/datafusion/functions/src/datetime/to_date.rs @@ -22,13 +22,11 @@ use arrow::error::ArrowError::ParseError; use arrow::{array::types::Date32Type, compute::kernels::cast_utils::Parser}; use datafusion_common::error::DataFusionError; use datafusion_common::{arrow_err, exec_err, internal_datafusion_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::{ ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility, }; use datafusion_macros::user_doc; use std::any::Any; -use std::sync::OnceLock; #[user_doc( doc_section(label = "Time and Date Functions"), diff --git a/datafusion/functions/src/math/abs.rs b/datafusion/functions/src/math/abs.rs index c0c7c6f0f6b6..e3d448083e26 100644 --- a/datafusion/functions/src/math/abs.rs +++ b/datafusion/functions/src/math/abs.rs @@ -18,7 +18,7 @@ //! math expressions use std::any::Any; -use std::sync::{Arc, OnceLock}; +use std::sync::Arc; use arrow::array::{ ArrayRef, Decimal128Array, Decimal256Array, Float32Array, Float64Array, Int16Array, @@ -27,7 +27,6 @@ use arrow::array::{ use arrow::datatypes::DataType; use arrow::error::ArrowError; use datafusion_common::{exec_err, not_impl_err, DataFusionError, Result}; -use datafusion_doc::DocSection; use datafusion_expr::interval_arithmetic::Interval; use datafusion_expr::sort_properties::{ExprProperties, SortProperties}; use datafusion_expr::{ diff --git a/datafusion/functions/src/string/ltrim.rs b/datafusion/functions/src/string/ltrim.rs index e0e83d1b01e3..e152ee84f668 100644 --- a/datafusion/functions/src/string/ltrim.rs +++ b/datafusion/functions/src/string/ltrim.rs @@ -22,12 +22,10 @@ use std::any::Any; use crate::string::common::*; use crate::utils::{make_scalar_function, utf8_to_str_type}; use datafusion_common::{exec_err, Result}; -use datafusion_doc::DocSection; use datafusion_expr::function::Hint; use datafusion_expr::{ColumnarValue, Documentation, TypeSignature, Volatility}; use datafusion_expr::{ScalarUDFImpl, Signature}; use datafusion_macros::user_doc; -use std::sync::OnceLock; /// Returns the longest string with leading characters removed. If the characters are not specified, whitespace is removed. /// ltrim('zzzytest', 'xyz') = 'test' diff --git a/datafusion/macros/src/lib.rs b/datafusion/macros/src/lib.rs index 54b688ac2a49..3f22708cc900 100644 --- a/datafusion/macros/src/lib.rs +++ b/datafusion/macros/src/lib.rs @@ -26,16 +26,19 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// declared on `AggregateUDF`, `WindowUDFImpl`, `ScalarUDFImpl` traits. /// /// Example: +/// ``` /// #[user_doc( /// doc_section(include = "true", label = "Time and Date Functions"), -/// description = r"Converts a value to a date (`YYYY-MM-DD`)." -/// sql_example = "```sql\n\ -/// \> select to_date('2023-01-31');\n\ -/// +-----------------------------+\n\ -/// | to_date(Utf8(\"2023-01-31\")) |\n\ -/// +-----------------------------+\n\ -/// | 2023-01-31 |\n\ -/// +-----------------------------+\n\"), +/// description = r"Converts a value to a date (`YYYY-MM-DD`).", +/// syntax_example = "to_date('2017-05-31', '%Y-%m-%d')", +/// sql_example = r#"```sql +/// > select to_date('2023-01-31'); +/// +-----------------------------+ +/// | to_date(Utf8(\"2023-01-31\")) | +/// +-----------------------------+ +/// | 2023-01-31 | +/// +-----------------------------+ +/// ```"#, /// standard_argument(name = "expression", prefix = "String"), /// argument( /// name = "format_n", @@ -48,40 +51,50 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// pub struct ToDateFunc { /// signature: Signature, /// } -/// +/// ``` /// will generate the following code /// -/// #[derive(Debug)] pub struct ToDateFunc { signature : Signature, } -/// use datafusion_doc :: DocSection; -/// use datafusion_doc :: DocumentationBuilder; -/// static DOCUMENTATION : OnceLock < Documentation > = OnceLock :: new(); -/// impl ToDateFunc -/// { -/// fn doc(& self) -> Option < & Documentation > -/// { -/// Some(DOCUMENTATION.get_or_init(|| -/// { -/// Documentation :: -/// builder(DocSection -/// { -/// include : true, label : "Time and Date Functions", description -/// : None -/// }, r"Converts a value to a date (`YYYY-MM-DD`).") -/// .with_syntax_example("to_date('2017-05-31', '%Y-%m-%d')".to_string(),"```sql\n\ -/// \> select to_date('2023-01-31');\n\ -/// +-----------------------------+\n\ -/// | to_date(Utf8(\"2023-01-31\")) |\n\ -/// +-----------------------------+\n\ -/// | 2023-01-31 |\n\ -/// +-----------------------------+\n\) -/// .with_standard_argument("expression", "String".into()) -/// .with_argument("format_n", -/// r"Optional [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) strings to use to parse the expression. Formats will be tried in the order -/// they appear with the first successful one being returned. If none of the formats successfully parse the expression -/// an error will be returned.").build() -/// })) +/// ``` +/// pub struct ToDateFunc { +/// signature: Signature, +/// } +/// impl ToDateFunc { +/// fn doc(&self) -> Option<&datafusion_doc::Documentation> { +/// static DOCUMENTATION: std::sync::LazyLock< +/// datafusion_doc::Documentation, +/// > = std::sync::LazyLock::new(|| { +/// datafusion_doc::Documentation::builder( +/// datafusion_doc::DocSection { +/// include: true, +/// label: "Time and Date Functions", +/// description: None, +/// }, +/// r"Converts a value to a date (`YYYY-MM-DD`).".to_string(), +/// "to_date('2017-05-31', '%Y-%m-%d')".to_string(), +/// ) +/// .with_sql_example( +/// r#"```sql +/// > select to_date('2023-01-31'); +/// +-----------------------------+ +/// | to_date(Utf8(\"2023-01-31\")) | +/// +-----------------------------+ +/// | 2023-01-31 | +/// +-----------------------------+ +/// ```"#, +/// ) +/// .with_standard_argument("expression", "String".into()) +/// .with_argument( +/// "format_n", +/// r"Optional [Chrono format](https://docs.rs/chrono/latest/chrono/format/strftime/index.html) strings to use to parse the expression. Formats will be tried in the order +/// they appear with the first successful one being returned. If none of the formats successfully parse the expression +/// an error will be returned.", +/// ) +/// .build() +/// }); +/// Some(&DOCUMENTATION) /// } /// } +/// ``` #[proc_macro_attribute] pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream { let mut doc_section_include: Option = None; @@ -235,19 +248,14 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream { } }); - let lock_name: proc_macro2::TokenStream = - format!("{name}_DOCUMENTATION").parse().unwrap(); - let generated = quote! { #input - static #lock_name: OnceLock = OnceLock::new(); - impl #name { - - fn doc(&self) -> Option<&Documentation> { - Some(#lock_name.get_or_init(|| { - Documentation::builder(DocSection { include: #doc_section_include, label: #doc_section_lbl, description: #doc_section_description }, + fn doc(&self) -> Option<&datafusion_doc::Documentation> { + static DOCUMENTATION: std::sync::LazyLock = + std::sync::LazyLock::new(|| { + datafusion_doc::Documentation::builder(datafusion_doc::DocSection { include: #doc_section_include, label: #doc_section_lbl, description: #doc_section_description }, #description.to_string(), #syntax_example.to_string()) #sql_example #alt_syntax_example @@ -255,7 +263,8 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream { #(#udf_args)* #(#related_udfs)* .build() - })) + }); + Some(&DOCUMENTATION) } } }; From b56ec1eae104cda272d54c06bbde3a3ed05ba294 Mon Sep 17 00:00:00 2001 From: jonahgao Date: Sun, 8 Dec 2024 00:30:20 +0800 Subject: [PATCH 2/4] Fix cargo doc --- datafusion/macros/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/datafusion/macros/src/lib.rs b/datafusion/macros/src/lib.rs index 3f22708cc900..a9eb8a720d3c 100644 --- a/datafusion/macros/src/lib.rs +++ b/datafusion/macros/src/lib.rs @@ -26,7 +26,7 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// declared on `AggregateUDF`, `WindowUDFImpl`, `ScalarUDFImpl` traits. /// /// Example: -/// ``` +/// ```ingore /// #[user_doc( /// doc_section(include = "true", label = "Time and Date Functions"), /// description = r"Converts a value to a date (`YYYY-MM-DD`).", @@ -54,7 +54,7 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// ``` /// will generate the following code /// -/// ``` +/// ```ingore /// pub struct ToDateFunc { /// signature: Signature, /// } From 290813d46c68db7d8fa95157941330c97ef7ee9b Mon Sep 17 00:00:00 2001 From: Oleks V Date: Sat, 7 Dec 2024 11:28:42 -0800 Subject: [PATCH 3/4] Update datafusion/macros/src/lib.rs --- datafusion/macros/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/macros/src/lib.rs b/datafusion/macros/src/lib.rs index a9eb8a720d3c..f07bfd660c7c 100644 --- a/datafusion/macros/src/lib.rs +++ b/datafusion/macros/src/lib.rs @@ -26,7 +26,7 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// declared on `AggregateUDF`, `WindowUDFImpl`, `ScalarUDFImpl` traits. /// /// Example: -/// ```ingore +/// ```ignore /// #[user_doc( /// doc_section(include = "true", label = "Time and Date Functions"), /// description = r"Converts a value to a date (`YYYY-MM-DD`).", From bd70243b4eef7dcd252ea6a0f30102e75a8e889a Mon Sep 17 00:00:00 2001 From: jonahgao Date: Sun, 8 Dec 2024 12:40:56 +0800 Subject: [PATCH 4/4] Fix doc comment --- datafusion/macros/src/user_doc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/macros/src/user_doc.rs b/datafusion/macros/src/user_doc.rs index f07bfd660c7c..441b3db2a133 100644 --- a/datafusion/macros/src/user_doc.rs +++ b/datafusion/macros/src/user_doc.rs @@ -54,7 +54,7 @@ use syn::{parse_macro_input, DeriveInput, LitStr}; /// ``` /// will generate the following code /// -/// ```ingore +/// ```ignore /// pub struct ToDateFunc { /// signature: Signature, /// }