4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LookerのLiquid変数の仕様変更に対応する

Last updated at Posted at 2021-05-25

前提

Lookerではフィルタの内容をLiquid変数として参照することができます(参照)。
リンク先にあるように使用の際にはsql_quoteを使用することが推奨されているため、if文で使う時には以下のように記述していました。

{% if _filters['view_name.field_name'] contains 'Tokyo' | sql_quote %}
 'Tokyo'
{% endif %}

アップデート以降の警告内容

 ゴールデンウィーク明けから、以下のような警告が出るようになりました。

This liquid does not conform to the Liquid language spec and will produce warnings or errors in a future version of Looker. Parse error was: Error parsing liquid: Liquid parse exception: parser error "mismatched input '|' expecting {TagEnd, '.', NEq, '==', '>=', '>', '<=', '<', '[', '?', 'contains', 'and', 'or'}" on line 000, index 000

Liquid文の中で|を使うなということのようですが、先述の通りLookerからはフィルタの内容を参照する時にはsql_quoteを推奨されており、一見矛盾となってしまいます。

解決方法

警告文を調べたところ、Looker公式のこちらの記事がヒットし、下記の記述を見つけました。

Assigning the output of the function chain to a variable and including that variable in the if conditional should resolve the warning.

if文の中で直接フィルタ内容を参照するのではなく、一旦別の変数にassignしてからif文で使うように、と解釈したので、先ほどのコードを下記のように書き換えたところ、警告がなくなりました。

{% assign filtered_dimension = _filters['view_name.field_name'] | sql_quote %}
{% if filtered_dimension contains 'Tokyo' %}
 'Tokyo'
{% endif %}

コメント

上記の記事に解決法そのものが書いてはあるのですが、Liquid記法に不慣れなこともあり具体的なコードに落とし込むところで試行錯誤してしまったため、備忘録として記録します……

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?