Skip to content

Commit 7244887

Browse files
author
Brian Hulette
committed
Add table unit tests...
.. also found and resolved some minor bugs (get(idx) batch length check should be <=, various extern issues with UMD builds)
1 parent 6719147 commit 7244887

File tree

4 files changed

+411
-11
lines changed

4 files changed

+411
-11
lines changed

js/src/Arrow.externs.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ Table.prototype.toString;
5454
Table.prototype.lengths;
5555
/** @type {?} */
5656
Table.prototype.batches;
57+
/** @type {?} */
58+
Table.prototype.countBy;
59+
/** @type {?} */
60+
Table.prototype.scan;
61+
/** @type {?} */
62+
Table.prototype.get;
63+
64+
let CountByResult = function() {};
65+
/** @type {?} */
66+
CountByResult.prototype.asJSON;
5767

5868
let Vector = function() {};
5969
/** @type {?} */

js/src/Arrow.ts

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

18-
import { Table, TableRow } from './table';
19-
import { lit, col } from './predicate';
18+
import { Table, TableRow, CountByResult } from './table';
19+
import { lit, col, Col, Value } from './predicate';
2020
import { Vector } from './vector/vector';
2121
import { Utf8Vector } from './vector/utf8';
2222
import { DictionaryVector } from './vector/dictionary';
@@ -54,8 +54,8 @@ Table['fromAsync'] = Table.fromAsync;
5454
BoolVector['pack'] = BoolVector.pack;
5555

5656
export { read, readAsync };
57-
export { Table, TableRow };
58-
export { lit, col };
57+
export { Table, TableRow, CountByResult };
58+
export { lit, col, Col, Value };
5959
export { Vector, StructRow };
6060
export { Uint64, Int64, Int128 };
6161
export { NumericVectorConstructor } from './vector/numeric';
@@ -94,9 +94,11 @@ try {
9494
// string indexers tell closure compiler not to rename these properties
9595
Arrow['lit'] = lit;
9696
Arrow['col'] = col;
97+
Arrow['Col'] = Col;
9798
Arrow['read'] = read;
98-
Arrow['readAsync'] = readAsync;
99+
Arrow['Value'] = Value;
99100
Arrow['Table'] = Table;
101+
Arrow['readAsync'] = readAsync;
100102
Arrow['Vector'] = Vector;
101103
Arrow['StructRow'] = StructRow;
102104
Arrow['BoolVector'] = BoolVector;
@@ -120,6 +122,7 @@ try {
120122
Arrow['Float32Vector'] = Float32Vector;
121123
Arrow['Float64Vector'] = Float64Vector;
122124
Arrow['DecimalVector'] = DecimalVector;
125+
Arrow['CountByResult'] = CountByResult;
123126
Arrow['TimestampVector'] = TimestampVector;
124127
Arrow['DictionaryVector'] = DictionaryVector;
125128
Arrow['FixedSizeListVector'] = FixedSizeListVector;

js/src/table.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export interface DataFrame {
4242
filter(predicate: Predicate): DataFrame;
4343
scan(next: NextFunc): void;
4444
count(): number;
45-
countBy(col: (Col|string)): Table;
45+
countBy(col: (Col|string)): CountByResult;
4646
}
4747

4848
function columnsFromBatches(batches: Vector[][]) {
@@ -87,7 +87,7 @@ export class Table implements DataFrame {
8787
}
8888
get(idx: number): TableRow {
8989
let batch = 0;
90-
while (idx > this.lengths[batch] && batch < this.lengths.length) {
90+
while (idx >= this.lengths[batch] && batch < this.lengths.length) {
9191
idx -= this.lengths[batch++];
9292
}
9393

@@ -114,7 +114,7 @@ export class Table implements DataFrame {
114114
count(): number {
115115
return this.lengths.reduce((acc, val) => acc + val);
116116
}
117-
countBy(count_by: (Col|string)): Table {
117+
countBy(count_by: (Col|string)): CountByResult {
118118
if (count_by instanceof String) {
119119
count_by = new Col(count_by);
120120
}
@@ -146,7 +146,7 @@ export class Table implements DataFrame {
146146
}
147147
}
148148

149-
return new Table({batches: [[keys, new Uint32Vector({data: counts})]]})
149+
return new CountByResult(keys, new Uint32Vector({data: counts}))
150150
}
151151
*[Symbol.iterator]() {
152152
for (let batch = -1; ++batch < this.lengths.length;) {
@@ -215,7 +215,7 @@ class FilteredDataFrame implements DataFrame {
215215
);
216216
}
217217

218-
countBy(count_by: (Col|string)): Table {
218+
countBy(count_by: (Col|string)): CountByResult {
219219
if (count_by instanceof String) {
220220
count_by = new Col(count_by);
221221
}
@@ -246,6 +246,22 @@ class FilteredDataFrame implements DataFrame {
246246
}
247247
}
248248

249-
return new Table({batches: [[keys, new Uint32Vector({data: counts})]]})
249+
return new CountByResult(keys, new Uint32Vector({data: counts}))
250+
}
251+
}
252+
253+
export class CountByResult extends Table implements DataFrame {
254+
constructor(readonly keys: Vector, readonly counts: Vector<number|null>) {
255+
super({batches: [[keys, counts]]});
256+
}
257+
258+
asJSON(): Object {
259+
let result: {[key: string]: number|null} = {};
260+
261+
for (let i = -1; ++i < this.length;) {
262+
result[this.keys.get(i)] = this.counts.get(i);
263+
}
264+
265+
return result;
250266
}
251267
}

0 commit comments

Comments
 (0)