Skip to content

Commit

Permalink
feat: Add columnArray array constructor argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Apr 28, 2021
1 parent 50285c8 commit d9c5df3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
10 changes: 8 additions & 2 deletions docs/api/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,12 @@ dt.columnAt(1).get(1) // 5
```

<hr/><a id="columnArray" href="#columnArray">#</a>
<em>table</em>.<b>columnArray</b>(<i>name</i>) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/column-table.js)
<em>table</em>.<b>columnArray</b>(<i>name</i>[, <i>arrayConstructor</i>]) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/column-table.js)

Get an array of values contained in the column with the given *name*. Unlike direct access through the table [column](#column) method, the array returned by this method respects any table filter or orderby criteria.
Get an array of values contained in the column with the given *name*. Unlike direct access through the table [column](#column) method, the array returned by this method respects any table filter or orderby criteria. By default, a standard [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) is returned; use the *arrayConstructor* argument to specify a [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray).

* *name*: The column name.
* *arrayConstructor*: An optional array constructor (default [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array)) to use to instantiate the output array. Note that errors or truncated values may occur when assigning to a typed array with an incompatible type.

*Examples*

Expand All @@ -236,6 +237,11 @@ aq.table({ a: [1, 2, 3], b: [4, 5, 6] })
.columnArray('b'); // [ 5, 6 ]
```

```js
aq.table({ a: [1, 2, 3], b: [4, 5, 6] })
.columnArray('b', Int32Array); // Int32Array.of(4, 5, 6)
```

<hr/><a id="columnIndex" href="#columnIndex">#</a>
<em>table</em>.<b>columnIndex</b>(<i>name</i>) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/table.js)

Expand Down
8 changes: 5 additions & 3 deletions src/table/column-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ export default class ColumnTable extends Table {
* Get an array of values contained in a column. The resulting array
* respects any table filter or orderby criteria.
* @param {string} name The column name.
* @return {import('./table').DataValue[]} The array of column values.
* @param {ArrayConstructor|import('./table').TypedArrayConstructor} [arrayConstructor=Array]
* The array constructor for instantiating the output array.
* @return {import('./table').DataValue[]|import('./table).TypedArray} The array of column values.
*/
columnArray(name) {
columnArray(name, arrayConstructor = Array) {
const column = this.column(name);
const array = Array(this.numRows());
const array = new arrayConstructor(this.numRows());
let idx = -1;
this.scan(row => array[++idx] = column.get(row), true);
return array;
Expand Down
16 changes: 14 additions & 2 deletions src/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ export default class Table extends Transformable {
* Get an array of values contained in a column. The resulting array
* respects any table filter or orderby criteria.
* @param {string} name The column name.
* @return {DataValue[]} The array of column values.
* @param {ArrayConstructor|TypedArrayConstructor} [arrayConstructor=Array]
* The array constructor for instantiating the output array.
* @return {DataValue[]|TypedArray} The array of column values.
*/
columnArray(name) { // eslint-disable-line no-unused-vars
columnArray(name, arrayConstructor) { // eslint-disable-line no-unused-vars
error('Not implemented');
}

Expand Down Expand Up @@ -450,6 +452,16 @@ export default class Table extends Transformable {
}
}

/**
* A typed array constructor.
* @typedef {Uint8ArrayConstructor|Uint16ArrayConstructor|Uint32ArrayConstructor|BigUint64ArrayConstructor|Int8ArrayConstructor|Int16ArrayConstructor|Int32ArrayConstructor|BigInt64ArrayConstructor|Float32ArrayConstructor|Float64ArrayConstructor} TypedArrayConstructor
*/

/**
* A typed array instance.
* @typedef {Uint8Array|Uint16Array|Uint32Array|BigUint64Array|Int8Array|Int16Array|Int32Array|BigInt64Array|Float32Array|Float64Array} TypedArray
*/

/**
* Backing table data.
* @typedef {object|Array} TableData
Expand Down
3 changes: 1 addition & 2 deletions test/arrow/profiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import tape from 'tape';
import { profiler } from '../../src/arrow/encode/profiler';
import {
Float64, Int16, Int32, Int64, Int8,
Uint16, Uint32, Uint64, Uint8,
util
Uint16, Uint32, Uint64, Uint8, util
} from 'apache-arrow';

const { compareTypes } = util;
Expand Down
29 changes: 29 additions & 0 deletions test/table/column-table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ tape('ColumnTable supports object output', t => {
t.end();
});

tape('ColumnTable supports column array output', t => {
const dt = new ColumnTable({
u: ['a', 'a', 'a', 'b', 'b'],
v: [2, 1, 4, 5, 3]
})
.filter(d => d.v > 1)
.orderby('v');

t.deepEqual(
dt.columnArray('u'),
['a', 'b', 'a', 'b'],
'column array, strings'
);

t.deepEqual(
dt.columnArray('v'),
[2, 3, 4, 5],
'column array, numbers'
);

t.deepEqual(
dt.columnArray('v', Int32Array),
Int32Array.of(2, 3, 4, 5),
'column array, typed array'
);

t.end();
});

tape('ColumnTable supports grouped object output', t => {
const dt = new ColumnTable({
u: ['a', 'a', 'a', 'b', 'b'],
Expand Down

0 comments on commit d9c5df3

Please sign in to comment.