Skip to content

Commit 9c08f82

Browse files
Document CREATE/DROP TEMPORARY FUNCTION in Sphinx reference
1 parent 1942d1a commit 9c08f82

6 files changed

Lines changed: 249 additions & 0 deletions

File tree

docs/sphinx/source/reference/sql_commands/DDL/CREATE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ CREATE
1212
CREATE/TYPE
1313
CREATE/INDEX
1414
CREATE/FUNCTION
15+
CREATE/TEMPORARY_FUNCTION
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Diagram(
2+
Stack(
3+
Sequence(
4+
Terminal('CREATE'),
5+
Optional(
6+
Sequence(Terminal('OR'), Terminal('REPLACE'))
7+
),
8+
Terminal('TEMPORARY'),
9+
Terminal('FUNCTION'),
10+
NonTerminal('functionName')
11+
),
12+
Sequence(
13+
Terminal('('),
14+
Optional(
15+
NonTerminal('functionParameters')
16+
),
17+
Terminal(')')
18+
),
19+
Sequence(
20+
Terminal('ON'),
21+
Terminal('COMMIT'),
22+
Terminal('DROP'),
23+
Terminal('FUNCTION'),
24+
Terminal('AS'),
25+
NonTerminal('selectStatement')
26+
)
27+
)
28+
)
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
==========================
2+
CREATE TEMPORARY FUNCTION
3+
==========================
4+
5+
.. _create_temporary_function:
6+
7+
Creates a transaction-scoped user-defined function that is automatically dropped when the transaction commits.
8+
9+
Syntax
10+
======
11+
12+
.. raw:: html
13+
:file: TEMPORARY_FUNCTION.diagram.svg
14+
15+
.. raw:: html
16+
:file: FUNCTION_params.diagram.svg
17+
18+
.. code-block:: sql
19+
20+
CREATE [OR REPLACE] TEMPORARY FUNCTION function_name (
21+
[IN] parameter_name data_type [DEFAULT default_value], ...
22+
) ON COMMIT DROP FUNCTION AS query
23+
24+
Parameters
25+
==========
26+
27+
``OR REPLACE``
28+
If specified, replaces an existing temporary function with the same name in the current transaction. Without this clause, creating a function with a name that already exists raises an error.
29+
30+
``function_name``
31+
The name of the temporary function to create. Scoped to the current transaction.
32+
33+
``parameter_name``
34+
The name of a function parameter. Parameters can be referenced in the function body using their names.
35+
36+
``data_type``
37+
The SQL data type of the parameter (e.g., ``BIGINT``, ``STRING``, ``INTEGER``).
38+
39+
``default_value``
40+
Optional default value for the parameter. If provided, the parameter becomes optional when calling the function.
41+
42+
``query``
43+
The SQL SELECT statement that defines the function body.
44+
45+
Returns
46+
=======
47+
48+
Returns the result of executing the function's query with the provided parameter values.
49+
50+
Examples
51+
========
52+
53+
Setup
54+
-----
55+
56+
For these examples, assume we have a ``restaurants`` table:
57+
58+
.. code-block:: sql
59+
60+
CREATE TABLE restaurants(
61+
rest_no BIGINT,
62+
name STRING,
63+
city STRING,
64+
PRIMARY KEY(rest_no))
65+
66+
INSERT INTO restaurants VALUES
67+
(1001, 'The Burger Place', 'New York'),
68+
(1002, 'Pizza Palace', 'New York'),
69+
(2001, 'Sushi World', 'San Francisco'),
70+
(2002, 'Taco Town', 'San Francisco')
71+
72+
Basic Temporary Function
73+
------------------------
74+
75+
Create a temporary function scoped to the current transaction:
76+
77+
.. code-block:: sql
78+
79+
CREATE TEMPORARY FUNCTION high_no_restaurants()
80+
ON COMMIT DROP FUNCTION AS
81+
SELECT * FROM restaurants WHERE rest_no > 1000
82+
83+
Call the function like a regular table-valued function:
84+
85+
.. code-block:: sql
86+
87+
SELECT * FROM high_no_restaurants()
88+
89+
The function is automatically dropped when the transaction commits.
90+
91+
Replacing an Existing Temporary Function
92+
-----------------------------------------
93+
94+
Use ``OR REPLACE`` to redefine a temporary function within the same transaction:
95+
96+
.. code-block:: sql
97+
98+
CREATE OR REPLACE TEMPORARY FUNCTION high_no_restaurants()
99+
ON COMMIT DROP FUNCTION AS
100+
SELECT * FROM restaurants WHERE rest_no > 2000
101+
102+
Temporary Function with Parameters
103+
------------------------------------
104+
105+
Temporary functions support the same parameter syntax as permanent functions:
106+
107+
.. code-block:: sql
108+
109+
CREATE TEMPORARY FUNCTION restaurants_in_city(IN target_city STRING)
110+
ON COMMIT DROP FUNCTION AS
111+
SELECT rest_no, name FROM restaurants WHERE city = target_city
112+
113+
.. code-block:: sql
114+
115+
SELECT * FROM restaurants_in_city('New York')
116+
117+
.. list-table::
118+
:header-rows: 1
119+
120+
* - :sql:`rest_no`
121+
- :sql:`name`
122+
* - :json:`1001`
123+
- :json:`"The Burger Place"`
124+
* - :json:`1002`
125+
- :json:`"Pizza Palace"`
126+
127+
Dropping Early
128+
--------------
129+
130+
A temporary function can be dropped explicitly before the transaction commits using :ref:`DROP TEMPORARY FUNCTION <drop_temporary_function>`:
131+
132+
.. code-block:: sql
133+
134+
DROP TEMPORARY FUNCTION high_no_restaurants
135+
136+
Important Notes
137+
===============
138+
139+
Transaction Scope
140+
-----------------
141+
142+
Temporary functions exist only for the duration of the transaction in which they are created. They are automatically dropped on commit and are not visible to other transactions or after the transaction ends.
143+
144+
Relationship to Permanent Functions
145+
-------------------------------------
146+
147+
Temporary functions are distinct from permanent functions created with :ref:`CREATE FUNCTION <create_function>`. Permanent functions are stored at the schema template level and persist across transactions. Creating a temporary function with the same name as an existing permanent function raises a ``DUPLICATE_FUNCTION`` error.
148+
149+
Parameter Syntax
150+
----------------
151+
152+
Temporary functions share the same parameter declaration syntax as permanent functions. See :ref:`CREATE FUNCTION <create_function>` for full details on positional parameters, named parameters, and default values.
153+
154+
See Also
155+
========
156+
157+
* :ref:`DROP TEMPORARY FUNCTION <drop_temporary_function>` - Explicitly drop a temporary function
158+
* :ref:`CREATE FUNCTION <create_function>` - Create a permanent function

docs/sphinx/source/reference/sql_commands/DDL/DROP.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ DROP
1010
DROP/SCHEMA_TEMPLATE
1111
DROP/DATABASE
1212
DROP/SCHEMA
13+
DROP/TEMPORARY_FUNCTION
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Diagram(
2+
Terminal('DROP'),
3+
Terminal('TEMPORARY'),
4+
Terminal('FUNCTION'),
5+
Optional(
6+
Sequence(Terminal('IF'), Terminal('EXISTS'))
7+
),
8+
NonTerminal('functionName')
9+
)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
========================
2+
DROP TEMPORARY FUNCTION
3+
========================
4+
5+
.. _drop_temporary_function:
6+
7+
Drops a temporary function within the current transaction.
8+
9+
Syntax
10+
======
11+
12+
.. raw:: html
13+
:file: TEMPORARY_FUNCTION.diagram.svg
14+
15+
.. code-block:: sql
16+
17+
DROP TEMPORARY FUNCTION [IF EXISTS] function_name
18+
19+
Parameters
20+
==========
21+
22+
``IF EXISTS``
23+
If specified, the statement is a no-op when the named temporary function does not exist. Without this clause, dropping a non-existent function raises an error.
24+
25+
``function_name``
26+
The name of the temporary function to drop. Must be a temporary function created in the current transaction.
27+
28+
Examples
29+
========
30+
31+
Drop an existing temporary function:
32+
33+
.. code-block:: sql
34+
35+
DROP TEMPORARY FUNCTION my_temp_func
36+
37+
Drop a temporary function without error if it does not exist:
38+
39+
.. code-block:: sql
40+
41+
DROP TEMPORARY FUNCTION IF EXISTS my_temp_func
42+
43+
Important Notes
44+
===============
45+
46+
Temporary functions are automatically dropped when the transaction commits. ``DROP TEMPORARY FUNCTION`` allows earlier removal within the same transaction — for example, to free the name for redefinition without using ``OR REPLACE``.
47+
48+
See Also
49+
========
50+
51+
* :ref:`CREATE TEMPORARY FUNCTION <create_temporary_function>` - Create a transaction-scoped function
52+
* :ref:`CREATE FUNCTION <create_function>` - Create a permanent function

0 commit comments

Comments
 (0)