rmoorman

rmoorman

Summary

I am currently trying to get a better understanding of elixir macros and in doing so encountered the section about dynamic queries in the ecto documentation. The example, however, leads to an unwanted true and part in the ecto query where and I am trying to have a look at how to solve that.

The trimmed down documentation example

So I tried out the example given and it seems to add a superfluous true and in front of the where clause. I trimmed the example down and put it inside a testcase:

defmodule MyMacroTest do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.reduce(params, dynamic(true), fn
      {"author", value}, dynamic ->
        dynamic([p], ^dynamic and p.author == ^value)
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end

which fails with

     Assertion with == failed
     code:  assert inspect(where(@q, ^filter_where(query))) == inspect(where(@q, [p], p.author == ^query["author"]))
     left:  "#Ecto.Query<from p0 in \"post\", where: true and p0.author == ^\"foo\">"
     right: "#Ecto.Query<from p0 in \"post\", where: p0.author == ^\"foo\">"

The fancy if statement :see_no_evil:

Switching out the dynamic(true) within the call to Enum.reduce/3 with nil and using if seems to work

defmodule MyMacro2Test do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.reduce(params, nil, fn
      {"author", value}, dynamic ->
        if dynamic do
          dynamic([p], ^dynamic and p.author == ^value)
        else
          dynamic([p], p.author == ^value)
        end
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end
Finished in 0.04 seconds (0.04s async, 0.00s sync)
1 test, 0 failures

Even though it works, it is not a thing of beauty and given that the dynamic macro basically outputs AST similar to what I would like, writing another macro appears to be the correct thing to do.

But here is the part I seem to struggle with

I now don’t know how to exactly extract that conditional into a macro. I guess I will need a macro because I need to wrap the dynamic macro and pass through the arguments in the same syntax.
Currently stuck with this version

defmodule MyMacroTest3 do
  use ExUnit.Case, async: true
  import Ecto.Query

  defmacro dynamic_and(dynamic, bindings, expr) do
    quote do
      if unquote(dynamic) do
        dynamic(unquote(bindings), unquote(dynamic) and unquote(expr))
      else
        dynamic(unquote(bindings), unquote(expr))
      end
    end
  end

  def filter_where(params) do
    Enum.reduce(params, nil, fn
      {"author", value}, dynamic ->
        dynamic_and(^dynamic, [p], p.author == ^value)
    end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))
  end
end

which gives me a compilation error about a misplaced operator ^dynamic

    The pin operator ^ is supported only inside matches or inside custom macros. Make sure you are inside a match or all necessary macros have been required
    │
 18 │         dynamic_and(^dynamic, [p], p.author == ^value)

I tried multiple variations of pinning and unquote/not unquote but I somehow currently fail to see where to look next for the correct way of solving this.
I also thought that a potential solution could involve returning some AST directly, or maybe it would be better to try to construct an %Ecto.Query.DynamicExpr struct myself.
Also got myself a copy of that Metaprogramming book and I am currently reading it hoping that it might help, and even got some bots involved :sweat_smile:

Anyway, I would really apreciate any pointers that could be thrown at me and thank you very much in advance! :slight_smile:

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

The solution here is Enum.map, Enum.reduce/2 over Enum.reduce/3:

params
|> Enum.map(fn 
  {"author", value} -> dynamic([p], p.author == ^value)
end)
|> Enum.reduce(fn a, b -> dynamic(^a and ^b) end)
rmoorman

rmoorman OP

Nice!!! Thank you so much @LostKobrakai!

Macros look so magical sometimes …
So all that was needed was to build a list of dynamic conditions through map and reducing them into a single dynamic expression.

One more question building on this. I adjusted the code like this following your suggestion:

defmodule MyMacro4Test do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.map(params, fn
      {"author", value} -> dynamic([p], p.author == ^value)
      {"category", value} -> dynamic([p], p.category == ^value)
      {"title", value} -> dynamic([p], p.title == ^value)
    end)
    |> Enum.reverse()
    |> Enum.reduce(fn a, b -> dynamic(^a and ^b) end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))

    query = %{"author" => "foo", "category" => "macros", "title" => "the secret sauce"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p],
                p.author == ^query["author"]
                and p.category == ^query["category"]
                and p.title == ^query["title"]))
  end
end

It works in order to build a functional query for the filter struct given. But there is one thing still bothering me a bit (for the sake of macros I guess :smiley:).

     Assertion with == failed
     code:  assert inspect(where(@q, ^filter_where(query))) ==
              inspect(
                where(
                  @q,
                  [p],
                  p.author == ^query["author"] and p.category == ^query["category"] and
                    p.title == ^query["title"]
                )
              )
     left:  "#Ecto.Query<from p0 in \"post\", where: p0.author == ^\"foo\" and (p0.category == ^\"macros\" and p0.title == ^\"the secret sauce\")>"
     right: "#Ecto.Query<from p0 in \"post\", where: p0.author == ^\"foo\" and p0.category == ^\"macros\" and p0.title == ^\"the secret sauce\">"

Note the additional ( and ) braces. I guess that is inherent to the way the dynamic expressions are folded into one.

Even though it seems to me that a and (b and c) and a and b and c are equivalent, is there a way to somehow construct an AST of and expressions and put that inside a single final call to dynamic?

LostKobrakai

LostKobrakai

You shouldn’t be testing queries based on their internals. They might change at any time. Also e.g. reordering your conditions would break your tests even though the query would be functionally equal to before. You don’t want brittle tests like that.

rmoorman

rmoorman OP

I can see that and testing it like that only served exploration purposes :slight_smile:

Furthermore I think I might have mixed up compile time vs run time with regards to a bit there too. So it may even not be possible at all to eliminate the braces :sweat_smile: (even if it was only for cosmetic purposes).

I guess I will have to dive deeper into macros soon to get a firmer grip on how they work.

Anyway, thank you very much for your time @LostKobrakai!

gregvaughn

gregvaughn

I love the Enum.reduce/2 approach, but from past experience, I recommend you protect against an empty list, because it’ll raise.

rmoorman

rmoorman OP

@LostKobrakai … you know what? I literally just had to adjust that call to dynamic from dynamic(^a and ^b) to dynamic(^b and ^a) :rofl:

for reference …

defmodule MyMacro4Test do
  use ExUnit.Case, async: true
  import Ecto.Query

  def filter_where(params) do
    Enum.map(params, fn
      {"author", value} -> dynamic([p], p.author == ^value)
      {"category", value} -> dynamic([p], p.category == ^value)
      {"title", value} -> dynamic([p], p.title == ^value)
    end)
    |> Enum.reduce(fn a, b -> dynamic(^b and ^a) end)
  end

  @q from(p in "post")

  test "author" do
    query = %{"author" => "foo"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p], p.author == ^query["author"]))

    query = %{"author" => "foo", "category" => "macros", "title" => "the secret sauce"}
    assert inspect(where(@q, ^filter_where(query))) ==
             inspect(where(@q, [p],
                p.author == ^query["author"]
                and p.category == ^query["category"]
                and p.title == ^query["title"]))
  end
end

Cannot wait to fully understand macros :slight_smile:

rmoorman

rmoorman OP

@gregvaughn I will take care to correctly check the list of valid query conditions before proceeding with the rest of the filter logic. Good advice! Thank you.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews