Hi Rafael,
To process a query, data base may need a certain volume of memory.
These memory could be used as building some intermediate results or some other internal structures, such as hash tables.
The error you mentioned happens when the system do not have enough memory for processing the query.
One of easiest workaround is to run the query on a machine with more memory installed.
Another handy way is restart DB and run the query.
In this way, only tables related to this query will be loaded into memory.
So your budget for processing this query will be larger.
If nothing works, there might something go crazy inside of the query, maybe an engine bug or a model bug.
Best Wishes,
Wu Di