diff options
Diffstat (limited to 'sql/core')
3 files changed, 7 insertions, 8 deletions
diff --git a/sql/core/src/main/scala/org/apache/spark/sql/Column.scala b/sql/core/src/main/scala/org/apache/spark/sql/Column.scala index 1f826887ac..37d559c8e4 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/Column.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/Column.scala @@ -835,8 +835,8 @@ class Column(protected[sql] val expr: Expression) extends Logging { * @since 1.3.0 */ def cast(to: DataType): Column = expr match { - // Lift alias out of cast so we can support col.as("name").cast(IntegerType) - case Alias(childExpr, name) => Alias(Cast(childExpr, to), name)() + // keeps the name of expression if possible when do cast. + case ne: NamedExpression => UnresolvedAlias(Cast(expr, to)) case _ => Cast(expr, to) } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/ColumnExpressionSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/ColumnExpressionSuite.scala index 4e988f074b..fa559c9c64 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/ColumnExpressionSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/ColumnExpressionSuite.scala @@ -588,12 +588,6 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext { } } - test("lift alias out of cast") { - compareExpressions( - col("1234").as("name").cast("int").expr, - col("1234").cast("int").as("name").expr) - } - test("columns can be compared") { assert('key.desc == 'key.desc) assert('key.desc != 'key.asc) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala index 832ea02cb6..6424f1f1d9 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala @@ -975,4 +975,9 @@ class DataFrameSuite extends QueryTest with SharedSQLContext { expected(except) ) } + + test("SPARK-10743: keep the name of expression if possible when do cast") { + val df = (1 to 10).map(Tuple1.apply).toDF("i").as("src") + assert(df.select($"src.i".cast(StringType)).columns.head === "i") + } } |