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

support local/package level and serializable python modules/macros #10080

Closed
wants to merge 1 commit into from

Conversation

woods-chen
Copy link

@woods-chen woods-chen commented May 2, 2024

resolves #10078

Suppose that we need to remove (dedent) the same number of leading whitespaces in each line of a sql command or a string of documentation. I've been googling for jinja solutions, however the solutions are quite complicated. And it would be quite ease if we can invoke a python module/function.
The proposed method is, we can write a macro in a python file "macros/my_python_module.py":

from textwrap import dedent

and then we call call the defined module/function in our model "models/test1.sql":

{% set query_cmd %}
    select col_1,
              col_2
    from table_name
    where col3=1
        and col4=2
{% endset %}
{{ my_python_module.dedent(query_cmd) }}

What's more, as the implementation of this python macro is within the project locally, we can call the macro using my_project.my_python_module.dedent if this project is imported to other projects as a package, just as what we do with a jinja macro.

Problem

Solution

add a PythonModule node class in additional to the Macro node class, which can be parsed and serialized.
However there are some limitations:

  1. the python macro is in project level which is the same as jinja macros, and should be called with the heading package name if we need to call the python macro in other projects.
  2. Each directory (python module) should have a "__init__.py" file inside except for the "macros" directory.

Checklist

  • I have read the contributing guide and understand what's expected of me
  • I have run this code in development and it appears to resolve the stated issue
  • This PR includes tests, or tests are not required/relevant for this PR
  • This PR has no interface changes (e.g. macros, cli, logs, json artifacts, config files, adapter interface, etc) or this PR has already received feedback and approval from Product or DX
  • This PR includes type annotations for new and modified functions

@woods-chen woods-chen requested a review from a team as a code owner May 2, 2024 12:19
@cla-bot cla-bot bot added the cla:yes label May 2, 2024
@github-actions github-actions bot added the community This PR is from a community member label May 2, 2024
@dbeatty10 dbeatty10 changed the title support local/package level and serializable python modules/macros. (… support local/package level and serializable python modules/macros May 2, 2024
@dbeatty10
Copy link
Contributor

Closing per #10078 (comment)

@dbeatty10 dbeatty10 closed this May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:yes community This PR is from a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] support for local/package level and serializable python macros.
2 participants