|
18 | 18 | import '../../jest-extensions.js'; |
19 | 19 | import { arange } from '../utils.js'; |
20 | 20 |
|
21 | | -import { RecordBatch, makeVector } from 'apache-arrow'; |
| 21 | +import { RecordBatch, makeVector, recordBatchFromArrays, Schema, Field, Int32, Float32, Float64, Utf8, Dictionary } from 'apache-arrow'; |
22 | 22 |
|
23 | 23 | function numsRecordBatch(i32Len: number, f32Len: number) { |
24 | 24 | return new RecordBatch({ |
@@ -130,3 +130,98 @@ describe(`RecordBatch`, () => { |
130 | 130 | }); |
131 | 131 | }); |
132 | 132 | }); |
| 133 | + |
| 134 | +describe(`recordBatchFromArrays()`, () => { |
| 135 | + test(`creates a RecordBatch from typed arrays and JavaScript arrays`, () => { |
| 136 | + const batch = recordBatchFromArrays({ |
| 137 | + a: new Float32Array([1, 2, 3]), |
| 138 | + b: [4, 5, 6], |
| 139 | + c: ['x', 'y', 'z'], |
| 140 | + }); |
| 141 | + |
| 142 | + expect(batch.numRows).toBe(3); |
| 143 | + expect(batch.numCols).toBe(3); |
| 144 | + expect(batch.getChild('a')!.type).toBeInstanceOf(Float32); |
| 145 | + expect(batch.getChild('b')!.type).toBeInstanceOf(Float64); |
| 146 | + expect(batch.getChild('c')!.type).toBeInstanceOf(Dictionary); |
| 147 | + }); |
| 148 | + |
| 149 | + test(`schema overrides type inference`, () => { |
| 150 | + const schema = new Schema([ |
| 151 | + new Field('a', new Int32), |
| 152 | + new Field('b', new Utf8), |
| 153 | + ]); |
| 154 | + const batch = recordBatchFromArrays({ a: [1, 2, 3], b: ['x', 'y', 'z'] }, schema); |
| 155 | + |
| 156 | + expect(batch.numRows).toBe(3); |
| 157 | + expect(batch.getChild('a')!.type).toBeInstanceOf(Int32); |
| 158 | + expect(batch.getChild('b')!.type).toBeInstanceOf(Utf8); |
| 159 | + expect(batch.getChild('a')!.toArray()).toEqual(new Int32Array([1, 2, 3])); |
| 160 | + }); |
| 161 | + |
| 162 | + test(`schema coerces TypedArray type`, () => { |
| 163 | + const schema = new Schema([new Field('a', new Int32)]); |
| 164 | + const batch = recordBatchFromArrays({ a: new Float32Array([1, 2, 3]) }, schema); |
| 165 | + expect(batch.getChild('a')!.type).toBeInstanceOf(Int32); |
| 166 | + expect(batch.getChild('a')!.toArray()).toEqual(new Int32Array([1, 2, 3])); |
| 167 | + }); |
| 168 | + |
| 169 | + test(`preserves schema metadata`, () => { |
| 170 | + const schema = new Schema( |
| 171 | + [new Field('a', new Int32)], |
| 172 | + new Map([['source', 'test']]) |
| 173 | + ); |
| 174 | + const batch = recordBatchFromArrays({ a: [1, 2, 3] }, schema); |
| 175 | + expect(batch.schema.metadata.get('source')).toBe('test'); |
| 176 | + }); |
| 177 | + |
| 178 | + test(`throws on missing schema field`, () => { |
| 179 | + const schema = new Schema([new Field('c', new Int32)]); |
| 180 | + expect(() => recordBatchFromArrays({ a: [1] }, schema)).toThrow(TypeError); |
| 181 | + expect(() => recordBatchFromArrays({ a: [1] }, schema)).toThrow(/Schema field "c" not found in input/); |
| 182 | + }); |
| 183 | + |
| 184 | + test(`handles different length columns via ensureSameLengthData`, () => { |
| 185 | + const schema = new Schema([ |
| 186 | + new Field('a', new Int32), |
| 187 | + new Field('b', new Int32), |
| 188 | + ]); |
| 189 | + const batch = recordBatchFromArrays({ a: [1, 2, 3], b: [4, 5] }, schema); |
| 190 | + expect(batch.numRows).toBe(3); |
| 191 | + expect(batch.getChild('a')!).toHaveLength(3); |
| 192 | + expect(batch.getChild('b')!).toHaveLength(3); |
| 193 | + expect(batch.getChild('b')!.nullCount).toBe(1); |
| 194 | + }); |
| 195 | + |
| 196 | + test(`preserves field ordering from schema`, () => { |
| 197 | + const schema = new Schema([ |
| 198 | + new Field('b', new Float64), |
| 199 | + new Field('a', new Int32), |
| 200 | + ]); |
| 201 | + const batch = recordBatchFromArrays({ a: [1, 2, 3], b: [4.0, 5.0, 6.0] }, schema); |
| 202 | + expect(batch.schema.fields[0].name).toBe('b'); |
| 203 | + expect(batch.schema.fields[1].name).toBe('a'); |
| 204 | + expect(batch.getChild('b')!.type).toBeInstanceOf(Float64); |
| 205 | + expect(batch.getChild('a')!.type).toBeInstanceOf(Int32); |
| 206 | + }); |
| 207 | + |
| 208 | + test(`handles empty arrays`, () => { |
| 209 | + const schema = new Schema([new Field('a', new Int32)]); |
| 210 | + const batch = recordBatchFromArrays({ a: new Int32Array(0) }, schema); |
| 211 | + expect(batch.numRows).toBe(0); |
| 212 | + expect(batch.numCols).toBe(1); |
| 213 | + expect(batch.getChild('a')!.type).toBeInstanceOf(Int32); |
| 214 | + }); |
| 215 | + |
| 216 | + test(`basic creation without schema infers types`, () => { |
| 217 | + const batch = recordBatchFromArrays({ |
| 218 | + f32: new Float32Array([1, 2]), |
| 219 | + nums: [1, 2, 3], |
| 220 | + strs: ['a', 'b'], |
| 221 | + }); |
| 222 | + |
| 223 | + expect(batch.getChild('f32')!.type).toBeInstanceOf(Float32); |
| 224 | + expect(batch.getChild('nums')!.type).toBeInstanceOf(Float64); |
| 225 | + expect(batch.getChild('strs')!.type).toBeInstanceOf(Dictionary); |
| 226 | + }); |
| 227 | +}); |
0 commit comments