Reference documentation and code samples for the BigQuery API class Google::Cloud::Bigquery::Routine.
Routine
A user-defined function or a stored procedure.
Inherits
- Object
Examples
Creating a new routine:
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.create_routine "my_routine" do |r| r.routine_type = "SCALAR_FUNCTION" r.language = "SQL" r.arguments = [ Google::Cloud::Bigquery::Argument.new(name: "x", data_type: "INT64") ] r.body = "x * 3" r.description = "My routine description" end puts routine.routine_id
Extended example:
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.create_routine "my_routine" do |r| r.routine_type = "SCALAR_FUNCTION" r.language = :SQL r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)" r.arguments = [ Google::Cloud::Bigquery::Argument.new( name: "arr", argument_kind: "FIXED_TYPE", data_type: Google::Cloud::Bigquery::StandardSql::DataType.new( type_kind: "ARRAY", array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new( type_kind: "STRUCT", struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new( fields: [ Google::Cloud::Bigquery::StandardSql::Field.new( name: "name", type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING") ), Google::Cloud::Bigquery::StandardSql::Field.new( name: "val", type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64") ) ] ) ) ) ) ] end
Retrieving and updating an existing routine:
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.update do |r| r.body = "x * 4" r.description = "My new routine description" end
Methods
#arguments
def arguments() -> Array<Argument>, nilThe input/output arguments of the routine. Optional.
-
(Array<Argument>, nil) — An array of argument objects, or
nilif the object is a reference (see #reference?).
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" puts "#{routine.routine_id} arguments:" routine.arguments.each do |arguments| puts "* #{arguments.name}" end
#arguments=
def arguments=(new_arguments)Updates the input/output arguments of the routine. Optional.
- new_arguments (Array<Argument>) — The new arguments.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.arguments = [ Google::Cloud::Bigquery::Argument.new(name: "x", data_type: "INT64") ]
#body
def body() -> String, nilThe body of the routine. Required.
For functions (#scalar_function?), this is the expression in the AS clause.
When the routine is a SQL function (#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 definition_body is concat(x, "\n", y) (\n is not replaced with linebreak).
When the routine is a JavaScript function (#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 definition_body is
"return "\n";\n"`
Note that both \n are replaced with linebreaks.
-
(String, nil) — The body of the routine, or
nilif the object is a reference (see #reference?).
#body=
def body=(new_body)Updates the body of the routine. Required.
For functions (#scalar_function?), this is the expression in the AS clause.
When the routine is a SQL function (#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 definition_body is concat(x, "\n", y) (\n is not replaced with linebreak).
When the routine is a JavaScript function (#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 definition_body is
"return "\n";\n"`
Note that both \n are replaced with linebreaks.
- new_body (String) — The new body of the routine.
#created_at
def created_at() -> Time, nilThe time when this routine was created.
-
(Time, nil) — The creation time, or
nilif the object is a reference (see #reference?).
#dataset_id
def dataset_id() -> StringThe ID of the dataset containing this routine.
- (String) — The dataset ID.
#delete
def delete() -> BooleanPermanently deletes the routine.
-
(Boolean) — Returns
trueif the routine was deleted.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.delete
#description
def description() -> String, nilThe description of the routine if defined. Optional. [Experimental]
-
(String, nil) — The routine description, or
nilif the object is a reference (see #reference?).
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.description #=> "My routine description"
#description=
def description=(new_description)Updates the description of the routine. Optional. [Experimental]
- new_description (String) — The new routine description.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.description #=> "My routine description" routine.description = "My updated routine description"
#determinism_level
def determinism_level() -> String, nilThe JavaScript UDF determinism level. Optional.
DETERMINISTIC- Deterministic indicates that two calls with the same input to a UDF yield the same output. If all JavaScript UDFs areDETERMINISTIC, the query result is potentially cachable.NOT_DETERMINISTIC- Not deterministic indicates that the output of the UDF is not guaranteed to yield the same output each time for a given set of inputs. If any JavaScript UDF isNOT_DETERMINISTIC, the query result is not cacheable.
Even if a JavaScript UDF is deterministic, many other factors can prevent usage of cached query results. Example factors include but not limited to: DDL/DML, non-deterministic SQL function calls, update of referenced tables/views/UDFs or imported JavaScript libraries. SQL UDFs cannot have determinism specified. Their determinism is automatically determined.
-
(String, nil) — The routine determinism level in upper case, or
nilif not set or the object is a reference (see #reference?).
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.determinism_level #=> "NOT_DETERMINISTIC"
#determinism_level=
def determinism_level=(new_determinism_level)Updates the JavaScript UDF determinism level. Optional.
DETERMINISTIC- Deterministic indicates that two calls with the same input to a UDF yield the same output. If all JavaScript UDFs areDETERMINISTIC, the query result is potentially cachable.NOT_DETERMINISTIC- Not deterministic indicates that the output of the UDF is not guaranteed to yield the same output each time for a given set of inputs. If any JavaScript UDF isNOT_DETERMINISTIC, the query result is not cacheable.
- new_determinism_level (String, nil) — The new routine determinism level in upper case.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.determinism_level #=> "NOT_DETERMINISTIC" routine.determinism_level = "DETERMINISTIC"
#determinism_level_deterministic?
def determinism_level_deterministic?() -> BooleanChecks if the value of #determinism_level is DETERMINISTIC. The default is false.
-
(Boolean) —
truewhenDETERMINISTICand the object is not a reference (see #reference?),falseotherwise.
#determinism_level_not_deterministic?
def determinism_level_not_deterministic?() -> BooleanChecks if the value of #determinism_level is NOT_DETERMINISTIC. The default is false.
-
(Boolean) —
truewhenNOT_DETERMINISTICand the object is not a reference (see #reference?),falseotherwise.
#etag
def etag() -> String, nilThe ETag hash of the routine.
-
(String, nil) — The ETag hash, or
nilif the object is a reference (see #reference?).
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.etag # "etag123456789"
#exists?
def exists?(force: false) -> BooleanDetermines whether the routine exists in the BigQuery service. The
result is cached locally. To refresh state, set force to true.
-
force (Boolean) (defaults to: false) — Force the latest resource representation to be
retrieved from the BigQuery service when
true. Otherwise the return value of this method will be memoized to reduce the number of API calls made to the BigQuery service. The default isfalse.
-
(Boolean) —
truewhen the routine exists in the BigQuery service,falseotherwise.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine", skip_lookup: true routine.exists? #=> true
#imported_libraries
def imported_libraries() -> Array<String>, nilThe list of the Google Cloud Storage URIs of imported JavaScript libraries. Optional. Only used if
#language is JAVASCRIPT (#javascript?).
-
(Array<String>, nil) — A frozen array of Google Cloud Storage URIs, e.g.
["gs://cloud-samples-data/bigquery/udfs/max-value.js"], ornilif the object is a reference (see #reference?).
#imported_libraries=
def imported_libraries=(new_imported_libraries)Updates the list of the Google Cloud Storage URIs of imported JavaScript libraries. Optional. Only used if
#language is JAVASCRIPT (#javascript?).
-
new_imported_libraries (Array<String>, nil) — An array of Google Cloud Storage URIs, e.g.
["gs://cloud-samples-data/bigquery/udfs/max-value.js"].
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.imported_libraries = [ "gs://cloud-samples-data/bigquery/udfs/max-value.js" ]
#javascript?
def javascript?() -> BooleanChecks if the value of #language is JAVASCRIPT. The default is false.
-
(Boolean) —
truewhenJAVASCRIPTand the object is not a reference (see #reference?),falseotherwise.
#language
def language() -> String, nilThe programming language of routine. Optional. Defaults to "SQL".
SQL- SQL language.JAVASCRIPT- JavaScript language.
-
(String, nil) — The language in upper case, or
nilif the object is a reference (see #reference?).
#language=
def language=(new_language)Updates the programming language of routine. Optional. Defaults to "SQL".
SQL- SQL language.JAVASCRIPT- JavaScript language.
- new_language (String) — The new language in upper case.
#modified_at
def modified_at() -> Time, nilThe time when this routine was last modified.
-
(Time, nil) — The last modified time, or
nilif the object is a reference (see #reference?).
#procedure?
def procedure?() -> BooleanChecks if the value of #routine_type is PROCEDURE. The default is false.
-
(Boolean) —
truewhenPROCEDUREand the object is not a reference (see #reference?),falseotherwise.
#project_id
def project_id() -> StringThe ID of the project containing this routine.
- (String) — The project ID.
#reference?
def reference?() -> BooleanWhether the routine was created without retrieving the resource representation from the BigQuery service.
-
(Boolean) —
truewhen the routine is just a local reference object,falseotherwise.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine", skip_lookup: true routine.reference? #=> true routine.reload! routine.reference? #=> false
#refresh!
def refresh!() -> Google::Cloud::Bigquery::RoutineReloads the routine with current data from the BigQuery service.
- (Google::Cloud::Bigquery::Routine) — Returns the reloaded routine.
Skip retrieving the routine from the service, then load it:
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine", skip_lookup: true routine.reload!
#reload!
def reload!() -> Google::Cloud::Bigquery::RoutineReloads the routine with current data from the BigQuery service.
- (Google::Cloud::Bigquery::Routine) — Returns the reloaded routine.
Skip retrieving the routine from the service, then load it:
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine", skip_lookup: true routine.reload!
#resource?
def resource?() -> BooleanWhether the routine was created with a resource representation from the BigQuery service.
-
(Boolean) —
truewhen the routine was created with a resource representation,falseotherwise.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine", skip_lookup: true routine.resource? #=> false routine.reload! routine.resource? #=> true
#resource_full?
def resource_full?() -> BooleanWhether the routine was created with a full resource representation from the BigQuery service.
-
(Boolean) —
truewhen the routine was created with a full resource representation,falseotherwise.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.resource_full? #=> true
#resource_partial?
def resource_partial?() -> BooleanWhether the routine was created with a partial resource representation from the BigQuery service by retrieval through Dataset#routines. See Models: list response for the contents of the partial representation. Accessing any attribute outside of the partial representation will result in loading the full representation.
-
(Boolean) —
truewhen the routine was created with a partial resource representation,falseotherwise.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routines.first routine.resource_partial? #=> true routine.description # Loads the full resource. routine.resource_partial? #=> false
#return_type
def return_type() -> Google::Cloud::Bigquery::StandardSql::DataType, nilThe return type of the routine. Optional if the routine is a SQL function (#sql?); required otherwise.
If absent, the return type is inferred from #body 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.
-
(Google::Cloud::Bigquery::StandardSql::DataType, nil) — The return type in upper case, or
nilif the object is a reference (see #reference?).
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.return_type.type_kind #=> "INT64"
#return_type=
def return_type=(new_return_type)Updates the return type of the routine. Optional if the routine is a SQL function (#sql?); required otherwise.
If absent, the return type is inferred from #body 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.
- new_return_type (Google::Cloud::Bigquery::StandardSql::DataType, String) — The new return type for the routine.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.return_type.type_kind #=> "INT64" routine.return_type = "STRING"
#routine_id
def routine_id() -> StringA unique ID for this routine, without the project name.
-
(String) — The ID must contain only letters (
[A-Za-z]), numbers ([0-9]), or underscores (_). The maximum length is 256 characters.
#routine_type
def routine_type() -> String, nilThe type of routine. Required.
SCALAR_FUNCTION- Non-builtin permanent scalar function.PROCEDURE- Stored procedure.
-
(String, nil) — The type of routine in upper case, or
nilif the object is a reference (see #reference?).
#routine_type=
def routine_type=(new_routine_type)Updates the type of routine. Required.
SCALAR_FUNCTION- Non-builtin permanent scalar function.PROCEDURE- Stored procedure.
- new_routine_type (String) — The new type of the routine in upper case.
#scalar_function?
def scalar_function?() -> BooleanChecks if the value of #routine_type is SCALAR_FUNCTION. The default is true.
-
(Boolean) —
truewhenSCALAR_FUNCTIONand the object is not a reference (see #reference?),falseotherwise.
#sql?
def sql?() -> BooleanChecks if the value of #language is SQL. The default is true.
-
(Boolean) —
truewhenSQLand the object is not a reference (see #reference?),falseotherwise.
#update
def update() { |routine| ... }Updates the routine with changes made in the given block in a single update request. The following attributes may be set: Updater#routine_type=, Updater#language=, Updater#arguments=, Updater#return_type=, Updater#imported_libraries=, Updater#body=, and Updater#description=.
- (routine) — A block for setting properties on the routine.
- routine (Google::Cloud::Bigquery::Routine::Updater) — An updater to set additional properties on the routine.
require "google/cloud/bigquery" bigquery = Google::Cloud::Bigquery.new dataset = bigquery.dataset "my_dataset" routine = dataset.routine "my_routine" routine.update do |r| r.routine_type = "SCALAR_FUNCTION" r.language = "SQL" r.arguments = [ Google::Cloud::Bigquery::Argument.new(name: "x", data_type: "INT64") ] r.body = "x * 3" r.description = "My new routine description" end