A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.snowflake.com/en/sql-reference/sql/create-function-spcs below:

Website Navigation


CREATE FUNCTION (Snowpark Container Services)

CREATE FUNCTION (Snowpark Container Services)

Creates a service function.

This command supports the following variants:

See also:

Service functions, CREATE EXTERNAL FUNCTION, DESC FUNCTION, DROP FUNCTION, ALTER FUNCTION, CREATE OR ALTER <object>

Syntax
CREATE [ OR REPLACE ] FUNCTION <name> ( [ <arg_name> <arg_data_type> ] [ , ... ] )
  RETURNS <result_data_type>
  [ [ NOT ] NULL ]
  [ { CALLED ON NULL INPUT | { RETURNS NULL ON NULL INPUT | STRICT } } ]
  [ { VOLATILE | IMMUTABLE } ]
  SERVICE = <service_name>
  ENDPOINT = <endpoint_name>
  [ COMMENT = '<string_literal>' ]
  [ CONTEXT_HEADERS = ( <context_function_1> [ , <context_function_2> ...] ) ]
  [ MAX_BATCH_ROWS = <integer> ]
  [ MAX_BATCH_RETRIES = <integer> ]
  [ ON_BATCH_FAILURE = { ABORT | RETURN_NULL } ]
  [ BATCH_TIMEOUT_SECS = <integer> ]
  AS '<http_path_to_request_handler>'

Copy

Variant syntax CREATE OR ALTER FUNCTION (Snowpark Container Services)

Creates a new service function if it doesn’t already exist, or transforms an existing service function into the service function defined in the statement. A CREATE OR ALTER FUNCTION (Snowpark Container Services) statement follows the syntax rules of a CREATE FUNCTION (Snowpark Container Services) statement and has the same limitations as an ALTER FUNCTION (Snowpark Container Services) statement.

Supported function alterations include changes to the following:

For more information, see CREATE OR ALTER FUNCTION (Snowpark Container Services) usage notes.

CREATE [ OR ALTER ] FUNCTION ...

Copy

Required parameters
name

Specifies the identifier (name) and any input arguments for the function.

( [ arg_name arg_data_type ] [ , ... ] )

Specifies the arguments/inputs for the service function. These should correspond to the arguments that the service expects.

If there are no arguments, then include the parentheses without any argument name(s) and data type(s).

RETURNS result_data_type

Specifies the data type of the result returned by the function.

SERVICE = service_name

Specifies the name of the Snowpark Container Services service.

ENDPOINT = endpoint_name

Specifies the name of the endpoint as defined in the service specification.

AS http_path_to_request_handler

Specifies the HTTP path to the service code that is executed when the function is called.

Optional parameters
[ [ NOT ] NULL ]

Specifies whether the function can return NULL values or must return only NON-NULL values. The default is NULL (that is, the function can return NULL).

CALLED ON NULL INPUT or . { RETURNS NULL ON NULL INPUT | STRICT }

Specifies the behavior of the function when called with null inputs. In contrast to system-defined functions, which always return null when any input is null, functions can handle null inputs, returning non-null values even when an input is null:

Default: CALLED ON NULL INPUT

{ VOLATILE | IMMUTABLE }

Specifies the behavior of the function when returning results:

  • VOLATILE: function might return different values for different rows, even for the same input (for example, due to non-determinism and statefulness).

  • IMMUTABLE: function assumes that the function, when called with the same inputs, will always return the same result. This guarantee is not checked. Specifying IMMUTABLE for a function that returns different values for the same input will result in undefined behavior.

Default: VOLATILE

MAX_BATCH_ROWS = integer

Specifies the batch size when sending data to a service to increase concurrency

MAX_BATCH_RETRIES = integer

Specifies the number of times you want Snowflake to retry a failed batch.

Default: 3

ON_BATCH_FAILURE = { ABORT | RETURN_NULL }

Specifies the behavior of the function after Snowflake reaches the maximum number of retries processing the batch.

Default: ABORT

BATCH_TIMEOUT_SECS = integer

Specifies the maximum duration for processing a single batch of rows, including retries (and polling for async function requests), after which Snowflake should terminate the batch request.

Acceptable Values: greater than 0 and less than or equal to 604800 seconds (7 days).

Default: 604800 seconds (7 days)

COMMENT = 'string_literal'

Specifies a comment for the function, which is displayed in the DESCRIPTION column in the SHOW FUNCTIONS and SHOW USER FUNCTIONS output.

Default: user-defined function

CONTEXT_HEADERS = ( context_function_1 [ , context_function_2 ...] )

This binds Snowflake context function results to HTTP headers. (For more information about Snowflake context functions, see: Context functions.)

Not all context functions are supported in context headers. The following are supported:

When function names are listed in the CONTEXT_HEADERS clause, the function names should not be quoted.

Snowflake prepends sf-context to the header before it’s written to the HTTP request.

Example:

CONTEXT_HEADERS = (current_timestamp)

Copy

In this example, Snowflake writes the header sf-context-current-timestamp into the HTTP request.

Context functions can generate characters that are illegal in HTTP header values, including (but not limited to) the following:

Snowflake replaces each sequence of one or more illegal characters with one space character. (The replacement is per sequence, not per character.)

For example, suppose that the context function CURRENT_STATEMENT() returns the following:

select
  /*ÄÎß묱©®*/
  my_service_function(1);

Copy

The value sent in sf-context-current-statement is the following:

select /* */ my_service_function(1);

Copy

To ensure that your service code can access the original result (with illegal characters) from the context function even if illegal characters have been replaced, Snowflake also sends a binary context header that contains the context function result encoded in base64.

In the example above, the value sent in the base64-encoded header is the result of the following call:

base64_encode('select\n/ÄÎß묱©®/\nmy_service_function(1)')

Copy

The remote service is responsible for decoding the base64 value if needed.

Each such base64 header is named according to the following convention:

sf-context-<context-function>-base64

Copy

In the example above, the name of the header would be the following:

sf-context-current-statement-base64

Copy

If no context headers are sent, then no base64 context headers are sent.

If the rows sent to a service function are split across multiple batches, then all batches contain the same context headers and the same binary context headers.

Access control requirements

A role used to execute this operation must have the following privileges at a minimum:

Privilege

Object

Notes

CREATE FUNCTION

Schema

USAGE

Service Endpoint

Usage on a service endpoint is granted to service roles defined in the service specification. You then grant the service role to the role creating the service function.

The USAGE privilege on the parent database and schema are required to perform operations on any object in a schema.

For instructions on creating a custom role with a specified set of privileges, see Creating custom roles.

For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

General usage notes CREATE OR ALTER FUNCTION (Snowpark Container Services) usage notes

The following alterations are not supported:

Examples Create a simple service function

In Tutorial-1, you create the following service function:

CREATE FUNCTION my_echo_udf (InputText VARCHAR)
  RETURNS VARCHAR
  SERVICE=echo_service
  ENDPOINT=echoendpoint
  AS '/echo';

Copy

This function connects with the specific ENDPOINT of the specified SERVICE. When you invoke this function, Snowflake sends a request to the /echo path inside the service container.

Note the following:

Alter a service function using the CREATE OR ALTER FUNCTION (Snowpark Container Services) command

Alter a function my_echo_udf to set the maximum number of batch rows to 100, and add a context header and endpoint:

CREATE OR ALTER FUNCTION my_echo_udf (InputText VARCHAR)
  RETURNS VARCHAR
  SERVICE = echo_service
  ENDPOINT = reverse_echoendpoint
  CONTEXT_HEADERS = (current_account)
  MAX_BATCH_ROWS = 100
  AS '/echo';

Copy


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4