title: What are the differences between saveAsTable and insertInto in different SaveMode(s)?
body: <p>I'm trying to write a <code>DataFrame</code> into <code>Hive</code> table (on <code>S3</code>) in <code>Overwrite</code> mode (necessary for my application) and need to decide between two methods of DataFrameWriter (Spark / Scala). From what I can read in the <a href="https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.DataFrameWriter" rel="noreferrer">documentation</a>, <code>df.write.saveAsTable</code> differs from <code>df.write.insertInto</code> in the following respects:</p>
  
  <ul>
  <li><code>saveAsTable</code> uses <strong>column-name based resolution</strong> while <code>insertInto</code> uses <strong>position-based resolution</strong></li>
  <li>In Append mode, <code>saveAsTable</code> pays more attention to underlying schema of the existing table to make certain resolutions</li>
  </ul>
  
  <p>Overall, it gives me the impression that <code>saveAsTable</code> is just a <em>smarter version</em> of <code>insertInto</code>. Alternatively, depending on use-case, one might prefer <code>insertInto</code></p>
  
  <p>But do each of these methods come with some caveats of their own like performance penalty in case of <code>saveAsTable</code> (since it packs in more features)? Are there any other differences in their behaviours apart from what is told (not very clearly) in the docs?</p>
  
  <hr>
  
  <p><strong>EDIT-1</strong></p>
  
  <p>Documentation says this regarding <code>insertInto</code></p>
  
  <blockquote>
    <p>Inserts the content of the DataFrame to the specified table</p>
  </blockquote>
  
  <p>and this for <code>saveAsTable</code></p>
  
  <blockquote>
    <p>In the case the table already exists, behavior of this function
    depends on the save mode, specified by the mode function</p>
  </blockquote>
  
  <p>Now I can list my doubts</p>
  
  <ul>
  <li>Does <code>insertInto</code> always expect the table to exist?</li>
  <li>Do <code>SaveMode</code>s have any impact on <code>insertInto</code>?</li>
  <li>If above answer is yes, then
  
  <ul>
  <li>what's the differences between <code>saveAsTable</code> with <code>SaveMode.Append</code> and <code>insertInto</code> given that table already exists?</li>
  <li>does <code>insertInto</code> with <code>SaveMode.Overwrite</code> make any sense?</li>
  </ul></li>
  </ul>