REST Resource: routines

Resource: Routine

A user-defined function or a stored procedure.

JSON representation
{
  "etag": string,
  "routineReference": {
    object (RoutineReference)
  },
  "routineType": enum (RoutineType),
  "creationTime": string,
  "lastModifiedTime": string,
  "language": enum (Language),
  "arguments": [
    {
      object (Argument)
    }
  ],
  "returnType": {
    object (StandardSqlDataType)
  },
  "returnTableType": {
    object (StandardSqlTableType)
  },
  "importedLibraries": [
    string
  ],
  "definitionBody": string,
  "description": string,
  "determinismLevel": enum (DeterminismLevel),
  "remoteFunctionOptions": {
    object (RemoteFunctionOptions)
  },
  "sparkOptions": {
    object (SparkOptions)
  },
  "dataGovernanceType": enum (DataGovernanceType)
}
Fields
etag

string

Output only. A hash of this resource.

routineReference

object (RoutineReference)

Required. Reference describing the ID of this routine.

routineType

enum (RoutineType)

Required. The type of routine.

creationTime

string (int64 format)

Output only. The time when this routine was created, in milliseconds since the epoch.

lastModifiedTime

string (int64 format)

Output only. The time when this routine was last modified, in milliseconds since the epoch.

language

enum (Language)

Optional. Defaults to "SQL" if remoteFunctionOptions field is absent, not set otherwise.

arguments[]

object (Argument)

Optional.

returnType

object (StandardSqlDataType)

Optional if language = "SQL"; required otherwise. Cannot be set if routineType = "TABLE_VALUED_FUNCTION".

If absent, the return type is inferred from definitionBody at query time in each query that references this routine. If present, then the evaluated result will be cast to the specified returned type at query time.

For example, for the functions created with the following statements:

  • CREATE FUNCTION Add(x FLOAT64, y FLOAT64) RETURNS FLOAT64 AS (x + y);

  • CREATE FUNCTION Increment(x FLOAT64) AS (Add(x, 1));

  • CREATE FUNCTION Decrement(x FLOAT64) RETURNS FLOAT64 AS (Add(x, -1));

The returnType is {typeKind: "FLOAT64"} for Add and Decrement, and is absent for Increment (inferred as FLOAT64 at query time).

Suppose the function Add is replaced by CREATE OR REPLACE FUNCTION Add(x INT64, y INT64) AS (x + y);

Then the inferred return type of Increment is automatically changed to INT64 at query time, while the return type of Decrement remains FLOAT64.

returnTableType

object (StandardSqlTableType)

Optional. Can be set only if routineType = "TABLE_VALUED_FUNCTION".

If absent, the return table type is inferred from definitionBody at query time in each query that references this routine. If present, then the columns in the evaluated table result will be cast to match the column types specified in return table type, at query time.

importedLibraries[]

string

Optional. If language = "JAVASCRIPT", this field stores the path of the imported JAVASCRIPT libraries.

definitionBody

string

Required. The body of the routine.

For functions, this is the expression in the AS clause.

If language=SQL, it is the substring inside (but excluding) the parentheses. For example, for the function created with the following statement:

CREATE FUNCTION JoinLines(x string, y string) as (concat(x, "\n", y))

The definitionBody is concat(x, "\n", y) (\n is not replaced with linebreak).

If language=JAVASCRIPT, it is the evaluated string in the AS clause. For example, for the function created with the following statement:

CREATE FUNCTION f() RETURNS STRING LANGUAGE js AS 'return "\n";\n'

The definitionBody is

return "\n";\n

Note that both \n are replaced with linebreaks.

description

string

Optional. The description of the routine, if defined.

determinismLevel

enum (DeterminismLevel)

Optional. The determinism level of the JavaScript UDF, if defined.

remoteFunctionOptions

object (RemoteFunctionOptions)

Optional. Remote function specific options.

sparkOptions

object (SparkOptions)

Optional. Spark specific options.

dataGovernanceType

enum (DataGovernanceType)

Optional. If set to DATA_MASKING, the function is validated and made available as a masking function. For more information, see Create custom masking routines.

RoutineReference

Id path of a routine.

JSON representation
{
  "projectId": string,
  "datasetId": string,
  "routineId": string
}
Fields
projectId

string

Required. The ID of the project containing this routine.

datasetId

string

Required. The ID of the dataset containing this routine.

routineId

string

Required. The ID of the routine. The ID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum length is 256 characters.

RoutineType

The fine-grained type of the routine.

Enums
ROUTINE_TYPE_UNSPECIFIED Default value.
SCALAR_FUNCTION Non-built-in persistent scalar function.
PROCEDURE Stored procedure.
TABLE_VALUED_FUNCTION Non-built-in persistent TVF.

Language

The language of the routine.

Enums
LANGUAGE_UNSPECIFIED Default value.
SQL SQL language.
JAVASCRIPT JavaScript language.
PYTHON Python language.
JAVA Java language.
SCALA Scala language.

Argument

Input/output argument of a function or a stored procedure.

JSON representation
{
  "name": string,
  "argumentKind": enum (ArgumentKind),
  "mode": enum (Mode),
  "dataType": {
    object (StandardSqlDataType)
  }
}
Fields
name

string

Optional. The name of this argument. Can be absent for function return argument.

argumentKind

enum (ArgumentKind)

Optional. Defaults to FIXED_TYPE.

mode

enum (Mode)

Optional. Specifies whether the argument is input or output. Can be set for procedures only.

dataType