audienceproject/spark-dynamodb

Exception when trying to get Boolean when all values are null.

Closed this issue · 10 comments

In TypeConversion, nullableGet method, catch is not capturing IncompatibleTypeException which is thrown when column has only null values. I think it should be included inside the catch and map to a null value.

Hi,

We’ll take a look after New Year’s.

I wrote this test to reproduce the problem.

import com.amazonaws.services.dynamodbv2.document.Item
import com.amazonaws.services.dynamodbv2.model.{
  AttributeDefinition,
  CreateTableRequest,
  KeySchemaElement,
  ProvisionedThroughput
}
import com.audienceproject.spark.dynamodb.implicits._

class TestNullBoolean extends AbstractInMemoryTest {
  test("Test Null") {
    dynamoDB.createTable(
      new CreateTableRequest()
        .withTableName("TestNullBoolean")
        .withAttributeDefinitions(new AttributeDefinition("Pk", "S"))
        .withKeySchema(new KeySchemaElement("Pk", "HASH"))
        .withProvisionedThroughput(new ProvisionedThroughput(5L, 5L))
    )

    val table = dynamoDB.getTable("TestNullBoolean")

    for ((_pk, _type, _value) <- Seq(
           ("id1", "type1", true),
           ("id2", "type2", null)
         )) {
      if (_type != "type2") {
        table.putItem(
          new Item()
            .withString("Pk", _pk)
            .withString("Type", _type)
            .withBoolean("Value", _value.asInstanceOf[Boolean])
        )
      } else {
        table.putItem(
          new Item()
            .withString("Pk", _pk)
            .withString("Type", _type)
            .withNull("Value")
        )
      }
    }

    val df = spark.read.dynamodbAs[BooleanClass]("TestNullBoolean")

    import spark.implicits._
    df.where($"Type" === "type2").show()
    client.deleteTable("TestNullBoolean")
  }
}
case class BooleanClass(Pk: String, Type: String, Value: Boolean)

Hi @juanyunism, I've shamelessly taken the code and commit it. It works fine. Do you need a new release now, or can you wait until we get more stuff in?

Hi @cosmincatalin, thank you for the quick response :).
Don't worry, release when you get more stuff in. Can you provide me the branch with the fix so I can download and build it to try it locally?

Thanks in advance.

Nevermind, just found it in master 😃

I used this in my build.sbt to use the latest changes.

lazy val root = (project in file(".")).dependsOn(depProject)
lazy val depProject = ProjectRef(
  uri("git://github.com/audienceproject/spark-dynamodb.git#master"),
  "spark-dynamodb"
)

Hi @cosmincatalin, is there any possibility you could cut a release with this fix included?

Sure, I’ll make a release before this weekend.

Released in 1.0.3