2010YOUY01 commented on code in PR #19367:
URL: https://github.com/apache/datafusion/pull/19367#discussion_r2638705858


##########
.github/workflows/rust.yml:
##########
@@ -708,6 +708,11 @@ jobs:
           # If you encounter an error, run './dev/update_function_docs.sh' and 
commit
           ./dev/update_function_docs.sh
           git diff --exit-code
+      - name: Check if metrics.md has been modified
+        run: |
+          # If you encounter an error, run './dev/update_metric_docs.sh' and 
commit
+          ./dev/update_metric_docs.sh

Review Comment:
   Can we also include this step to `./dev/rust_lint.rs` for local 
non-functional check? Ideally we can have a version that only do the checks, 
but won't update the files.



##########
datafusion/macros/src/lib.rs:
##########
@@ -0,0 +1,131 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#![doc(
+    html_logo_url = 
"https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg";,
+    html_favicon_url = 
"https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg";
+)]
+#![cfg_attr(docsrs, feature(doc_cfg))]
+#![deny(clippy::allow_attributes)]
+
+extern crate proc_macro;
+
+mod metric_doc_impl;
+mod user_doc_impl;
+
+use proc_macro::TokenStream;
+
+/// This procedural macro is intended to parse a rust custom attribute and 
create user documentation
+/// 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(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
+/// > 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",
+///         description = 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."
+///    )
+/// )]
+/// #[derive(Debug)]
+/// pub struct ToDateFunc {
+///     signature: Signature,
+/// }
+/// ```
+/// will generate the following code
+/// ```ignore
+/// 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 {
+    user_doc_impl::user_doc(args, input)
+}
+
+/// Helper attribute to register metrics structs or execs for documentation 
generation.
+/// Usage:
+/// - `#[metric_doc(common)]` on `*_Metrics` structs (derives 
`DocumentedMetrics`)
+/// - `#[metric_doc(M1, M2)]` on `*_Exec` structs to link metrics (derives 
`DocumentedExec`)

Review Comment:
   I recommend to add more examples to explain how to use this macro.
   
   Let's say we have an operator `UserDefinedExec`, and its inside has two 
struct for metrics `MetricsGroup1` and `MetricsGroup2`
   
   Is it the case we should add
   `#[metric_doc(MetricsGroup1, MetricsGroup2)]` to `UserDefinedExec`, and add 
`#[metric_doc]` to both struct `MetricsGroup1` and `MetricsGroup2`?
   
   Would it still be working if `MetricsGroup*` lives in a different file than 
the `UserDefinedExec`? 
   
   Is it possible to also test for:
   - exec and metrics struct lives in different file
   - one exec has multiple metrics struct



##########
datafusion/core/Cargo.toml:
##########
@@ -129,6 +129,7 @@ datafusion-datasource-parquet = { workspace = true, 
optional = true }
 datafusion-execution = { workspace = true }
 datafusion-expr = { workspace = true, default-features = false }
 datafusion-expr-common = { workspace = true }
+datafusion-doc = { workspace = true }

Review Comment:
   Looks like this new dependency and bin will be included in the release 
binary, can we guard them behind a feature?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to