Skip to content

Commit 71512e6

Browse files
alambmilenkovicm
andauthored
docs: Improve documentation for FunctionFactory / CREATE FUNCTION (#17859)
* docs: Improve documentation for FunctionFactory / CREATE FUNCTION * More docs * Improve docs * Update datafusion/core/src/execution/context/mod.rs Co-authored-by: Marko Milenković <[email protected]> --------- Co-authored-by: Marko Milenković <[email protected]>
1 parent 7dff1e1 commit 71512e6

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

datafusion/core/src/execution/context/mod.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,28 +1786,63 @@ impl From<SessionContext> for SessionStateBuilder {
17861786
/// A planner used to add extensions to DataFusion logical and physical plans.
17871787
#[async_trait]
17881788
pub trait QueryPlanner: Debug {
1789-
/// Given a `LogicalPlan`, create an [`ExecutionPlan`] suitable for execution
1789+
/// Given a [`LogicalPlan`], create an [`ExecutionPlan`] suitable for execution
17901790
async fn create_physical_plan(
17911791
&self,
17921792
logical_plan: &LogicalPlan,
17931793
session_state: &SessionState,
17941794
) -> Result<Arc<dyn ExecutionPlan>>;
17951795
}
17961796

1797-
/// A pluggable interface to handle `CREATE FUNCTION` statements
1798-
/// and interact with [SessionState] to registers new udf, udaf or udwf.
1797+
/// Interface for handling `CREATE FUNCTION` statements and interacting with
1798+
/// [SessionState] to create and register functions ([`ScalarUDF`],
1799+
/// [`AggregateUDF`], [`WindowUDF`], and [`TableFunctionImpl`]) dynamically.
1800+
///
1801+
/// Implement this trait to create user-defined functions in a custom way, such
1802+
/// as loading from external libraries or defining them programmatically.
1803+
/// DataFusion will parse `CREATE FUNCTION` statements into [`CreateFunction`]
1804+
/// structs and pass them to the [`create`](Self::create) method.
1805+
///
1806+
/// Note there is no default implementation of this trait provided in DataFusion,
1807+
/// because the implementation and requirements vary widely. Please see
1808+
/// [function_factory example] for a reference implementation.
1809+
///
1810+
/// [function_factory example]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/function_factory.rs
1811+
///
1812+
/// # Examples of syntax that can be supported
1813+
///
1814+
/// ```sql
1815+
/// CREATE FUNCTION f1(BIGINT)
1816+
/// RETURNS BIGINT
1817+
/// RETURN $1 + 1;
1818+
/// ```
1819+
/// or
1820+
/// ```sql
1821+
/// CREATE FUNCTION to_miles(DOUBLE)
1822+
/// RETURNS DOUBLE
1823+
/// LANGUAGE PYTHON
1824+
/// AS '
1825+
/// import pyarrow.compute as pc
1826+
///
1827+
/// conversation_rate_multiplier = 0.62137119
1828+
///
1829+
/// def to_miles(km_data):
1830+
/// return pc.multiply(km_data, conversation_rate_multiplier)
1831+
/// '
1832+
/// ```
17991833
18001834
#[async_trait]
18011835
pub trait FunctionFactory: Debug + Sync + Send {
1802-
/// Handles creation of user defined function specified in [CreateFunction] statement
1836+
/// Creates a new dynamic function from the SQL in the [CreateFunction] statement
18031837
async fn create(
18041838
&self,
18051839
state: &SessionState,
18061840
statement: CreateFunction,
18071841
) -> Result<RegisterFunction>;
18081842
}
18091843

1810-
/// Type of function to create
1844+
/// The result of processing a [`CreateFunction`] statement with [`FunctionFactory`].
1845+
#[derive(Debug, Clone)]
18111846
pub enum RegisterFunction {
18121847
/// Scalar user defined function
18131848
Scalar(Arc<ScalarUDF>),

datafusion/expr-common/src/signature.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub const FIXED_SIZE_LIST_WILDCARD: i32 = i32::MIN;
4949
/// optimizations. You should always define your function to have the strictest
5050
/// possible volatility to maximize performance and avoid unexpected
5151
/// results.
52-
///
5352
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
5453
pub enum Volatility {
5554
/// Always returns the same output when given the same input.

datafusion/expr/src/logical_plan/ddl.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,20 @@ impl PartialOrd for DropCatalogSchema {
458458
}
459459
}
460460

461-
/// Arguments passed to `CREATE FUNCTION`
461+
/// Arguments passed to the `CREATE FUNCTION` statement
462462
///
463-
/// Note this meant to be the same as from sqlparser's [`sqlparser::ast::Statement::CreateFunction`]
463+
/// These statements are turned into executable functions using [`FunctionFactory`]
464+
///
465+
/// # Notes
466+
///
467+
/// This structure purposely mirrors the structure in sqlparser's
468+
/// [`sqlparser::ast::Statement::CreateFunction`], but does not use it directly
469+
/// to avoid a dependency on sqlparser in the core crate.
470+
///
471+
///
472+
/// [`FunctionFactory`]: https://docs.rs/datafusion/latest/datafusion/execution/context/trait.FunctionFactory.html
464473
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
465474
pub struct CreateFunction {
466-
// TODO: There is open question should we expose sqlparser types or redefine them here?
467-
// At the moment it make more sense to expose sqlparser types and leave
468-
// user to convert them as needed
469475
pub or_replace: bool,
470476
pub temporary: bool,
471477
pub name: String,
@@ -511,6 +517,9 @@ impl PartialOrd for CreateFunction {
511517
}
512518
}
513519

520+
/// Part of the `CREATE FUNCTION` statement
521+
///
522+
/// See [`CreateFunction`] for details
514523
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
515524
pub struct OperateFunctionArg {
516525
// TODO: figure out how to support mode
@@ -541,6 +550,9 @@ impl<'a> TreeNodeContainer<'a, Expr> for OperateFunctionArg {
541550
}
542551
}
543552

553+
/// Part of the `CREATE FUNCTION` statement
554+
///
555+
/// See [`CreateFunction`] for details
544556
#[derive(Clone, PartialEq, Eq, PartialOrd, Hash, Debug)]
545557
pub struct CreateFunctionBody {
546558
/// LANGUAGE lang_name

0 commit comments

Comments
 (0)