Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use "show objects in schema" (#2174) #2322

Merged
merged 2 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added support for `db_groups` and `autocreate` flags in Redshift configurations. ([#1995](https://github.com/fishtown-analytics/dbt/issues/1995), [#2262](https://github.com/fishtown-analytics/dbt/pull/2262))
- Users can supply paths as arguments to `--models` and `--select`, either explicitily by prefixing with `path:` or implicitly with no prefix. ([#454](https://github.com/fishtown-analytics/dbt/issues/454), [#2258](https://github.com/fishtown-analytics/dbt/pull/2258))
- dbt now builds the relation cache for "dbt compile" and "dbt ls" as well as "dbt run" ([#1705](https://github.com/fishtown-analytics/dbt/issues/1705), [#2319](https://github.com/fishtown-analytics/dbt/pull/2319))
- Snowflake now uses "show terse objects" to build the relations cache instead of selecting from the information schema ([#2174](https://github.com/fishtown-analytics/dbt/issues/2174), [#2322](https://github.com/fishtown-analytics/dbt/pull/2322))

### Fixes
- When a jinja value is undefined, give a helpful error instead of failing with cryptic "cannot pickle ParserMacroCapture" errors ([#2110](https://github.com/fishtown-analytics/dbt/issues/2110), [#2184](https://github.com/fishtown-analytics/dbt/pull/2184))
Expand All @@ -28,7 +29,7 @@ Contributors:
- [@rodrigodelmonte](https://github.com/rodrigodelmonte) [#2298](https://github.com/fishtown-analytics/dbt/pull/2298)
- [@sumanau7](https://github.com/sumanau7) ([#2279](https://github.com/fishtown-analytics/dbt/pull/2279), [#2263](https://github.com/fishtown-analytics/dbt/pull/2263), [#2297](https://github.com/fishtown-analytics/dbt/pull/2297))

## dbt 0.16.1 (Relase date TBD)
## dbt 0.16.1 (April 14, 2020)

### Features
- Support for appending query comments to SQL queries. ([#2138](https://github.com/fishtown-analytics/dbt/issues/2138) [#2199](https://github.com/fishtown-analytics/dbt/issues/2199))
Expand Down
45 changes: 44 additions & 1 deletion plugins/snowflake/dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import agate

from dbt.adapters.sql import SQLAdapter
from dbt.adapters.sql.impl import LIST_SCHEMAS_MACRO_NAME
from dbt.adapters.sql.impl import (
LIST_SCHEMAS_MACRO_NAME,
LIST_RELATIONS_MACRO_NAME,
)
from dbt.adapters.snowflake import SnowflakeConnectionManager
from dbt.adapters.snowflake import SnowflakeRelation
from dbt.adapters.snowflake import SnowflakeColumn
Expand Down Expand Up @@ -99,3 +102,43 @@ def list_schemas(self, database: str) -> List[str]:
# want is 'name'

return [row['name'] for row in results]

def list_relations_without_caching(
self, information_schema, schema
) -> List[SnowflakeRelation]:
kwargs = {'information_schema': information_schema, 'schema': schema}
try:
results = self.execute_macro(
LIST_RELATIONS_MACRO_NAME,
kwargs=kwargs
)
except DatabaseException as exc:
# if the schema doesn't exist, we just want to return.
# Alternatively, we could query the list of schemas before we start
# and skip listing the missing ones, which sounds expensive.
if 'Object does not exist' in str(exc):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a really good call!

return []
raise

relations = []
quote_policy = {
'database': True,
'schema': True,
'identifier': True
}

columns = ['database_name', 'schema_name', 'name', 'kind']
for _database, _schema, _identifier, _type in results.select(columns):
try:
_type = self.Relation.get_relation_type(_type.lower())
except ValueError:
_type = self.Relation.External
relations.append(self.Relation.create(
database=_database,
schema=_schema,
identifier=_identifier,
quote_policy=quote_policy,
type=_type
))

return relations
32 changes: 16 additions & 16 deletions plugins/snowflake/dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@


{% macro snowflake__list_relations_without_caching(information_schema, schema) %}
{% call statement('list_relations_without_caching', fetch_result=True) -%}
select
table_catalog as database,
table_name as name,
table_schema as schema,
case when table_type = 'BASE TABLE' then 'table'
when table_type = 'VIEW' then 'view'
when table_type = 'MATERIALIZED VIEW' then 'materializedview'
when table_type = 'EXTERNAL TABLE' then 'external'
else table_type
end as table_type
from {{ information_schema }}.tables
where table_schema ilike '{{ schema }}'
and table_catalog ilike '{{ information_schema.database.lower() }}'
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{%- set db_name = adapter.quote_as_configured(information_schema.database, 'database') -%}
{%- set schema_name = adapter.quote_as_configured(schema, 'schema') -%}
{%- set sql -%}
show terse objects in {{ db_name }}.{{ schema_name }}
{%- endset -%}

{%- set result = run_query(sql) -%}
{% set maximum = 10000 %}
{% if (result | length) >= maximum %}
{% set msg %}
Too many schemas in schema {{ database }}.{{ schema }}! dbt can only get
information about schemas with fewer than {{ maximum }} objects.
{% endset %}
{% do exceptions.raise_compiler_error(msg) %}
{% endif %}
{%- do return(result) -%}
{% endmacro %}


Expand Down