Compose the ON portion of a JOIN?

Say I have a join like:

|> join(:right, [a], b in some_table, on: a.inserted_at == ^time and a.thing == ^thing)

I want to only do the a.thing == ^thing if the value of ^thing is not nil. Is there a way to conditionally add the a.thing == ^thing part? I know I can compose the on using dynamic fragments where I get a dynamic fragment with or without the part in question using a conditional or function overloading like:

def find_stuff(time, thing) do
   dynamic = get_on(time, thing)

   Queryable
   ...
   |> join(:right, [a], b in some_table, on: ^dynamic)
   ...
end

def get_on(time, thing) when is_nil(thing), 
   do: dynamic([a], a.inserted_at == ^time)

def get_on(time, thing),
   do: dynamic([a], a.inserted_at == ^time and a.thing == ^thing)

However, I’m looking for a way where I don’t have to rewrite the the on part multiple times to get a version with and without a.thing == ^thing because in reality its kinda long.

Alternatively, is there some syntax I could use like a.thing == * where * is a wildcard and matches everything? That way I could just present ^thing as its actual value if present and then the wildcard if it is nil like:

def find_stuff(thing) do
   thing_value = get_value(thing)

   Queryable
   ...
   |> join(:right, [a], b in some_table, on: a.inserted_at == ^time and a.thing == ^thing_value)
   ...
end

def get_value(thing) when is_nil(thing), do: the_wild_card_thing
def get_value(thing), do: thing
def get_on(time, thing) when is_nil(thing), 
   do: dynamic([a], a.inserted_at == ^time)

def get_on(time, thing do
  base = get_on(time, nil)
  dynamic([a], ^base and a.thing == ^thing)
end

ahhh…thanks worked like a charm!