diff --git a/js/src/vector.ts b/js/src/vector.ts index 84bdf7b8b63fb..17f70717f39da 100644 --- a/js/src/vector.ts +++ b/js/src/vector.ts @@ -80,10 +80,10 @@ export abstract class ListVectorBase extends Vector { public get values() { return this._data.values; } public get valueOffsets() { return this._data.valueOffsets; } public getValueOffset(index: number) { - return this.data.valueOffsets[index]; + return this.valueOffsets[index]; } public getValueLength(index: number) { - return this.data.valueOffsets[index + 1] - this.data.valueOffsets[index]; + return this.valueOffsets[index + 1] - this.valueOffsets[index]; } } diff --git a/js/src/vector/nested.ts b/js/src/vector/nested.ts index 49452f21b874d..3c92568e2ec72 100644 --- a/js/src/vector/nested.ts +++ b/js/src/vector/nested.ts @@ -21,10 +21,6 @@ import { View, Vector, createVector } from '../vector'; import { DataType, NestedType, DenseUnion, SparseUnion, Struct, Map_ } from '../type'; export abstract class NestedView implements View { - // @ts-ignore - public typeIds: Int8Array; - // @ts-ignore - public valueOffsets: any; public readonly numChildren: number; public readonly childData: Data[]; protected children: { [k: number]: Vector } = Object.create(null); @@ -56,9 +52,15 @@ export abstract class NestedView implements View { } export class UnionView extends NestedView { + public length: number; + // @ts-ignore + public typeIds: Int8Array; + // @ts-ignore + public valueOffsets?: Int32Array; constructor(data: Data) { super(data); - this.typeIds = this.typeIds; + this.length = data.length; + this.typeIds = data.typeIds; } public getNested(view: NestedView, index: number): T['TValue'] { return this.getUnionValue(view, index, this.typeIds, this.valueOffsets); @@ -68,8 +70,8 @@ export class UnionView exten return child ? child.get(index) : null; } public *[Symbol.iterator](): IterableIterator { + const length = this.length; const get = this.getUnionValue; - const length = this.data.length; const { typeIds, valueOffsets } = this; for (let index = -1; ++index < length;) { yield get(this, index, typeIds, valueOffsets); @@ -78,6 +80,8 @@ export class UnionView exten } export class DenseUnionView extends UnionView { + // @ts-ignore + public valueOffsets: Int32Array; constructor(data: Data) { super(data); this.valueOffsets = data.valueOffsets; @@ -91,41 +95,38 @@ export class DenseUnionView extends UnionView { } } -export abstract class TabularView extends NestedView { - constructor(data: Data) { - super(data); - this.typeIds = new Int8Array(data.childData.map((x) => x.typeId)); - this.valueOffsets = data.type.children.reduce((xs, x) => - (xs[x.name] = x.typeId) && xs || xs, Object.create(null)); +export class StructView extends NestedView { + public getNested(view: StructView, index: number) { + return new RowView(view as any, index); } } -export class MapView extends TabularView { +export class MapView extends NestedView { + // @ts-ignore + public typeIds: { [k: string]: number }; + constructor(data: Data) { + super(data); + this.typeIds = data.type.children.reduce((xs, x, i) => + (xs[x.name] = i) && xs || xs, Object.create(null)); + } public getNested(view: MapView, index: number) { - return new MapRow(view as any, index); + return new MapRowView(view as any, index); } } -export class MapRow extends DenseUnionView { - constructor(data: Data, protected rowIndex: number) { +export class RowView extends UnionView { + constructor(data: Data & NestedView, protected rowIndex: number) { super(data); + this.length = data.numChildren; } - protected getUnionValue(view: NestedView, index: number, typeIds: Int8Array, valueOffsets: any): any | null { - const child = view.getChildAt(typeIds[valueOffsets[index]]); + protected getUnionValue(view: NestedView, index: number, _typeIds: any, _valueOffsets?: any): any | null { + const child = view.getChildAt(index); return child ? child.get(this.rowIndex) : null; } } -export class StructView extends TabularView { - public getNested(view: StructView, index: number) { - return new StructRow(view as any, index); - } -} -export class StructRow extends UnionView { - constructor(data: Data, protected rowIndex: number) { - super(data); - } - protected getUnionValue(view: NestedView, index: number, typeIds: Int8Array, _valueOffsets?: any): any | null { +export class MapRowView extends RowView { + protected getUnionValue(view: NestedView, index: number, typeIds: any, _valueOffsets: any): any | null { const child = view.getChildAt(typeIds[index]); return child ? child.get(this.rowIndex) : null; }