sqlsnowflake-cloud-data-platformdbt

Return specific column from Snowflake `show tables` command


I am trying to return column: cluster_by using a snowflake query as below with a dbt macro. The macro will be used to infer if a model (and correct target relation) has a clustering property.

I have this code as a start, and tried reading snowflake documentation but their docs are for snowflake tables as sources, not a show tables command

DECLARE
    res RESULTSET DEFAULT (execute immediate 'show tables like ''obj_name''');
    BEGIN
      return table(res);
    END

Currently it returns the entire table, how do I change this so that I only return column cluster_by

expected result:

| cluster_by | 
| ---------- | 
| column     |

Have looked at result_set documentation on Snowflake but their documentation is for tables rather than a result set from the show tables... command


Solution

  • here's example:

    {% macro get_cluster_by(table_pattern) %}
      {% set res_query %}
        SHOW TABLES LIKE '{{ table_pattern }}'
      {% endset %}
    
      {% set res_id_query %}
        SELECT
          "query_id"
        FROM
          table(information_schema.query_history(end_time_range_start => DATEADD('minute', -5, CURRENT_TIMESTAMP())))
        WHERE
          "query_text" = '{{ res_query }}'
        ORDER BY
          "start_time" DESC
        LIMIT 1
      {% endset %}
    
      {% set res_id = run_query(res_id_query).columns[0].values()[0] %}
    
      {% set cluster_by_query %}
        SELECT
          "cluster_by"
        FROM
          table(result_scan('{{ res_id }}'))
      {% endset %}
    
      {% set result = run_query(cluster_by_query) %}
      {{ return(result) }}
    {% endmacro %}
    

    then call the macro in your dbt project as:

    {% set cluster_info = get_cluster_by('obj_name') %}