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
Update DbtTemplater to use JinjaTracer #1788
Merged
alanmcruickshank
merged 113 commits into
sqlfluff:main
from
barrywhart:bhart-issue_1783_update_dbt_templater_to_use_jinjatracer
Nov 9, 2021
+207
−107
Merged
Changes from 109 commits
Commits
Show all changes
113 commits
Select commit
Hold shift + click to select a range
6cd3175
Issue 1437: Implement a robust Jinja raw <---> templated mapping
fd61d38
Copy PythonTemplater.slice_file() to JinjaTemplater
1c48c41
Prototype new Jinja template mapping approach
a619497
Handle "templated" type by evaluating but outputting a UUID
012b575
Rename the field: kindred_raw -> unique_alternate
b15be0c
Tidying, renaming
2d71c6c
Add angle brackets around UUID, fix bug where it wasn't returning UUID
e7cb0d8
Add and popuate RawFileSlice.next_slice_indices
82018cb
First prototype of TemplateTracer
9f5a2af
Passes a test!
b445b0b
Add handling for the case where multiple templated parts are returned…
eaa5c5b
More bug fixes
cbba906
Get all the test__templater_jinja_slices tests updated & working
a66c5f9
Fix test__templater_jinja_error_variable
d6483bd
Fix a subtle bug that confounded int length with actual templated text
74dfd12
Fix bug handling "raw" Jinja blocks
4538be2
Bug fix: Not popping stack on "endif" or "endfor"
241bbf5
Fix bugs involving {% set %}
5faa136
Handle both flavors of Jinja "set" command
99c77c7
Handle where Jinja gives us multiple sections of output at once
b779eff
Fix bug checking templated file slice
0546af9
More rework of how to generate and process alt template output
27fba8d
Split alt template output on NUL ("\0") characters
7f0ad57
Fix right whitespace stripping case
ea42fdd
All the test__templater_jinja_slice_template tests are now working
f7d8cb9
Fix a few tests
5d74488
Fix some more tests
45294f0
Update a test with new results (more refined than before)
c163d96
Update a test whose results have (I think legitimately) changed
5201022
When looping, don't repeat the {% for %} TemplatedFileSlice
ccdc06b
Fix some integration issues related to RawFileSlice switching from Na…
2e2928e
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart c8b01f0
Linter
51508c3
Update Python tests affected by RawFileSlice changes
198f533
Updated test result -- looks better now!
5846b9a
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart c7ccb14
Move the internal error check _inside_ the loop
0d31e2d
Merge branch 'bhart-issue_1437_robust_jinja_raw_templated_mapping' of…
e5b9084
Generate nicer placeholder strings for omitted code
a5119f3
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 3e43762
Tweak placeholder gap output, update expected test output
863611b
Merge branch 'main' of https://github.com/sqlfluff/sqlfluff
781c03d
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
c14e3a7
Indentation improvements for templated code
9b8cf89
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
150e756
Tweaks to L003 to avoid breaking non-templated indentation
dfb69b4
Update existing L003 tests, add a new one
5841eee
More work on L003
2e6d28d
Tidying
f6bcb6d
Streamlining
d5e4011
Tidying
46d9f46
Comments, tidying
3017290
Tidying
cb595c6
Mypy
49474d1
PR review
3ac4346
Rename MetaSegment.template as MetaSegment.is_template
7e3701e
Refactor: Move tracer class to a new module, tidy up interface with J…
07c8309
mypy
c968a8b
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 25c616e
Update test result
f2af390
Update test result
e932136
mypy
f422e66
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
6d9d8c4
Fix DbtTemplater; it needed the old Jinja _slice_template() function
f3dd85a
Black, mypy
41d74e6
Linter
95a2497
mypy
3559f3c
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart cd737d1
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
f2ed81f
Merge branch 'bhart-issue_1437_robust_jinja_raw_templated_mapping' of…
0eb965d
Add a few "no cover" comments
3be29cc
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 84923fa
Add "no cover" comments, remove unused L003 code
0831bf6
Merge branch 'bhart-issue_1437_robust_jinja_raw_templated_mapping' of…
a55201b
Move tracer-specific RawFileSlice fields into a dictionary
43f457a
Black, mypy
5cbbb36
mypy
a02a7f8
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 6485c0f
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 3451f5c
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
barrywhart 5297c5a
Merge branch 'main' into bhart-issue_1437_robust_jinja_raw_templated_…
5cb6ea7
Merge branch 'bhart-issue_1437_robust_jinja_raw_templated_mapping' of…
b0a1306
Update DbtTemplater to use JinjaTracer
06359c9
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 238c29d
Add "no cover" comments for code "uncovered" by this PR
5371383
Merge branch 'bhart-issue_1783_update_dbt_templater_to_use_jinjatrace…
113781a
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
288549d
Add "no cover" comment
800daa6
Comments
e8dbbe1
Linter
5b272e2
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 86ef486
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 4e96b4d
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
alanmcruickshank 3db451e
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
f53ca89
Merge branch 'bhart-issue_1783_update_dbt_templater_to_use_jinjatrace…
3e448cc
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
b8f2864
Add missing __init__.py file
d7170ca
Add docstring
7bdc061
mypy
7ee9264
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 407e26c
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 08f402f
Add 1 1/2 dbt templater tests
fbb2de6
Merge branch 'bhart-issue_1783_update_dbt_templater_to_use_jinjatrace…
f8d74b4
Update test so it doesn't require a database
f35b00e
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 801265b
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 83bb966
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
e278f20
Fix broken test
725e87b
Merge branch 'bhart-issue_1783_update_dbt_templater_to_use_jinjatrace…
807ce6c
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 300c5b5
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart fae2ce2
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
barrywhart 5787fd2
Merge branch 'main' into bhart-issue_1783_update_dbt_templater_to_use…
alanmcruickshank File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...s/sqlfluff-templater-dbt/test/fixtures/dbt/dbt_project/models/my_new_project/last_day.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
with last_day_macro as ( | ||
|
||
select | ||
{{ dbt_utils.last_day('2021-11-05', 'month') }} | ||
|
||
) | ||
|
||
select * from last_day_macro |
2 changes: 2 additions & 0 deletions
2
...ater-dbt/test/fixtures/dbt/dbt_project/models/my_new_project/templated_inside_comment.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{# {{ 1 + 2 }} #} | ||
select 1 |
26 changes: 26 additions & 0 deletions
26
plugins/sqlfluff-templater-dbt/test/fixtures/dbt/last_day.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
with last_day_macro as ( | ||
|
||
select | ||
|
||
cast( | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
date_trunc('month', 2021-11-05) | ||
+ ((interval '1 month') * (1)) | ||
|
||
|
||
+ ((interval '1 day') * (-1)) | ||
|
||
|
||
|
||
as date) | ||
|
||
|
||
) | ||
|
||
select * from last_day_macro |
2 changes: 2 additions & 0 deletions
2
plugins/sqlfluff-templater-dbt/test/fixtures/dbt/templated_inside_comment.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
select 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -563,7 +563,7 @@ def _coalesce_types(elems: List[RawFileSlice]) -> str: | |
types = {elem.slice_type for elem in elems} | ||
# Replace block types with templated | ||
for typ in list(types): | ||
if typ.startswith("block_"): | ||
if typ.startswith("block_"): # pragma: no cover | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and elsewhere, we see that large areas of older slicing code are no longer used and could potentially be removed in a future PR. |
||
types.remove(typ) | ||
types.add("templated") | ||
# Take the easy route if they're all the same type | ||
|
@@ -614,7 +614,7 @@ def _split_uniques_coalesce_rest( | |
int_file_slice.templated_slice.stop | ||
- int_file_slice.templated_slice.start | ||
== 0 | ||
): | ||
): # pragma: no cover | ||
point_combo = int_file_slice.coalesce() | ||
templater_logger.debug( | ||
" Yielding Point Combination: %s", point_combo | ||
|
@@ -636,13 +636,13 @@ def _split_uniques_coalesce_rest( | |
templated_str=templated_str | ||
) | ||
if head_buffer: | ||
yield from head_buffer | ||
yield from head_buffer # pragma: no cover | ||
# Have we consumed the whole thing? | ||
if not int_file_slice.slice_buffer: | ||
continue | ||
continue # pragma: no cover | ||
|
||
# Try to yield simply again (post trim) | ||
try: | ||
try: # pragma: no cover | ||
simple_elem = int_file_slice.try_simple() | ||
templater_logger.debug(" Yielding Simple: %s", simple_elem) | ||
yield simple_elem | ||
|
@@ -833,25 +833,25 @@ def _split_uniques_coalesce_rest( | |
# formatting, but this class is also the base for the jinja templater | ||
# (and others?) so it may be used there. | ||
# One way uniques give us landmarks to try and estimate what to do with them. | ||
owu_templ_tuples = cls._sorted_occurrence_tuples( | ||
owu_templ_tuples = cls._sorted_occurrence_tuples( # pragma: no cover | ||
{key: templ_occs[key] for key in one_way_uniques} | ||
) | ||
|
||
templater_logger.debug( | ||
templater_logger.debug( # pragma: no cover | ||
" Handling One Way Uniques: %s", owu_templ_tuples | ||
) | ||
|
||
# Hang onto out *ending* position too from here. | ||
stops = ( | ||
stops = ( # pragma: no cover | ||
int_file_slice.source_slice.stop, | ||
int_file_slice.templated_slice.stop, | ||
) | ||
|
||
# OWU in this context refers to "One Way Unique" | ||
this_owu_idx: Optional[int] = None | ||
last_owu_idx: Optional[int] = None | ||
this_owu_idx: Optional[int] = None # pragma: no cover | ||
last_owu_idx: Optional[int] = None # pragma: no cover | ||
# Iterate through occurrence tuples of the one-way uniques. | ||
for raw, template_idx in owu_templ_tuples: | ||
for raw, template_idx in owu_templ_tuples: # pragma: no cover | ||
raw_idx = raw_occs[raw][0] | ||
raw_len = len(raw) | ||
|
||
|
@@ -907,10 +907,7 @@ def _split_uniques_coalesce_rest( | |
|
||
# If we succeeded in one of the above, we can also recurse | ||
# and be more intelligent with the other sections. | ||
if sub_section: # pragma: no cover | ||
# This assertion makes MyPy happy. In this case, we | ||
# never set source_slice without also setting | ||
# subsection. | ||
if sub_section: | ||
templater_logger.debug( | ||
" Attempting Subsplit [pre]: %s, %r", | ||
sub_section, | ||
|
@@ -949,7 +946,7 @@ def _split_uniques_coalesce_rest( | |
if last_owu_idx is None or last_owu_idx + 1 >= len( | ||
int_file_slice.slice_buffer | ||
): | ||
cur_idx = 0 # pragma: no cover | ||
cur_idx = 0 | ||
else: | ||
cur_idx = last_owu_idx + 1 | ||
|
||
|
@@ -1040,7 +1037,7 @@ def _split_uniques_coalesce_rest( | |
) | ||
|
||
# Yield anything from the tail buffer | ||
if tail_buffer: | ||
if tail_buffer: # pragma: no cover | ||
templater_logger.debug( | ||
" Yielding Tail Buffer [end]: %s", tail_buffer | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 started breaking after the recent L009 PR was merged. IIUC, using whitespace control at the end of the file caused all the trailing newlines to be (correctly) removed by Jinja, thus L009 would report an issue. I feel comfortable with this change, i.e. that the behavior was correct and that this fix makes sense.