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

Add support for altering BigQuery column types #2547

Merged
merged 2 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- Added intersection syntax for model selector ([#2167](https://github.com/fishtown-analytics/dbt/issues/2167), [#2417](https://github.com/fishtown-analytics/dbt/pull/2417))
- Extends model selection syntax with at most n-th parent/children `dbt run --models 3+m1+2` ([#2052](https://github.com/fishtown-analytics/dbt/issues/2052), [#2485](https://github.com/fishtown-analytics/dbt/pull/2485))
- Added support for renaming BigQuery relations ([#2520](https://github.com/fishtown-analytics/dbt/issues/2520), [#2521](https://github.com/fishtown-analytics/dbt/pull/2521))
- Added support for BigQuery authorized views ([#1718](https://github.com/fishtown-analytics/dbt/issues/1718), [#2517](https://github.com/fishtown-analytics/dbt/issues/2517))
- Added support for BigQuery authorized views ([#1718](https://github.com/fishtown-analytics/dbt/issues/1718), [#2517](https://github.com/fishtown-analytics/dbt/pull/2517))
- Added support for altering BigQuery column types ([#2546](https://github.com/fishtown-analytics/dbt/issues/2546), [#2547](https://github.com/fishtown-analytics/dbt/pull/2547))

### Fixes
- Fixed an error in create_adapter_plugins.py script when -dependency arg not passed ([#2507](https://github.com/fishtown-analytics/dbt/issues/2507), [#2508](https://github.com/fishtown-analytics/dbt/pull/2508))
Expand All @@ -20,7 +21,7 @@ Contributors:
- [@alf-mindshift](https://github.com/alf-mindshift) ([#2431](https://github.com/fishtown-analytics/dbt/pull/2431))
- [@scarrucciu](https://github.com/scarrucciu) ([#2508](https://github.com/fishtown-analytics/dbt/pull/2508))
- [@southpolemonkey](https://github.com/southpolemonkey) ([#2511](https://github.com/fishtown-analytics/dbt/issues/2511))
- [@azhard](https://github.com/azhard) ([#2517](https://github.com/fishtown-analytics/dbt/issues/2517), ([#2521](https://github.com/fishtown-analytics/dbt/pull/2521)))
- [@azhard](https://github.com/azhard) ([#2517](https://github.com/fishtown-analytics/dbt/pull/2517), ([#2521](https://github.com/fishtown-analytics/dbt/pull/2521)), [#2547](https://github.com/fishtown-analytics/dbt/pull/2547))

## dbt 0.17.0 (June 08, 2020)

Expand Down
28 changes: 28 additions & 0 deletions plugins/bigquery/dbt/include/bigquery/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,31 @@
{% macro bigquery__rename_relation(from_relation, to_relation) -%}
{% do adapter.rename_relation(from_relation, to_relation) %}
{% endmacro %}

{% macro bigquery__alter_column_type(relation, column_name, new_column_type) -%}
{#
Changing a column's data type using a query requires you to scan the entire table.
The query charges can be significant if the table is very large.

https://cloud.google.com/bigquery/docs/manually-changing-schemas#changing_a_columns_data_type
#}
{% set relation_columns = get_columns_in_relation(relation) %}

{% set sql %}
select
{%- for col in relation_columns -%}
{% if col.column == column_name %}
CAST({{ col.quoted }} AS {{ new_column_type }}) AS {{ col.quoted }}
{%- else %}
{{ col.quoted }}
{%- endif %}
{%- if not loop.last %},{% endif -%}
{%- endfor %}
from {{ relation }}
{% endset %}

{% call statement('alter_column_type') %}
{{ create_table_as(False, relation, sql)}}
{%- endcall %}

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
models:
- name: model
tests:
- is_type:
column_map:
int64_col: ['string', 'not number']
float64_col: ['float', 'number']
numeric_col: ['numeric', 'number']
string_col: ['string', 'not number']
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{ config(materialized='table') }}
select
CAST(1 as int64) as int64_col,
CAST(2.0 as float64) as float64_col,
CAST(3.0 as numeric) as numeric_col,
CAST('3' as string) as string_col,
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Macro to alter a column type
{% macro test_alter_column_type(model_name, column_name, new_column_type) %}
{% set relation = ref(model_name) %}
{{ alter_column_type(relation, column_name, new_column_type) }}
{% endmacro %}
28 changes: 28 additions & 0 deletions test/integration/056_column_type_tests/test_alter_column_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from test.integration.base import DBTIntegrationTest, use_profile
import yaml


class TestAlterColumnTypes(DBTIntegrationTest):
@property
def schema(self):
return '056_alter_column_types'

def run_and_alter_and_test(self, alter_column_type_args):
self.assertEqual(len(self.run_dbt(['run'])), 1)
self.run_dbt(['run-operation', 'test_alter_column_type', '--args', alter_column_type_args])
self.assertEqual(len(self.run_dbt(['test'])), 1)


class TestBigQueryAlterColumnTypes(TestAlterColumnTypes):
@property
def models(self):
return 'bq_models_alter_type'

@use_profile('bigquery')
def test_bigquery_column_types(self):
alter_column_type_args = yaml.safe_dump({
'model_name': 'model',
'column_name': 'int64_col',
'new_column_type': 'string'
})
self.run_and_alter_and_test(alter_column_type_args)