title: Spark SQL error AnalysisException: cannot resolve column_name
body: <p>This is a common error in Spark SQL, I tried all other answers but no differences!
  I want to read the following small CSV file from HDFS (or even local filesystem).</p>
  
  <pre><code>+----+-----------+----------+-------------------+-----------+------+------+------+------+------+
  |  id| first_name| last_name|                ssn|      test1| test2| test3| test4| final| grade|
  +----+-----------+----------+-------------------+-----------+------+------+------+------+------+
  | 4.0|      Dandy|       Jim|        087-75-4321|       47.0|   1.0|  23.0|  36.0|  45.0|    C+|
  |13.0|   Elephant|       Ima|        456-71-9012|       45.0|   1.0|  78.0|  88.0|  77.0|    B-|
  |14.0|   Franklin|     Benny|        234-56-2890|       50.0|   1.0|  90.0|  80.0|  90.0|    B-|
  |15.0|     George|       Boy|        345-67-3901|       40.0|   1.0|  11.0|  -1.0|   4.0|     B|
  |16.0|  Heffalump|    Harvey|        632-79-9439|       30.0|   1.0|  20.0|  30.0|  40.0|     C|
  +----+-----------+----------+-------------------+-----------+------+------+------+------+------+
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>List&lt;String&gt; cols = new ArrayList&lt;&gt;();
          Collections.addAll(cols, "id, first_name".replaceAll("\\s+", "").split(","));
  Dataset&lt;Row&gt; temp = spark.read()
                  .format("org.apache.spark.csv")
                  .option("header", true)
                  .option("inferSchema", true)
                  .option("timestampFormat", "yyyy/MM/dd HH:mm:ss ZZ")
                  .csv(path)
                  .selectExpr(JavaConverters.asScalaIteratorConverter(cols.iterator()).asScala().toSeq());
  </code></pre>
  
  <p>but it errors:</p>
  
  <pre><code>Exception in thread "main" org.apache.spark.sql.AnalysisException: resolved attribute(s) 'first_name missing from  last_name#14,      test1#16,id#12, test4#19, ssn#15, test3#18, grade#21, test2#17, final#20, first_name#13 in operator 'Project [id#12, 'first_name];;
  'Project [id#12, 'first_name]
  +- Relation[id#12, first_name#13, last_name#14, ssn#15,      test1#16, test2#17, test3#18, test4#19, final#20, grade#21] csv
  </code></pre>
  
  <p>In some cases it works without error:</p>
  
  <ol>
  <li>If I didn't select anything, it will successfully get all data.</li>
  <li>If I just select column "id".</li>
  </ol>
  
  <p>Even I tried using <em>views</em> and <em>SQL</em> method:</p>
  
  <pre><code>df.createOrReplaceTempView("csvFile");
  spark.sql(SELECT id, first_name FROM csvFile).show()
  </code></pre>
  
  <p>but I got the same errro!</p>
  
  <p>I did the same with the same data that I read from the database, and it was without any error.</p>
  
  <p>I use Spark <code>2.2.1</code>.</p>