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

Lift + shift for cross-db macros #25

Merged
merged 15 commits into from
Aug 4, 2022
8 changes: 7 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ jobs:
python3.8 -m venv venv
. venv/bin/activate
pip install --upgrade pip setuptools
pip install --pre --upgrade dbt-spark[ODBC]
pip install -r dev-requirements.txt
mkdir -p ~/.dbt
cp integration_tests/ci/sample.profiles.yml ~/.dbt/profiles.yml

- run:
name: "Run Functional Tests"
command: |
. venv/bin/activate
python3 -m pytest tests/functional --profile databricks_sql_endpoint

- run:
name: "Run Tests - dbt-utils"

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/**/dbt_packages/
/**/logs/
/**/env/
/**/__pycache__/
test.env
2 changes: 1 addition & 1 deletion dbt-utils
Submodule dbt-utils updated 105 files
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'spark_utils'
version: '0.3.0'
config-version: 2
require-dbt-version: [">=1.0.0", "<2.0.0"]
require-dbt-version: [">=1.2.0", "<2.0.0"]
macro-paths: ["macros"]
5 changes: 5 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pytest
pyodbc==4.0.32
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-spark.git#egg=dbt-spark[ODBC]
Comment on lines +1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is helpful!

15 changes: 11 additions & 4 deletions integration_tests/dbt_utils/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ seeds:
models:
dbt_utils_integration_tests:
+file_format: delta

sql:
# macro doesn't work for this integration test (schema pattern)
test_get_relations_by_pattern:
Expand All @@ -41,13 +42,19 @@ models:
test_pivot_apostrophe:
+enabled: false
generic_tests:
# integration test doesn't work
# default version of this integration test uses an explicit cast to 'datetime'
# which SparkSQL does not support. override with our own version
test_recency:
+enabled: false
cross_db_utils:
# integration test doesn't work
test_any_value:
+enabled: false
# integration test doesn't work
test_listagg:
+enabled: false

tests:
dbt_utils_integration_tests:
cross_db_utils:
# expect exactly two failures
# (both use "order by", which isn't supported in SparkSQL)
assert_equal_test_listagg_actual__expected:
error_if: ">2"
2 changes: 2 additions & 0 deletions integration_tests/dbt_utils/models/test_recency.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select
{{ dbt_utils.date_trunc('day', dbt_utils.current_timestamp()) }} as today
3 changes: 0 additions & 3 deletions macros/dbt_utils/cross_db_utils/concat.sql

This file was deleted.

62 changes: 0 additions & 62 deletions macros/dbt_utils/cross_db_utils/dateadd.sql

This file was deleted.

107 changes: 0 additions & 107 deletions macros/dbt_utils/cross_db_utils/datediff.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro assert_not_null(function, arg) -%}
{{ return(adapter.dispatch('assert_not_null', 'dbt')(function, arg)) }}
{%- endmacro %}
3 changes: 3 additions & 0 deletions macros/dbt_utils/cross_db_utils/deprecated/concat.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro spark__concat(fields) -%}
{{ return(adapter.dispatch('concat', 'dbt')(fields)) }}
{%- endmacro %}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{# numeric ------------------------------------------------ #}

{% macro spark__type_numeric() %}
decimal(28, 6)
{{ return(adapter.dispatch('type_numeric', 'dbt')()) }}
{% endmacro %}
6 changes: 6 additions & 0 deletions macros/dbt_utils/cross_db_utils/deprecated/dateadd.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% macro spark__dateadd(datepart, interval, from_date_or_timestamp) %}
-- dispatch here gets very very confusing
-- we just need to hint to dbt that this is a required macro for resolving dbt.spark__datediff()
-- {{ assert_not_null() }}
{{ return(adapter.dispatch('dateadd', 'dbt')(datepart, interval, from_date_or_timestamp)) }}
{% endmacro %}
6 changes: 6 additions & 0 deletions macros/dbt_utils/cross_db_utils/deprecated/datediff.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% macro spark__datediff(first_date, second_date, datepart) %}
-- dispatch here gets very very confusing
-- we just need to hint to dbt that this is a required macro for resolving dbt.spark__datediff()
-- {{ assert_not_null() }}
{{ return(adapter.dispatch('datediff', 'dbt')(first_date, second_date, datepart)) }}
{% endmacro %}
3 changes: 3 additions & 0 deletions macros/dbt_utils/cross_db_utils/deprecated/split_part.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro spark__split_part(string_text, delimiter_text, part_number) %}
{{ return(adapter.dispatch('split_part', 'dbt')(string_text, delimiter_text, part_number)) }}
{% endmacro %}
23 changes: 0 additions & 23 deletions macros/dbt_utils/cross_db_utils/split_part.sql

This file was deleted.

9 changes: 0 additions & 9 deletions macros/etc/assert_not_null.sql

This file was deleted.

8 changes: 8 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[pytest]
filterwarnings =
ignore:.*'soft_unicode' has been renamed to 'soft_str'*:DeprecationWarning
ignore:unclosed file .*:ResourceWarning
env_files =
test.env
testpaths =
tests/functional
Comment on lines +1 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last Week I Learned (LWIL):

Example

Let's say I have an environment variable defined locally:
DBT_DATABASE_HOST=localhost

And test.env contains:
DBT_DATABASE_HOST=somethingelse

By default, pytest-dotenv will keep the former and ignore the latter. Scroll down to see how to configure the opposite behavior.

Override default behavior

So it depending on how we want pytest + pytest-dotenv to behave, we either will want to include the following (or not!):

env_override_existing_values = 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great LWIL!

This makes sense to me, insofar as I might have a test.env, but then also want to do:

export DBT_DATABASE_HOST=localhost
python3 -m pytest ...

I would indeed expect the env var I've just supplied to override the one in test.env

Loading