forked from deephaven/deephaven-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add arrow Schema as supported type (deephaven#6285)
This is in support of deephaven#6023, which needs a way to encode Schema as VARBINARY. This also serves as the potential hook points needed to implement something like #58.
- Loading branch information
1 parent
744357d
commit b823ccd
Showing
5 changed files
with
218 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
extensions/barrage/src/main/java/io/deephaven/extensions/barrage/util/ArrowIpcUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||
// | ||
package io.deephaven.extensions.barrage.util; | ||
|
||
import org.apache.arrow.vector.ipc.ReadChannel; | ||
import org.apache.arrow.vector.ipc.WriteChannel; | ||
import org.apache.arrow.vector.ipc.message.MessageSerializer; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.channels.Channels; | ||
|
||
public class ArrowIpcUtil { | ||
public static long serialize(OutputStream outputStream, Schema schema) throws IOException { | ||
// not buffered. no flushing needed. not closing write channel | ||
return MessageSerializer.serialize(new WriteChannel(Channels.newChannel(outputStream)), schema); | ||
} | ||
|
||
public static Schema deserialize(InputStream in) throws IOException { | ||
// not buffered. not closing read channel | ||
return MessageSerializer.deserializeSchema(new ReadChannel(Channels.newChannel(in))); | ||
} | ||
|
||
public static Schema deserialize(byte[] buf, int offset, int length) throws IOException { | ||
return deserialize(new ByteArrayInputStream(buf, offset, length)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
extensions/barrage/src/test/java/io/deephaven/extensions/barrage/util/ArrowIpcUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending | ||
// | ||
package io.deephaven.extensions.barrage.util; | ||
|
||
import org.apache.arrow.vector.types.Types; | ||
import org.apache.arrow.vector.types.pojo.Field; | ||
import org.apache.arrow.vector.types.pojo.FieldType; | ||
import org.apache.arrow.vector.types.pojo.Schema; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ArrowIpcUtilTest { | ||
|
||
public static final Field FOO = new Field("Foo", FieldType.nullable(Types.MinorType.INT.getType()), null); | ||
public static final Field BAR = new Field("Bar", FieldType.notNullable(Types.MinorType.INT.getType()), null); | ||
public static final Field BAZ = new Field("Baz", | ||
new FieldType(true, Types.MinorType.VARCHAR.getType(), null, Map.of("k1", "v1", "k2", "v2")), null); | ||
|
||
private static final Schema SCHEMA_1 = new Schema(List.of(FOO, BAR, BAZ)); | ||
private static final Schema SCHEMA_2 = | ||
new Schema(List.of(FOO, BAR, BAZ), Map.of("key1", "value1", "key2", "value2")); | ||
|
||
@Test | ||
public void testSchemas() throws IOException { | ||
verifySerDeser(SCHEMA_1); | ||
verifySerDeser(SCHEMA_2); | ||
} | ||
|
||
// A bit circular, but better than nothing. | ||
public static void verifySerDeser(Schema schema) throws IOException { | ||
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
final long length = ArrowIpcUtil.serialize(baos, schema); | ||
assertThat(length).isEqualTo(baos.size()); | ||
Schema deserialized = ArrowIpcUtil.deserialize(baos.toByteArray(), 0, (int) length); | ||
assertThat(deserialized).isEqualTo(schema); | ||
} | ||
} |