Not every writer goes into their story with an ending in mind, and good ones know that even if they do have an intended ending, it might change as they get deeper into the story. Sometimes, a director will watch a rough cut of their movie and realize that their intended ending isn't working, and other times, test audiences will react negatively, but re-shoots are needed either way.