title: spark sql where clause after select
body: <p>Consider the following spark-sql query:</p>
  
  <pre><code>  Seq(("b", 2), ("d", 4), ("a", 1), ("c", 3))
    .toDF("letter", "number")
    .select($"letter")
    .where($"number" &gt; 1)
    .show
  </code></pre>
  
  <p>The original query can even be split up, and the behavior remains:</p>
  
  <pre><code>val letters =
    Seq(("b", 2), ("d", 4), ("a", 1), ("c", 3))
    .toDF("letter", "number")
    .select($"letter")
  
  letters
    .where($"number" &gt; 1)
    .show
  </code></pre>
  
  <p>This looks to be related to lazy loading, but, I'm not sure exactly what is happening here.</p>
  
  <p>Why is it possible to include <code>$"number"</code> in the where clause, when only letter should remain?</p>
  
  <p><strong>EDIT 1</strong>
  Here is the explain:</p>
  
  <pre><code>letters.explain(true)
  == Parsed Logical Plan ==
  'Project [unresolvedalias('letter, None)]
  +- Project [_1#76942 AS letter#76955, _2#76943 AS number#76956]
     +- LocalRelation [_1#76942, _2#76943]
  
  == Analyzed Logical Plan ==
  letter: string
  Project [letter#76955]
  +- Project [_1#76942 AS letter#76955, _2#76943 AS number#76956]
     +- LocalRelation [_1#76942, _2#76943]
  
  == Optimized Logical Plan ==
  LocalRelation [letter#76955]
  
  == Physical Plan ==
  LocalTableScan [letter#76955]
  </code></pre>