LoginSignup
1
0

More than 1 year has passed since last update.

dbtのmacroを使用したクエリでsqlfluffのL001エラーが発生した場合の対処法

Posted at

何が起こったのか?

dbt coreの以下のmacroを使用したクエリで、sqlfluffのリントを実行したらエラーL001 | Unnecessary trailing whitespace.が発生した。不要なスペースは無いし、インデントはおかしくないのになぜかエラーが出る。

models/test_query.sql
select
    column_1,
    column_2,
    {{ test_macro() }} as column_3 -- このマクロでL001のエラーが発生する
from source
macros/test_macro.sql
{% macro test_macro() %}
'test'
{% endmacro %}
# sqlfluffのリントを実行
$ sqlfluff lint models/test_query.sql
== [models/test_query.sql] FAIL                                                                   
L:   4 | P:   1 | L001 | Unnecessary trailing whitespace.
All Finished 📜 🎉!

試しにsqlfluff fixを実行したところ以下に自動修正されたが、これではインデントがあっていない。

select
    column_1,
    column_2,
{{ test_macro('test') }} as column_3
from source

エラーが発生した環境

dbt-bigquery==1.1.0
dbt-core==1.1.2
sqlfluff==1.3.2

.sqlfluffの設定

.sqlfluff
[sqlfluff]
dialect = bigquery
templater = jinja

[sqlfluff:templater:jinja]
load_macros_from_path = macros

[sqlfluff:indentation]
indented_joins = True
indented_using_on = True

[sqlfluff:rules]
tab_space_size = 4
max_line_length = 80
indent_unit = space
comma_style = trailing
single_table_references = consistent

対応

以下のように、macroのmacro, endmacroにダッシュ-が入れることでエラーが解消した。ちなみに、ダッシュを入れることにより、compile後のクエリも構成が少し変わるので確認すると面白い。

macros/test_macro.sql
{% macro test_macro() -%} -- 末尾にマイナスを入れる
'test'
{%- endmacro %} -- 頭にマイナスを入れる

エラーが発生した原因

TBD
jinajaではダッシュをスペースの制御に使っているとこのこと。その関係でエラーが発生していると思われる。ただ、sqlfluffがどのようにマクロをパースして、上記エラーがでるのかは現在調査中。

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0