Skip to content

Commit

Permalink
rename numRows to length, add table.getColumn()
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Jan 22, 2018
1 parent e81082f commit b7f5bfb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
10 changes: 8 additions & 2 deletions js/src/Arrow.externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ Table.prototype.schema;
/** @type {?} */
Table.prototype.columns;
/** @type {?} */
Table.prototype.numCols;
Table.prototype.length;
/** @type {?} */
Table.prototype.numRows;
Table.prototype.numCols;
/** @type {?} */
Table.prototype.get;
/** @type {?} */
Table.prototype.getColumn;
/** @type {?} */
Table.prototype.getColumnAt;
/** @type {?} */
Table.prototype.getColumnIndex;
/** @type {?} */
Table.prototype.toArray;
/** @type {?} */
Table.prototype.select;
Expand Down
8 changes: 4 additions & 4 deletions js/src/recordbatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class RecordBatch extends StructVector {
);
}
public readonly schema: Schema;
public readonly length: number;
public readonly numCols: number;
public readonly numRows: number;
public readonly columns: Vector<any>[];
constructor(schema: Schema, data: Data<Struct>, view: View<Struct>);
constructor(schema: Schema, numRows: Long | number, cols: Data<any> | Vector[]);
Expand All @@ -40,7 +40,7 @@ export class RecordBatch extends StructVector {
const data = args[1] as Data<Struct>;
super(data, args[2]);
this.schema = args[0];
this.numRows = data.length;
this.length = data.length;
this.numCols = this.schema.fields.length;
this.columns = data instanceof ChunkedData
? data.childVectors
Expand All @@ -60,7 +60,7 @@ export class RecordBatch extends StructVector {
super(new NestedData(new Struct(schema.fields), numRows, null, columnsData));
this.schema = schema;
this.columns = columns;
this.numRows = numRows;
this.length = numRows;
this.numCols = schema.fields.length;
}
}
Expand All @@ -71,7 +71,7 @@ export class RecordBatch extends StructVector {
const fields = this.schema.fields;
const namesToKeep = columnNames.reduce((xs, x) => (xs[x] = true) && xs, Object.create(null));
return new RecordBatch(
this.schema.select(...columnNames), this.numRows,
this.schema.select(...columnNames), this.length,
this.columns.filter((_, index) => namesToKeep[fields[index].name])
);
}
Expand Down
38 changes: 27 additions & 11 deletions js/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export class Table implements DataFrame {
}

public readonly schema: Schema;
public readonly length: number;
public readonly numCols: number;
public readonly numRows: number;
// List of inner RecordBatches
public readonly batches: RecordBatch[];
// List of inner Vectors, possibly spanning batches
Expand Down Expand Up @@ -98,12 +98,21 @@ export class Table implements DataFrame {
columns.map((col, idx) => col.concat(batch.columns[idx])),
batches[0].columns
);
this.length = this.batchesUnion.length;
this.numCols = this.batchesUnion.numCols;
this.numRows = this.batchesUnion.numRows;
}
public get(index: number): Struct['TValue'] {
return this.batchesUnion.get(index)!;
}
public getColumn(name: string) {
return this.getColumnAt(this.getColumnIndex(name));
}
public getColumnAt(index: number) {
return this.columns[index];
}
public getColumnIndex(name: string) {
return this.schema.fields.findIndex((f) => f.name === name);
}
public [Symbol.iterator](): IterableIterator<Struct['TValue']> {
return this.batchesUnion[Symbol.iterator]() as any;
}
Expand All @@ -116,7 +125,7 @@ export class Table implements DataFrame {
// load batches
const batch = batches[batchIndex];
// yield all indices
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
for (let index = -1, numRows = batch.length; ++index < numRows;) {
next(index, batch);
}
}
Expand All @@ -142,7 +151,7 @@ export class Table implements DataFrame {
count_by.bind(batch);
const keys = (count_by.vector as DictionaryVector).indicies;
// yield all indices
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
for (let index = -1, numRows = batch.length; ++index < numRows;) {
let key = keys.get(index);
if (key !== null) { counts[key]++; }
}
Expand All @@ -152,6 +161,13 @@ export class Table implements DataFrame {
public select(...columnNames: string[]) {
return new Table(this.batches.map((batch) => batch.select(...columnNames)));
}
public toString(separator?: string) {
let str = '';
for (const row of this.rowsToString(separator)) {
str += row + '\n';
}
return str;
}
public rowsToString(separator = ' | '): TableToStringIterator {
return new TableToStringIterator(tableRowsToString(this, separator));
}
Expand All @@ -176,7 +192,7 @@ class FilteredDataFrame implements DataFrame {
const batch = batches[batchIndex];
const predicate = this.predicate.bind(batch);
// yield all indices
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
for (let index = -1, numRows = batch.length; ++index < numRows;) {
if (predicate(index, batch)) { next(index, batch); }
}
}
Expand All @@ -196,7 +212,7 @@ class FilteredDataFrame implements DataFrame {
const batch = batches[batchIndex];
const predicate = this.predicate.bind(batch);
// yield all indices
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
for (let index = -1, numRows = batch.length; ++index < numRows;) {
if (predicate(index, batch)) { ++sum; }
}
}
Expand Down Expand Up @@ -229,7 +245,7 @@ class FilteredDataFrame implements DataFrame {
count_by.bind(batch);
const keys = (count_by.vector as DictionaryVector).indicies;
// yield all indices
for (let index = -1, numRows = batch.numRows; ++index < numRows;) {
for (let index = -1, numRows = batch.length; ++index < numRows;) {
let key = keys.get(index);
if (key !== null && predicate(index, batch)) { counts[key]++; }
}
Expand All @@ -251,7 +267,7 @@ export class CountByResult extends Table implements DataFrame {
public asJSON(): Object {
const [values, counts] = this.columns;
const result = {} as { [k: string]: number | null };
for (let i = -1; ++i < this.numRows;) {
for (let i = -1; ++i < this.length;) {
result[values.get(i)] = counts.get(i);
}
return result;
Expand Down Expand Up @@ -282,20 +298,20 @@ export class TableToStringIterator implements IterableIterator<string> {
}
}

function *tableRowsToString(table: Table, separator = ' | ') {
function* tableRowsToString(table: Table, separator = ' | ') {
const fields = table.schema.fields;
const header = ['row_id', ...fields.map((f) => `${f}`)].map(stringify);
const maxColumnWidths = header.map(x => x.length);
// Pass one to convert to strings and count max column widths
for (let i = -1, n = table.numRows - 1; ++i < n;) {
for (let i = -1, n = table.length - 1; ++i < n;) {
let val, row = [i, ...table.get(i)];
for (let j = -1, k = row.length; ++j < k; ) {
val = stringify(row[j]);
maxColumnWidths[j] = Math.max(maxColumnWidths[j], val.length);
}
}
yield header.map((x, j) => leftPad(x, ' ', maxColumnWidths[j])).join(separator);
for (let i = -1, n = table.numRows; ++i < n;) {
for (let i = -1, n = table.length; ++i < n;) {
yield [i, ...table.get(i)]
.map((x) => stringify(x))
.map((x, j) => leftPad(x, ' ', maxColumnWidths[j]))
Expand Down
4 changes: 2 additions & 2 deletions js/test/integration/validate-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ function testReaderIntegration() {
const jsonRecordBatches = toArray(read(jsonData));
const binaryRecordBatches = toArray(read(arrowBuffers));
for (const [jsonRecordBatch, binaryRecordBatch] of zip(jsonRecordBatches, binaryRecordBatches)) {
expect(jsonRecordBatch.length).toEqual(binaryRecordBatch.length);
expect(jsonRecordBatch.numCols).toEqual(binaryRecordBatch.numCols);
expect(jsonRecordBatch.numRows).toEqual(binaryRecordBatch.numRows);
for (let i = -1, n = jsonRecordBatch.numCols; ++i < n;) {
(expect(jsonRecordBatch.columns[i]) as any).toEqualVector(binaryRecordBatch.columns[i]);
}
Expand All @@ -124,8 +124,8 @@ function testTableFromBuffersIntegration() {
expect.hasAssertions();
const jsonTable = Table.from(jsonData);
const binaryTable = Table.from(arrowBuffers);
expect(jsonTable.length).toEqual(binaryTable.length);
expect(jsonTable.numCols).toEqual(binaryTable.numCols);
expect(jsonTable.numRows).toEqual(binaryTable.numRows);
for (let i = -1, n = jsonTable.numCols; ++i < n;) {
(expect(jsonTable.columns[i]) as any).toEqualVector(binaryTable.columns[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions js/test/unit/table-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe(`Table`, () => {
[new Float32Array([ 0.3])[0], -1, 'a']
];
test(`has the correct length`, () => {
expect(table.numRows).toEqual(values.length);
expect(table.length).toEqual(values.length);
});
test(`gets expected values`, () => {
for (let i = -1; ++i < values.length;) {
Expand Down Expand Up @@ -332,7 +332,7 @@ describe(`Table`, () => {
[new Float32Array([ 0.1])[0], -1, 'c'],
];
test(`has the correct length`, () => {
expect(table.numRows).toEqual(values.length);
expect(table.length).toEqual(values.length);
});
test(`gets expected values`, () => {
for (let i = -1; ++i < values.length;) {
Expand Down

0 comments on commit b7f5bfb

Please sign in to comment.