Skip to content

Commit

Permalink
Move Table out of the Vector hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Hulette committed Jan 12, 2018
1 parent 1d60aa1 commit a9fff89
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
10 changes: 5 additions & 5 deletions js/src/Arrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// specific language governing permissions and limitations
// under the License.

import { Table } from './vector/table';
import { Table, TableRow } from './table';
import { lit, col } from './predicate';
import { Vector } from './vector/vector';
import { Utf8Vector } from './vector/utf8';
import { DictionaryVector } from './vector/dictionary';
Expand Down Expand Up @@ -45,8 +46,6 @@ import {
TimestampVector,
} from './vector/numeric';

import { lit, col } from './vector/predicate';

// closure compiler always erases static method names:
// https://github.com/google/closure-compiler/issues/1776
// set them via string indexers to save them from the mangler
Expand All @@ -55,7 +54,9 @@ Table['fromAsync'] = Table.fromAsync;
BoolVector['pack'] = BoolVector.pack;

export { read, readAsync };
export { Table, Vector, StructRow };
export { Table, TableRow };
export { lit, col };
export { Vector, StructRow };
export { Uint64, Int64, Int128 };
export { NumericVectorConstructor } from './vector/numeric';
export { List, TypedArray, TypedArrayConstructor } from './vector/types';
Expand Down Expand Up @@ -86,7 +87,6 @@ export {
FixedSizeListVector,
};

export { lit, col } from './vector/predicate';


/* These exports are needed for the closure umd targets */
Expand Down
4 changes: 2 additions & 2 deletions js/src/bin/arrow2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ files.forEach((source) => {
});

function printTable(table: Arrow.Table) {
let header = [...table.columns.map((_, i) => table.key(i))].map(stringify);
let header = [...table.columns.map((c) => c.name)].map(stringify);
let maxColumnWidths = header.map(x => x.length);
// Pass one to convert to strings and count max column widths
for (let i = -1, n = table.length - 1; ++i < n;) {
Expand Down Expand Up @@ -132,4 +132,4 @@ function stringify(x: any) {
: `${x}`;
}

})();
})();
4 changes: 2 additions & 2 deletions js/src/vector/predicate.ts → js/src/predicate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vector } from "../vector/vector";
import { DictionaryVector } from "../vector/dictionary";
import { Vector } from "./vector/vector";
import { DictionaryVector } from "./vector/dictionary";

export type ValueFunc<T> = (idx: number, cols: Vector[]) => T|null;
export type PredicateFunc = (idx: number, cols: Vector[]) => boolean;
Expand Down
27 changes: 16 additions & 11 deletions js/src/vector/table.ts → js/src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@
// specific language governing permissions and limitations
// under the License.

import { Vector } from './vector';
import { StructVector, StructRow } from './struct';
import { read, readAsync } from '../reader/arrow';
import { Vector } from './vector/vector';
import { read, readAsync } from './reader/arrow';
import { Predicate } from './predicate';

export type NextFunc = (idx: number, cols: Vector[]) => void;

export class DataFrameRow extends StructRow<any> {
constructor (batch: Vector[], idx: number) {
super(new StructVector({columns: batch}), idx);
export class TableRow {
constructor (readonly batch: Vector[], readonly idx: number) {}
toArray() {
return this.batch.map((vec) => vec.get(this.idx));
}
toString() {
return this.toArray().map((x) => JSON.stringify(x)).join(', ');
}
*[Symbol.iterator]() {
for (const vec of this.batch) {
yield vec.get(this.idx);
}
}
}

export interface DataFrame {
Expand All @@ -46,7 +51,7 @@ function columnsFromBatches(batches: Vector[][]) {
);
}

export class Table extends StructVector<any> implements DataFrame {
export class Table implements DataFrame {
static from(sources?: Iterable<Uint8Array | Buffer | string> | object | string) {
let batches: Vector<any>[][] = [[]];
if (sources) {
Expand All @@ -73,20 +78,20 @@ export class Table extends StructVector<any> implements DataFrame {
readonly lengths: Uint32Array;
readonly length: number;
constructor(argv: { batches: Vector<any>[][] }) {
super({columns: columnsFromBatches(argv.batches)});
this.batches = argv.batches;
this.columns = columnsFromBatches(this.batches);
this.lengths = new Uint32Array(this.batches.map((batch) => batch[0].length));

this.length = this.lengths.reduce((acc, length) => acc + length);
}
get(idx: number): DataFrameRow {
get(idx: number): TableRow {
let batch = 0;
while (idx > this.lengths[batch] && batch < this.lengths.length)
idx -= this.lengths[batch++];

if (batch === this.lengths.length) throw new Error("Overflow")

else return new DataFrameRow(this.batches[batch], idx);
else return new TableRow(this.batches[batch], idx);
}
filter(predicate: Predicate): DataFrame {
return new FilteredDataFrame(this, predicate);
Expand Down Expand Up @@ -116,7 +121,7 @@ export class Table extends StructVector<any> implements DataFrame {

// yield all indices
for (let idx = -1; ++idx < length;) {
yield new DataFrameRow(columns, idx);
yield new TableRow(columns, idx);
}
}
}
Expand Down

0 comments on commit a9fff89

Please sign in to comment.