Skip to content

Commit b7f5bfb

Browse files
committed
rename numRows to length, add table.getColumn()
1 parent e81082f commit b7f5bfb

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

js/src/Arrow.externs.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ Table.prototype.schema;
4040
/** @type {?} */
4141
Table.prototype.columns;
4242
/** @type {?} */
43-
Table.prototype.numCols;
43+
Table.prototype.length;
4444
/** @type {?} */
45-
Table.prototype.numRows;
45+
Table.prototype.numCols;
4646
/** @type {?} */
4747
Table.prototype.get;
4848
/** @type {?} */
49+
Table.prototype.getColumn;
50+
/** @type {?} */
51+
Table.prototype.getColumnAt;
52+
/** @type {?} */
53+
Table.prototype.getColumnIndex;
54+
/** @type {?} */
4955
Table.prototype.toArray;
5056
/** @type {?} */
5157
Table.prototype.select;

js/src/recordbatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class RecordBatch extends StructVector {
3030
);
3131
}
3232
public readonly schema: Schema;
33+
public readonly length: number;
3334
public readonly numCols: number;
34-
public readonly numRows: number;
3535
public readonly columns: Vector<any>[];
3636
constructor(schema: Schema, data: Data<Struct>, view: View<Struct>);
3737
constructor(schema: Schema, numRows: Long | number, cols: Data<any> | Vector[]);
@@ -40,7 +40,7 @@ export class RecordBatch extends StructVector {
4040
const data = args[1] as Data<Struct>;
4141
super(data, args[2]);
4242
this.schema = args[0];
43-
this.numRows = data.length;
43+
this.length = data.length;
4444
this.numCols = this.schema.fields.length;
4545
this.columns = data instanceof ChunkedData
4646
? data.childVectors
@@ -60,7 +60,7 @@ export class RecordBatch extends StructVector {
6060
super(new NestedData(new Struct(schema.fields), numRows, null, columnsData));
6161
this.schema = schema;
6262
this.columns = columns;
63-
this.numRows = numRows;
63+
this.length = numRows;
6464
this.numCols = schema.fields.length;
6565
}
6666
}
@@ -71,7 +71,7 @@ export class RecordBatch extends StructVector {
7171
const fields = this.schema.fields;
7272
const namesToKeep = columnNames.reduce((xs, x) => (xs[x] = true) && xs, Object.create(null));
7373
return new RecordBatch(
74-
this.schema.select(...columnNames), this.numRows,
74+
this.schema.select(...columnNames), this.length,
7575
this.columns.filter((_, index) => namesToKeep[fields[index].name])
7676
);
7777
}

js/src/table.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export class Table implements DataFrame {
6363
}
6464

6565
public readonly schema: Schema;
66+
public readonly length: number;
6667
public readonly numCols: number;
67-
public readonly numRows: number;
6868
// List of inner RecordBatches
6969
public readonly batches: RecordBatch[];
7070
// List of inner Vectors, possibly spanning batches
@@ -98,12 +98,21 @@ export class Table implements DataFrame {
9898
columns.map((col, idx) => col.concat(batch.columns[idx])),
9999
batches[0].columns
100100
);
101+
this.length = this.batchesUnion.length;
101102
this.numCols = this.batchesUnion.numCols;
102-
this.numRows = this.batchesUnion.numRows;
103103
}
104104
public get(index: number): Struct['TValue'] {
105105
return this.batchesUnion.get(index)!;
106106
}
107+
public getColumn(name: string) {
108+
return this.getColumnAt(this.getColumnIndex(name));
109+
}
110+
public getColumnAt(index: number) {
111+
return this.columns[index];
112+
}
113+
public getColumnIndex(name: string) {
114+
return this.schema.fields.findIndex((f) => f.name === name);
115+
}
107116
public [Symbol.iterator](): IterableIterator<Struct['TValue']> {
108117
return this.batchesUnion[Symbol.iterator]() as any;
109118
}
@@ -116,7 +125,7 @@ export class Table implements DataFrame {
116125
// load batches
117126
const batch = batches[batchIndex];
118127
// yield all indices
119-
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
128+
for (let index = -1, numRows = batch.length; ++index < numRows;) {
120129
next(index, batch);
121130
}
122131
}
@@ -142,7 +151,7 @@ export class Table implements DataFrame {
142151
count_by.bind(batch);
143152
const keys = (count_by.vector as DictionaryVector).indicies;
144153
// yield all indices
145-
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
154+
for (let index = -1, numRows = batch.length; ++index < numRows;) {
146155
let key = keys.get(index);
147156
if (key !== null) { counts[key]++; }
148157
}
@@ -152,6 +161,13 @@ export class Table implements DataFrame {
152161
public select(...columnNames: string[]) {
153162
return new Table(this.batches.map((batch) => batch.select(...columnNames)));
154163
}
164+
public toString(separator?: string) {
165+
let str = '';
166+
for (const row of this.rowsToString(separator)) {
167+
str += row + '\n';
168+
}
169+
return str;
170+
}
155171
public rowsToString(separator = ' | '): TableToStringIterator {
156172
return new TableToStringIterator(tableRowsToString(this, separator));
157173
}
@@ -176,7 +192,7 @@ class FilteredDataFrame implements DataFrame {
176192
const batch = batches[batchIndex];
177193
const predicate = this.predicate.bind(batch);
178194
// yield all indices
179-
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
195+
for (let index = -1, numRows = batch.length; ++index < numRows;) {
180196
if (predicate(index, batch)) { next(index, batch); }
181197
}
182198
}
@@ -196,7 +212,7 @@ class FilteredDataFrame implements DataFrame {
196212
const batch = batches[batchIndex];
197213
const predicate = this.predicate.bind(batch);
198214
// yield all indices
199-
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
215+
for (let index = -1, numRows = batch.length; ++index < numRows;) {
200216
if (predicate(index, batch)) { ++sum; }
201217
}
202218
}
@@ -229,7 +245,7 @@ class FilteredDataFrame implements DataFrame {
229245
count_by.bind(batch);
230246
const keys = (count_by.vector as DictionaryVector).indicies;
231247
// yield all indices
232-
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
248+
for (let index = -1, numRows = batch.length; ++index < numRows;) {
233249
let key = keys.get(index);
234250
if (key !== null && predicate(index, batch)) { counts[key]++; }
235251
}
@@ -251,7 +267,7 @@ export class CountByResult extends Table implements DataFrame {
251267
public asJSON(): Object {
252268
const [values, counts] = this.columns;
253269
const result = {} as { [k: string]: number | null };
254-
for (let i = -1; ++i < this.numRows;) {
270+
for (let i = -1; ++i < this.length;) {
255271
result[values.get(i)] = counts.get(i);
256272
}
257273
return result;
@@ -282,20 +298,20 @@ export class TableToStringIterator implements IterableIterator<string> {
282298
}
283299
}
284300

285-
function *tableRowsToString(table: Table, separator = ' | ') {
301+
function* tableRowsToString(table: Table, separator = ' | ') {
286302
const fields = table.schema.fields;
287303
const header = ['row_id', ...fields.map((f) => `${f}`)].map(stringify);
288304
const maxColumnWidths = header.map(x => x.length);
289305
// Pass one to convert to strings and count max column widths
290-
for (let i = -1, n = table.numRows - 1; ++i < n;) {
306+
for (let i = -1, n = table.length - 1; ++i < n;) {
291307
let val, row = [i, ...table.get(i)];
292308
for (let j = -1, k = row.length; ++j < k; ) {
293309
val = stringify(row[j]);
294310
maxColumnWidths[j] = Math.max(maxColumnWidths[j], val.length);
295311
}
296312
}
297313
yield header.map((x, j) => leftPad(x, ' ', maxColumnWidths[j])).join(separator);
298-
for (let i = -1, n = table.numRows; ++i < n;) {
314+
for (let i = -1, n = table.length; ++i < n;) {
299315
yield [i, ...table.get(i)]
300316
.map((x) => stringify(x))
301317
.map((x, j) => leftPad(x, ' ', maxColumnWidths[j]))

js/test/integration/validate-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ function testReaderIntegration() {
109109
const jsonRecordBatches = toArray(read(jsonData));
110110
const binaryRecordBatches = toArray(read(arrowBuffers));
111111
for (const [jsonRecordBatch, binaryRecordBatch] of zip(jsonRecordBatches, binaryRecordBatches)) {
112+
expect(jsonRecordBatch.length).toEqual(binaryRecordBatch.length);
112113
expect(jsonRecordBatch.numCols).toEqual(binaryRecordBatch.numCols);
113-
expect(jsonRecordBatch.numRows).toEqual(binaryRecordBatch.numRows);
114114
for (let i = -1, n = jsonRecordBatch.numCols; ++i < n;) {
115115
(expect(jsonRecordBatch.columns[i]) as any).toEqualVector(binaryRecordBatch.columns[i]);
116116
}
@@ -124,8 +124,8 @@ function testTableFromBuffersIntegration() {
124124
expect.hasAssertions();
125125
const jsonTable = Table.from(jsonData);
126126
const binaryTable = Table.from(arrowBuffers);
127+
expect(jsonTable.length).toEqual(binaryTable.length);
127128
expect(jsonTable.numCols).toEqual(binaryTable.numCols);
128-
expect(jsonTable.numRows).toEqual(binaryTable.numRows);
129129
for (let i = -1, n = jsonTable.numCols; ++i < n;) {
130130
(expect(jsonTable.columns[i]) as any).toEqualVector(binaryTable.columns[i]);
131131
}

js/test/unit/table-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe(`Table`, () => {
127127
[new Float32Array([ 0.3])[0], -1, 'a']
128128
];
129129
test(`has the correct length`, () => {
130-
expect(table.numRows).toEqual(values.length);
130+
expect(table.length).toEqual(values.length);
131131
});
132132
test(`gets expected values`, () => {
133133
for (let i = -1; ++i < values.length;) {
@@ -332,7 +332,7 @@ describe(`Table`, () => {
332332
[new Float32Array([ 0.1])[0], -1, 'c'],
333333
];
334334
test(`has the correct length`, () => {
335-
expect(table.numRows).toEqual(values.length);
335+
expect(table.length).toEqual(values.length);
336336
});
337337
test(`gets expected values`, () => {
338338
for (let i = -1; ++i < values.length;) {

0 commit comments

Comments
 (0)