Skip to content

change nested value structure; add toString to all values #40

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
Nov 1, 2024
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
26 changes: 26 additions & 0 deletions api/src/DuckDBType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,89 @@ export class DuckDBTinyIntType extends BaseDuckDBType<DuckDBTypeId.TINYINT> {
super(DuckDBTypeId.TINYINT);
}
public static readonly instance = new DuckDBTinyIntType();
public static readonly Max = 2 ** 7 - 1;
public static readonly Min = -(2 ** 7);
}

export class DuckDBSmallIntType extends BaseDuckDBType<DuckDBTypeId.SMALLINT> {
private constructor() {
super(DuckDBTypeId.SMALLINT);
}
public static readonly instance = new DuckDBSmallIntType();
public static readonly Max = 2 ** 15 - 1;
public static readonly Min = -(2 ** 15);
}

export class DuckDBIntegerType extends BaseDuckDBType<DuckDBTypeId.INTEGER> {
private constructor() {
super(DuckDBTypeId.INTEGER);
}
public static readonly instance = new DuckDBIntegerType();
public static readonly Max = 2 ** 31 - 1;
public static readonly Min = -(2 ** 31);
}

export class DuckDBBigIntType extends BaseDuckDBType<DuckDBTypeId.BIGINT> {
private constructor() {
super(DuckDBTypeId.BIGINT);
}
public static readonly instance = new DuckDBBigIntType();
public static readonly Max = 2n ** 63n - 1n;
public static readonly Min = -(2n ** 63n);
}

export class DuckDBUTinyIntType extends BaseDuckDBType<DuckDBTypeId.UTINYINT> {
private constructor() {
super(DuckDBTypeId.UTINYINT);
}
public static readonly instance = new DuckDBUTinyIntType();
public static readonly Max = 2 ** 8 - 1;
public static readonly Min = 0;
}

export class DuckDBUSmallIntType extends BaseDuckDBType<DuckDBTypeId.USMALLINT> {
private constructor() {
super(DuckDBTypeId.USMALLINT);
}
public static readonly instance = new DuckDBUSmallIntType();
public static readonly Max = 2 ** 16 - 1;
public static readonly Min = 0;
}

export class DuckDBUIntegerType extends BaseDuckDBType<DuckDBTypeId.UINTEGER> {
private constructor() {
super(DuckDBTypeId.UINTEGER);
}
public static readonly instance = new DuckDBUIntegerType();
public static readonly Max = 2 ** 32 - 1;
public static readonly Min = 0;
}

export class DuckDBUBigIntType extends BaseDuckDBType<DuckDBTypeId.UBIGINT> {
private constructor() {
super(DuckDBTypeId.UBIGINT);
}
public static readonly instance = new DuckDBUBigIntType();
public static readonly Max = 2n ** 64n - 1n;
public static readonly Min = 0n;
}

export class DuckDBFloatType extends BaseDuckDBType<DuckDBTypeId.FLOAT> {
private constructor() {
super(DuckDBTypeId.FLOAT);
}
public static readonly instance = new DuckDBFloatType();
public static readonly Min = Math.fround(-3.4028235e+38);
public static readonly Max = Math.fround( 3.4028235e+38);
}

export class DuckDBDoubleType extends BaseDuckDBType<DuckDBTypeId.DOUBLE> {
private constructor() {
super(DuckDBTypeId.DOUBLE);
}
public static readonly instance = new DuckDBDoubleType();
public static readonly Min = -Number.MAX_VALUE;
public static readonly Max = Number.MAX_VALUE;
}

export class DuckDBTimestampType extends BaseDuckDBType<DuckDBTypeId.TIMESTAMP> {
Expand Down Expand Up @@ -124,13 +144,17 @@ export class DuckDBHugeIntType extends BaseDuckDBType<DuckDBTypeId.HUGEINT> {
super(DuckDBTypeId.HUGEINT);
}
public static readonly instance = new DuckDBHugeIntType();
public static readonly Max = 2n ** 127n - 1n;
public static readonly Min = -(2n ** 127n);
}

export class DuckDBUHugeIntType extends BaseDuckDBType<DuckDBTypeId.UHUGEINT> {
private constructor() {
super(DuckDBTypeId.UHUGEINT);
}
public static readonly instance = new DuckDBUHugeIntType();
public static readonly Max = 2n ** 128n - 1n;
public static readonly Min = 0n;
}

export class DuckDBVarCharType extends BaseDuckDBType<DuckDBTypeId.VARCHAR> {
Expand Down Expand Up @@ -314,6 +338,8 @@ export class DuckDBVarIntType extends BaseDuckDBType<DuckDBTypeId.VARINT> {
super(DuckDBTypeId.VARINT);
}
public static readonly instance = new DuckDBVarIntType();
public static readonly Max: bigint = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n;
public static readonly Min: bigint = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n;
}

export class DuckDBSQLNullType extends BaseDuckDBType<DuckDBTypeId.SQLNULL> {
Expand Down
53 changes: 30 additions & 23 deletions api/src/DuckDBVector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,22 @@ const getBoolean = makeGetBoolean();

function getDecimal2(dataView: DataView, offset: number, type: DuckDBDecimalType): DuckDBDecimalValue<number> {
const scaledValue = getInt16(dataView, offset);
return new DuckDBDecimalValue(type, scaledValue);
return new DuckDBDecimalValue(type.width, type.scale, scaledValue);
}

function getDecimal4(dataView: DataView, offset: number, type: DuckDBDecimalType): DuckDBDecimalValue<number> {
const scaledValue = getInt32(dataView, offset);
return new DuckDBDecimalValue(type, scaledValue);
return new DuckDBDecimalValue(type.width, type.scale, scaledValue);
}

function getDecimal8(dataView: DataView, offset: number, type: DuckDBDecimalType): DuckDBDecimalValue<bigint> {
const scaledValue = getInt64(dataView, offset);
return new DuckDBDecimalValue(type, scaledValue);
return new DuckDBDecimalValue(type.width, type.scale, scaledValue);
}

function getDecimal16(dataView: DataView, offset: number, type: DuckDBDecimalType): DuckDBDecimalValue<bigint> {
const scaledValue = getInt128(dataView, offset);
return new DuckDBDecimalValue(type, scaledValue);
return new DuckDBDecimalValue(type.width, type.scale, scaledValue);
}

function vectorData(vector: duckdb.Vector, byteCount: number): Uint8Array {
Expand Down Expand Up @@ -391,6 +391,13 @@ export abstract class DuckDBVector<TValue extends DuckDBValue = DuckDBValue> {
public abstract get itemCount(): number;
public abstract getItem(itemIndex: number): TValue | null;
public abstract slice(offset: number, length: number): DuckDBVector<TValue>;
public toArray(): (TValue | null)[] {
const items: (TValue | null)[] = [];
for (let i = 0; i < this.itemCount; i++) {
items.push(this.getItem(i));
}
return items;
}
}

export class DuckDBBooleanVector extends DuckDBVector<boolean> {
Expand Down Expand Up @@ -1302,17 +1309,17 @@ export class DuckDBEnum4Vector extends DuckDBVector<string> {
}
}

export class DuckDBListVector<TValue extends DuckDBValue = DuckDBValue> extends DuckDBVector<DuckDBListValue> {
export class DuckDBListVector extends DuckDBVector<DuckDBListValue> {
private readonly listType: DuckDBListType;
private readonly entryData: BigUint64Array;
private readonly validity: DuckDBValidity;
private readonly childData: DuckDBVector<TValue>;
private readonly childData: DuckDBVector;
private readonly _itemCount: number;
constructor(
listType: DuckDBListType,
entryData: BigUint64Array,
validity: DuckDBValidity,
childData: DuckDBVector<TValue>,
childData: DuckDBVector,
itemCount: number,
) {
super();
Expand Down Expand Up @@ -1340,7 +1347,7 @@ export class DuckDBListVector<TValue extends DuckDBValue = DuckDBValue> extends
public override get itemCount(): number {
return this._itemCount;
}
public getItemVector(itemIndex: number): DuckDBVector<TValue> | null {
public getItemVector(itemIndex: number): DuckDBVector | null {
if (!this.validity.itemValid(itemIndex)) {
return null;
}
Expand All @@ -1349,16 +1356,16 @@ export class DuckDBListVector<TValue extends DuckDBValue = DuckDBValue> extends
const length = Number(this.entryData[entryDataStartIndex + 1]);
return this.childData.slice(offset, length);
}
public override getItem(itemIndex: number): DuckDBListValue<TValue> | null {
public override getItem(itemIndex: number): DuckDBListValue | null {
const vector = this.getItemVector(itemIndex);
if (!vector) {
return null;
}
return new DuckDBListValue(this.listType, vector);
return new DuckDBListValue(vector.toArray());
}
public override slice(offset: number, length: number): DuckDBListVector<TValue> {
public override slice(offset: number, length: number): DuckDBListVector {
const entryDataStartIndex = offset * 2;
return new DuckDBListVector<TValue>(
return new DuckDBListVector(
this.listType,
this.entryData.slice(entryDataStartIndex, entryDataStartIndex + length * 2),
this.validity.slice(offset),
Expand Down Expand Up @@ -1401,12 +1408,12 @@ export class DuckDBStructVector extends DuckDBVector<DuckDBStructValue> {
if (!this.validity.itemValid(itemIndex)) {
return null;
}
const values: DuckDBValue[] = [];
const entries: { [name: string]: DuckDBValue } = {};
const entryCount = this.structType.entries.length;
for (let i = 0; i < entryCount; i++) {
values.push(this.entryVectors[i].getItem(itemIndex));
entries[this.structType.entries[i].name] = this.entryVectors[i].getItem(itemIndex);
}
return new DuckDBStructValue(this.structType, values);
return new DuckDBStructValue(entries);
}
public getItemValue(itemIndex: number, entryIndex: number): DuckDBValue | null {
if (!this.validity.itemValid(itemIndex)) {
Expand Down Expand Up @@ -1461,7 +1468,7 @@ export class DuckDBMapVector extends DuckDBVector<DuckDBMapValue> {
const value = itemVector.getItemValue(i, 1);
entries.push({ key, value });
}
return new DuckDBMapValue(this.mapType, entries);
return new DuckDBMapValue(entries);
}
public override slice(offset: number, length: number): DuckDBMapVector {
return new DuckDBMapVector(
Expand All @@ -1471,15 +1478,15 @@ export class DuckDBMapVector extends DuckDBVector<DuckDBMapValue> {
}
}

export class DuckDBArrayVector<TValue extends DuckDBValue = DuckDBValue> extends DuckDBVector<DuckDBArrayValue<TValue>> {
export class DuckDBArrayVector extends DuckDBVector<DuckDBArrayValue> {
private readonly arrayType: DuckDBArrayType;
private readonly validity: DuckDBValidity;
private readonly childData: DuckDBVector<TValue>;
private readonly childData: DuckDBVector;
private readonly _itemCount: number;
constructor(
arrayType: DuckDBArrayType,
validity: DuckDBValidity,
childData: DuckDBVector<TValue>,
childData: DuckDBVector,
itemCount: number,
) {
super();
Expand Down Expand Up @@ -1508,14 +1515,14 @@ export class DuckDBArrayVector<TValue extends DuckDBValue = DuckDBValue> extends
public override get itemCount(): number {
return this._itemCount;
}
public override getItem(itemIndex: number): DuckDBArrayValue<TValue> | null {
public override getItem(itemIndex: number): DuckDBArrayValue | null {
if (!this.validity.itemValid(itemIndex)) {
return null;
}
return new DuckDBArrayValue(this.arrayType, this.childData.slice(itemIndex * this.arrayType.length, this.arrayType.length));
return new DuckDBArrayValue(this.childData.slice(itemIndex * this.arrayType.length, this.arrayType.length).toArray());
}
public override slice(offset: number, length: number): DuckDBArrayVector<TValue> {
return new DuckDBArrayVector<TValue>(
public override slice(offset: number, length: number): DuckDBArrayVector {
return new DuckDBArrayVector(
this.arrayType,
this.validity.slice(offset),
this.childData.slice(offset * this.arrayType.length, length * this.arrayType.length),
Expand Down
Loading