[SPARK-2059][SQL] Add analysis checks

This replaces #1263 with a test case.