Skip to content

Commit

Permalink
refactor!: More refactoring and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jheer committed Jul 11, 2024
1 parent 22eb676 commit a48f5ec
Show file tree
Hide file tree
Showing 47 changed files with 662 additions and 614 deletions.
4 changes: 2 additions & 2 deletions docs/api/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ const colValues = Array.from(table.values('colA'));
<hr/><a id="data" href="#data">#</a>
<em>table</em>.<b>data</b>() · [Source](https://github.com/uwdata/arquero/blob/master/src/table/table.js)

Returns the internal table storage data structure: an object with column names for keys and column arrays for values.
Returns the internal table storage data structure: an object with column names for keys and column arrays for values. This method returns the same structure used by the Table (not a copy) and its contents should not be modified.

<hr/><a id="get" href="#get">#</a>
<em>table</em>.<b>get</b>(<i>name</i>[, <i>row</i>]) · [Source](https://github.com/uwdata/arquero/blob/master/src/table/column-table.js)
Expand Down Expand Up @@ -428,7 +428,7 @@ Perform a table scan, invoking the provided *callback* function for each row of

* *callback*: Function invoked for each row of the table. The callback is invoked with the following arguments:
* *row*: The table row index.
* *data*: The backing table data store.
* *data*: The backing table data store (as returned by table [`data`](#data) method).
* *stop*: A function to stop the scan early. The callback can invoke *stop()* to prevent future scan calls.
* *order*: A boolean flag (default `false`), indicating if the table should be scanned in the order determined by [orderby](verbs#orderby). This argument has no effect if the table is unordered.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
],
"license": "BSD-3-Clause",
"author": "Jeffrey Heer (http://idl.cs.washington.edu)",
"exports": "./src/index-node.js",
"exports": "./src/index.js",
"unpkg": "dist/arquero.min.js",
"jsdelivr": "dist/arquero.min.js",
"types": "dist/types/index.d.ts",
"browser": {
"./src/index-node.js": "./src/index.js"
"./src/index.js": "./src/index-browser.js"
},
"repository": {
"type": "git",
Expand Down
11 changes: 2 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import bundleSize from 'rollup-plugin-bundle-size';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';

function onwarn(warning, defaultHandler) {
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
defaultHandler(warning);
}
}

const name = 'aq';
const external = [ 'apache-arrow', 'node-fetch' ];
const external = [ 'apache-arrow' ];
const globals = { 'apache-arrow': 'Arrow' };
const plugins = [
bundleSize(),
Expand All @@ -18,10 +12,9 @@ const plugins = [

export default [
{
input: 'src/index.js',
input: 'src/index-browser.js',
external,
plugins,
onwarn,
output: [
{
file: 'dist/arquero.js',
Expand Down
32 changes: 32 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// export internal class and method definitions
export { BitSet } from './table/BitSet.js';
export { Table } from './table/Table.js';
export { ColumnTable } from './table/ColumnTable.js';
export { default as Reducer } from './verbs/reduce/reducer.js';
export { default as parse } from './expression/parse.js';
export { default as walk_ast } from './expression/ast/walk.js';

// public API
export { seed } from './util/random.js';
export { default as fromArrow } from './arrow/from-arrow.js';
export { default as fromCSV } from './format/from-csv.js';
export { default as fromFixed } from './format/from-fixed.js';
export { default as fromJSON } from './format/from-json.js';
export { default as toArrow } from './arrow/to-arrow.js';
export { default as toArrowIPC } from './arrow/to-arrow-ipc.js';
export { default as toCSV } from './format/to-csv.js';
export { default as toHTML } from './format/to-html.js';
export { default as toJSON } from './format/to-json.js';
export { default as toMarkdown } from './format/to-markdown.js';
export { default as bin } from './helpers/bin.js';
export { default as escape } from './helpers/escape.js';
export { default as desc } from './helpers/desc.js';
export { default as field } from './helpers/field.js';
export { default as frac } from './helpers/frac.js';
export { default as names } from './helpers/names.js';
export { default as rolling } from './helpers/rolling.js';
export { all, endswith, matches, not, range, startswith } from './helpers/selection.js';
export { default as agg } from './verbs/helpers/agg.js';
export { default as op } from './op/op-api.js';
export { addAggregateFunction, addFunction, addWindowFunction } from './op/register.js';
export { table, from } from './table/index.js';
1 change: 1 addition & 0 deletions src/format/from-text-rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function(next, names, options) {
}
}

/** @type {import('../table/types.js').ColumnData} */
const columns = {};
names.forEach((name, i) => columns[name] = values[i]);
return new ColumnTable(columns, names);
Expand Down
2 changes: 2 additions & 0 deletions src/index-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './api.js';
export { load, loadArrow, loadCSV, loadFixed, loadJSON } from './format/load-url.js';
8 changes: 0 additions & 8 deletions src/index-node.js

This file was deleted.

35 changes: 2 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
// export internal class and method definitions
export { BitSet } from './table/BitSet.js';
export { Table } from './table/Table.js';
export { ColumnTable } from './table/ColumnTable.js';
export { default as Reducer } from './verbs/reduce/reducer.js';
export { default as parse } from './expression/parse.js';
export { default as walk_ast } from './expression/ast/walk.js';

// public API
export { seed } from './util/random.js';
export { default as fromArrow } from './arrow/from-arrow.js';
export { default as fromCSV } from './format/from-csv.js';
export { default as fromFixed } from './format/from-fixed.js';
export { default as fromJSON } from './format/from-json.js';
export { load, loadArrow, loadCSV, loadFixed, loadJSON } from './format/load-url.js';
export { default as toArrow } from './arrow/to-arrow.js';
export { default as toArrowIPC } from './arrow/to-arrow-ipc.js';
export { default as toCSV } from './format/to-csv.js';
export { default as toHTML } from './format/to-html.js';
export { default as toJSON } from './format/to-json.js';
export { default as toMarkdown } from './format/to-markdown.js';
export { default as bin } from './helpers/bin.js';
export { default as escape } from './helpers/escape.js';
export { default as desc } from './helpers/desc.js';
export { default as field } from './helpers/field.js';
export { default as frac } from './helpers/frac.js';
export { default as names } from './helpers/names.js';
export { default as rolling } from './helpers/rolling.js';
export { all, endswith, matches, not, range, startswith } from './helpers/selection.js';
export { default as agg } from './verbs/helpers/agg.js';
export { default as op } from './op/op-api.js';
export * from './op/register.js';
export * from './table/index.js';
export * from './api.js';
export { load, loadArrow, loadCSV, loadFixed, loadJSON } from './format/load-file.js';
22 changes: 11 additions & 11 deletions src/table/ColumnTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ColumnTable extends Table {
* @example table.assign(table1, table2)
*/
assign(...tables) {
return assign(this, tables.flat());
return assign(this, ...tables);
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ export class ColumnTable extends Table {
* @example table.groupby({ key: d => d.colA + d.colB })
*/
groupby(...keys) {
return groupby(this, keys.flat());
return groupby(this, ...keys);
}

/**
Expand Down Expand Up @@ -168,7 +168,7 @@ export class ColumnTable extends Table {
* @example table.orderby(desc(d => d.a))
*/
orderby(...keys) {
return orderby(this, keys.flat());
return orderby(this, ...keys);
}

/**
Expand Down Expand Up @@ -204,7 +204,7 @@ export class ColumnTable extends Table {
* @example table.rename({ a: 'a2', b: 'b2' })
*/
rename(...columns) {
return rename(this, columns.flat());
return rename(this, ...columns);
}

/**
Expand Down Expand Up @@ -273,7 +273,7 @@ export class ColumnTable extends Table {
* @example table.select({ colA: 'newA', colB: 'newB' })
*/
select(...columns) {
return select(this, columns.flat());
return select(this, ...columns);
}

/**
Expand Down Expand Up @@ -313,7 +313,7 @@ export class ColumnTable extends Table {
* @example table.dedupe({ abs: d => op.abs(d.a) })
*/
dedupe(...keys) {
return dedupe(this, keys.flat());
return dedupe(this, ...keys);
}

/**
Expand Down Expand Up @@ -460,7 +460,7 @@ export class ColumnTable extends Table {
* @example table.lookup(other, ['key1', 'key2'], 'value1', 'value2')
*/
lookup(other, on, ...values) {
return lookup(this, other, on, values.flat());
return lookup(this, other, on, ...values);
}

/**
Expand Down Expand Up @@ -730,7 +730,7 @@ export class ColumnTable extends Table {
* @example table.concat([other1, other2])
*/
concat(...tables) {
return concat(this, tables.flat());
return concat(this, ...tables);
}

/**
Expand All @@ -747,7 +747,7 @@ export class ColumnTable extends Table {
* @example table.union([other1, other2])
*/
union(...tables) {
return union(this, tables.flat());
return union(this, ...tables);
}

/**
Expand All @@ -763,7 +763,7 @@ export class ColumnTable extends Table {
* @example table.intersect([other1, other2])
*/
intersect(...tables) {
return intersect(this, tables.flat());
return intersect(this, ...tables);
}

/**
Expand All @@ -779,7 +779,7 @@ export class ColumnTable extends Table {
* @example table.except([other1, other2])
*/
except(...tables) {
return except(this, tables.flat());
return except(this, ...tables);
}

// -- Table Output Formats ------------------------------------------------
Expand Down
40 changes: 23 additions & 17 deletions src/table/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export class Table {

/**
* Instantiate a Table instance.
* @param {object} columns An object mapping column names to values.
* @param {string[]} [names] An ordered list of column names.
* @param {import('./types.js').ColumnData} columns
* An object mapping column names to values.
* @param {string[]} [names]
* An ordered list of column names.
* @param {import('./BitSet.js').BitSet} [filter]
* A filtering BitSet.
* @param {import('./types.js').GroupBySpec} [group]
Expand All @@ -26,17 +28,29 @@ export class Table {
* An object mapping parameter names to values.
*/
constructor(columns, names, filter, group, order, params) {
const data = { ...columns };
const data = Object.freeze({ ...columns });
names = names ?? Object.keys(data);
const nrows = names.length ? data[names[0]].length : 0;
/** @private */
this._names = Object.freeze(names);
/** @private */
this._data = data;
/** @private */
this._total = nrows;
/** @private */
this._nrows = filter?.count() ?? nrows;
/** @private */
this._mask = filter ?? null;
/** @private */
this._group = group ?? null;
/** @private */
this._order = order ?? null;
/** @private */
this._params = params;
/** @private */
this._index = null;
/** @private */
this._partitions = null;
}

/**
Expand Down Expand Up @@ -94,9 +108,10 @@ export class Table {
*/
get [Symbol.toStringTag]() {
if (!this._names) return 'Object'; // bail if called on prototype
const nr = this.numRows() + ' row' + (this.numRows() !== 1 ? 's' : '');
const nc = this.numCols() + ' col' + (this.numCols() !== 1 ? 's' : '');
return `Table: ${nc} x ${nr}`
const nr = this.numRows();
const nc = this.numCols();
const plural = v => v !== 1 ? 's' : '';
return `Table: ${nc} col${plural(nc)} x ${nr} row${plural(nr)}`
+ (this.isFiltered() ? ` (${this.totalRows()} backing)` : '')
+ (this.isGrouped() ? `, ${this._group.size} groups` : '')
+ (this.isOrdered() ? ', ordered' : '');
Expand Down Expand Up @@ -127,9 +142,9 @@ export class Table {
}

/**
* Returns the internal table storage data structure.
* Get the backing column data for this table.
* @return {import('./types.js').ColumnData}
* The backing table storage data structure.
* Object of named column instances.
*/
data() {
return this._data;
Expand Down Expand Up @@ -233,15 +248,6 @@ export class Table {
return this._names.indexOf(name);
}

/**
* Get the backing set of columns for this table.
* @return {import('./types.js').ColumnData}
* Object of named column instances.
*/
columns() {
return this._data;
}

/**
* Get the column instance with the given name.
* @param {string} name The column name.
Expand Down
4 changes: 4 additions & 0 deletions src/table/columns-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import isObject from '../util/is-object.js';
import isRegExp from '../util/is-regexp.js';
import isString from '../util/is-string.js';

/**
* @return {import('./types.js').ColumnData}
*/
export function columnsFrom(values, names) {
const raise = type => error(`Illegal argument type: ${type || typeof values}`);
// @ts-ignore
return values instanceof Map ? fromKeyValuePairs(values.entries(), names)
: isDate(values) ? raise('Date')
: isRegExp(values) ? raise('RegExp')
Expand Down
1 change: 1 addition & 0 deletions src/table/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { columnsFrom } from './columns-from.js';
*/
export function table(columns, names) {
if (columns instanceof ColumnTable) return columns;
/** @type {import('./types.js').ColumnData} */
const data = {};
const keys = [];
for (const [key, value] of entries(columns)) {
Expand Down
7 changes: 4 additions & 3 deletions src/verbs/assign.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { columnSet } from '../table/ColumnSet.js';
import { table as newTable } from '../table/index.js';
import { Table } from '../table/Table.js';
import error from '../util/error.js';

export function assign(table, others) {
export function assign(table, ...others) {
others = others.flat();
const nrows = table.numRows();
const base = table.reify();
const cols = columnSet(base).groupby(base.groups());
others.forEach(input => {
input = newTable(input);
input = input instanceof Table ? input : new Table(input);
if (input.numRows() !== nrows) error('Assign row counts do not match');
input = input.reify();
input.columnNames(name => cols.add(name, input.column(name)));
Expand Down
3 changes: 2 additions & 1 deletion src/verbs/concat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { columnSet } from '../table/ColumnSet.js';
import NULL from '../util/null.js';

export function concat(table, others) {
export function concat(table, ...others) {
others = others.flat();
const trows = table.numRows();
const nrows = trows + others.reduce((n, t) => n + t.numRows(), 0);
if (trows === nrows) return table;
Expand Down
3 changes: 2 additions & 1 deletion src/verbs/dedupe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { groupby } from './groupby.js';
import { filter } from './filter.js';

export function dedupe(table, keys = []) {
export function dedupe(table, ...keys) {
keys = keys.flat();
const gt = groupby(table, keys.length ? keys : table.columnNames());
return filter(gt, 'row_number() === 1').ungroup().reify();
}
Loading

0 comments on commit a48f5ec

Please sign in to comment.