Skip to content

feat: Add UUID scalar #54

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 1 commit 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
7 changes: 7 additions & 0 deletions src/scalar/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DataType, Precision } from '@apache-arrow/esnext-esm';

import { UUIDType } from '../types/uuid.js';

import { Bool } from './bool.js';
import { Date } from './date.js';
import { Float32 } from './float32.js';
Expand All @@ -13,6 +15,7 @@ import { Timestamp } from './timestamp.js';
import { Uint16 } from './uint16.js';
import { Uint32 } from './uint32.js';
import { Uint64 } from './uint64.js';
import { UUID } from './uuid.js';

export type Stringable = { toString: () => string };

Expand Down Expand Up @@ -86,5 +89,9 @@ export const newScalar = (dataType: DataType): Scalar<Stringable> => {
return new Date(dataType.unit);
}

if (dataType instanceof UUIDType) {
return new UUID();
}

return new Text();
};
2 changes: 1 addition & 1 deletion src/scalar/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class Text implements Scalar<string> {

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

return NULL_VALUE;
Expand Down
68 changes: 68 additions & 0 deletions src/scalar/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Utf8 as ArrowString } from '@apache-arrow/esnext-esm';
import { validate } from 'uuid';

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

export class UUID implements Scalar<string> {
private _valid = false;
private _value = '';

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

public get dataType() {
return new ArrowString();
}

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

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

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

if (typeof value === 'string') {
this._value = value;
this._valid = validate(value);
return;
}

if (value instanceof Uint8Array) {
this._value = new TextDecoder().decode(value);
this._valid = validate(this._value);
return;
}

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

if (typeof value!.toString === 'function' && value!.toString !== Object.prototype.toString) {
this._value = value!.toString();
this._valid = validate(this._value);
return;
}

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

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

return NULL_VALUE;
}
}
53 changes: 36 additions & 17 deletions src/scheduler/cqid.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createHash } from 'node:crypto';

import test from 'ava';
import { NIL as NIL_UUID } from 'uuid';

import { createColumn } from '../schema/column.js';
import { addCQIDsColumns, cqIDColumn } from '../schema/meta.js';
Expand All @@ -10,21 +9,49 @@ import { createTable } from '../schema/table.js';
import { setCQId } from './cqid.js';

test('setCQId - should set to random value if deterministicCQId is false', (t): void => {
const resource = new Resource(addCQIDsColumns(createTable({ name: 'table1' })), null, null);
const resource = new Resource(
addCQIDsColumns(
createTable({
name: 'table1',
columns: [
createColumn({ name: 'pk1', primaryKey: true, unique: true, notNull: true }),
createColumn({ name: 'pk2', primaryKey: true, unique: true, notNull: true }),
createColumn({ name: 'pk3', primaryKey: true, unique: true, notNull: true }),
createColumn({ name: 'non_pk' }),
],
}),
),
null,
null,
);

setCQId(resource, false, () => 'random');
setCQId(resource, false, () => NIL_UUID);

t.is(resource.getColumnData(cqIDColumn.name).valid, true);
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), 'random');
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), NIL_UUID);
});

test('setCQId - should set to random value if deterministicCQId is true and table does not have non _cq_id PKs', (t): void => {
const resource = new Resource(addCQIDsColumns(createTable({ name: 'table1' })), null, null);
const resource = new Resource(
addCQIDsColumns(
createTable({
name: 'table1',
columns: [
createColumn({ name: 'pk1', primaryKey: false, unique: true, notNull: true }),
createColumn({ name: 'pk2', primaryKey: false, unique: true, notNull: true }),
createColumn({ name: 'pk3', primaryKey: false, unique: true, notNull: true }),
createColumn({ name: 'non_pk' }),
],
}),
),
null,
null,
);

setCQId(resource, true, () => 'random');
setCQId(resource, true, () => NIL_UUID);

t.is(resource.getColumnData(cqIDColumn.name).valid, true);
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), 'random');
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), NIL_UUID);
});

test('setCQId - should set to fixed value if deterministicCQId is true and table has non _cq_id PKs', (t): void => {
Expand All @@ -49,16 +76,8 @@ test('setCQId - should set to fixed value if deterministicCQId is true and table
resource.setColumData('pk3', 'pk3-value');
resource.setColumData('non_pk', 'non-pk-value');

const expectedSha256 = createHash('sha256');
expectedSha256.update('pk1');
expectedSha256.update('pk1-value');
expectedSha256.update('pk2');
expectedSha256.update('pk2-value');
expectedSha256.update('pk3');
expectedSha256.update('pk3-value');

setCQId(resource, true);

t.is(resource.getColumnData(cqIDColumn.name).valid, true);
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), expectedSha256.digest('hex'));
t.is(resource.getColumnData(cqIDColumn.name).value.toString(), '415bd5dd-9bac-5806-b9d1-c53f17d37455');
});
24 changes: 12 additions & 12 deletions src/scheduler/cqid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash } from 'node:crypto';

import { v4 as uuidv4 } from 'uuid';
import { v4 as uuidv4, v5 as uuidv5, NIL as NIL_UUID } from 'uuid';

import { cqIDColumn } from '../schema/meta.js';
import { Resource } from '../schema/resource.js';
Expand All @@ -9,20 +9,20 @@ import { getPrimaryKeys } from '../schema/table.js';
export const setCQId = (resource: Resource, deterministicCQId: boolean, generator: () => string = uuidv4) => {
const randomCQId = generator();
if (!deterministicCQId) {
resource.setCqId(randomCQId);
return resource.setCqId(randomCQId);
}

const primaryKeys = getPrimaryKeys(resource.table);
const hasNonCqPKs = primaryKeys.some((pk) => pk !== cqIDColumn.name);
if (hasNonCqPKs) {
const sha256 = createHash('sha256');
primaryKeys.sort();
for (const pk of primaryKeys) {
sha256.update(pk);
sha256.update(resource.getColumnData(pk).toString());
}
return resource.setCqId(sha256.digest('hex'));
const cqOnlyPK = primaryKeys.every((pk) => pk === cqIDColumn.name);
if (cqOnlyPK) {
return resource.setCqId(randomCQId);
}

return resource.setCqId(randomCQId);
const sha256 = createHash('sha256');
primaryKeys.sort();
for (const pk of primaryKeys) {
sha256.update(pk);
sha256.update(resource.getColumnData(pk).toString());
}
return resource.setCqId(uuidv5(sha256.digest('hex'), NIL_UUID));
};
4 changes: 2 additions & 2 deletions src/schema/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ test('parentCqUUIDResolver - should set to _cq_id column value when parent has i
const table = addCQIDsColumns(createTable({ name: 'table1', relations: [createTable({ name: 'table1-child1' })] }));

const parentResource = new Resource(table, null, null);
parentResource.setColumData(cqIDColumn.name, 'parent-cq-id');
parentResource.setColumData(cqIDColumn.name, '9241a9cb-f580-420f-8fd7-46d2c4f55ccb');
const childResource = new Resource(table.relations[0], parentResource, null);

parentCqUUIDResolver()({ id: () => '' }, childResource, cqParentIDColumn);

t.is(childResource.getColumnData(cqParentIDColumn.name).value, 'parent-cq-id');
t.is(childResource.getColumnData(cqParentIDColumn.name).value, '9241a9cb-f580-420f-8fd7-46d2c4f55ccb');
t.is(childResource.getColumnData(cqParentIDColumn.name).valid, true);
});
10 changes: 10 additions & 0 deletions src/types/uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ export class UUIDType extends DataType<Type.Utf8> {
get typeId(): Type.Utf8 {
return Type.Utf8;
}

public toString() {
return `uuid`;
}

protected static [Symbol.toStringTag] = ((proto: UUIDType) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(<any>proto).ArrayType = Uint8Array;
return (proto[Symbol.toStringTag] = 'uuid');
})(UUIDType.prototype);
}