Skip to content

feat: List scalars #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/scalar/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import test from 'ava';

import { Int64 } from './int64.js';
import { List } from './list.js';

test('list', (t) => {
const l = new List(Int64);
t.deepEqual(l, new List(Int64));

l.value = [new Int64(17), new Int64(19), new Int64(null)];
t.is(l.length, 3);
});

test('list equality', (t) => {
const l1 = new List(Int64);
l1.value = [new Int64(17), new Int64(19), new Int64(null)];

const l2 = new List(Int64);
l2.value = [new Int64(17), new Int64(19), new Int64(null)];

t.deepEqual(l1, l2);
});

test('list inequality', (t) => {
const l1 = new List(Int64);
l1.value = new Int64(4);

const l2 = new List(Int64);
l2.value = new Int64(7);

t.notDeepEqual(l1, l2);
});

test('list equality when invalid', (t) => {
const l1 = new List(Int64);
l1.value = new Int64(null);

const l2 = new List(Int64);
l2.value = new Int64(null);

t.deepEqual(l1, l2);
});
86 changes: 86 additions & 0 deletions src/scalar/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { DataType, List as ArrowList } from '@apache-arrow/esnext-esm';

import { Scalar } from './scalar.js';
import { isInvalid, NULL_VALUE } from './util.js';

type TVector<T extends Scalar<unknown>> = T[];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TVector because Vector is:

export type Vector = Scalar<unknown>[];

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation: Difference between TVector and Vector types

TVector

type TVector<T extends Scalar<unknown>> = T[];
  • TVector is a generic type.
  • It creates an array type, but the type of items in this array is based on whatever type you provide when using TVector.
    • For example:
      • If you use TVector<Int64>, then it's equivalent to Int64[].
      • If you use TVector<Bool>, then it's equivalent to Bool[].
  • This flexibility allows you to specify the item type of the array whenever you utilize TVector.

Vector

export type Vector = Scalar<unknown>[];
  • Vector is not a generic.
  • It explicitly states that Vector is an array of Scalar<unknown>.
  • This means there's no flexibility when using Vector; it's always an array of Scalar<unknown>.

Summary

  • TVector provides flexibility on the array item's type, letting you specify it when using the type.
  • Vector is fixed to always be an array of Scalar<unknown>.


export class List<T extends Scalar<unknown>> implements Scalar<TVector<T>> {
private _type: new (value?: unknown) => T;
private _valid = false;
private _value: TVector<T> = [];

constructor(scalarType: new (value?: unknown) => T, initialValue?: unknown) {
this._type = scalarType;
if (!isInvalid(initialValue)) this.value = initialValue;
}

get dataType(): DataType {
return new ArrowList(this._type.prototype.dataType);
}

set value(inputValue: unknown) {
if (isInvalid(inputValue)) {
this._valid = false;
this._value = [];
return;
}

const inputArray = Array.isArray(inputValue) ? inputValue : [inputValue];
const temporaryVector: TVector<T> = [];

if (inputArray.length > 0) {
const firstItemScalar = new this._type();
firstItemScalar.value = inputArray[0];
const firstItemType = Object.getPrototypeOf(firstItemScalar).constructor;

for (const item of inputArray) {
const scalar = new this._type();
scalar.value = item;

if (Object.getPrototypeOf(scalar).constructor !== firstItemType) {
throw new Error(
`Type mismatch: All items should be of the same type as the first item. Expected type ${
firstItemType.name
}, but got ${Object.getPrototypeOf(scalar).constructor.name}`,
);
}

temporaryVector.push(scalar);
}

this._value = temporaryVector;
this._valid = true; // List becomes valid if we successfully process values
return;
}

this._valid = true; // An empty list is valid
}

get valid(): boolean {
return this._valid;
}

get value(): TVector<T> {
return this._value;
}

toString(): string {
if (!this._valid) {
return NULL_VALUE;
}
return `[${this._value.map((v) => v.toString()).join(', ')}]`;
}

get length(): number {
return this._value.length;
}

// If you need an equality method, you can add an equals method similar to the Python __eq__
equals(other: List<T>): boolean {
if (!other) return false;
if (this.constructor !== other.constructor) return false;
if (this._valid !== other.valid) return false;
return JSON.stringify(this._value) === JSON.stringify(other.value); // crude equality check for objects, might need refinement.
}
}