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

Feature/first class pg #174

Merged
merged 1 commit into from
Oct 19, 2016
Merged

Feature/first class pg #174

merged 1 commit into from
Oct 19, 2016

Conversation

drewbanin
Copy link
Contributor

@drewbanin drewbanin commented Oct 1, 2016

Previously, the only targettype option was "redshift". Now, this type field can be set to "postgres" instead. Everything works almost exactly the same, but this differentiation is used for injecting variables into the compilation context.

In this branch, one new context variable is added, sql_now. In Redshift, this expands to getdate() and in Postgres, it expands to clock_timestamp(). While now() is more canonical (I think?), now() reflects the time of the start of the transaction, whereas clock_timestamp() reflects the wall-clock time when the statement is executed. The primary use case forsql_now` is for pre-and-post-hook audit records, we definitely want wall-clock time.

# profiles.yml
user:
  outputs:
    dev:
      type: postgres
      host: localhost
      user: Drew
....

An example of sql_where (and macros):

# audit_macros.sql
{% set audit_table = ref('audit') %}

{% macro log_run_with_status(status) %}

insert into {{ audit_table }}
    (invocation_id, run_started_at, run_at, model, status)
values
    ('{{ invocation_id }}', '{{ run_started_at }}', {{ sql_now }}, '{{ this.name }}', '{{ status }}')
--                                                        ^
--                                                        |
--                                      sql_now ----------+

{% endmacro %}

Which compiles to:

insert into "analytics"."audit"
    (invocation_id, run_started_at, run_at, model, status)
values
    ('{{ invocation_id }}', '{{ run_started_at }}', clock_timestamp(), 'accounts', 'started');

In sum, this is a pretty minor change, but it has pretty big ramifications for how we think about packages. Curious to hear your thoughts, @jthandy

@drewbanin drewbanin added the enhancement New feature or request label Oct 1, 2016
@drewbanin drewbanin added this to the 0.5.1 Release milestone Oct 1, 2016
@jthandy
Copy link
Member

jthandy commented Oct 2, 2016

I follow the logic and generally am supportive. Here are the things I'm thinking about:

  • I feel weird about having a wide open namespace where a bunch of random variables are just floating around. Instead, I would like to think about what a useful eventual namespace would be and begin to build towards that. For instance, I think this might be something like sql.now where the sql namespace contains a bunch of cross-database compatibility functions for sql. I can imagine a large number of other namespaces outside of sql. For instance, invocation could be a namespace, connection could be a namespace--and those are just the things that we're beginning to reference here.
  • it's not clear to me how some of these variables are interpolated in compilation and some are interpolated at run time. I feel like there should be some syntactical difference between those, no? They are functionally very different. Or maybe it's intentionally transparent to the user? This is the first time that dbt compile will produce non-runnable SQL.

It might be good to chat about this stuff live.

@jthandy jthandy closed this Oct 2, 2016
@jthandy jthandy reopened this Oct 2, 2016
@jthandy
Copy link
Member

jthandy commented Oct 2, 2016

Sorry didn't mean to close!

@drewbanin
Copy link
Contributor Author

Merging this. The macro stuff was more speculative, but the pg support is useful as-is

@drewbanin drewbanin merged commit f68fb38 into release/0.5.1 Oct 19, 2016
@drewbanin drewbanin deleted the feature/first-class-pg branch October 19, 2016 18:30
yu-iskw pushed a commit to yu-iskw/dbt that referenced this pull request Aug 17, 2021
* Reducing whitespace in group_by

reducing whitespace in compiled SQL for legibility

* Updated white spacing as per Drew's preference

now remove line break after `group by`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants