Skip to content
Snippets Groups Projects
Commit 2cbe96e6 authored by Takuya UESHIN's avatar Takuya UESHIN Committed by Reynold Xin
Browse files

[SPARK-15400][SQL] CreateNamedStruct and CreateNamedStructUnsafe should...

[SPARK-15400][SQL] CreateNamedStruct and CreateNamedStructUnsafe should preserve metadata of value expressions if it is NamedExpression.

## What changes were proposed in this pull request?

`CreateNamedStruct` and `CreateNamedStructUnsafe` should preserve metadata of value expressions if it is `NamedExpression` like `CreateStruct` or `CreateStructUnsafe` are doing.

## How was this patch tested?

Existing tests.

Author: Takuya UESHIN <ueshin@happy-camper.st>

Closes #13193 from ueshin/issues/SPARK-15400.
parent e8adc552
No related branches found
No related tags found
No related merge requests found
......@@ -252,9 +252,13 @@ case class CreateNamedStruct(children: Seq[Expression]) extends Expression {
private lazy val names = nameExprs.map(_.eval(EmptyRow))
override lazy val dataType: StructType = {
val fields = names.zip(valExprs).map { case (name, valExpr) =>
StructField(name.asInstanceOf[UTF8String].toString,
valExpr.dataType, valExpr.nullable, Metadata.empty)
val fields = names.zip(valExprs).map {
case (name, valExpr: NamedExpression) =>
StructField(name.asInstanceOf[UTF8String].toString,
valExpr.dataType, valExpr.nullable, valExpr.metadata)
case (name, valExpr) =>
StructField(name.asInstanceOf[UTF8String].toString,
valExpr.dataType, valExpr.nullable, Metadata.empty)
}
StructType(fields)
}
......@@ -365,8 +369,11 @@ case class CreateNamedStructUnsafe(children: Seq[Expression]) extends Expression
private lazy val names = nameExprs.map(_.eval(EmptyRow).toString)
override lazy val dataType: StructType = {
val fields = names.zip(valExprs).map { case (name, valExpr) =>
StructField(name, valExpr.dataType, valExpr.nullable, Metadata.empty)
val fields = names.zip(valExprs).map {
case (name, valExpr: NamedExpression) =>
StructField(name, valExpr.dataType, valExpr.nullable, valExpr.metadata)
case (name, valExpr) =>
StructField(name, valExpr.dataType, valExpr.nullable, Metadata.empty)
}
StructType(fields)
}
......
......@@ -228,4 +228,22 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
checkErrorMessage(structType, IntegerType, "Field name should be String Literal")
checkErrorMessage(otherType, StringType, "Can't extract value from")
}
test("ensure to preserve metadata") {
val metadata = new MetadataBuilder()
.putString("key", "value")
.build()
def checkMetadata(expr: Expression): Unit = {
assert(expr.dataType.asInstanceOf[StructType]("a").metadata === metadata)
assert(expr.dataType.asInstanceOf[StructType]("b").metadata === Metadata.empty)
}
val a = AttributeReference("a", IntegerType, metadata = metadata)()
val b = AttributeReference("b", IntegerType)()
checkMetadata(CreateStruct(Seq(a, b)))
checkMetadata(CreateNamedStruct(Seq("a", a, "b", b)))
checkMetadata(CreateStructUnsafe(Seq(a, b)))
checkMetadata(CreateNamedStructUnsafe(Seq("a", a, "b", b)))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment