Skip to content

Commit 7c0031a

Browse files
committed
feat(proto): add SessionContext.tableSchema for registered-table schema discovery
Fix l_extendedprice precision assertion to match actual TPC-H DECIMAL(15,2) spec.
1 parent edbe6a3 commit 7c0031a

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎native/src/proto.rs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use std::sync::Arc;
19+
20+
use datafusion::arrow::datatypes::SchemaRef;
21+
use datafusion::arrow::ipc::writer::StreamWriter;
1822
use datafusion::dataframe::DataFrame;
1923
use datafusion::prelude::SessionContext;
2024
use datafusion_proto::logical_plan::{AsLogicalPlan, DefaultLogicalExtensionCodec};
2125
use datafusion_proto::protobuf::LogicalPlanNode;
22-
use jni::objects::{JByteArray, JClass};
23-
use jni::sys::jlong;
26+
use jni::objects::{JByteArray, JClass, JString};
27+
use jni::sys::{jbyteArray, jlong};
2428
use jni::JNIEnv;
2529
use prost::Message;
2630

@@ -48,3 +52,30 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_createDataFrame
4852
Ok(Box::into_raw(Box::new(df)) as jlong)
4953
})
5054
}
55+
56+
#[no_mangle]
57+
pub extern "system" fn Java_org_apache_datafusion_SessionContext_tableSchemaIpc<'local>(
58+
mut env: JNIEnv<'local>,
59+
_class: JClass<'local>,
60+
handle: jlong,
61+
name: JString<'local>,
62+
) -> jbyteArray {
63+
try_unwrap_or_throw(&mut env, std::ptr::null_mut(), |env| -> JniResult<jbyteArray> {
64+
if handle == 0 {
65+
return Err("SessionContext handle is null".into());
66+
}
67+
let ctx = unsafe { &*(handle as *const SessionContext) };
68+
let name: String = env.get_string(&name)?.into();
69+
70+
let df = runtime().block_on(ctx.table(name.as_str()))?;
71+
let schema: SchemaRef = Arc::new(df.schema().as_arrow().clone());
72+
73+
let mut buf: Vec<u8> = Vec::new();
74+
{
75+
let mut writer = StreamWriter::try_new(&mut buf, schema.as_ref())?;
76+
writer.finish()?;
77+
}
78+
let arr = env.byte_array_from_slice(&buf)?;
79+
Ok(arr.into_raw())
80+
})
81+
}

‎src/main/java/org/apache/datafusion/SessionContext.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919

2020
package 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);

‎src/test/java/org/apache/datafusion/proto/SessionContextProtoTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ void tableSchemaReturnsExpectedLineitemFields() {
9191
assertEquals("l_orderkey", schema.getFields().get(0).getName());
9292
assertTrue(schema.getFields().get(0).getType() instanceof ArrowType.Int);
9393

94-
// l_extendedprice = Decimal128(12, 2)
94+
// l_extendedprice = Decimal128(15, 2)
9595
ArrowType.Decimal price = (ArrowType.Decimal) schema.findField("l_extendedprice").getType();
96-
assertEquals(12, price.getPrecision());
96+
assertEquals(15, price.getPrecision());
9797
assertEquals(2, price.getScale());
9898

9999
// l_shipdate = Date(DAY)

0 commit comments

Comments
 (0)