diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c066530c11..3600952defd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ - Fix variable `_dbt_max_partition` declaration and initialization for BigQuery incremental models ([#2940](https://github.com/fishtown-analytics/dbt/issues/2940), [#2976](https://github.com/fishtown-analytics/dbt/pull/2976)) - Moving from 'master' to 'HEAD' default branch in git ([#3057](https://github.com/fishtown-analytics/dbt/issues/3057), [#3104](https://github.com/fishtown-analytics/dbt/issues/3104), [#3117](https://github.com/fishtown-analytics/dbt/issues/3117))) - Requirement on `dataclasses` is relaxed to be between `>=0.6,<0.9` allowing dbt to cohabit with other libraries which required higher versions. ([#3150](https://github.com/fishtown-analytics/dbt/issues/3150), [#3151](https://github.com/fishtown-analytics/dbt/pull/3151)) -- Raise a proper error message if dbt parses a macro twice due to macro duplication or misconfiguration. ([#2449](https://github.com/fishtown-analytics/dbt/issues/2449)) +- Add feature to add `_n` alias to same column names in SQL query ([#3147](https://github.com/fishtown-analytics/dbt/issues/3147), [#3158](https://github.com/fishtown-analytics/dbt/pull/3158)) +- Raise a proper error message if dbt parses a macro twice due to macro duplication or misconfiguration. ([#2449](https://github.com/fishtown-analytics/dbt/issues/2449), [#3165](https://github.com/fishtown-analytics/dbt/pull/3165)) ### Features - Add optional configs for `require_partition_filter` and `partition_expiration_days` in BigQuery ([#1843](https://github.com/fishtown-analytics/dbt/issues/1843), [#2928](https://github.com/fishtown-analytics/dbt/pull/2928)) @@ -26,6 +27,7 @@ Contributors: - [@pcasteran](https://github.com/pcasteran) ([#2976](https://github.com/fishtown-analytics/dbt/pull/2976)) - [@VasiliiSurov](https://github.com/VasiliiSurov) ([#3104](https://github.com/fishtown-analytics/dbt/pull/3104)) - [@bastienboutonnet](https://github.com/bastienboutonnet) ([#3151](https://github.com/fishtown-analytics/dbt/pull/3151)) +- [@techytushar](https://github.com/techytushar) ([#3158](https://github.com/fishtown-analytics/dbt/pull/3158)) - [@cgopalan](https://github.com/cgopalan) ([#3165](https://github.com/fishtown-analytics/dbt/pull/3165)) ## dbt 0.19.1 (Release TBD) diff --git a/core/dbt/adapters/sql/connections.py b/core/dbt/adapters/sql/connections.py index c49fae3a323..11d8dce773a 100644 --- a/core/dbt/adapters/sql/connections.py +++ b/core/dbt/adapters/sql/connections.py @@ -99,7 +99,14 @@ def process_results( column_names: Iterable[str], rows: Iterable[Any] ) -> List[Dict[str, Any]]: - + unique_col_names = dict() + for idx in range(len(column_names)): + col_name = column_names[idx] + if col_name in unique_col_names: + unique_col_names[col_name] += 1 + column_names[idx] = f'{col_name}_{unique_col_names[col_name]}' + else: + unique_col_names[column_names[idx]] = 1 return [dict(zip(column_names, row)) for row in rows] @classmethod diff --git a/test/unit/test_sql_result.py b/test/unit/test_sql_result.py new file mode 100644 index 00000000000..9041c9417d2 --- /dev/null +++ b/test/unit/test_sql_result.py @@ -0,0 +1,18 @@ +import unittest +from dbt.adapters.sql.connections import SQLConnectionManager + +class TestProcessSQLResult(unittest.TestCase): + def test_duplicated_columns(self): + cols_with_one_dupe = ['a', 'b', 'a', 'd'] + rows = [(1, 2, 3, 4)] + self.assertEqual( + SQLConnectionManager.process_results(cols_with_one_dupe, rows), + [{"a": 1, "b": 2, "a_2": 3, "d": 4}] + ) + + cols_with_more_dupes = ['a', 'a', 'a', 'b'] + rows = [(1, 2, 3, 4)] + self.assertEqual( + SQLConnectionManager.process_results(cols_with_more_dupes, rows), + [{"a": 1, "a_2": 2, "a_3": 3, "b": 4}] + )