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 @@ -95,30 +95,24 @@ object ResolveInlineTables extends Rule[LogicalPlan]
private[analysis] def findCommonTypesAndCast(table: UnresolvedInlineTable):
ResolvedInlineTable = {
// For each column, traverse all the values and find a common data type and nullability.
val fields = table.rows.transpose.zip(table.names).map { case (column, name) =>
val (fields, columns) = table.rows.transpose.zip(table.names).map { case (column, name) =>
val inputTypes = column.map(_.dataType)
val tpe = TypeCoercion.findWiderTypeWithoutStringPromotion(inputTypes).getOrElse {
table.failAnalysis(
errorClass = "INVALID_INLINE_TABLE.INCOMPATIBLE_TYPES_IN_INLINE_TABLE",
messageParameters = Map("colName" -> toSQLId(name)))
}
StructField(name, tpe, nullable = column.exists(_.nullable))
}
val attributes = DataTypeUtils.toAttributes(StructType(fields))
assert(fields.size == table.names.size)

val castedRows: Seq[Seq[Expression]] = table.rows.map { row =>
row.zipWithIndex.map {
case (e, ci) =>
val targetType = fields(ci).dataType
val castedExpr = if (DataTypeUtils.sameType(e.dataType, targetType)) {
e
} else {
cast(e, targetType)
}
castedExpr
val newColumn = column.map {
case expr if DataTypeUtils.sameType(expr.dataType, tpe) =>
expr
case expr =>
cast(expr, tpe)
}
}
(StructField(name, tpe, nullable = column.exists(_.nullable)), newColumn)
}.unzip
assert(fields.size == table.names.size)
val attributes = DataTypeUtils.toAttributes(StructType(fields))
val castedRows: Seq[Seq[Expression]] = columns.transpose

ResolvedInlineTable(castedRows, attributes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ class ResolveInlineTablesSuite extends AnalysisTest with BeforeAndAfter {

test("cast and execute") {
val table = UnresolvedInlineTable(Seq("c1"), Seq(Seq(lit(1)), Seq(lit(2L))))
val resolved = ResolveInlineTables.findCommonTypesAndCast(table)
val converted = ResolveInlineTables.earlyEvalIfPossible(resolved).asInstanceOf[LocalRelation]
val resolved = ResolveInlineTables(table)
assert(resolved.isInstanceOf[LocalRelation])
val converted = resolved.asInstanceOf[LocalRelation]

assert(converted.output.map(_.dataType) == Seq(LongType))
assert(converted.data.size == 2)
Expand All @@ -98,12 +99,11 @@ class ResolveInlineTablesSuite extends AnalysisTest with BeforeAndAfter {
test("cast and execute CURRENT_LIKE expressions") {
val table = UnresolvedInlineTable(Seq("c1"), Seq(
Seq(CurrentTimestamp()), Seq(CurrentTimestamp())))
val casted = ResolveInlineTables.findCommonTypesAndCast(table)
val earlyEval = ResolveInlineTables.earlyEvalIfPossible(casted)
val resolved = ResolveInlineTables(table)
// Early eval should keep it in expression form.
assert(earlyEval.isInstanceOf[ResolvedInlineTable])
assert(resolved.isInstanceOf[ResolvedInlineTable])

EvalInlineTables(ComputeCurrentTime(earlyEval)) match {
EvalInlineTables(ComputeCurrentTime(resolved)) match {
case LocalRelation(output, data, _) =>
assert(output.map(_.dataType) == Seq(TimestampType))
assert(data.size == 2)
Expand Down