> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# CREATE FUNCTION

Creates a new [UDF/UDTF](https://docs.kinetica.com/content/sql/udf.md#sql-udf) with the given options.  A
UDTF is simply a UDF defined to return a single table, using the
`RETURNS TABLE` clause.

```sql title="CREATE FUNCTION Syntax" theme={null}
CREATE [OR REPLACE] FUNCTION <function name>
[RETURNS TABLE (<column definition list>)]
[MODE = '<execution mode>']
[RUN_COMMAND = '<command>']
[RUN_COMMAND_ARGS = '<command args>']
FILE PATHS '<file paths>'
[WITH OPTIONS (<option name> = '<option value>'[,...])]
```

## Parameters

> [accordion] OR REPLACE
>
> Any existing UDF with the same name will be dropped before creating this one

> [accordion] \<function name>
>
> Name of the UDF, which can be referenced in subsequent commands

> [accordion] RETURNS TABLE
>
> Specifies that the UDF returns a table after execution and defines the column names and types
> to which the function's output data is expected to conform
>
> > [!NOTE]
> >
> > This defines the UDF as a *User-Defined Table Function (UDTF)*, which allows
> > the function to be called within a SQL query; see
> > [Executing Functions](https://docs.kinetica.com/content/sql/udf/executing-functions.md#sql-execute-function) for details.

> [accordion] MODE
>
> Name of the execution mode for the UDF; the following modes are available:
>
> <table>
>   <thead>
>     <tr>
>       <th>Constant</th>
>       <th>Description</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>`DISTRIBUTED`</td>
>       <td>The function's processing will be distributed among the nodes in the cluster</td>
>     </tr>
>
>     <tr>
>       <td>`NONDISTRIBUTED`</td>
>       <td>The function's processing will be completed on a single node in the cluster</td>
>     </tr>
>   </tbody>
> </table>

> [accordion] RUN\_COMMAND
>
> The command used to execute the files associated with the UDF; e.g., `python`

> [accordion] RUN\_COMMAND\_ARGS
>
> The arguments supplied alongside the given `RUN_COMMAND`; e.g.:
>
> ```
> 'udf.py','--username','jdoe','--password','jdoe123'
> ```

> [accordion] FILE PATHS
>
> A comma-separated list of single-quoted file paths from which the function and any additional
> files will be loaded; the file paths should be [KiFS](https://docs.kinetica.com/content/tools/kifs.md) locations and exist
> prior to the UDF/UDTF creation.

> [accordion] WITH OPTIONS
>
> Optional indicator that a comma-delimited list of option/value assignments will follow
>
> <table>
>   <thead>
>     <tr>
>       <th>Option</th>
>       <th>Description</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>`max_concurrency_per_node`</td>
>       <td>The maximum number of concurrent instances of the function that will be executed per node. Setting to `0` allows unlimited concurrency. The default value is `0`.</td>
>     </tr>
>
>     <tr>
>       <td>`set_environment`</td>
>       <td>The [Python UDF environment](https://docs.kinetica.com/content/sql/udf/create-function-environment.md#sql-create-function-environment) to use for this Python UDF.</td>
>     </tr>
>   </tbody>
> </table>

## Examples

Before creating a UDF/UDTF, upload the function file,
udf\_st.py to a new directory, udf, in
*KiFS*:

```sql Create UDF Directory Example theme={null}
CREATE DIRECTORY 'udf'
```

```sql UPLOAD UDF FILE Example theme={null}
UPLOAD FILE 'udf_st.py'
INTO 'udf'
```

To create a distributed Python `udf_st` UDF that uses that file:

```sql CREATE FUNCTION (UDF) Example theme={null}
CREATE FUNCTION udf_st
MODE = 'distributed'
RUN_COMMAND = 'python'
RUN_COMMAND_ARGS = 'udf/udf_st.py'
FILE PATHS 'kifs://udf/udf_st.py'
WITH OPTIONS (SET_ENVIRONMENT = 'udfe_st')
```

To create the same function as a UDTF that returns a result set instead:

```sql CREATE FUNCTION (UDTF) Example theme={null}
CREATE FUNCTION UDTF_ST
RETURNS TABLE
(
	id SMALLINT NOT NULL,
	score REAL
)
MODE = 'distributed'
RUN_COMMAND = 'python'
RUN_COMMAND_ARGS = 'udf/udf_st.py'
FILE PATHS 'kifs://udf/udf_st.py'
WITH OPTIONS (SET_ENVIRONMENT = 'udfe_st')
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.