Skip to content

feat: More scalars #50

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 11 commits into from
Aug 11, 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
68 changes: 68 additions & 0 deletions src/scalar/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { DataType, Date_ as ArrowDate, DateUnit } from '@apache-arrow/esnext-esm';
import { DateTime } from 'luxon';

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

export class Date implements Scalar<DateTime> {
private _valid = false;
private _value: DateTime = DateTime.fromMillis(0);
private _unit: DateUnit;

public constructor(unit: DateUnit, v?: unknown) {
this._unit = unit;
this.value = v;
return this;
}

public get dataType(): DataType {
return new ArrowDate(this._unit);
}

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

public get value(): DateTime {
return this._value;
}

public set value(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}

if (value instanceof Date && value.dataType === this.dataType) {
this._valid = value.valid;
this._value = value.value;
return;
}

let dateValue: DateTime | null = null;

if (typeof value === 'string') {
dateValue = DateTime.fromISO(value, { setZone: true });

if (!dateValue.isValid) {
dateValue = DateTime.fromFormat(value, 'yyyy-MM-dd', { zone: 'utc' });
}
}

if (dateValue && dateValue.isValid) {
this._value = dateValue;
this._valid = true;
return;
}

throw new Error(`Unable to set '${value}' as Date`);
}

public toString(): string {
if (this._valid) {
return this._value.toISO()!;
}

return NULL_VALUE;
}
}
76 changes: 76 additions & 0 deletions src/scalar/float32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { DataType, Float32 as ArrowFloat32 } from '@apache-arrow/esnext-esm';

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

export class Float32 implements Scalar<number> {
private _valid = false;
private _value: number = 0;

public constructor(v?: unknown) {
this.value = v;
return this;
}

public get dataType(): DataType {
return new ArrowFloat32();
}

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

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

public set value(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}

if (value instanceof Float32) {
this._valid = value.valid;
this._value = value.value;
return;
}

if (typeof value === 'number') {
if (!this.validFloat32(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Float32`);
}

this._value = value;
this._valid = true;
return;
}

const floatValue = Number.parseFloat(String(value));
if (!Number.isNaN(floatValue)) {
if (!this.validFloat32(floatValue)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Float32`);
}

this._value = floatValue;
this._valid = true;
return;
}

throw new Error(`Unable to set '${value}' as Float32`);
}

public toString() {
if (this._valid) {
return String(this._value);
}

return NULL_VALUE;
}

validFloat32(n: number) {
const float32 = new Float32Array(1);
float32[0] = n;
return float32[0] === n;
}
}
74 changes: 74 additions & 0 deletions src/scalar/int16.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { DataType, Int16 as ArrowInt16 } from '@apache-arrow/esnext-esm';
import { bigIntToNumber } from '@apache-arrow/esnext-esm/util/bigint.js';

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

export class Int16 implements Scalar<bigint> {
private _valid = false;
private _value: bigint = BigInt(0);

public constructor(v?: unknown) {
this.value = v;
return this;
}

public get dataType(): DataType {
return new ArrowInt16();
}

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

public get value(): bigint {
return this._value;
}

public set value(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}

if (value instanceof Int16) {
this._valid = value.valid;
this._value = value.value;
return;
}

if (typeof value === 'bigint') {
if (!this.validInt16(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int16`);
}
this._value = value;
this._valid = true;
return;
}

if (typeof value === 'number') {
const v = BigInt(value);
if (!this.validInt16(v)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int16`);
}
this._value = v;
this._valid = true;
return;
}

throw new Error(`Unable to set '${value}' as Int16`);
}

public toString() {
if (this._valid) {
return String(this._value);
}

return NULL_VALUE;
}

validInt16(n: bigint) {
const number_ = bigIntToNumber(n);
return Number.isSafeInteger(number_) && number_ >= -32_768 && number_ <= 32_767;
}
}
74 changes: 74 additions & 0 deletions src/scalar/int32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { DataType, Int32 as ArrowInt32 } from '@apache-arrow/esnext-esm';
import { bigIntToNumber } from '@apache-arrow/esnext-esm/util/bigint.js';

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

export class Int32 implements Scalar<bigint> {
private _valid = false;
private _value: bigint = BigInt(0);

public constructor(v?: unknown) {
this.value = v;
return this;
}

public get dataType(): DataType {
return new ArrowInt32();
}

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

public get value(): bigint {
return this._value;
}

public set value(value: unknown) {
if (isInvalid(value)) {
this._valid = false;
return;
}

if (value instanceof Int32) {
this._valid = value.valid;
this._value = value.value;
return;
}

if (typeof value === 'bigint') {
if (!this.validInt32(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int32`);
}
this._value = value;
this._valid = true;
return;
}

if (typeof value === 'number') {
const v = BigInt(value);
if (!this.validInt32(v)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int32`);
}
this._value = v;
this._valid = true;
return;
}

throw new Error(`Unable to set '${value}' as Int32`);
}

public toString() {
if (this._valid) {
return String(this._value);
}

return NULL_VALUE;
}

validInt32(n: bigint) {
const number_ = bigIntToNumber(n);
return Number.isSafeInteger(number_) && number_ >= -2_147_483_648 && number_ <= 2_147_483_647;
}
}
15 changes: 13 additions & 2 deletions src/scalar/int64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataType, Int64 as ArrowInt64 } from '@apache-arrow/esnext-esm';
import { bigIntToNumber } from '@apache-arrow/esnext-esm/util/bigint.js';

import { Scalar } from './scalar.js';
import { isInvalid, NULL_VALUE } from './util.js';
Expand Down Expand Up @@ -37,16 +38,20 @@ export class Int64 implements Scalar<bigint> {
}

if (typeof value === 'bigint') {
if (!this.validInt64(value)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int64`);
}
this._value = value;
this._valid = true;
return;
}

if (typeof value === 'number') {
if (!Number.isSafeInteger(value)) {
const v = BigInt(value);
if (!this.validInt64(v)) {
throw new TypeError(`Value '${value}' cannot be safely converted to Int64`);
}
this._value = BigInt(value);
this._value = v;
this._valid = true;
return;
}
Expand All @@ -61,4 +66,10 @@ export class Int64 implements Scalar<bigint> {

return NULL_VALUE;
}

validInt64(n: bigint) {
const MIN_INT64 = BigInt('-9223372036854775808'); // -2^63
const MAX_INT64 = BigInt('9223372036854775807'); // 2^63 - 1
return Number.isSafeInteger(bigIntToNumber(n)) && n >= MIN_INT64 && n <= MAX_INT64;
}
}
20 changes: 10 additions & 10 deletions src/scalar/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ import { Int64 } from './int64.js';
import { List } from './list.js';

test('list', (t) => {
const l = new List(Int64);
t.deepEqual(l, new List(Int64));
const l = new List(new Int64());
t.deepEqual(l, new List(new 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);
const l1 = new List(new Int64());
l1.value = [new Int64(17), new Int64(19), new Int64(null)];

const l2 = new List(Int64);
const l2 = new List(new 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);
const l1 = new List(new Int64());
l1.value = new Int64(4);

const l2 = new List(Int64);
const l2 = new List(new 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 l1 = new List(new Int64());
l1.value = new Int64();

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

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