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

fix(format): Fixed SQL view formatting issue not following SQLFluff recommandations #1154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Cedric-Magnan
Copy link

Problem

If the sql string variable in the relations/view/create.sql macro has multiple lines, the SQL view generated by dbt has one first line indented by 4 spaces, but the rest of the SQL code has no indentation.

This does not respect SQLFluff best practices for indentation : https://docs.sqlfluff.com/en/stable/layout.html#indentation

The indentation should be treated as a hint to the reader of the structure of the code.
This explains the common practice within most languages that nested elements (for example the contents of a set of brackets in a function call) should be indented one step from the outer elements.
It’s also convention that elements with the same level in a nested structure should have the same indentation, at least with regards to their local surroundings.

Solution

By adding the Jinja2 indent function :
https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.indent

We are able to indent all content from the sql variable (except the first line which is already indented) !

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

…ecommandations

If the `sql` string variable in the `relations/view/create.sql` macro has multiple lines, the SQL view generated by dbt has one first line indented by 4 spaces, but the rest of the SQL code has no indentation.

This does not respect SQLFluff best practices for indentation :
https://docs.sqlfluff.com/en/stable/layout.html#indentation

```md
The indentation should be treated as a hint to the reader of the structure of the code.
This explains the common practice within most languages that nested elements (for example the contents of a set of brackets in a function call) should be indented one step from the outer elements.
It’s also convention that elements with the same level in a nested structure should have the same indentation, at least with regards to their local surroundings.
```
@Cedric-Magnan Cedric-Magnan requested a review from a team as a code owner July 30, 2024 08:12
Copy link

cla-bot bot commented Jul 30, 2024

Thanks for your pull request, and welcome to our community! We require contributors to sign our Contributor License Agreement and we don't seem to have your signature on file. Check out this article for more information on why we have a CLA.

In order for us to review and merge your code, please submit the Individual Contributor License Agreement form attached above above. If you have questions about the CLA, or if you believe you've received this message in error, please reach out through a comment on this PR.

CLA has not been signed by users: @Cedric-Magnan

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.

1 participant