Skip to content

Commit

Permalink
Support cross-database column lookups
Browse files Browse the repository at this point in the history
Fixes: #679
  • Loading branch information
drewbanin committed Mar 30, 2018
1 parent af9b3bb commit 46a8df2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion dbt/adapters/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,10 @@ def get_existing_schemas(cls, profile, model_name=None):

@classmethod
def get_columns_in_table(cls, profile, schema_name, table_name,
model_name=None):
database=None, model_name=None):

# BigQuery does not have databases -- the database parameter is here
# for consistency with the base implementation

conn = cls.get_connection(profile, model_name)
client = conn.get('handle')
Expand Down
14 changes: 9 additions & 5 deletions dbt/adapters/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,32 +192,36 @@ def get_missing_columns(cls, profile,
if col_name in missing_columns]

@classmethod
def _get_columns_in_table_sql(cls, schema_name, table_name):
def _get_columns_in_table_sql(cls, schema_name, table_name, database):

schema_filter = '1=1'
if schema_name is not None:
schema_filter = "table_schema = '{}'".format(schema_name)

db_prefix = '' if database is None else '{}.'format(database)

sql = """
select
column_name,
data_type,
character_maximum_length,
numeric_precision || ',' || numeric_scale as numeric_size
from information_schema.columns
from {db_prefix}information_schema.columns
where table_name = '{table_name}'
and {schema_filter}
order by ordinal_position
""".format(table_name=table_name, schema_filter=schema_filter).strip()
""".format(db_prefix=db_prefix,
table_name=table_name,
schema_filter=schema_filter).strip()

return sql

@classmethod
def get_columns_in_table(cls, profile, schema_name, table_name,
model_name=None):
database=None, model_name=None):

sql = cls._get_columns_in_table_sql(schema_name, table_name)
sql = cls._get_columns_in_table_sql(schema_name, table_name, database)
connection, cursor = cls.add_query(
profile, sql, model_name)

Expand Down
5 changes: 4 additions & 1 deletion dbt/adapters/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def date_function(cls):
return 'getdate()'

@classmethod
def _get_columns_in_table_sql(cls, schema_name, table_name):
def _get_columns_in_table_sql(cls, schema_name, table_name, database):
# Redshift doesn't support cross-database queries,
# so we can ignore the `database` argument

# TODO : how do we make this a macro?
if schema_name is None:
table_schema_filter = '1=1'
Expand Down

0 comments on commit 46a8df2

Please sign in to comment.