Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,29 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
builder.append("]")
builder.build()
})
case StructType(fields) =>
buildCast[InternalRow](_, row => {
val builder = new UTF8StringBuilder
builder.append("[")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any reasons for using the same brackets [] for Struct, Map and Array?

How about [] for arrays, {} for structs and <> for maps or somehow distinguish them. At least, it is not consistent with toHiveString which prints {} for structs and maps.

@maropu maropu Jul 29, 2020

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have no strong reason. In this PR, I just followed the existing format. Looks like we can change it to { for consistency (But, we might need to update the migration doc cuz it change the casting behaivour). cc: @cloud-fan

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with the new format.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the PR #29308 . Please, review it.

if (row.numFields > 0) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, it seems we have no chance to hit row.numFields == 0 here though, I just leave this for strict checks.

val st = fields.map(_.dataType)
val toUTF8StringFuncs = st.map(castToString)
if (!row.isNullAt(0)) {
builder.append(toUTF8StringFuncs(0)(row.get(0, st(0))).asInstanceOf[UTF8String])
}
var i = 1
while (i < row.numFields) {
builder.append(",")
if (!row.isNullAt(i)) {
builder.append(" ")
builder.append(toUTF8StringFuncs(i)(row.get(i, st(i))).asInstanceOf[UTF8String])
}
i += 1
}
}
builder.append("]")
builder.build()
})
case _ => buildCast[Any](_, o => UTF8String.fromString(o.toString))
}

Expand Down Expand Up @@ -732,6 +755,39 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
""".stripMargin
}

private def writeStructToStringBuilder(
st: Seq[DataType],
row: String,
buffer: String,
ctx: CodegenContext): String = {
val structToStringCode = st.zipWithIndex.map { case (ft, i) =>
val fieldToStringCode = castToStringCode(ft, ctx)
val funcName = ctx.freshName("fieldToString")
ctx.addNewFunction(funcName,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to create a function, it's called only once.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we can create functions, for data types that appeared more than once among struct fields.

@cloud-fan cloud-fan Jan 9, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW here we may hit 64kb compile error if there are a lot of fields in this struct. We should use ctx.splitExpressions

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, l'll update soon

s"""
|private UTF8String $funcName(${ctx.javaType(ft)} field) {
| UTF8String fieldStr = null;
| ${fieldToStringCode("field", "fieldStr", null /* resultIsNull won't be used */)}
| return fieldStr;
|}
""".stripMargin)

s"""
|${if (i != 0) s"""$buffer.append(",");""" else ""}
|if (!$row.isNullAt($i)) {
| ${if (i != 0) s"""$buffer.append(" ");""" else ""}
| $buffer.append($funcName(${ctx.getValue(row, ft, s"$i")}));
|}
""".stripMargin
}

s"""
|$buffer.append("[");
|${structToStringCode.mkString("\n")}
|$buffer.append("]");
""".stripMargin
}

private[this] def castToStringCode(from: DataType, ctx: CodegenContext): CastFunction = {
from match {
case BinaryType =>
Expand Down Expand Up @@ -765,6 +821,17 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
|$evPrim = $buffer.build();
""".stripMargin
}
case StructType(fields) =>
(c, evPrim, evNull) => {
val buffer = ctx.freshName("buffer")
val bufferClass = classOf[UTF8StringBuilder].getName
val writeStructCode = writeStructToStringBuilder(fields.map(_.dataType), c, buffer, ctx)
s"""
|$bufferClass $buffer = new $bufferClass();
|$writeStructCode;
|$evPrim = $buffer.build();
""".stripMargin
}
case _ =>
(c, evPrim, evNull) => s"$evPrim = UTF8String.fromString(String.valueOf($c));"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,20 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
StringType)
checkEvaluation(ret5, "[1 -> [1, 2, 3], 2 -> [4, 5, 6]]")
}

test("SPARK-22981 Cast struct to string") {
val ret1 = cast(Literal.create((1, "a", 0.1)), StringType)
checkEvaluation(ret1, "[1, a, 0.1]")
val ret2 = cast(Literal.create(Tuple3[Int, String, String](1, null, "a")), StringType)
checkEvaluation(ret2, "[1,, a]")
val ret3 = cast(Literal.create(
(Date.valueOf("2014-12-03"), Timestamp.valueOf("2014-12-03 15:05:00"))), StringType)
checkEvaluation(ret3, "[2014-12-03, 2014-12-03 15:05:00]")
val ret4 = cast(Literal.create(((1, "a"), 5, 0.1)), StringType)
checkEvaluation(ret4, "[[1, a], 5, 0.1]")
val ret5 = cast(Literal.create((Seq(1, 2, 3), "a", 0.1)), StringType)
checkEvaluation(ret5, "[[1, 2, 3], a, 0.1]")
val ret6 = cast(Literal.create((1, Map(1 -> "a", 2 -> "b", 3 -> "c"))), StringType)
checkEvaluation(ret6, "[1, [1 -> a, 2 -> b, 3 -> c]]")
}
}