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 Trino support #115

Merged
merged 2 commits into from
Nov 2, 2023
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
39 changes: 39 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,42 @@ jobs:
- store_artifacts:
path: ./logs


integration-tests-trino:

docker:
- image: cimg/python:3.11
- image: trinodb/trino:431

resource_class: small

environment:
DBT_PROFILES_DIR: ./integration_tests/ci
DBT_PROJECT_DIR: ./integration_tests
DBT_VERSION: 1.6.*

steps:
- checkout
- run:
name: Install dbt adapter packages
command: |
python3 -m venv venv
. venv/bin/activate
pip install "dbt-trino==$DBT_VERSION"
- run: *dbt-deps
- setup_remote_docker
- run:
name: Run Trino server
command: |
docker run --name trino -p 8080:8080 -d -v `pwd`/integration_tests/docker/trino/catalog:/etc/trino/catalog trinodb/trino:431
timeout 5m bash -c -- 'while ! docker logs trino 2>&1 | tail -n 1 | grep "SERVER STARTED"; do sleep 2; done'
- run:
name: "Run Tests - Trino"
command: |
. venv/bin/activate
dbt build -t trino --project-dir $DBT_PROJECT_DIR


workflows:
version: 2
test-all:
Expand All @@ -145,3 +181,6 @@ workflows:
- integration-tests-spark-thrift:
requires:
- hold
- integration-tests-trino:
requires:
- hold
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ This package supports:
* BigQuery
* DuckDB
* Spark
* Trino

## Variables

Expand Down
11 changes: 11 additions & 0 deletions integration_tests/ci/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ integration_tests:
connect_timeout: 60
retry_all: true

trino:
type: trino
method: none
host: localhost
port: 8080
user: admin
catalog: memory
schema: default
timezone: UTC
threads: 4

target: postgres
2 changes: 2 additions & 0 deletions integration_tests/docker/trino/catalog/memory.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connector.name=memory
memory.max-data-per-node=128MB
16 changes: 16 additions & 0 deletions integration_tests/macros/get_test_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ select
{{ return([48,49]) }}
{%- endmacro %}

{% macro trino__get_test_week_of_year() -%}
{# weeks_of_year for '2020-11-29' and '2020-12-01', respectively #}
{# trino uses ISO year #}
{{ return([48,49]) }}
{%- endmacro %}


{% macro get_test_week_start_date() -%}
{{ return(adapter.dispatch('get_test_week_start_date', 'dbt_date_integration_tests') ()) }}
Expand All @@ -103,6 +109,11 @@ select
{{ return(['2020-11-23', '2020-11-30']) }}
{%- endmacro %}

{% macro trino__get_test_week_start_date() -%}
{# trino does not support non-iso weeks #}
{{ return(['2020-11-23', '2020-11-30']) }}
{%- endmacro %}


{% macro get_test_week_end_date() -%}
{{ return(adapter.dispatch('get_test_week_end_date', 'dbt_date_integration_tests') ()) }}
Expand All @@ -117,6 +128,11 @@ select
{{ return(['2020-11-29', '2020-12-06']) }}
{%- endmacro %}

{% macro trino__get_test_week_end_date() -%}
{# trino does not support non-iso weeks #}
{{ return(['2020-11-29', '2020-12-06']) }}
{%- endmacro %}


{% macro get_test_timestamps() -%}
{{ return(adapter.dispatch('get_test_timestamps', 'dbt_date_integration_tests') ()) }}
Expand Down
4 changes: 4 additions & 0 deletions macros/calendar_date/convert_timezone.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ from_utc_timestamp(
'{{ target_tz }}'
)
{%- endmacro -%}

{%- macro trino__convert_timezone(column, target_tz, source_tz) -%}
cast((at_timezone(with_timezone(cast({{ column }} as {{ dbt.type_timestamp() }}), '{{ source_tz }}'), '{{ target_tz }}')) as {{ dbt.type_timestamp() }})
{%- endmacro -%}
4 changes: 4 additions & 0 deletions macros/calendar_date/date_part.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
{% macro bigquery__date_part(datepart, date) -%}
extract({{ datepart }} from {{ date }})
{%- endmacro %}

{% macro trino__date_part(datepart, date) -%}
extract({{ datepart }} from {{ date }})
{%- endmacro %}
5 changes: 5 additions & 0 deletions macros/calendar_date/day_name.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@
{%- set f = 'E' if short else 'EEEE' -%}
date_format({{ date }}, '{{ f }}')
{%- endmacro %}

{%- macro trino__day_name(date, short) -%}
{%- set f = 'a' if short else 'W' -%}
date_format({{ date }}, '%{{ f }}')
{%- endmacro %}
16 changes: 16 additions & 0 deletions macros/calendar_date/day_of_week.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@
{{ dbt_date.date_part(dow, date) }}

{%- endmacro %}


{%- macro trino__day_of_week(date, isoweek) -%}

{%- set dow = dbt_date.date_part('day_of_week', date) -%}

{%- if isoweek -%}
{{ dow }}
{%- else -%}
case
when {{ dow }} = 7 then 1
else {{ dow }} + 1
end
{%- endif -%}

{%- endmacro %}
4 changes: 4 additions & 0 deletions macros/calendar_date/day_of_year.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
{%- macro spark__day_of_year(date) -%}
dayofyear({{ date }})
{%- endmacro %}

{%- macro trino__day_of_year(date) -%}
{{ dbt_date.date_part('day_of_year', date) }}
{%- endmacro %}
17 changes: 17 additions & 0 deletions macros/calendar_date/from_unixtimestamp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@
}}
{% endif -%}
{%- endmacro %}

{%- macro trino__from_unixtimestamp(epochs, format) -%}
{%- if format == "seconds" -%}
cast(from_unixtime({{ epochs }}) AT TIME ZONE 'UTC' as {{ dbt.type_timestamp() }})
{%- elif format == "milliseconds" -%}
cast(from_unixtime_nanos({{ epochs }} * pow(10, 6)) AT TIME ZONE 'UTC' as {{ dbt.type_timestamp() }})
{%- elif format == "microseconds" -%}
cast(from_unixtime_nanos({{ epochs }} * pow(10, 3)) AT TIME ZONE 'UTC' as {{ dbt.type_timestamp() }})
{%- elif format == "nanoseconds" -%}
cast(from_unixtime_nanos({{ epochs }}) AT TIME ZONE 'UTC' as {{ dbt.type_timestamp() }})
{%- else -%}
{{ exceptions.raise_compiler_error(
"value " ~ format ~ " for `format` for from_unixtimestamp is not supported."
)
}}
{% endif -%}
{%- endmacro %}
4 changes: 4 additions & 0 deletions macros/calendar_date/iso_week_of_year.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ cast({{ dbt_date.date_part(week_type, date) }} as {{ dbt.type_int() }})
{%- macro spark__iso_week_of_year(date) -%}
{{ dbt_date._iso_week_of_year(date, 'week') }}
{%- endmacro %}

{%- macro trino__iso_week_of_year(date) -%}
{{ dbt_date._iso_week_of_year(date, 'week') }}
{%- endmacro %}
4 changes: 4 additions & 0 deletions macros/calendar_date/iso_week_start.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ cast({{ dbt.date_trunc(week_type, date) }} as date)
{%- macro spark__iso_week_start(date) -%}
{{ dbt_date._iso_week_start(date, 'week') }}
{%- endmacro %}

{%- macro trino__iso_week_start(date) -%}
{{ dbt_date._iso_week_start(date, 'week') }}
{%- endmacro %}
5 changes: 5 additions & 0 deletions macros/calendar_date/month_name.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@
{%- set f = 'LLL' if short else 'LLLL' -%}
date_format({{ date }}, '{{ f }}')
{%- endmacro %}

{%- macro trino__month_name(date, short) -%}
{%- set f = 'b' if short else 'M' -%}
date_format({{ date }}, '%{{ f }}')
{%- endmacro %}
3 changes: 3 additions & 0 deletions macros/calendar_date/to_unixtimestamp.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
unix_timestamp({{ timestamp }})
{%- endmacro %}

{%- macro trino__to_unixtimestamp(timestamp) -%}
to_unixtime({{ timestamp }} AT TIME ZONE 'UTC')
{%- endmacro %}
30 changes: 30 additions & 0 deletions macros/get_base_dates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,33 @@ select
from
date_spine d
{% endmacro %}


{% macro trino__get_base_dates(start_date, end_date, n_dateparts, datepart) %}

{%- if start_date and end_date -%}
{%- set start_date="cast('" ~ start_date ~ "' as " ~ dbt.type_timestamp() ~ ")" -%}
{%- set end_date="cast('" ~ end_date ~ "' as " ~ dbt.type_timestamp() ~ ")" -%}

{%- elif n_dateparts and datepart -%}

{%- set start_date = dbt.dateadd(datepart, -1 * n_dateparts, dbt_date.now()) -%}
{%- set end_date = dbt_date.tomorrow() -%}
{%- endif -%}

with date_spine as
(

{{ dbt_date.date_spine(
datepart=datepart,
start_date=start_date,
end_date=end_date,
)
}}

)
select
cast(d.date_{{ datepart }} as {{ dbt.type_timestamp() }}) as date_{{ datepart }}
from
date_spine d
{% endmacro %}