There is redundancy between this function and other parts of our SQL generation. Not sure if its worth refactoring or not. @szeiger WDYT?