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

[query] support importing empty JSON objects #14202

Merged
merged 4 commits into from
Feb 3, 2024
Merged
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
10 changes: 7 additions & 3 deletions hail/src/main/scala/is/hail/expr/AnnotationImpex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,15 @@ object JSONAnnotationImpex {
if (t.size == 0)
Annotation.empty
else {
val annotationSize =
if (padNulls) t.size
else jfields.map { case (name, _) =>
val annotationSize = if (padNulls) {
t.size
} else if (jfields.size == 0) {
0
} else {
jfields.map { case (name, _) =>
t.selfField(name).map(_.index).getOrElse(-1)
}.max + 1
}
val a = Array.fill[Any](annotationSize)(null)

for ((name, jv2) <- jfields) {
Expand Down
25 changes: 24 additions & 1 deletion hail/src/test/scala/is/hail/methods/ExprSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import is.hail.check.Prop._
import is.hail.check.Properties
import is.hail.expr._
import is.hail.expr.ir.IRParser
import is.hail.types.virtual.{TInt32, Type}
import is.hail.types.virtual._
import is.hail.utils.StringEscapeUtils._

import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.testng.annotations.Test
import org.apache.spark.sql.Row

class ExprSuite extends HailSuite {

Expand Down Expand Up @@ -70,6 +71,28 @@ class ExprSuite extends HailSuite {
p.check()
}

@Test def testImportEmptyJSONObjectAsStruct(): Unit =
assert(JSONAnnotationImpex.importAnnotation(parse("{}"), TStruct()) == Row())

@Test def testExportEmptyJSONObjectAsStruct(): Unit =
assert(compact(render(JSONAnnotationImpex.exportAnnotation(Row(), TStruct()))) == "{}")

@Test def testRoundTripEmptyJSONObject(): Unit = {
val actual = JSONAnnotationImpex.exportAnnotation(
JSONAnnotationImpex.importAnnotation(parse("{}"), TStruct()),
TStruct(),
)
assert(compact(render(actual)) == "{}")
}

@Test def testRoundTripEmptyStruct(): Unit = {
val actual = JSONAnnotationImpex.importAnnotation(
JSONAnnotationImpex.exportAnnotation(Row(), TStruct()),
TStruct(),
)
assert(actual == Row())
}

@Test def testImpexes(): Unit = {

val g = for {
Expand Down
Loading