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 find and replace to json extract #92

Merged
merged 9 commits into from
Nov 29, 2022

Conversation

fivetran-reneeli
Copy link

@fivetran-reneeli fivetran-reneeli commented Nov 7, 2022

What change does this PR introduce?

Add a find+replace to our pivot_json_extract macro, similar to (' ', '_') but with ('.', '_').

If this PR introduces a new macro, how did you test the new macro?

n/a

If this PR introduces a modification to an existing macro, which packages is the macro currently present in and what steps were taken to test compatibility across packages?

Stripe
Issue fivetran/dbt_stripe#45
Testing with dbt compile -t snowflake -m stg_stripe__plan and dbt run -t snowflake -m stg_stripe__plan

Did you update the README to reflect the macro addition/modifications?

  • Yes
  • No (provide further explanation)

Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

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

@fivetran-reneeli your approach here makes sense and definitely addresses the feature.id type metadata fields that the customer was experiencing. However, as you pointed out, it won't solve the reserved word error or other errors we may not be aware of at the moment.

I was able to do some digging and think I found a unique workaround that can address both the . issue and the reserved word error. See below for the modified macro code I added to make this work:

{% macro pivot_json_extract(string, list_of_properties) %}

{%- for property in list_of_properties -%}
{%- if property is not string -%}
replace( {{ fivetran_utils.json_extract(string, property.name) }}, '"', '') as {{ property.alias if property.alias else property.name | replace(' ', '_') | replace('.', '_') | lower }}

{%- else -%}
replace( {{ fivetran_utils.json_extract(string, property) }}, '"', '') as {{ property | replace(' ', '_') | lower }}

{%- endif -%}
{%- if not loop.last -%},{%- endif %}
{% endfor -%}

{% endmacro %}

With this update to the macro, we allow the variable to accept a dictionary in addition to the normal string behavior. This means, if a customer wants to alias the fields or is experiencing a reserved word or incompatible name, then they can change it to their liking!

I was able to get this to work using the following variable config:

  stripe__plan_metadata:
    - limit
    - version
    - name: feature.id
      alias: feature_id
    - name: plan.handle
      alias: plan_handle
    - name: set
      alias: sets

I feel we should update the macro for the next iteration of fivetran_utils to follow this behavior. This could then address the customers issue. We would just want to update the dbt_stripe and dbt_stripe_source README's to show how a customer may leverage these new dictionary arguments if needed.

@fivetran-reneeli
Copy link
Author

Hi @fivetran-joemarkiewicz , I updated the readmes in the stripe packages to reflect these new instructions. Let me know if they make sense!
stripe
stripe source

@fivetran-joemarkiewicz
Copy link
Contributor

Thanks @fivetran-reneeli I noticed this PR didn't apply the updates to the macro. Would you be able to make those updates as well?

Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

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

@fivetran-reneeli I had one comment that I included in the notes below. Can you test this on your end and make sure it works as expected.

Additionally, be sure to add a CHANGELOG entry. Let me know once the updates are applied and I can re-review.

@@ -1,9 +1,13 @@
{% macro pivot_json_extract(string, list_of_properties) %}

{%- for property in list_of_properties -%}
{%- if property is not string -%}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we discussed during that quick show and tell that it may be better to use the following. This way you must be using a dictionary instead of not string. This way if customers need to pass an int or something, they aren't hit with an error.

Suggested change
{%- if property is not string -%}
{%- if property is mapping -%}

Copy link
Author

Choose a reason for hiding this comment

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

Oh yes that's right-- testing it now

@fivetran-reneeli
Copy link
Author

Test with the following packages.yml in dbt_stripe_source

packages:
# - package: fivetran/fivetran_utils
#   version: [">=0.4.0", "<0.5.0"]
- package: dbt-labs/spark_utils
  version: [">=0.3.0", "<0.4.0"]

- git: https://github.com/fivetran/dbt_fivetran_utils.git
  revision: bugfix/json_replace
  warn-unpinned: false

@fivetran-reneeli
Copy link
Author

And adding this to the project yml:

    stripe__plan_metadata:
        - name: limit
          alias: limit_z
        - name: feature.id
          alias: feature_id
        - name: plan.handle
          alias: plan_handle
        - name: set
          alias: sets
        - name: 123
          alias: onetwothree

@fivetran-reneeli
Copy link
Author

Hi @fivetran-joemarkiewicz , I made the updates and also updated the changelog and readmes and versioning. Lmk if it looks good!

Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

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

@fivetran-reneeli just a few small comments regarding the versioning of the package and folding the changelog entries into the soon to be released 0.4.1 entry.

Let me know once the updates have been applied and I can re-review

CHANGELOG.md Outdated
@@ -1,3 +1,8 @@
# dbt_fivetran_utils v0.4.2
Copy link
Contributor

Choose a reason for hiding this comment

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

0.4.2 hasn't been released yet. We should fold these into the 0.4.1 changelog entries instead.

Copy link
Author

Choose a reason for hiding this comment

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

updated

dbt_project.yml Outdated
@@ -1,4 +1,4 @@
name: 'fivetran_utils'
version: '0.4.1'
version: '0.4.2'
Copy link
Contributor

Choose a reason for hiding this comment

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

0.4.1 isn't released yet, we can keep this as 0.4.1

Copy link
Author

Choose a reason for hiding this comment

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

updated

@@ -1,4 +1,4 @@
name: 'fivetran_utils_integration_tests'
version: '0.4.1'
version: '0.4.2'
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, we can keep as 0.4.1

Copy link
Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor

@fivetran-joemarkiewicz fivetran-joemarkiewicz left a comment

Choose a reason for hiding this comment

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

I just have one small request to update the CHANGELOG. Once you commit that suggestion, this will be good to merge into releases/v0.4.latest

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Joe Markiewicz <74217849+fivetran-joemarkiewicz@users.noreply.github.com>
@fivetran-reneeli fivetran-reneeli merged commit 7fd3724 into releases/v0.4.latest Nov 29, 2022
@fivetran-reneeli fivetran-reneeli deleted the bugfix/json_replace branch November 29, 2022 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants