Skip to content

Commit a9fff89

Browse files
author
Brian Hulette
committed
Move Table out of the Vector hierarchy
1 parent 1d60aa1 commit a9fff89

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

js/src/Arrow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { Table } from './vector/table';
18+
import { Table, TableRow } from './table';
19+
import { lit, col } from './predicate';
1920
import { Vector } from './vector/vector';
2021
import { Utf8Vector } from './vector/utf8';
2122
import { DictionaryVector } from './vector/dictionary';
@@ -45,8 +46,6 @@ import {
4546
TimestampVector,
4647
} from './vector/numeric';
4748

48-
import { lit, col } from './vector/predicate';
49-
5049
// closure compiler always erases static method names:
5150
// https://github.com/google/closure-compiler/issues/1776
5251
// set them via string indexers to save them from the mangler
@@ -55,7 +54,9 @@ Table['fromAsync'] = Table.fromAsync;
5554
BoolVector['pack'] = BoolVector.pack;
5655

5756
export { read, readAsync };
58-
export { Table, Vector, StructRow };
57+
export { Table, TableRow };
58+
export { lit, col };
59+
export { Vector, StructRow };
5960
export { Uint64, Int64, Int128 };
6061
export { NumericVectorConstructor } from './vector/numeric';
6162
export { List, TypedArray, TypedArrayConstructor } from './vector/types';
@@ -86,7 +87,6 @@ export {
8687
FixedSizeListVector,
8788
};
8889

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

9191

9292
/* These exports are needed for the closure umd targets */

js/src/bin/arrow2csv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ files.forEach((source) => {
9898
});
9999

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

135-
})();
135+
})();

js/src/vector/predicate.ts renamed to js/src/predicate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Vector } from "../vector/vector";
2-
import { DictionaryVector } from "../vector/dictionary";
1+
import { Vector } from "./vector/vector";
2+
import { DictionaryVector } from "./vector/dictionary";
33

44
export type ValueFunc<T> = (idx: number, cols: Vector[]) => T|null;
55
export type PredicateFunc = (idx: number, cols: Vector[]) => boolean;

js/src/vector/table.ts renamed to js/src/table.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
import { Vector } from './vector';
19-
import { StructVector, StructRow } from './struct';
20-
import { read, readAsync } from '../reader/arrow';
18+
import { Vector } from './vector/vector';
19+
import { read, readAsync } from './reader/arrow';
2120
import { Predicate } from './predicate';
2221

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

25-
export class DataFrameRow extends StructRow<any> {
26-
constructor (batch: Vector[], idx: number) {
27-
super(new StructVector({columns: batch}), idx);
24+
export class TableRow {
25+
constructor (readonly batch: Vector[], readonly idx: number) {}
26+
toArray() {
27+
return this.batch.map((vec) => vec.get(this.idx));
2828
}
2929
toString() {
3030
return this.toArray().map((x) => JSON.stringify(x)).join(', ');
3131
}
32+
*[Symbol.iterator]() {
33+
for (const vec of this.batch) {
34+
yield vec.get(this.idx);
35+
}
36+
}
3237
}
3338

3439
export interface DataFrame {
@@ -46,7 +51,7 @@ function columnsFromBatches(batches: Vector[][]) {
4651
);
4752
}
4853

49-
export class Table extends StructVector<any> implements DataFrame {
54+
export class Table implements DataFrame {
5055
static from(sources?: Iterable<Uint8Array | Buffer | string> | object | string) {
5156
let batches: Vector<any>[][] = [[]];
5257
if (sources) {
@@ -73,20 +78,20 @@ export class Table extends StructVector<any> implements DataFrame {
7378
readonly lengths: Uint32Array;
7479
readonly length: number;
7580
constructor(argv: { batches: Vector<any>[][] }) {
76-
super({columns: columnsFromBatches(argv.batches)});
7781
this.batches = argv.batches;
82+
this.columns = columnsFromBatches(this.batches);
7883
this.lengths = new Uint32Array(this.batches.map((batch) => batch[0].length));
7984

8085
this.length = this.lengths.reduce((acc, length) => acc + length);
8186
}
82-
get(idx: number): DataFrameRow {
87+
get(idx: number): TableRow {
8388
let batch = 0;
8489
while (idx > this.lengths[batch] && batch < this.lengths.length)
8590
idx -= this.lengths[batch++];
8691

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

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

117122
// yield all indices
118123
for (let idx = -1; ++idx < length;) {
119-
yield new DataFrameRow(columns, idx);
124+
yield new TableRow(columns, idx);
120125
}
121126
}
122127
}

0 commit comments

Comments
 (0)