Skip to content

Commit 1910962

Browse files
author
Brian Hulette
committed
Add optional bind callback to scan
1 parent 5bdf17f commit 1910962

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

js/src/Arrow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import * as predicate_ from './predicate';
2525
import { Vector } from './vector';
2626
import { RecordBatch } from './recordbatch';
2727
import { Schema, Field, Type } from './type';
28-
import { Table, DataFrame, NextFunc, CountByResult } from './table';
28+
import { Table, DataFrame, NextFunc, BindFunc, CountByResult } from './table';
2929
import { read, readAsync } from './ipc/reader/arrow';
3030

3131
export import View = vector_.View;
@@ -36,7 +36,7 @@ export import TimeBitWidth = type_.TimeBitWidth;
3636
export import TypedArrayConstructor = type_.TypedArrayConstructor;
3737

3838
export { read, readAsync };
39-
export { Table, DataFrame, NextFunc, CountByResult };
39+
export { Table, DataFrame, NextFunc, BindFunc, CountByResult };
4040
export { Field, Schema, RecordBatch, Vector, Type };
4141

4242
export namespace util {

js/src/table.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import { isPromise, isAsyncIterable } from './util/compat';
2323
import { Vector, DictionaryVector, IntVector, StructVector } from './vector';
2424
import { ChunkedView } from './vector/chunked';
2525

26-
export type NextFunc = (idx: number, cols: RecordBatch) => void;
26+
export type NextFunc = (idx: number, batch: RecordBatch) => void;
27+
export type BindFunc = (batch: RecordBatch) => void;
2728

2829
export interface DataFrame {
2930
filter(predicate: Predicate): DataFrame;
30-
scan(next: NextFunc): void;
31+
scan(next: NextFunc, bind?: BindFunc): void;
3132
count(): number;
3233
countBy(col: (Col|string)): CountByResult;
3334
}
@@ -128,11 +129,12 @@ export class Table implements DataFrame {
128129
public filter(predicate: Predicate): DataFrame {
129130
return new FilteredDataFrame(this.batches, predicate);
130131
}
131-
public scan(next: NextFunc) {
132+
public scan(next: NextFunc, bind?: BindFunc) {
132133
const batches = this.batches, numBatches = batches.length;
133134
for (let batchIndex = -1; ++batchIndex < numBatches;) {
134135
// load batches
135136
const batch = batches[batchIndex];
137+
if (bind) { bind(batch); }
136138
// yield all indices
137139
for (let index = -1, numRows = batch.length; ++index < numRows;) {
138140
next(index, batch);
@@ -189,7 +191,7 @@ class FilteredDataFrame implements DataFrame {
189191
this.batches = batches;
190192
this.predicate = predicate;
191193
}
192-
public scan(next: NextFunc) {
194+
public scan(next: NextFunc, bind?: BindFunc) {
193195
// inlined version of this:
194196
// this.parent.scan((idx, columns) => {
195197
// if (this.predicate(idx, columns)) next(idx, columns);
@@ -199,6 +201,7 @@ class FilteredDataFrame implements DataFrame {
199201
for (let batchIndex = -1; ++batchIndex < numBatches;) {
200202
// load batches
201203
const batch = batches[batchIndex];
204+
if (bind) { bind(batch); }
202205
const predicate = this.predicate.bind(batch);
203206
// yield all indices
204207
for (let index = -1, numRows = batch.length; ++index < numRows;) {

0 commit comments

Comments
 (0)