Background
For investigation purposes, I am trying to convert a piece of code from another thread from keyword-based-query to a macro-based-query. I am doing this because I am interested in finding out how fast it is.
Code
tops =
from top in "file_info_with_counts",
where: top.home_id == parent_as(:parent).home_id,
where: top.path == parent_as(:parent).path,
order_by: [asc: top.item_id],
limit: 3,
select: %{id: top.id}
from parent in "file_info_with_counts",
as: :parent,
group_by: [parent.home_id, parent.path],
lateral_join: top in subquery(tops),
on: true,
select: %{home_id: parent.home_id, path: parent.path, item_id: top.id}
My original attempts successfully convert the tops
subquery, but I can’t convert the other one:
tops =
"file_info_with_counts"
|> where(
[fi],
fi.home_id == parent_as(:parent).home_id and
fi.path == parent_as(:parent).path
)
|> order_by([fi], asc: fi.id)
|> limit(3)
|> select([fi], %{
item_id: fi.id,
home_id: fi.home_id,
path: fi.path
})
|> subquery()
# cant think of a proper name for this really
parent_q =
"file_info_with_counts"
|> join(:inner_lateral, [item], top in ^tops, on: true)
|> group_by([item], [parent_as(:parent).home_id, parent_as(:parent).path])
|> select([item], %{
home_id: parent_as(:item).home_id,
path: parent_as(:item).path,
item_id: item.item_id
})
|> subquery()
Clearly I am doing something wrong, as not matter what I do, I can never define :parent
. How can I fix this?