Skip to content

[SPARK-20798] GenerateUnsafeProjection should check if a value is null before calling the getter #18030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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 @@ -50,10 +50,17 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro
fieldTypes: Seq[DataType],
bufferHolder: String): String = {
val fieldEvals = fieldTypes.zipWithIndex.map { case (dt, i) =>
val fieldName = ctx.freshName("fieldName")
val code = s"final ${ctx.javaType(dt)} $fieldName = ${ctx.getValue(input, dt, i.toString)};"
val isNull = s"$input.isNullAt($i)"
ExprCode(code, isNull, fieldName)
val javaType = ctx.javaType(dt)
val isNullVar = ctx.freshName("isNull")
val valueVar = ctx.freshName("value")
val defaultValue = ctx.defaultValue(dt)
val readValue = ctx.getValue(input, dt, i.toString)
val code =
s"""
boolean $isNullVar = $input.isNullAt($i);
$javaType $valueVar = $isNullVar ? $defaultValue : $readValue;
"""
ExprCode(code, isNullVar, valueVar)
}

s"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.expressions.codegen

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.BoundReference
import org.apache.spark.sql.catalyst.util.{ArrayData, MapData}
import org.apache.spark.sql.types.{DataType, Decimal, StringType, StructType}
import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}

class GenerateUnsafeProjectionSuite extends SparkFunSuite {
test("Test unsafe projection string access pattern") {
val dataType = (new StructType).add("a", StringType)
val exprs = BoundReference(0, dataType, nullable = true) :: Nil
val projection = GenerateUnsafeProjection.generate(exprs)
val result = projection.apply(InternalRow(AlwaysNull))
assert(!result.isNullAt(0))
assert(result.getStruct(0, 1).isNullAt(0))
}
}

object AlwaysNull extends InternalRow {
override def numFields: Int = 1
override def setNullAt(i: Int): Unit = {}
override def copy(): InternalRow = this
override def anyNull: Boolean = true
override def isNullAt(ordinal: Int): Boolean = true
override def update(i: Int, value: Any): Unit = notSupported
override def getBoolean(ordinal: Int): Boolean = notSupported
override def getByte(ordinal: Int): Byte = notSupported
override def getShort(ordinal: Int): Short = notSupported
override def getInt(ordinal: Int): Int = notSupported
override def getLong(ordinal: Int): Long = notSupported
override def getFloat(ordinal: Int): Float = notSupported
override def getDouble(ordinal: Int): Double = notSupported
override def getDecimal(ordinal: Int, precision: Int, scale: Int): Decimal = notSupported
override def getUTF8String(ordinal: Int): UTF8String = notSupported
override def getBinary(ordinal: Int): Array[Byte] = notSupported
override def getInterval(ordinal: Int): CalendarInterval = notSupported
override def getStruct(ordinal: Int, numFields: Int): InternalRow = notSupported
override def getArray(ordinal: Int): ArrayData = notSupported
override def getMap(ordinal: Int): MapData = notSupported
override def get(ordinal: Int, dataType: DataType): AnyRef = notSupported
private def notSupported: Nothing = throw new UnsupportedOperationException
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,33 +198,39 @@ public boolean anyNull() {

@Override
public Decimal getDecimal(int ordinal, int precision, int scale) {
if (columns[ordinal].isNullAt(rowId)) return null;
return columns[ordinal].getDecimal(rowId, precision, scale);
}

@Override
public UTF8String getUTF8String(int ordinal) {
if (columns[ordinal].isNullAt(rowId)) return null;
return columns[ordinal].getUTF8String(rowId);
}

@Override
public byte[] getBinary(int ordinal) {
if (columns[ordinal].isNullAt(rowId)) return null;
return columns[ordinal].getBinary(rowId);
}

@Override
public CalendarInterval getInterval(int ordinal) {
if (columns[ordinal].isNullAt(rowId)) return null;
final int months = columns[ordinal].getChildColumn(0).getInt(rowId);
final long microseconds = columns[ordinal].getChildColumn(1).getLong(rowId);
return new CalendarInterval(months, microseconds);
}

@Override
public InternalRow getStruct(int ordinal, int numFields) {
if (columns[ordinal].isNullAt(rowId)) return null;
return columns[ordinal].getStruct(rowId);
}

@Override
public ArrayData getArray(int ordinal) {
if (columns[ordinal].isNullAt(rowId)) return null;
return columns[ordinal].getArray(rowId);
}

Expand Down