Skip to content

Commit 3760085

Browse files
committed
Add parseSchema (#72)
* Add parseSchema * fmt
1 parent a072c38 commit 3760085

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ const WASM_MEMORY: WebAssembly.Memory = ...
2424
const field = parseField(WASM_MEMORY.buffer, fieldPtr);
2525
```
2626

27+
### `parseSchema`
28+
29+
Parse an [`ArrowSchema`](https://arrow.apache.org/docs/format/CDataInterface.html#the-arrowschema-structure) C FFI struct into an `arrow.Schema` instance. Note that the underlying field **must** be a `Struct` type. In essence a `Struct` field is used to mimic a `Schema` while only being one field.
30+
31+
- `buffer` (`ArrayBuffer`): The [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory) instance to read from.
32+
- `ptr` (`number`): The numeric pointer in `buffer` where the C struct is located.
33+
34+
```js
35+
const WASM_MEMORY: WebAssembly.Memory = ...
36+
const schema = parseSchema(WASM_MEMORY.buffer, fieldPtr);
37+
```
38+
2739
### `parseData`
2840

2941
Parse an [`ArrowArray`](https://arrow.apache.org/docs/format/CDataInterface.html#the-arrowarray-structure) C FFI struct into an [`arrow.Data`](https://arrow.apache.org/docs/js/classes/Arrow_dom.Data.html) instance. Multiple `Data` instances can be joined to make an [`arrow.Vector`](https://arrow.apache.org/docs/js/classes/Arrow_dom.Vector.html).

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export { parseVector, parseData } from "./vector";
21
export { parseField } from "./field";
32
export { parseRecordBatch } from "./record-batch";
3+
export { parseSchema } from "./schema";
4+
export { parseVector, parseData } from "./vector";

src/schema.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as arrow from "apache-arrow";
2+
import { parseField } from "./field";
3+
4+
/**
5+
Parse an [`ArrowSchema`](https://arrow.apache.org/docs/format/CDataInterface.html#the-arrowschema-structure) C FFI struct into an `arrow.Schema` instance. Note that the underlying field **must** be a `Struct` type. In essence a `Struct` field is used to mimic a `Schema` while only being one field.
6+
7+
- `buffer` (`ArrayBuffer`): The [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory) instance to read from.
8+
- `ptr` (`number`): The numeric pointer in `buffer` where the C struct is located.
9+
10+
```js
11+
const WASM_MEMORY: WebAssembly.Memory = ...
12+
const schema = parseSchema(WASM_MEMORY.buffer, fieldPtr);
13+
```
14+
*/
15+
export function parseSchema(buffer: ArrayBuffer, ptr: number): arrow.Schema {
16+
const field = parseField(buffer, ptr);
17+
if (!isStructField(field)) {
18+
throw new Error("Expected struct");
19+
}
20+
21+
return unpackStructField(field);
22+
}
23+
24+
function isStructField(field: arrow.Field): field is arrow.Field<arrow.Struct> {
25+
return field.typeId == arrow.Type.Struct;
26+
}
27+
28+
function unpackStructField(field: arrow.Field<arrow.Struct>): arrow.Schema {
29+
const fields = field.type.children;
30+
const metadata = field.metadata;
31+
// TODO: support dictionaries parameter for dictionary-encoded arrays
32+
return new arrow.Schema(fields, metadata);
33+
}

0 commit comments

Comments
 (0)