-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark: Add read/write support for UUIDs #7399
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,10 +18,13 @@ | |
| */ | ||
| package org.apache.iceberg.spark.data; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.stream.Stream; | ||
| import org.apache.iceberg.FieldMetrics; | ||
| import org.apache.iceberg.orc.OrcValueWriter; | ||
| import org.apache.iceberg.util.UUIDUtil; | ||
| import org.apache.orc.TypeDescription; | ||
| import org.apache.orc.storage.common.type.HiveDecimal; | ||
| import org.apache.orc.storage.ql.exec.vector.BytesColumnVector; | ||
|
|
@@ -42,6 +45,10 @@ static OrcValueWriter<?> strings() { | |
| return StringWriter.INSTANCE; | ||
| } | ||
|
|
||
| static OrcValueWriter<?> uuids() { | ||
| return UUIDWriter.INSTANCE; | ||
| } | ||
|
|
||
| static OrcValueWriter<?> timestampTz() { | ||
| return TimestampTzWriter.INSTANCE; | ||
| } | ||
|
|
@@ -73,6 +80,16 @@ public void nonNullWrite(int rowId, UTF8String data, ColumnVector output) { | |
| } | ||
| } | ||
|
|
||
| private static class UUIDWriter implements OrcValueWriter<UTF8String> { | ||
| private static final UUIDWriter INSTANCE = new UUIDWriter(); | ||
|
|
||
| @Override | ||
| public void nonNullWrite(int rowId, UTF8String data, ColumnVector output) { | ||
| ByteBuffer buffer = UUIDUtil.convertToByteBuffer(UUID.fromString(data.toString())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allocates a buffer. We may want to have a buffer here as a thread-local or a field to avoid allocation in a tight loop.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with that observation and I initially used a Thread local to reduce byte[] allocation but couldn't get it to work because
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably worth mentioning in a comment?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense, I've added a comment to this as part of #7496 |
||
| ((BytesColumnVector) output).setRef(rowId, buffer.array(), 0, buffer.array().length); | ||
| } | ||
| } | ||
|
|
||
| private static class TimestampTzWriter implements OrcValueWriter<Long> { | ||
| private static final TimestampTzWriter INSTANCE = new TimestampTzWriter(); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,10 +21,12 @@ | |
| import java.math.BigDecimal; | ||
| import java.nio.ByteBuffer; | ||
| import org.apache.arrow.memory.ArrowBuf; | ||
| import org.apache.arrow.vector.FixedSizeBinaryVector; | ||
| import org.apache.arrow.vector.ValueVector; | ||
| import org.apache.arrow.vector.VarCharVector; | ||
| import org.apache.arrow.vector.complex.ListVector; | ||
| import org.apache.iceberg.arrow.vectorized.GenericArrowVectorAccessorFactory; | ||
| import org.apache.iceberg.util.UUIDUtil; | ||
| import org.apache.spark.sql.types.Decimal; | ||
| import org.apache.spark.sql.vectorized.ArrowColumnVector; | ||
| import org.apache.spark.sql.vectorized.ColumnarArray; | ||
|
|
@@ -74,6 +76,11 @@ public UTF8String ofRow(VarCharVector vector, int rowId) { | |
| null, vector.getDataBuffer().memoryAddress() + start, end - start); | ||
| } | ||
|
|
||
| @Override | ||
| public UTF8String ofRow(FixedSizeBinaryVector vector, int rowId) { | ||
| return UTF8String.fromString(UUIDUtil.convert(vector.get(rowId)).toString()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to get the underlying array and offset?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| @Override | ||
| public UTF8String ofBytes(byte[] bytes) { | ||
| return UTF8String.fromBytes(bytes); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -329,6 +329,8 @@ public Object primitive(Type.PrimitiveType primitive) { | |
| return UTF8String.fromString((String) obj); | ||
| case DECIMAL: | ||
| return Decimal.apply((BigDecimal) obj); | ||
| case UUID: | ||
| return UTF8String.fromString(UUID.nameUUIDFromBytes((byte[]) obj).toString()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my guess would be because |
||
| default: | ||
| return obj; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.