alamb commented on code in PR #14544:
URL: https://github.com/apache/datafusion/pull/14544#discussion_r1947719701


##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -145,6 +220,24 @@ This "works" in isolation, i.e. if you have a slice of 
`ArrayRef`s, you can call
 `ArrayRef` with 1 added to each value.
 
 ```rust
+# use std::sync::Arc;
+# use datafusion::arrow::array::{ArrayRef, Int64Array};
+# use datafusion::common::cast::as_int64_array;
+# use datafusion::common::Result;
+# use datafusion::logical_expr::ColumnarValue;
+#
+# pub fn add_one(args: &[ColumnarValue]) -> Result<ColumnarValue> {
+#     // Error handling omitted for brevity

Review Comment:
   👍 



##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -55,62 +55,138 @@ of arguments.
 This a lower level API with more functionality but is more complex, also 
documented in [`advanced_udf.rs`].
 
 ```rust
+use std::sync::Arc;
 use std::any::Any;
+use std::sync::LazyLock;
 use arrow::datatypes::DataType;
+use datafusion_common::cast::as_int64_array;
 use datafusion_common::{DataFusionError, plan_err, Result};
-use datafusion_expr::{col, ColumnarValue, Signature, Volatility};
+use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, 
Signature, Volatility};
+use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
+use datafusion::arrow::array::{ArrayRef, Int64Array};
 use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
 
+/// This struct for a simple UDF that adds one to an int32
 #[derive(Debug)]
 struct AddOne {
-    signature: Signature
-};
+  signature: Signature,
+}
 
 impl AddOne {
-    fn new() -> Self {
-        Self {
-            signature: Signature::uniform(1, vec![DataType::Int32], 
Volatility::Immutable)
-        }
-    }
+  fn new() -> Self {
+    Self {
+      signature: Signature::uniform(1, vec![DataType::Int32], 
Volatility::Immutable),
+     }
+  }
 }
 
+static DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
+    Documentation::builder(DOC_SECTION_MATH, "Add one to an int32", 
"add_one(2)")
+        .with_argument("arg1", "The int32 number to add one to")
+        .build()
+});
+
 /// Implement the ScalarUDFImpl trait for AddOne
 impl ScalarUDFImpl for AddOne {
-    fn as_any(&self) -> &dyn Any { self }
-    fn name(&self) -> &str { "add_one" }
-    fn signature(&self) -> &Signature { &self.signature }
-    fn return_type(&self, args: &[DataType]) -> Result<DataType> {
-        if !matches!(args.get(0), Some(&DataType::Int32)) {
-            return plan_err!("add_one only accepts Int32 arguments");
-        }
-        Ok(DataType::Int32)
-    }
-    // The actual implementation would add one to the argument
-    fn invoke_batch(&self, args: &[ColumnarValue], _number_rows: usize) -> 
Result<ColumnarValue> {
-        let args = columnar_values_to_array(args)?;
+   fn as_any(&self) -> &dyn Any { self }
+   fn name(&self) -> &str { "add_one" }
+   fn signature(&self) -> &Signature { &self.signature }
+   fn return_type(&self, args: &[DataType]) -> Result<DataType> {
+     if !matches!(args.get(0), Some(&DataType::Int32)) {
+       return plan_err!("add_one only accepts Int32 arguments");
+     }
+     Ok(DataType::Int32)
+   }
+   // The actual implementation would add one to the argument
+   fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {

Review Comment:
   💯 



##########
docs/source/library-user-guide/adding-udfs.md:
##########
@@ -55,62 +55,138 @@ of arguments.
 This a lower level API with more functionality but is more complex, also 
documented in [`advanced_udf.rs`].
 
 ```rust
+use std::sync::Arc;
 use std::any::Any;
+use std::sync::LazyLock;
 use arrow::datatypes::DataType;
+use datafusion_common::cast::as_int64_array;
 use datafusion_common::{DataFusionError, plan_err, Result};
-use datafusion_expr::{col, ColumnarValue, Signature, Volatility};
+use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, 
Signature, Volatility};
+use datafusion_expr::scalar_doc_sections::DOC_SECTION_MATH;
+use datafusion::arrow::array::{ArrayRef, Int64Array};
 use datafusion_expr::{ScalarUDFImpl, ScalarUDF};
 
+/// This struct for a simple UDF that adds one to an int32
 #[derive(Debug)]
 struct AddOne {
-    signature: Signature
-};
+  signature: Signature,
+}
 
 impl AddOne {
-    fn new() -> Self {
-        Self {
-            signature: Signature::uniform(1, vec![DataType::Int32], 
Volatility::Immutable)
-        }
-    }
+  fn new() -> Self {
+    Self {
+      signature: Signature::uniform(1, vec![DataType::Int32], 
Volatility::Immutable),
+     }
+  }
 }
 
+static DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {

Review Comment:
   Actually @comphead  and @Omega359  made some nicer ways to create this 
documentation with macros (we can fix this as a follow on PR)



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to