Skip to content

Commit b931417

Browse files
rxinjeanlyn
authored andcommitted
[SPARK-8026][SQL] Add Column.alias to Scala/Java DataFrame API
Author: Reynold Xin <[email protected]> Closes apache#6565 from rxin/alias and squashes the following commits: 286d880 [Reynold Xin] [SPARK-8026][SQL] Add Column.alias to Scala/Java DataFrame API
1 parent c93d605 commit b931417

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

sql/core/src/main/scala/org/apache/spark/sql/Column.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,18 @@ class Column(protected[sql] val expr: Expression) extends Logging {
716716
*/
717717
def endsWith(literal: String): Column = this.endsWith(lit(literal))
718718

719+
/**
720+
* Gives the column an alias. Same as `as`.
721+
* {{{
722+
* // Renames colA to colB in select output.
723+
* df.select($"colA".alias("colB"))
724+
* }}}
725+
*
726+
* @group expr_ops
727+
* @since 1.4.0
728+
*/
729+
def alias(alias: String): Column = as(alias)
730+
719731
/**
720732
* Gives the column an alias.
721733
* {{{

sql/core/src/test/scala/org/apache/spark/sql/ColumnExpressionSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ import org.apache.spark.sql.types._
2727
class ColumnExpressionSuite extends QueryTest {
2828
import org.apache.spark.sql.TestData._
2929

30+
test("alias") {
31+
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
32+
assert(df.select(df("a").as("b")).columns.head === "b")
33+
assert(df.select(df("a").alias("b")).columns.head === "b")
34+
}
35+
3036
test("single explode") {
3137
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
3238
checkAnswer(

0 commit comments

Comments
 (0)