Generic composable Ecto query w/ dynamic field name in query expression : <p>I'm trying to allow for passing in a field name and running it in an Ecto query expression dynamically, like so:</p>

<pre><code>def count_distinct(query, field_name) when is_binary(field_name) do
  query
  |&gt; select([x], count(Map.fetch!(x, field_name), :distinct))
end
</code></pre>

<p>However, I get this compilation error:</p>

<pre><code>(Ecto.Query.CompileError) `Map.fetch!(x, field_name)` is not a valid query expression
</code></pre>

<p>Is there any way to accomplish this?</p>
