Fixes on the optimization for the joins