From 46a8df2bb9cfe234980730954a6e18e172704769 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Thu, 29 Mar 2018 21:37:07 -0400 Subject: [PATCH] Support cross-database column lookups Fixes: https://github.com/fishtown-analytics/dbt/issues/679 --- dbt/adapters/bigquery.py | 5 ++++- dbt/adapters/default.py | 14 +++++++++----- dbt/adapters/redshift.py | 5 ++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/dbt/adapters/bigquery.py b/dbt/adapters/bigquery.py index 339ed772f73..6d50764024a 100644 --- a/dbt/adapters/bigquery.py +++ b/dbt/adapters/bigquery.py @@ -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') diff --git a/dbt/adapters/default.py b/dbt/adapters/default.py index 05d63f68e85..68cfb889fdc 100644 --- a/dbt/adapters/default.py +++ b/dbt/adapters/default.py @@ -192,12 +192,14 @@ 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, @@ -205,19 +207,21 @@ def _get_columns_in_table_sql(cls, schema_name, table_name): 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) diff --git a/dbt/adapters/redshift.py b/dbt/adapters/redshift.py index 0286e90cf93..e9217c0f4f1 100644 --- a/dbt/adapters/redshift.py +++ b/dbt/adapters/redshift.py @@ -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'