Skip to content

Commit

Permalink
Allow the use of libraries in macro definitions (#3803)
Browse files Browse the repository at this point in the history
* Allow the use of libraries in macro definitions

* Move library usage in macro test to separate test case
  • Loading branch information
bjgbeelen committed Aug 30, 2022
1 parent 6687f9b commit 6cfb215
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/sqlfluff/core/templaters/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ def get_context(self, fname=None, config=None, **kw) -> Dict:
live_context = super().get_context(fname=fname, config=config)
# Apply dbt builtin functions if we're allowed.
if config:
# first make libraries available in the context
# so they can be used by the macros too
live_context.update(self._extract_libraries_from_config(config=config))

apply_dbt_builtins = config.get_section(
(self.templater_selector, self.name, "apply_dbt_builtins")
)
Expand Down Expand Up @@ -316,7 +320,6 @@ def get_context(self, fname=None, config=None, **kw) -> Dict:
)
)

live_context.update(self._extract_libraries_from_config(config=config))
return live_context

def template_builder(
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templater/jinja_r_library_in_macro/jinja.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ query_proxy('xyz') }}
24 changes: 24 additions & 0 deletions test/fixtures/templater/jinja_r_library_in_macro/jinja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
file:
statement:
select_statement:
select_clause:
keyword: SELECT
select_clause_element:
numeric_literal: '56'
from_clause:
keyword: FROM
from_expression:
from_expression_element:
table_expression:
table_reference:
- naked_identifier: sch1
- dot: .
- naked_identifier: foo_xyz
where_clause:
keyword: WHERE
expression:
column_reference:
naked_identifier: x
comparison_operator:
raw_comparison_operator: '='
numeric_literal: '23'
6 changes: 6 additions & 0 deletions test/fixtures/templater/jinja_r_library_in_macro/libs/bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Module used to test bar within the jinja template."""


def equals(col, val):
"""Return a string that has col = val."""
return f"{col} = {val}"
7 changes: 7 additions & 0 deletions test/fixtures/templater/jinja_r_library_in_macro/libs/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Module used to test foo within the jinja template."""
schema = "sch1"


def table(name):
"""Return the parameter with foo_ in front of it."""
return f"foo_{name}"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am just a text file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% macro query_proxy(tbl) %}SELECT 56
FROM {{ foo.schema }}.{{ foo.bar("xyz") }}
WHERE {{ bar.equals("x", 23) }}
{% endmacro %}

0 comments on commit 6cfb215

Please sign in to comment.