Skip to content

Reading Arrow NullVector #550

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

Merged
merged 4 commits into from
Feb 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.apache.arrow.vector.Float8Vector
import org.apache.arrow.vector.IntVector
import org.apache.arrow.vector.LargeVarBinaryVector
import org.apache.arrow.vector.LargeVarCharVector
import org.apache.arrow.vector.NullVector
import org.apache.arrow.vector.SmallIntVector
import org.apache.arrow.vector.TimeMicroVector
import org.apache.arrow.vector.TimeMilliVector
Expand Down Expand Up @@ -172,6 +173,10 @@ private fun StructVector.values(range: IntRange): List<Map<String, Any?>?> = ran
getObject(it)
}

private fun NullVector.values(range: IntRange): List<Nothing?> = range.map {
getObject(it) as Nothing?
}

private fun VarCharVector.values(range: IntRange): List<String?> = range.map {
if (isNull(it)) {
null
Expand Down Expand Up @@ -204,6 +209,12 @@ private fun LargeVarCharVector.values(range: IntRange): List<String?> = range.ma
}
}

internal fun nothingType(nullable: Boolean): KType = if (nullable) {
typeOf<List<Nothing?>>()
} else {
typeOf<List<Nothing>>()
}.arguments.first().type!!

private inline fun <reified T> List<T?>.withTypeNullable(
expectedNulls: Boolean,
nullabilityOptions: NullabilityOptions,
Expand All @@ -212,6 +223,15 @@ private inline fun <reified T> List<T?>.withTypeNullable(
return this to typeOf<T>().withNullability(nullable)
}

@JvmName("withTypeNullableNothingList")
private fun List<Nothing?>.withTypeNullable(
expectedNulls: Boolean,
nullabilityOptions: NullabilityOptions,
): Pair<List<Nothing?>, KType> {
val nullable = nullabilityOptions.applyNullability(this, expectedNulls)
return this to nothingType(nullable)
}

private fun readField(root: VectorSchemaRoot, field: Field, nullability: NullabilityOptions): AnyBaseCol {
try {
val range = 0 until root.rowCount
Expand Down Expand Up @@ -245,6 +265,7 @@ private fun readField(root: VectorSchemaRoot, field: Field, nullability: Nullabi
is TimeStampMilliVector -> vector.values(range).withTypeNullable(field.isNullable, nullability)
is TimeStampSecVector -> vector.values(range).withTypeNullable(field.isNullable, nullability)
is StructVector -> vector.values(range).withTypeNullable(field.isNullable, nullability)
is NullVector -> vector.values(range).withTypeNullable(field.isNullable, nullability)
else -> {
throw NotImplementedError("reading from ${vector.javaClass.canonicalName} is not implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,12 @@ internal fun assertEstimations(exampleFrame: AnyFrame, expectedNullable: Boolean
timeNanoCol.forEachIndexed { i, element ->
assertValueOrNull(iBatch(i), element, LocalTime.ofNanoOfDay(iBatch(i).toLong()))
}

exampleFrame.getColumnOrNull("nulls")?.let { nullCol ->
nullCol.type() shouldBe nothingType(hasNulls)
assert(hasNulls)
nullCol.values().forEach {
assert(it == null)
}
}
}
Binary file modified dataframe-arrow/src/test/resources/test-illegal.arrow.feather
Binary file not shown.
Binary file modified dataframe-arrow/src/test/resources/test-illegal.arrow.ipc
Binary file not shown.
Binary file modified dataframe-arrow/src/test/resources/test-with-nulls.arrow.feather
Binary file not shown.
Binary file modified dataframe-arrow/src/test/resources/test-with-nulls.arrow.ipc
Binary file not shown.