title: Spark SQL - reading csv with schema
body: <p>I encountered the problem while trying to use Spark for simple reading a CSV file. After such operation I would like to ensure that:</p>
  
  <ul>
  <li>the data types are correct (with using provided schema)</li>
  <li>the headers are correct against provided schema</li>
  </ul>
  
  <p>That's the code I use and have problems with:</p>
  
  <pre><code>val schema = Encoders.product[T].schema
  val df = spark.read
   .schema(schema)
   .option("header", "true")
   .csv(fileName)
  </code></pre>
  
  <p>The type <code>T</code> is of type <code>Product</code>, i. e. case class. This works but it doesn't check if <strong>column names</strong> are correct, so I can give another file and as long as the data types are correct the error doesn't occur and I am unaware that the user provided the wrong file but by some coincidence with correct data types with the proper ordering.</p>
  
  <p>I tried to use options which infers the schema and then use <code>.as[T]</code> method on Dataset, but in case when any column other than <code>String</code> contains only null it's interpreted by Spark as <code>String</code> column, but in my schema it is <code>Integer</code>. So cast exception occurs, but column names has been checked all right. </p>
  
  <p>To summarize: I found solution that I can ensure correct data types but no headers and other solution that I can validate headers but have problems with data types. Is there any way to achieve both, i. e. complete validation of headers and types?</p>
  
  <p>I am using Spark 2.2.0.</p>