Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
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 @@ -131,7 +131,8 @@ object Cast {
case (from: DecimalType, to: NumericType) if from.isTighterThan(to) => true
case (f, t) if legalNumericPrecedence(f, t) => true
case (DateType, TimestampType) => true
case (_, StringType) => true
case (_: AtomicType, StringType) => true
case (_: CalendarIntervalType, StringType) => true
Comment thread
gengliangwang marked this conversation as resolved.

// Spark supports casting between long and timestamp, please see `longToTimestamp` and
// `timestampToLong` for details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,43 @@ class EncoderResolutionSuite extends PlanTest {
encoder.resolveAndBind(attrs)
}

test("SPARK-28497: complex type is not compatible with string encoder schema") {
val encoder = ExpressionEncoder[String]

{

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.

Each block might be easily extracted with inner function to remove duplications. attr (element in attrs), and string representation of attr type are different and remaining is exactly same.

val attrs = Seq('a.struct('x.long))
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""

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.

super nit: its ok just check .contains("Cannot up cast a from struct<x:bigint> to string")?

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.

+1

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 am OK with either way. I was following the other test cases in the suite. Let me change it to .contains...

|Cannot up cast `a` from struct<x:bigint> to string.
|The type path of the target object is:
|- root class: "java.lang.String"
|You can either add an explicit cast to the input data or choose a higher precision type
""".stripMargin.trim + " of the field in the target object")
}

{
val attrs = Seq('a.array(StringType))
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""
|Cannot up cast `a` from array<string> to string.

@HyukjinKwon HyukjinKwon Jul 25, 2019

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.

It doesn't necessarily compare the whole message. We can check if the message contains some keywords.

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.

Oh, it was the same comment as #25242 (comment)

|The type path of the target object is:
|- root class: "java.lang.String"
|You can either add an explicit cast to the input data or choose a higher precision type
""".stripMargin.trim + " of the field in the target object")
}

{
val attrs = Seq('a.map(StringType, StringType))
assert(intercept[AnalysisException](encoder.resolveAndBind(attrs)).message ==
s"""
|Cannot up cast `a` from map<string,string> to string.
|The type path of the target object is:
|- root class: "java.lang.String"
|You can either add an explicit cast to the input data or choose a higher precision type
""".stripMargin.trim + " of the field in the target object")
}
}

test("throw exception if real type is not compatible with encoder schema") {
val msg1 = intercept[AnalysisException] {
ExpressionEncoder[StringIntClass].resolveAndBind(Seq('a.string, 'b.long))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,11 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
}
}
}
numericTypes.foreach { dt =>
makeComplexTypes(dt, true).foreach { complexType =>
assert(!Cast.canUpCast(complexType, StringType))
}
}
}

test("SPARK-27671: cast from nested null type in struct") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,9 @@ class UserDefinedTypeSuite extends QueryTest with SharedSQLContext with ParquetT
val ret = Cast(Literal(data, udt), StringType, None)
checkEvaluation(ret, "(1.0, 3.0, 5.0, 7.0, 9.0)")
}

test("SPARK-28497 Can't up cast UserDefinedType to string") {
val udt = new TestUDT.MyDenseVectorUDT()
assert(!Cast.canUpCast(udt, StringType))
}
}