Categories:

System functions (System Information)

SYSTEM$DBT_GET_LAST_FAILED_RUN_TARGET

Returns the location of dbt artifacts from the most recent failed execution of a dbt project object within the previous 14 days. You can import these artifacts into another dbt project execution and use them with --state.

Syntax

SYSTEM$DBT_GET_LAST_FAILED_RUN_TARGET (
  '<object_name>'
  [ , '<commands>' [ , '<target_path>' ] ]
)

Arguments

object_name

String that specifies the fully qualified name of the dbt project object whose dbt artifacts you want to retrieve.

commands

Optional comma-separated list of dbt commands to include when searching execution history. The default list is compile, build, run, and docs generate.

For a compound command, specify the command and its subcommand, such as docs generate or source freshness. To retrieve source freshness results, pass source freshness explicitly. For an example, see Retrieve source freshness results.

target_path

Optional target path to match. When specified, the function considers only executions that used that target path.

Returns

Returns the location of dbt artifacts produced by the object’s most recent failed qualifying execution. Use the location with the IMPORTS parameter of EXECUTE DBT PROJECT or the Snowflake CLI --import option.

Returns NULL if no qualifying failed execution with a populated target directory is available in the lookback window.

The location contains standalone target artifacts such as manifest.json, run_results.json, and sources.json when present. The function doesn’t generate missing files or return dbt_artifacts.zip. When you import the location AS 'state', Snowflake mounts the target artifacts at ./imports/state. For the full archive from a known query, use SYSTEM$LOCATE_DBT_ARCHIVE.

Access control requirements

The role used to call this function must have the MONITOR privilege on the dbt project object.

Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent 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.

Usage notes

  • The dbt project object must have a failed execution with a populated target directory within the previous 14 days.
  • Deployment-time auto compile doesn’t create dbt artifacts for this function. Complete a qualifying execution after deployment.
  • --import or IMPORTS makes the returned dbt artifacts available to an execution. You must also point dbt to the mounted directory with --state.
  • To retrieve dbt artifacts for a specific query ID instead, use SYSTEM$LOCATE_DBT_ARTIFACTS or SYSTEM$LOCATE_DBT_ARCHIVE.

Examples

Return the location of dbt artifacts from the most recent failed run or build execution:

SELECT SYSTEM$DBT_GET_LAST_FAILED_RUN_TARGET(
  'prod_database.dbt_projects.production_project',
  'run,build'
);

Import the returned dbt artifacts as state, which mounts them at ./imports/state, and explicitly rerun errored resources and their downstream dependencies:

EXECUTE DBT PROJECT prod_database.dbt_projects.production_project
  ARGS = 'run --state ./imports/state --select result:error+'
  IMPORTS = (
    SYSTEM$DBT_GET_LAST_FAILED_RUN_TARGET(
      'prod_database.dbt_projects.production_project',
      'run,build'
    ) AS 'state'
  );

Import dbt artifacts from the most recent failed execution that used --target-path my_target. To retain the default command filter when you specify a target path, pass the default command list as the second argument:

EXECUTE DBT PROJECT prod_database.dbt_projects.production_project
  ARGS = 'run --state ./imports/state --select result:error+'
  IMPORTS = (
    SYSTEM$DBT_GET_LAST_FAILED_RUN_TARGET(
      'prod_database.dbt_projects.production_project',
      'compile,build,run,docs generate',
      'my_target'
    ) AS 'state'
  );

In Snowsight, open the failed query in Query History, then select Re-run in the dbt Output section and select From last failed run target to execute a statement that reruns the models that errored and their downstream dependencies.

To rerun failed resources from a specific execution instead of the most recent one, look up its query ID and use SYSTEM$LOCATE_DBT_ARTIFACTS. For an example, see Rerun failed models from a specific execution.