title: Is it possible to alias columns programmatically in spark sql?
body: <p>In spark SQL (perhaps only HiveQL) one can do:</p>
  
  <pre><code>select sex, avg(age) as avg_age
  from humans
  group by sex
  </code></pre>
  
  <p>which would result in a <code>DataFrame</code> with columns named <code>"sex"</code> and <code>"avg_age"</code>.</p>
  
  <p>How can <code>avg(age)</code> be aliased to <code>"avg_age"</code> without using textual SQL?</p>
  
  <p><strong>Edit:</strong>
  After zero323 's answer, I need to add the constraint that:</p>
  
  <p><em>The column-to-be-renamed's name may not be known/guaranteed or even addressable</em>. In textual SQL, using "select EXPR as NAME" removes the requirement to have an intermediate name for EXPR. This is also the case in the example above, where "avg(age)" could get a variety of auto-generated names (which also vary among spark releases and sql-context backends).</p>