1919
2020package org .apache .datafusion ;
2121
22+ import java .io .ByteArrayInputStream ;
23+ import java .nio .channels .Channels ;
24+
25+ import org .apache .arrow .vector .ipc .ReadChannel ;
26+ import org .apache .arrow .vector .ipc .message .MessageSerializer ;
27+ import org .apache .arrow .vector .types .pojo .Schema ;
28+
2229/**
2330 * A DataFusion session context.
2431 *
@@ -71,6 +78,25 @@ public DataFrame fromProto(byte[] planBytes) {
7178 return new DataFrame (dfHandle );
7279 }
7380
81+ /**
82+ * Return the Arrow {@link Schema} of a registered table. Transferred via Arrow IPC; no {@link
83+ * org.apache.arrow.memory.BufferAllocator} is required because a schema carries no buffer data.
84+ *
85+ * @throws RuntimeException if {@code tableName} is not registered in this context.
86+ */
87+ public Schema tableSchema (String tableName ) {
88+ if (nativeHandle == 0 ) {
89+ throw new IllegalStateException ("SessionContext is closed" );
90+ }
91+ byte [] ipcBytes = tableSchemaIpc (nativeHandle , tableName );
92+ try {
93+ return MessageSerializer .deserializeSchema (
94+ new ReadChannel (Channels .newChannel (new ByteArrayInputStream (ipcBytes ))));
95+ } catch (java .io .IOException e ) {
96+ throw new RuntimeException ("Failed to deserialize IPC schema" , e );
97+ }
98+ }
99+
74100 public void registerParquet (String name , String path ) {
75101 if (nativeHandle == 0 ) {
76102 throw new IllegalStateException ("SessionContext is closed" );
@@ -92,6 +118,8 @@ public void close() {
92118
93119 private static native long createDataFrameFromProto (long handle , byte [] planBytes );
94120
121+ private static native byte [] tableSchemaIpc (long handle , String tableName );
122+
95123 private static native void registerParquet (long handle , String name , String path );
96124
97125 private static native void closeSessionContext (long handle );
0 commit comments