From 52f1e0e43b8cdec6f17cf23519b244b1fde020ed Mon Sep 17 00:00:00 2001 From: Brian Hulette Date: Mon, 29 Jan 2018 12:55:51 -0500 Subject: [PATCH] <, > are not commutative, misc cleanup --- js/src/predicate.ts | 22 ++++++++++++++++++---- js/src/table.ts | 3 +++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/js/src/predicate.ts b/js/src/predicate.ts index 4a97cf00cefbc..9d55274bd880b 100644 --- a/js/src/predicate.ts +++ b/js/src/predicate.ts @@ -64,8 +64,6 @@ export class Col extends Value { this.vector = batch.getChildAt(this.colidx)!; return this.vector.get.bind(this.vector); } - - emitString() { return `cols[${this.colidx}].get(idx)`; } } export abstract class Predicate { @@ -86,7 +84,7 @@ export abstract class ComparisonPredicate extends Predicate { return this._bindLitLit(batch, this.left, this.right); } else { // right is a Col - return this._bindColLit(batch, this.right as Col, this.left); + return this._bindLitCol(batch, this.left, this.right as Col); } } else { // left is a Col if (this.right instanceof Literal) { @@ -100,6 +98,7 @@ export abstract class ComparisonPredicate extends Predicate { protected abstract _bindLitLit(batch: RecordBatch, left: Literal, right: Literal): PredicateFunc; protected abstract _bindColCol(batch: RecordBatch, left: Col, right: Col): PredicateFunc; protected abstract _bindColLit(batch: RecordBatch, col: Col, lit: Literal): PredicateFunc; + protected abstract _bindLitCol(batch: RecordBatch, lit: Literal, col: Col): PredicateFunc; } export abstract class CombinationPredicate extends Predicate { @@ -169,6 +168,11 @@ export class Equals extends ComparisonPredicate { return (idx: number, cols: RecordBatch) => col_func(idx, cols) == lit.v; } } + + protected _bindLitCol(batch: RecordBatch, lit: Literal, col: Col) { + // Equals is comutative + return this._bindColLit(batch, col, lit); + } } export class LTeq extends ComparisonPredicate { @@ -187,6 +191,11 @@ export class LTeq extends ComparisonPredicate { const col_func = col.bind(batch); return (idx: number, cols: RecordBatch) => col_func(idx, cols) <= lit.v; } + + protected _bindLitCol(batch: RecordBatch, lit: Literal, col: Col) { + const col_func = col.bind(batch); + return (idx: number, cols: RecordBatch) => lit.v <= col_func(idx, cols); + } } export class GTeq extends ComparisonPredicate { @@ -205,7 +214,12 @@ export class GTeq extends ComparisonPredicate { const col_func = col.bind(batch); return (idx: number, cols: RecordBatch) => col_func(idx, cols) >= lit.v; } + + protected _bindLitCol(batch: RecordBatch, lit: Literal, col: Col) { + const col_func = col.bind(batch); + return (idx: number, cols: RecordBatch) => lit.v >= col_func(idx, cols); + } } -export function lit(n: number): Value { return new Literal(n); } +export function lit(v: any): Value { return new Literal(v); } export function col(n: string): Col { return new Col(n); } diff --git a/js/src/table.ts b/js/src/table.ts index 82c7ef56da6e1..3e50d16e3724d 100644 --- a/js/src/table.ts +++ b/js/src/table.ts @@ -201,6 +201,9 @@ class FilteredDataFrame implements DataFrame { for (let batchIndex = -1; ++batchIndex < numBatches;) { // load batches const batch = batches[batchIndex]; + // TODO: bind batches lazily + // If predicate doesn't match anything in the batch we don't need + // to bind the callback if (bind) { bind(batch); } const predicate = this.predicate.bind(batch); // yield all indices