Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -2454,7 +2454,7 @@ class Analyzer(
} else {
// always add an UpCast. it will be removed in the optimizer if it is unnecessary.

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.

comment needs update.

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.

Also, we might need to update the comment in the header;

   * - Insert safe casts when data types do not match

https://github.com/apache/spark/pull/25239/files#diff-57b3d87be744b7d79a9beacf8e5e5eb2R2353

Some(Alias(
UpCast(queryExpr, tableAttr.dataType), tableAttr.name
Cast(queryExpr, tableAttr.dataType), tableAttr.name
)(
explicitMetadata = Option(tableAttr.metadata)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,19 +442,34 @@ object DataType {
fieldCompatible

case (w: AtomicType, r: AtomicType) =>
if (!Cast.canUpCast(w, r)) {
addError(s"Cannot safely cast '$context': $w to $r")
if (!canWriteAtomicType(w, r)) {
addError(s"Cannot assign '$context': $w to $r")
false
} else {
true
}

case (w, r) if w.sameType(r) && !w.isInstanceOf[NullType] =>
case (NullType, _) => true

case (w, r) if w.sameType(r) =>
true

case (w, r) =>
addError(s"Cannot write '$context': $w is incompatible with $r")
false
}
}

private def canWriteAtomicType(from: AtomicType, to: AtomicType): Boolean = (from, to) match {
case _ if from == to => true
case (_: NumericType, _: NumericType) => true
case (_, StringType) => true
case (DateType, TimestampType) => true
case (TimestampType, DateType) => true
// Spark supports casting between long and timestamp, please see `longToTimestamp` and
// `timestampToLong` for details.
case (TimestampType, LongType) => true
case (LongType, TimestampType) => true
case _ => false
}
}