Apache DataFusion SQL Query Engine
doc-gen: make user_doc to work with predefined consts (#14086)
ding-young80d3131564d2
diff --git a/datafusion-cli/Cargo.lock b/datafusion-cli/Cargo.lockindex 9be9e50cc7..5562d98983 100644--- a/datafusion-cli/Cargo.lock+++ b/datafusion-cli/Cargo.lock@@ -123,11 +123,12 @@ dependencies = [[[package]]name = "anstyle-wincon"-version = "3.0.6"+version = "3.0.7"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125"+checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e"dependencies = ["anstyle",+ "once_cell","windows-sys 0.59.0",]@@ -531,7 +532,7 @@ dependencies = ["aws-runtime","aws-smithy-async","aws-smithy-http",- "aws-smithy-json 0.61.1",+ "aws-smithy-json 0.61.2","aws-smithy-runtime","aws-smithy-runtime-api","aws-smithy-types",@@ -553,7 +554,7 @@ dependencies = ["aws-runtime","aws-smithy-async","aws-smithy-http",- "aws-smithy-json 0.61.1",+ "aws-smithy-json 0.61.2","aws-smithy-runtime","aws-smithy-runtime-api","aws-smithy-types",@@ -575,7 +576,7 @@ dependencies = ["aws-runtime","aws-smithy-async","aws-smithy-http",- "aws-smithy-json 0.61.1",+ "aws-smithy-json 0.61.2","aws-smithy-query","aws-smithy-runtime","aws-smithy-runtime-api",@@ -613,9 +614,9 @@ dependencies = [[[package]]name = "aws-smithy-async"-version = "1.2.3"+version = "1.2.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "427cb637d15d63d6f9aae26358e1c9a9c09d5aa490d64b09354c8217cfef0f28"+checksum = "fa59d1327d8b5053c54bf2eaae63bf629ba9e904434d0835a28ed3c0ed0a614e"dependencies = ["futures-util","pin-project-lite",@@ -624,9 +625,9 @@ dependencies = [[[package]]name = "aws-smithy-http"-version = "0.60.11"+version = "0.60.12"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5c8bc3e8fdc6b8d07d976e301c02fe553f72a39b7a9fea820e023268467d7ab6"+checksum = "7809c27ad8da6a6a68c454e651d4962479e81472aa19ae99e59f9aba1f9713cc"dependencies = ["aws-smithy-runtime-api","aws-smithy-types",@@ -653,9 +654,9 @@ dependencies = [[[package]]name = "aws-smithy-json"-version = "0.61.1"+version = "0.61.2"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ee4e69cc50921eb913c6b662f8d909131bb3e6ad6cb6090d3a39b66fc5c52095"+checksum = "623a51127f24c30776c8b374295f2df78d92517386f77ba30773f15a30ce1422"dependencies = ["aws-smithy-types",]@@ -672,9 +673,9 @@ dependencies = [[[package]]name = "aws-smithy-runtime"-version = "1.7.6"+version = "1.7.7"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a05dd41a70fc74051758ee75b5c4db2c0ca070ed9229c3df50e9475cda1cb985"+checksum = "865f7050bbc7107a6c98a397a9fcd9413690c27fa718446967cf03b2d3ac517e"dependencies = ["aws-smithy-async","aws-smithy-http",@@ -716,9 +717,9 @@ dependencies = [[[package]]name = "aws-smithy-types"-version = "1.2.11"+version = "1.2.12"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "38ddc9bd6c28aeb303477170ddd183760a956a03e083b3902a990238a7e3792d"+checksum = "a28f6feb647fb5e0d5b50f0472c19a7db9462b74e2fec01bb0b44eedcc834e97"dependencies = ["base64-simd","bytes",@@ -822,9 +823,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"[[package]]name = "bitflags"-version = "2.7.0"+version = "2.8.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be"+checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"[[package]]name = "blake2"@@ -1531,6 +1532,7 @@ dependencies = [name = "datafusion-macros"version = "44.0.0"dependencies = [+ "datafusion-expr","quote","syn",]@@ -2459,9 +2461,9 @@ dependencies = [[[package]]name = "js-sys"-version = "0.3.76"+version = "0.3.77"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"+checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"dependencies = ["once_cell","wasm-bindgen",@@ -2589,7 +2591,7 @@ version = "0.1.3"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","libc",]@@ -2617,9 +2619,9 @@ dependencies = [[[package]]name = "log"-version = "0.4.22"+version = "0.4.25"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"+checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"[[package]]name = "lz4_flex"@@ -2674,9 +2676,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"[[package]]name = "miniz_oxide"-version = "0.8.2"+version = "0.8.3"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394"+checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"dependencies = ["adler2",]@@ -2707,7 +2709,7 @@ version = "0.28.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","cfg-if","cfg_aliases 0.1.1","libc",@@ -2869,9 +2871,9 @@ dependencies = [[[package]]name = "outref"-version = "0.5.1"+version = "0.5.2"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "4030760ffd992bef45b0ae3f10ce1aba99e33464c90d14dd7c039884963ddc7a"+checksum = "1a80800c0488c3a21695ea981a54918fbb37abf04f4d0720c453632255e2ff0e"[[package]]name = "parking_lot"@@ -3234,7 +3236,7 @@ version = "0.5.8"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0",][[package]]@@ -3413,7 +3415,7 @@ version = "0.38.43"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","errno","libc","linux-raw-sys",@@ -3530,7 +3532,7 @@ version = "14.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "7803e8936da37efd9b6d4478277f4b2b9bb5cdb37a113e8d63222e58da647e63"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","cfg-if","clipboard-win","fd-lock",@@ -3592,7 +3594,7 @@ version = "2.11.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","core-foundation 0.9.4","core-foundation-sys","libc",@@ -3605,7 +3607,7 @@ version = "3.2.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "271720403f46ca04f7ba6f55d438f8bd878d6b8ca0a1046e8228c4145bcbb316"dependencies = [- "bitflags 2.7.0",+ "bitflags 2.8.0","core-foundation 0.10.0","core-foundation-sys","libc",@@ -4266,9 +4268,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"[[package]]name = "uuid"-version = "1.11.1"+version = "1.12.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b913a3b5fe84142e269d63cc62b64319ccaf89b748fc31fe025177f767a756c4"+checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4"dependencies = ["getrandom","serde",@@ -4322,20 +4324,21 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"[[package]]name = "wasm-bindgen"-version = "0.2.99"+version = "0.2.100"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"+checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"dependencies = ["cfg-if","once_cell",+ "rustversion","wasm-bindgen-macro",][[package]]name = "wasm-bindgen-backend"-version = "0.2.99"+version = "0.2.100"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"+checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"dependencies = ["bumpalo","log",@@ -4347,9 +4350,9 @@ dependencies = [[[package]]name = "wasm-bindgen-futures"-version = "0.4.49"+version = "0.4.50"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2"+checksum = "555d470ec0bc3bb57890405e5d4322cc9ea83cebb085523ced7be4144dac1e61"dependencies = ["cfg-if","js-sys",@@ -4360,9 +4363,9 @@ dependencies = [[[package]]name = "wasm-bindgen-macro"-version = "0.2.99"+version = "0.2.100"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"+checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"dependencies = ["quote","wasm-bindgen-macro-support",@@ -4370,9 +4373,9 @@ dependencies = [[[package]]name = "wasm-bindgen-macro-support"-version = "0.2.99"+version = "0.2.100"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"+checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"dependencies = ["proc-macro2","quote",@@ -4383,9 +4386,12 @@ dependencies = [[[package]]name = "wasm-bindgen-shared"-version = "0.2.99"+version = "0.2.100"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"+checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"+dependencies = [+ "unicode-ident",+][[package]]name = "wasm-streams"@@ -4402,9 +4408,9 @@ dependencies = [[[package]]name = "web-sys"-version = "0.3.76"+version = "0.3.77"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"+checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"dependencies = ["js-sys","wasm-bindgen",diff --git a/datafusion/expr/src/udf.rs b/datafusion/expr/src/udf.rsindex 51c42b5c4c..ffac82265a 100644--- a/datafusion/expr/src/udf.rs+++ b/datafusion/expr/src/udf.rs@@ -881,6 +881,22 @@ pub mod scalar_doc_sections {]}+ pub const fn doc_sections_const() -> &'static [DocSection] {+ &[+ DOC_SECTION_MATH,+ DOC_SECTION_CONDITIONAL,+ DOC_SECTION_STRING,+ DOC_SECTION_BINARY_STRING,+ DOC_SECTION_REGEX,+ DOC_SECTION_DATETIME,+ DOC_SECTION_ARRAY,+ DOC_SECTION_STRUCT,+ DOC_SECTION_MAP,+ DOC_SECTION_HASHING,+ DOC_SECTION_OTHER,+ ]+ }+pub const DOC_SECTION_MATH: DocSection = DocSection {include: true,label: "Math Functions",diff --git a/datafusion/macros/Cargo.toml b/datafusion/macros/Cargo.tomlindex 0c4fdb10a3..8946640f20 100644--- a/datafusion/macros/Cargo.toml+++ b/datafusion/macros/Cargo.toml@@ -37,5 +37,6 @@ path = "src/user_doc.rs"proc-macro = true[dependencies]+datafusion-expr = { workspace = true }quote = "1.0.37"syn = { version = "2.0.79", features = ["full"] }diff --git a/datafusion/macros/src/user_doc.rs b/datafusion/macros/src/user_doc.rsindex e4b6a0f3b1..6ca90ed376 100644--- a/datafusion/macros/src/user_doc.rs+++ b/datafusion/macros/src/user_doc.rs@@ -16,6 +16,7 @@// under the License.extern crate proc_macro;+use datafusion_expr::scalar_doc_sections::doc_sections_const;use proc_macro::TokenStream;use quote::quote;use syn::{parse_macro_input, DeriveInput, LitStr};@@ -24,11 +25,12 @@ use syn::{parse_macro_input, DeriveInput, LitStr};/// from it by constructing a `DocumentBuilder()` automatically. The `Documentation` can be/// retrieved from the `documentation()` method/// declared on `AggregateUDF`, `WindowUDFImpl`, `ScalarUDFImpl` traits.-///+/// For `doc_section`, this macro will try to find corresponding predefined `DocSection` by label field+/// Predefined `DocSection` can be found in datafusion/expr/src/udf.rs/// Example:/// ```ignore/// #[user_doc(-/// doc_section(include = "true", label = "Time and Date Functions"),+/// doc_section(label = "Time and Date Functions"),/// 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@@ -97,9 +99,7 @@ use syn::{parse_macro_input, DeriveInput, LitStr};/// ```#[proc_macro_attribute]pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {- let mut doc_section_include: Option<LitStr> = None;let mut doc_section_lbl: Option<LitStr> = None;- let mut doc_section_desc: Option<LitStr> = None;let mut description: Option<LitStr> = None;let mut syntax_example: Option<LitStr> = None;@@ -112,15 +112,9 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {let parser = syn::meta::parser(|meta| {if meta.path.is_ident("doc_section") {meta.parse_nested_meta(|meta| {- if meta.path.is_ident("include") {- doc_section_include = meta.value()?.parse()?;- return Ok(());- } else if meta.path.is_ident("label") {+ if meta.path.is_ident("label") {doc_section_lbl = meta.value()?.parse()?;return Ok(());- } else if meta.path.is_ident("description") {- doc_section_desc = meta.value()?.parse()?;- return Ok(());}Ok(())})@@ -192,13 +186,18 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {let input = parse_macro_input!(input as DeriveInput);let name = input.clone().ident;- let doc_section_include: bool = if let Some(doc_section_include) = doc_section_include- {- doc_section_include.value().parse().unwrap()- } else {- true- };-+ if doc_section_lbl.is_none() {+ eprintln!("label for doc_section should exist");+ }+ let label = doc_section_lbl.as_ref().unwrap().value();+ // Try to find a predefined const by label first.+ // If there is no match but label exists, default value will be used for include and description+ let doc_section_option = doc_sections_const().iter().find(|ds| ds.label == label);+ let (doc_section_include, doc_section_label, doc_section_desc) =+ match doc_section_option {+ Some(section) => (section.include, section.label, section.description),+ None => (true, label.as_str(), None),+ };let doc_section_description = doc_section_desc.map(|desc| quote! { Some(#desc)}).unwrap_or(quote! { None });@@ -255,7 +254,7 @@ pub fn user_doc(args: TokenStream, input: TokenStream) -> TokenStream {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: #doc_section_include, label: #doc_section_lbl, description: #doc_section_description },+ datafusion_doc::Documentation::builder(datafusion_doc::DocSection { include: #doc_section_include, label: #doc_section_label, description: #doc_section_description },#description.to_string(), #syntax_example.to_string())#sql_example#(#alt_syntax_example)*