Skip to content
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

Fix NPE when fetching a non-exist key from a GroupBy w/ derivation #922

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions online/src/main/scala/ai/chronon/online/FetcherBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,10 @@ class FetcherBase(kvStore: KVStore,
throw ex
}
if (groupByServingInfo.groupBy.hasDerivations) {
// Make sure groupByResponse is not null since it will be used to calculate derivations
val safeGroupByResponse: Map[String, AnyRef] = Option(groupByResponse).getOrElse(Map.empty)
xiaohui-sun marked this conversation as resolved.
Show resolved Hide resolved
val derivedMapTry: Try[Map[String, AnyRef]] = Try {
applyDeriveFunc(groupByServingInfo.deriveFunc, request, groupByResponse)
applyDeriveFunc(groupByServingInfo.deriveFunc, request, safeGroupByResponse)
}
val derivedMap = derivedMapTry match {
case Success(derivedMap) =>
Expand All @@ -511,7 +513,7 @@ class FetcherBase(kvStore: KVStore,
val renameOnlyDeriveFunction =
buildRenameOnlyDerivationFunction(groupByServingInfo.groupBy.derivationsScala)
val renameOnlyDerivedMapTry: Try[Map[String, AnyRef]] = Try {
renameOnlyDeriveFunction(request.keys, groupByResponse)
renameOnlyDeriveFunction(request.keys, safeGroupByResponse)
.mapValues(_.asInstanceOf[AnyRef])
.toMap
}
Expand Down
38 changes: 37 additions & 1 deletion spark/src/test/scala/ai/chronon/spark/test/FetcherTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ class FetcherTest extends TestCase {
)
),
accuracy = Accuracy.TEMPORAL,
metaData = Builders.MetaData(name = "unit_test/fetcher_mutations_gb", namespace = namespace, team = "chronon")
metaData = Builders.MetaData(name = "unit_test/fetcher_mutations_gb", namespace = namespace, team = "chronon"),
derivations=Seq(
Builders.Derivation(name = "*", expression = "*"),
Builders.Derivation(name = "rating_average_1d_same", expression = "rating_average_1d")
)
)

val joinConf = Builders.Join(
Expand Down Expand Up @@ -689,6 +693,38 @@ class FetcherTest extends TestCase {
assertEquals(joinConf.joinParts.size() + derivationExceptionTypes.size, responseMap.size)
assertTrue(responseMap.keys.forall(_.endsWith("_exception")))
}

def testTemporalFetchGroupByNonExistKey(): Unit = {
val namespace = "non_exist_key_group_by_fetch"
val joinConf = generateMutationData(namespace)
val endDs = "2021-04-10"
val spark: SparkSession = SparkSessionBuilder.build(sessionName + "_" + Random.alphanumeric.take(6).mkString, local = true)
val tableUtils = TableUtils(spark)
val kvStoreFunc = () => OnlineUtils.buildInMemoryKVStore("FetcherTest")
val inMemoryKvStore = kvStoreFunc()
val mockApi = new MockApi(kvStoreFunc, namespace)
@transient lazy val fetcher = mockApi.buildFetcher(debug=false)

joinConf.joinParts.toScala.foreach(jp =>
OnlineUtils.serve(tableUtils,
inMemoryKvStore,
kvStoreFunc,
namespace,
endDs,
jp.groupBy,
dropDsOnWrite = true))

// a random key that doesn't exist
val nonExistKey = 123L
val request = Request("unit_test/fetcher_mutations_gb",
Map("listing_id" -> nonExistKey.asInstanceOf[AnyRef]))
val response = fetcher.fetchGroupBys(Seq(request))
val result = Await.result(response, Duration(10, SECONDS))

// result should be "null" if the key is not found
val expected: Map[String, AnyRef] = Map("rating_average_1d_same" -> null)
assertEquals(expected, result.head.values.get)
}
}

object FetcherTestUtil {
Expand Down