Skip to content

Commit

Permalink
add a column cache to Relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Apr 11, 2019
1 parent 367fce3 commit 56d49f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
19 changes: 17 additions & 2 deletions core/dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,7 @@ def rename_relation(self, from_relation, to_relation):
)

@abc.abstractmethod
@available
def get_columns_in_relation(self, relation):
def _get_columns_in_relation(self, relation):
"""Get a list of the columns in the given Relation.
:param self.Relation relation: The relation to query for.
Expand All @@ -453,6 +452,22 @@ def get_columns_in_relation(self, relation):
'`get_columns_in_relation` is not implemented for this adapter!'
)

@available
def get_columns_in_relation(self, relation):
"""Get a list of the columns in the given Relation. Use the cache if it
is available.
:param self.Relation relation: The relation to query for.
:return: Information about all columns in the given relation.
:rtype: List[self.Column]
"""
columns = relation.column_cache
if columns is None:
columns = self._get_columns_in_relation(relation)
if dbt.flags.USE_CACHE:
relation.column_cache = columns
return columns

@available_deprecated('get_columns_in_relation')
def get_columns_in_table(self, schema, identifier):
"""DEPRECATED: Get a list of the columns in the given table."""
Expand Down
4 changes: 4 additions & 0 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class BaseRelation(APIObject):

PATH_ELEMENTS = ['database', 'schema', 'identifier']

def __init__(self, *args, **kwargs):
super(BaseRelation, self).__init__(*args, **kwargs)
self.column_cache = None

def matches(self, database=None, schema=None, identifier=None):
search = filter_null_values({
'database': database,
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/adapters/sql/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def alter_column_type(self, relation, column_name, new_column_type):
3. Drop the existing column (cascade!)
4. Rename the new column to existing column
"""
relation.column_cache = None
kwargs = {
'relation': relation,
'column_name': column_name,
Expand Down Expand Up @@ -150,7 +151,7 @@ def rename_relation(self, from_relation, to_relation):
kwargs=kwargs
)

def get_columns_in_relation(self, relation):
def _get_columns_in_relation(self, relation):
return self.execute_macro(
GET_COLUMNS_IN_RELATION_MACRO_NAME,
kwargs={'relation': relation}
Expand Down
2 changes: 1 addition & 1 deletion plugins/bigquery/dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def list_schemas(self, database):
include_all=True)
return [ds.dataset_id for ds in all_datasets]

def get_columns_in_relation(self, relation):
def _get_columns_in_relation(self, relation):
try:
table = self.connections.get_bq_table(
database=relation.database,
Expand Down

0 comments on commit 56d49f8

Please sign in to comment.