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 @@ -21,6 +21,7 @@ import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.BindReferences.bindReferences
import org.apache.spark.sql.catalyst.expressions.aggregate.NoOp
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.DecimalType


/**
Expand Down Expand Up @@ -72,7 +73,7 @@ class InterpretedMutableProjection(expressions: Seq[Expression]) extends Mutable

private[this] val fieldWriters: Array[Any => Unit] = validExprs.map { case (e, i) =>
val writer = InternalRow.getWriter(i, e.dataType)
if (!e.nullable) {
if (!e.nullable || e.dataType.isInstanceOf[DecimalType]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good catch! It's better to add some code comments to explain it, or refactor the code to make codegen and interpreted code paths share some util functions to update an internal row.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can follow up, since calendar interval has the same problem (in the case of calendar interval, the issue exists in both InterpretedMutableProjection and InterpretedUnsafeProjection).

(v: Any) => writer(mutableRow, v)
} else {
(v: Any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,68 @@ class MutableProjectionSuite extends SparkFunSuite with ExpressionEvalHelper {
assert(SafeProjection.create(fixedLengthTypes)(projUnsafeRow) === inputRow)
}

def testRows(
bufferSchema: StructType,
buffer: InternalRow,
scalaRows: Seq[Seq[Any]]): Unit = {
val bufferTypes = bufferSchema.map(_.dataType).toArray
val proj = createMutableProjection(bufferTypes)

scalaRows.foreach { scalaRow =>
val inputRow = InternalRow.fromSeq(scalaRow.zip(bufferTypes).map {
case (v, dataType) => CatalystTypeConverters.createToCatalystConverter(dataType)(v)
})
val projRow = proj.target(buffer)(inputRow)
assert(SafeProjection.create(bufferTypes)(projRow) === inputRow)
}
}

testBothCodegenAndInterpreted("SPARK-41395: unsafe buffer with null decimal (high precision)") {
val bufferSchema = StructType(Array(
StructField("dec1", DecimalType(27, 2), nullable = true),
StructField("dec2", DecimalType(27, 2), nullable = true)))
val buffer = UnsafeProjection.create(bufferSchema)
.apply(new GenericInternalRow(bufferSchema.length))
val scalaRows = Seq(
Seq(null, null),
Seq(BigDecimal(77.77), BigDecimal(245.00)))
testRows(bufferSchema, buffer, scalaRows)
}

testBothCodegenAndInterpreted("SPARK-41395: unsafe buffer with null decimal (low precision)") {
val bufferSchema = StructType(Array(
StructField("dec1", DecimalType(10, 2), nullable = true),
StructField("dec2", DecimalType(10, 2), nullable = true)))
val buffer = UnsafeProjection.create(bufferSchema)
.apply(new GenericInternalRow(bufferSchema.length))
val scalaRows = Seq(
Seq(null, null),
Seq(BigDecimal(77.77), BigDecimal(245.00)))
testRows(bufferSchema, buffer, scalaRows)
}

testBothCodegenAndInterpreted("SPARK-41395: generic buffer with null decimal (high precision)") {
val bufferSchema = StructType(Array(
StructField("dec1", DecimalType(27, 2), nullable = true),
StructField("dec2", DecimalType(27, 2), nullable = true)))
val buffer = new GenericInternalRow(bufferSchema.length)
val scalaRows = Seq(
Seq(null, null),
Seq(BigDecimal(77.77), BigDecimal(245.00)))
testRows(bufferSchema, buffer, scalaRows)
}

testBothCodegenAndInterpreted("SPARK-41395: generic buffer with null decimal (low precision)") {
val bufferSchema = StructType(Array(
StructField("dec1", DecimalType(10, 2), nullable = true),
StructField("dec2", DecimalType(10, 2), nullable = true)))
val buffer = new GenericInternalRow(bufferSchema.length)
val scalaRows = Seq(
Seq(null, null),
Seq(BigDecimal(77.77), BigDecimal(245.00)))
testRows(bufferSchema, buffer, scalaRows)
}

testBothCodegenAndInterpreted("variable-length types") {
val proj = createMutableProjection(variableLengthTypes)
val scalaValues = Seq("abc", BigDecimal(10),
Expand Down