Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
support for model and seed table properties (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet committed Aug 11, 2021
1 parent cc83402 commit 43c6e2b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ hive.allow-drop-table=true
hive.allow-rename-table=true
```

#### Use table properties to configure connector specifics

Trino/Presto connectors use table properties to configure connector specifics.

Check the Presto/Trino connector documentation for more information.

```
{{
config(
materialized='table',
properties={
"format": "'PARQUET'",
"partitioning": "ARRAY['bucket(id, 2)']",
}
)
}}
```

### Reporting bugs and contributing code

Expand Down
9 changes: 9 additions & 0 deletions dbt/adapters/presto/impl.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from dataclasses import dataclass
from typing import Dict, Optional
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.presto import PrestoConnectionManager, PrestoColumn
from dbt.adapters.base.impl import AdapterConfig

import agate


@dataclass
class PrestoConfig(AdapterConfig):
properties: Optional[Dict[str, str]] = None


class PrestoAdapter(SQLAdapter):
Column = PrestoColumn
ConnectionManager = PrestoConnectionManager
AdapterSpecificConfigs = PrestoConfig

@classmethod
def date_function(cls):
Expand Down
40 changes: 38 additions & 2 deletions dbt/include/presto/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,45 @@
{% endmacro %}


{% macro presto__create_csv_table(model, agate_table) %}
{%- set column_override = model['config'].get('column_types', {}) -%}
{%- set quote_seed_column = model['config'].get('quote_columns', None) -%}
{%- set _properties = config.get('properties') -%}

{% set sql %}
create table {{ this.render() }} (
{%- for col_name in agate_table.column_names -%}
{%- set inferred_type = adapter.convert_type(agate_table, loop.index0) -%}
{%- set type = column_override.get(col_name, inferred_type) -%}
{%- set column_name = (col_name | string) -%}
{{ adapter.quote_seed_column(column_name, quote_seed_column) }} {{ type }} {%- if not loop.last -%}, {%- endif -%}
{%- endfor -%}
) {{ properties(_properties) }}
{% endset %}

{% call statement('_') -%}
{{ sql }}
{%- endcall %}

{{ return(sql) }}
{% endmacro %}

{% macro properties(properties) %}
{%- if properties is not none -%}
WITH (
{%- for key, value in properties.items() -%}
{{ key }} = {{ value }}
{%- if not loop.last -%}{{ ',\n ' }}{%- endif -%}
{%- endfor -%}
)
{%- endif -%}
{%- endmacro -%}


{% macro presto__create_table_as(temporary, relation, sql) -%}
create table
{{ relation }}
{%- set _properties = config.get('properties') -%}
create table {{ relation }}
{{ properties(_properties) }}
as (
{{ sql }}
);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def test_acquire_connection(self):

connection.handle

self.assertEquals(connection.state, 'open')
self.assertNotEquals(connection.handle, None)
self.assertEqual(connection.state, 'open')
self.assertNotEqual(connection.handle, None)

def test_cancel_open_connections_empty(self):
self.assertEqual(len(list(self.adapter.cancel_open_connections())), 0)
Expand Down

0 comments on commit 43c6e2b

Please sign in to comment.