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

Fix insert_by_period for Postgres #36

Merged
merged 6 commits into from
Nov 22, 2022
Merged

Conversation

LMandtler
Copy link

The insert_by_period materialization does not work for a postgres target.

Earlier discussions on this topic:

  1. https://discourse.getdbt.com/t/a-materialization-for-problematic-initial-builds/60/7
  2. Modernize insert_by_period materialization #32

This is fixed in this PR by taking the ideas of the first link above.
Disclaimer: These are not my ideas. I am just a regular user of this materialization and am happy to have it fixed.

The insert_by_period materialization depends on dbt_utils getdate() functions. So to get insert_by_period work, dbt_utils must be added to the packages.
The getdate() function is not valid across different warehouses. Therefore, we need to use the corresponding dbt_utils function
Postgres needs subqueries to have an alias
In the creation of the relation node, Postgres cannot work with a schema. Therefore I created a small macro, where I use dispatch to differentiate between postgres and the other warehouses.
@LMandtler
Copy link
Author

The fixes for Postgres have now been implemented and tested against a postgres instance. However, I had no chance to test against any other backends. Reviews are very welcome.

Copy link
Contributor

@joellabes joellabes left a comment

Choose a reason for hiding this comment

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

Looks good! I recommend not creating a dependency on dbt utils as the timestamp code has just been moved out and this will cause issues. Other than that excited to get this merged 🎉

@@ -12,7 +12,7 @@
{{ dateadd('millisecond',
-1,
"nullif('" ~ stop_date ~ "','')::timestamp") }},
getdate()
{{ dbt_utils.current_timestamp() }}
Copy link
Contributor

Choose a reason for hiding this comment

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

In dbt v1.3, this changed to dbt.current_timestamp() (note dbt not dbt_utils), and in dbt utils 1.0 (coming very very soon) current_timestamp() is being removed. I recommend not creating a dependency on utils and instead using the one provided in the dbt namespace directly.

Note that this would create a requirement of dbt Core 1.3 for this project, which you can enforce by adding a require-dbt-version block https://docs.getdbt.com/reference/project-configs/require-dbt-version

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for pointing me to this. This is resolved now.

This is needed to resolve dbt.current_timestamp() which was introduced in 1.3.0
@joellabes joellabes merged commit af8eb0b into dbt-labs:main Nov 22, 2022
@joellabes
Copy link
Contributor

Thanks @LMandtler!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants