Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 145 additions & 1 deletion docs/sql-ref-syntax-ddl-create-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,148 @@ license: |
limitations under the License.
---

**This page is under construction**
### Description
`CREATE FUNCTION` statement is used to create a temporary or permanent function
in Spark. Temporary functions are scoped at a session level where as permanent
functions are created in the persistent catalog and are made available to
all sessions. The resources specified in the `USING` clause are made available
to all executors when they are executed for the first time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to explain we can create/register temporary SQL functions via Python/Scala/Java APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile I have created a place holder link for custom scalar functions. There is already a place holder for aggregate functions and a lot of content is already in place.


### Syntax
{% highlight sql %}
CREATE [ OR REPLACE ] [ TEMPORARY ] FUNCTION [ IF NOT EXISTS ]
function_name AS class_name [ resource_locations ]
{% endhighlight %}

### Parameters
<dl>
<dt><code><em>OR REPLACE</em></code></dt>
<dd>
If specified, the resources for function are reloaded. This is mainly useful
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the function

to pick up any changes made to the implementation of the function. This
parameter is mutually exclusive to <code>IF NOT EXISTS</code> and can not
be specified together.
</dd>
<dt><code><em>TEMPORARY</em></code></dt>
<dd>
Indicates the scope of function being created. When TEMPORARY is specified, the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: code-format TEMPORARY?

created function is valid in the current session. No persistent entry is made
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid -> valid and visible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile thanks.. fixed.

in the catalog for these kind of functions.
</dd>
<dt><code><em>IF NOT EXISTS</em></code></dt>
<dd>
If specified, creates the function only when it does not exist. The creation
of function succeeds (no error is thrown), if the specified function already
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove comma

exists in the system. This parameter is mutually exclusive to <code> OR REPLACE</code>
and can not be specified together.
</dd>
<dt><code><em>function_name</em></code></dt>
<dd>
Specifies a name of funnction to be created. The function name may be
optionally qualified with a database name. <br><br>
<b>Syntax:</b>
<code>
[database_name.]function_name
</code>
</dd>
<dt><code><em>class_name</em></code></dt>
<dd>
Specifies the name of the class that provides the implementation for function to be created.
The implementing class should extend from one of the base classes in `Hive` as follows:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserDefinedAggregateFunction. We also support this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile I have created a place holder link for custom scalar functions. There is already a place holder for aggregate functions and a lot of content is already in place.

<ul>
<li>Should extend UDF or UDAF in org.apache.hadoop.hive.ql.exec</li>
<li>Should extend AbstractGenericUDAFResolver, GenericUDF, or GenericUDTF in org.apache.hadoop.hive.ql.udf.generic.</li>
</ul>
</dd>
<dt><code><em>resource_locations</em></code></dt>
<dd>
Specifies the list of resources that contain the implementation of the function
along with its dependencies. <br><br>
<b>Syntax:</b>
<code>
USING { { (JAR | FILE ) resource_uri} , ...}
</code>
</dd>
</dl>

### Examples
{% highlight sql %}
-- 1. Create a simple UDF `SimpleUdf` that adds the supplied integet value by 10.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

integet -> integral

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile fixed.

-- import org.apache.hadoop.hive.ql.exec.UDF;
-- public class SimpleUdf extends UDF {
-- public int evaluate(int value) {
-- return value + 10;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indent this more

-- }
-- }
-- 2. Compile and place it in a jar file called `SimpleUdf.jar` in /tmp.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: jar -> JAR


-- Create a table called `test` and insert two rows.
CREATE TABLE test(c1 INT);
INSERT INTO test VALUES (1), (2);

-- Create a permanent function called `simple_udf`.
CREATE FUNCTION simple_udf AS 'SimpleUdf'
USING JAR '/tmp/SimpleUdf.jar';

-- Verify that the function is in the registry.
SHOW USER FUNCTIONS;
+------------------+
| function|
+------------------+
|default.simple_udf|
+------------------+

-- Invoke the function. Every selected value should be incremented by 10.
SELECT simple_udf(c1) AS function_return_value FROM t1;
+---------------------+
|function_return_value|
+---------------------+
| 11|
| 12|
+---------------------+

-- Created a temporary function.
CREATE TEMPORARY FUNCTION simple_temp_udf AS 'SimpleUdf'
USING JAR '/tmp/SimpleUdf.jar';

-- Verify that the newly created temporary function is in the registry.
-- Please note that the temporary function does not have a qualified
-- database associated with it.
SHOW USER FUNCTIONS;
+------------------+
| function|
+------------------+
|default.simple_udf|
| simple_temp_udf|
+------------------+

-- 1. Mofify `SimpleUdf`'s implementation to add supplied integet value by 20.
-- import org.apache.hadoop.hive.ql.exec.UDF;

-- public class SimpleUdfR extends UDF {
-- public int evaluate(int value) {
-- return value + 20;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent here

-- }
-- }
-- 2. Compile and place it in a jar file called `SimpleUdfR.jar` in /tmp.

-- Replace the implementation of `simple_udf`
CREATE OR REPLACE FUNCTION simple_udf AS 'SimpleUdfR'
USING JAR '/tmp/SimpleUdfR.jar';

-- Invoke the function. Every selected value should be incremented by 20.
SELECT simple_udf(c1) AS function_return_value FROM t1;
+---------------------+
|function_return_value|
+---------------------+
| 21|
| 22|
+---------------------+

{% endhighlight %}

### Related statements
- [SHOW FUNCTIONS](sql-ref-syntax-aux-show-functions.html)
- [DESCRIBE FUNCTION](sql-ref-syntax-aux-describe-function.html)
- [DESCRIBE FUNCTION](sql-ref-syntax-aux-describe-function.html)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gatorsmile oops.. thanks for catching it. Will fix.

- [DROP FUNCTION](sql-ref-syntax-ddl-drop-function.html)