-
Notifications
You must be signed in to change notification settings - Fork 472
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: Fetch Macro #160
Feature: Fetch Macro #160
Conversation
changed macro reference 'fetch' to 'dbt_utils.fetch'
… feature/get_column_from_sql
…lytics/dbt-utils into feature/get_column_from_sql
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couple of comments around documentation + tests, but the core fetch
macro looks great! Thanks for splitting this out from #123 - this looks great
README.md
Outdated
Usage: | ||
``` | ||
-- Returns a dictionary of the users table where the state is California | ||
{% set ___ = dbt_utils.fetch("select * from ref{{'users'}} where state = 'CA' ") %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good idea to make usage docs like this directly usable. Can you replace ___
here with users
? I also think this is a syntax error - it should be something like:
{% set users = dbt_utils.fetch("select * from " ~ ref('users') ~ " where state = 'CA' ") %}
} %} | ||
|
||
{% set actual_dictionary=dbt_utils.fetch( | ||
"select * from {{ ref('data_fetch') }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing here - I don't think you want to use curly brackets inside of a set
definition. Try:
"select * from " ~ ref('data_fetch')
"select * from {{ ref('data_fetch') }}" | ||
) %} | ||
|
||
{% if actual_dictionary | lower == expected_dictionary | lower %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if you can call lower
on a dictionary - I'd recommend plucking out the fields that you care about, so:
{% if actual_dictionary['col_1'] | lower != expected_dictionary['col_1'] | lower %}
select 1
{% elif actual_dictionary['col_2'] | lower != expected_dictionary['col_2'] | lower %}
select 1
{% elif ... %}
{% else %}
select 1 limit 0
{% endif %}
The limit 0
approach tends to work better than where false
, as I think there's a database out there (postgres? bigquery? I forget) which doesn't let you use a where
predicate if you don't also have a from
clause in the query.
Overall though, this is a slick looking test!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually something I'm having troubles/need help with. The fetch macro returns numbers in the form decimal('1'), decimal('2'), decimal('3')
rather than 1,2,3
. I'm not sure how this would (or should) be converted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has this decimal('1') been fixed? I'm having similar issues with dates which come as datetime.date(1970, 1, 1)
macros/sql/fetch.sql
Outdated
@@ -0,0 +1,21 @@ | |||
{% macro fetch(sql) %} | |||
{# This macro returns a dictionary of the form {column_name: (tuple_of_results)} #} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3 this
Oops - didn't mean to approve this quite yet! Let me know about my comments here - happy to look at the tests together if that would be helpful :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(see above)
@drewbanin I made the adjustments, the tests are passing. Can you take a look at assert_fetch_objects_equal.sql? I switched the brackets for square brackets in setting the expected dictionary. I applied the list filter to that for consistency, but it is less simple. |
update all expected_columns to lists
@ChrisLawliss it's just the Snowflake tests which are failing here, which to me always shouts "CAPITALIZATION!" I bet fields like |
I think that the new changes could be made a bit more compact |
Yo late to the party here. Wondering if |
Hi, me again 👋 So I was playing with the in a macro I was writing, and when I used
When I instead used
Is it because the call to Should we use |
acc88e4
to
cbdd230
Compare
74a5110
to
02c5dd0
Compare
02c5dd0
to
1843a59
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ended up spending a bit of time today on this so we can get it merged. I think this macro is super valuable and I'm excited to get it into utils!
So annoying that the testing took so much longer than writing the macro, but that's how writing code goes sometimes!
This PR adds fetch macro which returns a dictionary from a sql query.
Todo: