Skip to content

Commit

Permalink
Merge pull request #2 from cadl/hudi-primary-keys
Browse files Browse the repository at this point in the history
fix: build error primarykey
  • Loading branch information
cadl authored Aug 25, 2022
2 parents 5c0fda2 + b2e038d commit 227fe57
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion dbt/include/spark/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
{%- set options = config.get('options') -%}
{%- if config.get('file_format') == 'hudi' -%}
{%- set unique_key = config.get('unique_key') -%}
{%- set expected_options_primarykey = unique_key|join(',') if unique_key is sequence else unique_key -%}
{%- set expected_options_primarykey = unique_key|join(',') if unique_key is sequence and (unique_key is not string and unique_key is not mapping) else unique_key -%}

{%- if unique_key is not none and options is none -%}
{%- set options = {'primaryKey': expected_options_primarykey} -%}
{%- elif unique_key is not none and options is not none and 'primaryKey' not in options -%}
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,25 @@ def test_macros_create_table_as_hudi_options(self):
sql = self.__run_macro(template, 'spark__create_table_as', False, 'my_table', 'select 1 as id').strip()
self.assertEqual(sql, 'create table my_table using hudi options (primaryKey "id" ) as select 1 as id')

self.config['file_format'] = 'hudi'
self.config['unique_key'] = ['a', 'b']
self.config['options'] = {}
sql = self.__run_macro(template, 'spark__create_table_as', False, 'my_table', 'select 1 as a, 1 as b').strip()
self.assertEqual(sql, 'create table my_table using hudi options (primaryKey "a,b" ) as select 1 as a, 1 as b')

self.config['file_format'] = 'hudi'
self.config['unique_key'] = ['a', 'b']
self.config['options'] = {'primaryKey': 'a,b'}
sql = self.__run_macro(template, 'spark__create_table_as', False, 'my_table', 'select 1 as a, 1 as b').strip()
self.assertEqual(sql, 'create table my_table using hudi options (primaryKey "a,b" ) as select 1 as a, 1 as b')

self.config['file_format'] = 'hudi'
self.config['unique_key'] = 'uuid'
self.config['options'] = {'primaryKey': 'id'}
sql = self.__run_macro(template, 'spark__create_table_as', False, 'my_table', 'select 1 as id')
self.assertIn('mock.raise_compiler_error()', sql)


def test_macros_create_table_as_partition(self):
template = self.__get_template('adapters.sql')

Expand Down

0 comments on commit 227fe57

Please sign in to comment.