Functions​

This section describes the DDL commands pertaining to functions.

Create function​

Define a new function.

  1. [ with with-item [, ...] ]
  2. create function name ([ argspec ] [, ... ]) -> returnspec
  3. using ( expr );
  4. [ with with-item [, ...] ]
  5. create function name ([ argspec ] [, ... ]) -> returnspec
  6. using language functionbody ;
  7. [ with with-item [, ...] ]
  8. create function name ([ argspec ] [, ... ]) -> returnspec
  9. "{" subcommand [, ...] "}" ;
  10. where argspec is:
  11. [ argkind ] argname: [ typequal ] argtype [ = default ]
  12. argkind is:
  13. [ { variadic | named only } ]
  14. typequal is:
  15. [ { set of | optional } ]
  16. and returnspec is:
  17. [ typequal ] rettype
  18. and subcommand is one of
  19. set volatility := {'Immutable' | 'Stable' | 'Volatile'} ;
  20. create annotation annotation-name := value ;
  21. using ( expr ) ;
  22. using language functionbody ;

Description​

The command create function defines a new function. If name is qualified with a module name, then the function is created in that module, otherwise it is created in the current module.

The function name must be distinct from that of any existing function with the same argument types in the same module. Functions of different argument types can share a name, in which case the functions are called overloaded functions.

Parameters​

Most sub-commands and options of this command are identical to the SDL function declaration, with some additional features listed below:

set volatility := {‘Immutable’ | ‘Stable’ | ‘Volatile’}

Function volatility determines how aggressively the compiler can optimize its invocations. Other than a slight syntactical difference this is the same as the corresponding SDL declaration.

create annotation annotation-name := value

Set the function’s annotation-name to value.

See create annotation for details.

Examples​

Define a function returning the sum of its arguments:

  1. create function mysum(a: int64, b: int64) -> int64
  2. using (
  3. select a + b
  4. );

The same, but using a variadic argument and an explicit language:

  1. create function mysum(variadic argv: int64) -> int64
  2. using edgeql $$
  3. select sum(array_unpack(argv))
  4. $$;

Define a function using the block syntax:

  1. create function mysum(a: int64, b: int64) -> int64 {
  2. using (
  3. select a + b
  4. );
  5. create annotation title := "My sum function.";
  6. };

Alter function​

Change the definition of a function.

  1. [ with with-item [, ...] ]
  2. alter function name ([ argspec ] [, ... ]) "{"
  3. subcommand [, ...]
  4. "}"
  5. where argspec is:
  6. [ argkind ] argname: [ typequal ] argtype [ = default ]
  7. and subcommand is one of
  8. set volatility := {'Immutable' | 'Stable' | 'Volatile'} ;
  9. reset volatility ;
  10. rename to newname ;
  11. create annotation annotation-name := value ;
  12. alter annotation annotation-name := value ;
  13. drop annotation annotation-name ;
  14. using ( expr ) ;
  15. using language functionbody ;

Description​

The command alter function changes the definition of a function. The command allows to change annotations, the volatility level, and other attributes.

Subcommands​

The following subcommands are allowed in the alter function block in addition to the commands common to the create function:

reset volatility

Remove explicitly specified volatility in favor of the volatility inferred from the function body.

rename to newname

Change the name of the function to newname.

alter annotation annotation-name;

Alter function annotation-name. See alter annotation for details.

drop annotation annotation-name;

Remove function annotation-name. See drop annotation for details.

reset errmessage;

Remove the error message from this abstract constraint. The error message specified in the base abstract constraint will be used instead.

Example​

  1. create function mysum(a: int64, b: int64) -> int64 {
  2. using (
  3. select a + b
  4. );
  5. create annotation title := "My sum function.";
  6. };
  7. alter function mysum(a: int64, b: int64) {
  8. set volatility := 'Immutable';
  9. DROP ANNOTATION title;
  10. };
  11. alter function mysum(a: int64, b: int64) {
  12. using (
  13. select (a + b) * 100
  14. )
  15. };

Drop function​

Remove a function.

  1. [ with with-item [, ...] ]
  2. drop function name ([ argspec ] [, ... ]);
  3. where argspec is:
  4. [ argkind ] argname: [ typequal ] argtype [ = default ]

Description​

The command drop function removes the definition of an existing function. The argument types to the function must be specified, since there can be different functions with the same name.

Parameters​

name

The name (optionally module-qualified) of an existing function.

argname

The name of an argument used in the function definition.

argmode

The mode of an argument: set of or optional or variadic.

argtype

The data type(s) of the function’s arguments (optionally module-qualified), if any.

Example​

Remove the mysum function:

  1. drop function mysum(a: int64, b: int64);

See also

Schema > Functions

SDL > Functions

Reference > Function calls

Introspection > Functions

Cheatsheets > Functions