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 @@ -20,13 +20,13 @@ package org.apache.spark.sql.catalyst.analysis
import scala.util.control.NonFatal

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{AliasHelper, EvalHelper}
import org.apache.spark.sql.catalyst.expressions.{AliasHelper, Cast, EvalHelper}
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.AlwaysProcess
import org.apache.spark.sql.catalyst.types.DataTypeUtils
import org.apache.spark.sql.catalyst.util.TypeUtils.{toSQLExpr, toSQLId}
import org.apache.spark.sql.types.{StructField, StructType}
import org.apache.spark.sql.types.{DecimalType, StructField, StructType}

/**
* An analyzer rule that replaces [[UnresolvedInlineTable]] with [[LocalRelation]].
Expand Down Expand Up @@ -100,7 +100,13 @@ object ResolveInlineTables extends Rule[LogicalPlan]
errorClass = "INVALID_INLINE_TABLE.INCOMPATIBLE_TYPES_IN_INLINE_TABLE",
messageParameters = Map("colName" -> toSQLId(name)))
}
StructField(name, tpe, nullable = column.exists(_.nullable))
tpe match {
case d: DecimalType =>
StructField(name, tpe,
nullable = column.exists(_.nullable) ||
inputTypes.exists(t => !Cast.canNullSafeCastToDecimal(t, d)))
case _ => StructField(name, tpe, nullable = column.exists(_.nullable))
}
}
val attributes = DataTypeUtils.toAttributes(StructType(fields))
assert(fields.size == table.names.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4713,6 +4713,12 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
val df6 = df3.join(df2, col("df3.zaak_id") === col("df2.customer_id"), "outer")
df5.crossJoin(df6)
}

test("SPARK-46448: InlineTable column is nullable if converting to Decimal could be null") {
val plan = sql("SELECT * FROM values(-0.172787979),(533704665545018957788294905796.5)")
Copy link
Contributor

Choose a reason for hiding this comment

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

wait, so we still get null value at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is only a defensive change because Cast.canNullSafeCastToDecimal(t, d) still has code to return false. though we know that no null value will happen at the end.

assert(plan.schema.fields.length == 1)
assert(plan.schema.fields.head.nullable)
}
}

case class Foo(bar: Option[String])