From d9c5df3bf9dcd49f59b48b55926cf13ac20af920 Mon Sep 17 00:00:00 2001 From: jheer Date: Wed, 28 Apr 2021 12:55:51 +0200 Subject: [PATCH] feat: Add columnArray array constructor argument. --- docs/api/table.md | 10 ++++++++-- src/table/column-table.js | 8 +++++--- src/table/table.js | 16 ++++++++++++++-- test/arrow/profiler-test.js | 3 +-- test/table/column-table-test.js | 29 +++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 9 deletions(-) diff --git a/docs/api/table.md b/docs/api/table.md index 64074f25..2fa5601e 100644 --- a/docs/api/table.md +++ b/docs/api/table.md @@ -217,11 +217,12 @@ dt.columnAt(1).get(1) // 5 ```
# -table.columnArray(name) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/column-table.js) +table.columnArray(name[, arrayConstructor]) · [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* @@ -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) +``` +
# table.columnIndex(name) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/table.js) diff --git a/src/table/column-table.js b/src/table/column-table.js index 0142b2e3..e3d0f900 100644 --- a/src/table/column-table.js +++ b/src/table/column-table.js @@ -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; diff --git a/src/table/table.js b/src/table/table.js index de2a1fd3..1ae71344 100644 --- a/src/table/table.js +++ b/src/table/table.js @@ -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'); } @@ -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 diff --git a/test/arrow/profiler-test.js b/test/arrow/profiler-test.js index 60b467a9..d96bb209 100644 --- a/test/arrow/profiler-test.js +++ b/test/arrow/profiler-test.js @@ -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; diff --git a/test/table/column-table-test.js b/test/table/column-table-test.js index ccb85378..6da41bb9 100644 --- a/test/table/column-table-test.js +++ b/test/table/column-table-test.js @@ -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'],