カテゴリ:

システム関数 (システム情報)

SYSTEM$SET_RETURN_VALUE

タスクの戻り値を明示的に設定します。

タスクの DAG では、タスクはこの関数を呼び出して戻り値を設定できます。このタスクを先行タスクとして識別する別のタスク(タスク定義で AFTER キーワードを使用)は、 SYSTEM$GET_PREDECESSOR_RETURN_VALUE を使用して、先行タスクにより設定された戻り値を取得できます。

構文

SYSTEM$SET_RETURN_VALUE( '<string_expression>' )
Copy

引数

string_expression

戻り値として設定する文字列。文字列のサイズは10 kB以下にする必要があります(UTF8 でエンコードされている場合)。

戻り値を設定するタスクを作成します。先行タスクの完了後に実行される2番目の子タスクを作成します。子タスクは、先行タスクによって設定された戻り値を取得し( SYSTEM$GET_PREDECESSOR_RETURN_VALUE を呼び出すことにより)、テーブル行に挿入します。

-- create a table to store the return values.
create or replace table return_values (str varchar);

-- create a task that sets the return value for the task.
create task set_return_value
  warehouse=return_task_wh
  schedule='1 minute' as
  call system$set_return_value('The quick brown fox jumps over the lazy dog');

-- create a task that identifies the first task as the predecessor task and retrieves the return value set for that task.
create task get_return_value
  warehouse=return_task_wh
  after set_return_value
  as
    insert into return_values values(system$get_predecessor_return_value());


-- Note that if there are multiple predecessor tasks that are enabled, you must specify the name of the task to retrieve the return value for that task.
create task get_return_value_by_pred
  warehouse=return_task_wh
  after set_return_value
  as
    insert into return_values values(system$get_predecessor_return_value('SET_RETURN_VALUE'));

-- resume task (using ALTER TASK ... RESUME).
-- wait for task to run on schedule.

select distinct(str) from return_values;
+-----------------------------------------------+
|                      STR                      |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+

select distinct(RETURN_VALUE)
  from table(information_schema.task_history())
  where RETURN_VALUE is not NULL;

+-----------------------------------------------+
|                  RETURN_VALUE                 |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+
Copy

最初の例に似ていますが、タスクの戻り値を設定し、個別のストアドプロシージャを呼び出して取得します。

-- create a table to store the return values.
create or replace table return_values_sp (str varchar);

-- create a stored procedure that sets the return value for the task.
create or replace procedure set_return_value_sp()
returns string
language javascript
execute as caller
as $$
  var stmt = snowflake.createStatement({sqlText:`call system$set_return_value('The quick brown fox jumps over the lazy dog');`});
  var res = stmt.execute();
$$;

-- create a stored procedure that inserts the return value for the predecessor task into the 'return_values_sp' table.
create or replace procedure get_return_value_sp()
returns string
language javascript
execute as caller
as $$
var stmt = snowflake.createStatement({sqlText:`insert into return_values_sp values(system$get_predecessor_return_value());`});
var res = stmt.execute();
$$;

-- create a task that calls the set_return_value stored procedure.
create task set_return_value_t
warehouse=warehouse1
schedule='1 minute'
as
  call set_return_value_sp();

-- create a task that calls the get_return_value stored procedure.
create task get_return_value_t
warehouse=warehouse1
after set_return_value_t
as
  call get_return_value_sp();

-- resume task.
-- wait for task to run on schedule.

select distinct(str) from return_values_sp;
+-----------------------------------------------+
|                      STR                      |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+

select distinct(RETURN_VALUE)
  from table(information_schema.task_history())
  where RETURN_VALUE is not NULL;

+-----------------------------------------------+
|                  RETURN_VALUE                 |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+
Copy