Skip to content

Commit 02c2c6b

Browse files
committed
feat: More scalars
1 parent 00a842a commit 02c2c6b

File tree

6 files changed

+372
-2
lines changed

6 files changed

+372
-2
lines changed

src/scalar/int16.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DataType, Int16 as ArrowInt16 } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Int16 implements Scalar<bigint> {
7+
private _valid = false;
8+
private _value: bigint = BigInt(0);
9+
10+
public constructor(v?: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType(): DataType {
16+
return new ArrowInt16();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): bigint {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (value instanceof Int16) {
34+
this._valid = value.valid;
35+
this._value = value.value;
36+
return;
37+
}
38+
39+
if (typeof value === 'bigint') {
40+
if (!this.validInt16(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Int16`);
42+
}
43+
this._value = value;
44+
this._valid = true;
45+
return;
46+
}
47+
48+
if (typeof value === 'number') {
49+
const v = BigInt(value);
50+
if (!this.validInt16(v)) {
51+
throw new TypeError(`Value '${value}' cannot be safely converted to Int16`);
52+
}
53+
this._value = v;
54+
this._valid = true;
55+
return;
56+
}
57+
58+
throw new Error(`Unable to set '${value}' as Int16`);
59+
}
60+
61+
public toString() {
62+
if (this._valid) {
63+
return String(this._value);
64+
}
65+
66+
return NULL_VALUE;
67+
}
68+
69+
validInt16(n: bigint) {
70+
return Number.isSafeInteger(n) && n >= -32_768 && n <= 32_767;
71+
}
72+
}

src/scalar/int32.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DataType, Int32 as ArrowInt32 } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Int32 implements Scalar<bigint> {
7+
private _valid = false;
8+
private _value: bigint = BigInt(0);
9+
10+
public constructor(v?: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType(): DataType {
16+
return new ArrowInt32();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): bigint {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (value instanceof Int32) {
34+
this._valid = value.valid;
35+
this._value = value.value;
36+
return;
37+
}
38+
39+
if (typeof value === 'bigint') {
40+
if (!this.validInt32(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Int32`);
42+
}
43+
this._value = value;
44+
this._valid = true;
45+
return;
46+
}
47+
48+
if (typeof value === 'number') {
49+
const v = BigInt(value);
50+
if (!this.validInt32(v)) {
51+
throw new TypeError(`Value '${value}' cannot be safely converted to Int32`);
52+
}
53+
this._value = v;
54+
this._valid = true;
55+
return;
56+
}
57+
58+
throw new Error(`Unable to set '${value}' as Int32`);
59+
}
60+
61+
public toString() {
62+
if (this._valid) {
63+
return String(this._value);
64+
}
65+
66+
return NULL_VALUE;
67+
}
68+
69+
validInt32(n: bigint) {
70+
return Number.isSafeInteger(n) && n >= -2_147_483_648 && n <= 2_147_483_647;
71+
}
72+
}

src/scalar/int64.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,20 @@ export class Int64 implements Scalar<bigint> {
3737
}
3838

3939
if (typeof value === 'bigint') {
40+
if (!this.validInt64(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Int64`);
42+
}
4043
this._value = value;
4144
this._valid = true;
4245
return;
4346
}
4447

4548
if (typeof value === 'number') {
46-
if (!Number.isSafeInteger(value)) {
49+
const v = BigInt(value);
50+
if (!this.validInt64(v)) {
4751
throw new TypeError(`Value '${value}' cannot be safely converted to Int64`);
4852
}
49-
this._value = BigInt(value);
53+
this._value = v;
5054
this._valid = true;
5155
return;
5256
}
@@ -61,4 +65,10 @@ export class Int64 implements Scalar<bigint> {
6165

6266
return NULL_VALUE;
6367
}
68+
69+
validInt64(n: bigint) {
70+
const MIN_INT64 = BigInt('-9223372036854775808'); // -2^63
71+
const MAX_INT64 = BigInt('9223372036854775807'); // 2^63 - 1
72+
return Number.isSafeInteger(n) && n >= MIN_INT64 && n <= MAX_INT64;
73+
}
6474
}

src/scalar/uint16.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DataType, Uint16 as ArrowUint16 } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Uint16 implements Scalar<bigint> {
7+
private _valid = false;
8+
private _value: bigint = BigInt(0);
9+
10+
public constructor(v?: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType(): DataType {
16+
return new ArrowUint16();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): bigint {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (value instanceof Uint16) {
34+
this._valid = value.valid;
35+
this._value = value.value;
36+
return;
37+
}
38+
39+
if (typeof value === 'bigint') {
40+
if (!this.validUint16(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint16`);
42+
}
43+
this._value = value;
44+
this._valid = true;
45+
return;
46+
}
47+
48+
if (typeof value === 'number') {
49+
const v = BigInt(value);
50+
if (!this.validUint16(v)) {
51+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint16`);
52+
}
53+
this._value = v;
54+
this._valid = true;
55+
return;
56+
}
57+
58+
throw new Error(`Unable to set '${value}' as Uint16`);
59+
}
60+
61+
public toString() {
62+
if (this._valid) {
63+
return String(this._value);
64+
}
65+
66+
return NULL_VALUE;
67+
}
68+
69+
validUint16(n: bigint) {
70+
return Number.isSafeInteger(n) && n >= 0 && n <= 65535;
71+
}
72+
}

src/scalar/uint32.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DataType, Uint32 as ArrowUint32 } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Uint32 implements Scalar<bigint> {
7+
private _valid = false;
8+
private _value: bigint = BigInt(0);
9+
10+
public constructor(v?: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType(): DataType {
16+
return new ArrowUint32();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): bigint {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (value instanceof Uint32) {
34+
this._valid = value.valid;
35+
this._value = value.value;
36+
return;
37+
}
38+
39+
if (typeof value === 'bigint') {
40+
if (!this.validUint32(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint32`);
42+
}
43+
this._value = value;
44+
this._valid = true;
45+
return;
46+
}
47+
48+
if (typeof value === 'number') {
49+
const v = BigInt(value);
50+
if (!this.validUint32(v)) {
51+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint32`);
52+
}
53+
this._value = v;
54+
this._valid = true;
55+
return;
56+
}
57+
58+
throw new Error(`Unable to set '${value}' as Uint32`);
59+
}
60+
61+
public toString() {
62+
if (this._valid) {
63+
return String(this._value);
64+
}
65+
66+
return NULL_VALUE;
67+
}
68+
69+
validUint32(n: bigint) {
70+
return Number.isSafeInteger(n) && n >= 0 && n <= 4294967295;
71+
}
72+
}

src/scalar/uint64.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { DataType, Uint64 as ArrowUint64 } from '@apache-arrow/esnext-esm';
2+
3+
import { Scalar } from './scalar.js';
4+
import { isInvalid, NULL_VALUE } from './util.js';
5+
6+
export class Uint64 implements Scalar<bigint> {
7+
private _valid = false;
8+
private _value: bigint = BigInt(0);
9+
10+
public constructor(v?: unknown) {
11+
this.value = v;
12+
return this;
13+
}
14+
15+
public get dataType(): DataType {
16+
return new ArrowUint64();
17+
}
18+
19+
public get valid(): boolean {
20+
return this._valid;
21+
}
22+
23+
public get value(): bigint {
24+
return this._value;
25+
}
26+
27+
public set value(value: unknown) {
28+
if (isInvalid(value)) {
29+
this._valid = false;
30+
return;
31+
}
32+
33+
if (value instanceof Uint64) {
34+
this._valid = value.valid;
35+
this._value = value.value;
36+
return;
37+
}
38+
39+
if (typeof value === 'bigint') {
40+
if (!this.validUint64(value)) {
41+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint64`);
42+
}
43+
this._value = value;
44+
this._valid = true;
45+
return;
46+
}
47+
48+
if (typeof value === 'number') {
49+
const v = BigInt(value);
50+
if (!this.validUint64(v)) {
51+
throw new TypeError(`Value '${value}' cannot be safely converted to Uint64`);
52+
}
53+
this._value = v;
54+
this._valid = true;
55+
return;
56+
}
57+
58+
throw new Error(`Unable to set '${value}' as Uint64`);
59+
}
60+
61+
public toString() {
62+
if (this._valid) {
63+
return String(this._value);
64+
}
65+
66+
return NULL_VALUE;
67+
}
68+
69+
validUint64(n: bigint) {
70+
return Number.isSafeInteger(n) && n >= 0 && n <= 18446744073709551615n;
71+
}
72+
}

0 commit comments

Comments
 (0)