Skip to content

Commit 37771bd

Browse files
committed
value functions, logical type alias, append default
1 parent b018317 commit 37771bd

File tree

7 files changed

+973
-46
lines changed

7 files changed

+973
-46
lines changed

bindings/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pkgs/**/*.so
55
pkgs/**/*.dylib
66
pkgs/**/*.dll
77
test/tsconfig.tsbuildinfo
8+
*Sigs.json
89
__pycache__

bindings/pkgs/@duckdb/node-bindings/duckdb.d.ts

Lines changed: 203 additions & 9 deletions
Large diffs are not rendered by default.

bindings/src/duckdb_node_bindings.cpp

Lines changed: 545 additions & 27 deletions
Large diffs are not rendered by default.

bindings/test/appender.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ suite('appender', () => {
8989
varchar varchar, \
9090
blob blob, \
9191
null_column integer, \
92+
integer_with_default integer default 42\
9293
)');
9394
try {
9495
await expectResult(createResult, {
@@ -107,7 +108,7 @@ suite('appender', () => {
107108

108109
const appender = duckdb.appender_create(connection, 'main', 'appender_target');
109110
try {
110-
expect(duckdb.appender_column_count(appender)).toBe(20);
111+
expect(duckdb.appender_column_count(appender)).toBe(21);
111112

112113
const expectedLogicalTypes = [
113114
BOOLEAN,
@@ -130,6 +131,7 @@ suite('appender', () => {
130131
VARCHAR,
131132
BLOB,
132133
INTEGER,
134+
INTEGER,
133135
];
134136
for (let i = 0; i < expectLogicalType.length; i++) {
135137
const column_type = duckdb.appender_column_type(appender, i);
@@ -160,6 +162,7 @@ suite('appender', () => {
160162
duckdb.append_varchar(appender, '🦆🦆🦆🦆🦆🦆');
161163
duckdb.append_blob(appender, Buffer.from('thisisalongblob\x00withnullbytes'));
162164
duckdb.append_null(appender);
165+
duckdb.append_default(appender);
163166

164167
duckdb.appender_end_row(appender);
165168
// explicitly calling flush and close is unnecessary because destroy does both, but this exercises them.
@@ -193,6 +196,7 @@ suite('appender', () => {
193196
{ name: 'varchar', logicalType: VARCHAR },
194197
{ name: 'blob', logicalType: BLOB },
195198
{ name: 'null_column', logicalType: INTEGER },
199+
{ name: 'integer_with_default', logicalType: INTEGER },
196200
],
197201
chunks: [
198202
{
@@ -218,6 +222,7 @@ suite('appender', () => {
218222
data(16, [true], ['🦆🦆🦆🦆🦆🦆']),
219223
data(16, [true], [Buffer.from('thisisalongblob\x00withnullbytes')]),
220224
data(4, [false], [null]),
225+
data(4, [true], [42]),
221226
],
222227
},
223228
],

bindings/test/logical_type.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import duckdb from '@duckdb/node-bindings';
22
import { expect, suite, test } from 'vitest';
33

44
suite('logical_type', () => {
5-
test('create, get, and destroy', () => {
5+
test('create, get id, get/set alias, and destroy', () => {
66
const int_type = duckdb.create_logical_type(duckdb.Type.INTEGER);
77
try {
88
expect(duckdb.get_type_id(int_type)).toBe(duckdb.Type.INTEGER);
99
expect(duckdb.logical_type_get_alias(int_type)).toBeNull();
10+
duckdb.logical_type_set_alias(int_type, 'my_logical_type');
11+
expect(duckdb.logical_type_get_alias(int_type)).toBe('my_logical_type');
1012
} finally {
1113
duckdb.destroy_logical_type(int_type);
1214
}

bindings/test/prepared_statements.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ suite('prepared statements', () => {
390390
duckdb.bind_value(prepared, 3, array_value);
391391
expect(duckdb.param_type(prepared, 3)).toBe(duckdb.Type.ARRAY);
392392

393+
// TODO: map value?
394+
393395
const result = await duckdb.execute_prepared(prepared);
394396
try {
395397
await expectResult(result, {

bindings/test/values.test.ts

Lines changed: 213 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,228 @@
11
import duckdb from '@duckdb/node-bindings';
22
import { expect, suite, test } from 'vitest';
3+
import { expectLogicalType } from './utils/expectLogicalType';
4+
import {
5+
BIGINT,
6+
BLOB,
7+
BOOLEAN,
8+
DATE,
9+
DOUBLE,
10+
FLOAT,
11+
HUGEINT,
12+
INTEGER,
13+
INTERVAL,
14+
SMALLINT,
15+
TIME,
16+
TIME_TZ,
17+
TIMESTAMP,
18+
TINYINT,
19+
UBIGINT,
20+
UHUGEINT,
21+
UINTEGER,
22+
USMALLINT,
23+
UTINYINT,
24+
VARCHAR,
25+
} from './utils/expectedLogicalTypes';
326

427
suite('values', () => {
5-
test('varchar', () => {
6-
const varchar_text = 'varchar_text';
7-
const varchar_value = duckdb.create_varchar(varchar_text);
28+
test('bool', () => {
29+
const input = true;
30+
const bool_value = duckdb.create_bool(input);
831
try {
9-
expect(duckdb.get_varchar(varchar_value)).toBe(varchar_text);
32+
expectLogicalType(duckdb.get_value_type(bool_value), BOOLEAN);
33+
expect(duckdb.get_bool(bool_value)).toBe(input);
1034
} finally {
11-
duckdb.destroy_value(varchar_value);
35+
duckdb.destroy_value(bool_value);
36+
}
37+
});
38+
test('int8', () => {
39+
const input = 127;
40+
const int8_value = duckdb.create_int8(input);
41+
try {
42+
expectLogicalType(duckdb.get_value_type(int8_value), TINYINT);
43+
expect(duckdb.get_int8(int8_value)).toBe(input);
44+
} finally {
45+
duckdb.destroy_value(int8_value);
46+
}
47+
});
48+
test('uint8', () => {
49+
const input = 255;
50+
const uint8_value = duckdb.create_uint8(input);
51+
try {
52+
expectLogicalType(duckdb.get_value_type(uint8_value), UTINYINT);
53+
expect(duckdb.get_uint8(uint8_value)).toBe(input);
54+
} finally {
55+
duckdb.destroy_value(uint8_value);
56+
}
57+
});
58+
test('int16', () => {
59+
const input = 32767;
60+
const int16_value = duckdb.create_int16(input);
61+
try {
62+
expectLogicalType(duckdb.get_value_type(int16_value), SMALLINT);
63+
expect(duckdb.get_int16(int16_value)).toBe(input);
64+
} finally {
65+
duckdb.destroy_value(int16_value);
66+
}
67+
});
68+
test('uint16', () => {
69+
const input = 65535;
70+
const uint16_value = duckdb.create_uint16(input);
71+
try {
72+
expectLogicalType(duckdb.get_value_type(uint16_value), USMALLINT);
73+
expect(duckdb.get_uint16(uint16_value)).toBe(input);
74+
} finally {
75+
duckdb.destroy_value(uint16_value);
76+
}
77+
});
78+
test('int32', () => {
79+
const input = 2147483647;
80+
const int32_value = duckdb.create_int32(input);
81+
try {
82+
expectLogicalType(duckdb.get_value_type(int32_value), INTEGER);
83+
expect(duckdb.get_int32(int32_value)).toBe(input);
84+
} finally {
85+
duckdb.destroy_value(int32_value);
86+
}
87+
});
88+
test('uint32', () => {
89+
const input = 4294967295;
90+
const uint32_value = duckdb.create_uint32(input);
91+
try {
92+
expectLogicalType(duckdb.get_value_type(uint32_value), UINTEGER);
93+
expect(duckdb.get_uint32(uint32_value)).toBe(input);
94+
} finally {
95+
duckdb.destroy_value(uint32_value);
1296
}
1397
});
1498
test('int64', () => {
15-
const int64_bigint = 12345n;
16-
const int64_value = duckdb.create_int64(int64_bigint);
99+
const input = 9223372036854775807n;
100+
const int64_value = duckdb.create_int64(input);
17101
try {
18-
expect(duckdb.get_int64(int64_value)).toBe(int64_bigint);
102+
expectLogicalType(duckdb.get_value_type(int64_value), BIGINT);
103+
expect(duckdb.get_int64(int64_value)).toBe(input);
19104
} finally {
20105
duckdb.destroy_value(int64_value);
21106
}
22107
});
108+
test('uint64', () => {
109+
const input = 18446744073709551615n;
110+
const uint64_value = duckdb.create_uint64(input);
111+
try {
112+
expectLogicalType(duckdb.get_value_type(uint64_value), UBIGINT);
113+
expect(duckdb.get_uint64(uint64_value)).toBe(input);
114+
} finally {
115+
duckdb.destroy_value(uint64_value);
116+
}
117+
});
118+
test('hugeint', () => {
119+
const input = 170141183460469231731687303715884105727n;
120+
const hugeint_value = duckdb.create_hugeint(input);
121+
try {
122+
expectLogicalType(duckdb.get_value_type(hugeint_value), HUGEINT);
123+
expect(duckdb.get_hugeint(hugeint_value)).toBe(input);
124+
} finally {
125+
duckdb.destroy_value(hugeint_value);
126+
}
127+
});
128+
test('uhugeint', () => {
129+
const input = 340282366920938463463374607431768211455n;
130+
const uhugeint_value = duckdb.create_uhugeint(input);
131+
try {
132+
expectLogicalType(duckdb.get_value_type(uhugeint_value), UHUGEINT);
133+
expect(duckdb.get_uhugeint(uhugeint_value)).toBe(input);
134+
} finally {
135+
duckdb.destroy_value(uhugeint_value);
136+
}
137+
});
138+
test('float', () => {
139+
const input = 3.4028234663852886e+38;
140+
const float_value = duckdb.create_float(input);
141+
try {
142+
expectLogicalType(duckdb.get_value_type(float_value), FLOAT);
143+
expect(duckdb.get_float(float_value)).toBe(input);
144+
} finally {
145+
duckdb.destroy_value(float_value);
146+
}
147+
});
148+
test('double', () => {
149+
const input = 1.7976931348623157e+308;
150+
const double_value = duckdb.create_double(input);
151+
try {
152+
expectLogicalType(duckdb.get_value_type(double_value), DOUBLE);
153+
expect(duckdb.get_double(double_value)).toBe(input);
154+
} finally {
155+
duckdb.destroy_value(double_value);
156+
}
157+
});
158+
test('date', () => {
159+
const input = { days: 2147483646 };
160+
const date_value = duckdb.create_date(input);
161+
try {
162+
expectLogicalType(duckdb.get_value_type(date_value), DATE);
163+
expect(duckdb.get_date(date_value)).toStrictEqual(input);
164+
} finally {
165+
duckdb.destroy_value(date_value);
166+
}
167+
});
168+
test('time', () => {
169+
const input = { micros: 86400000000 };
170+
const time_value = duckdb.create_time(input);
171+
try {
172+
expectLogicalType(duckdb.get_value_type(time_value), TIME);
173+
expect(duckdb.get_time(time_value)).toStrictEqual(input);
174+
} finally {
175+
duckdb.destroy_value(time_value);
176+
}
177+
});
178+
test('time_tz', () => {
179+
const input = { bits: 1449551462400115198n };
180+
const time_tz_value = duckdb.create_time_tz_value(input);
181+
try {
182+
expectLogicalType(duckdb.get_value_type(time_tz_value), TIME_TZ);
183+
expect(duckdb.get_time_tz(time_tz_value)).toStrictEqual(input);
184+
} finally {
185+
duckdb.destroy_value(time_tz_value);
186+
}
187+
});
188+
test('timestamp', () => {
189+
const input = { micros: 9223372036854775806n };
190+
const timestamp_value = duckdb.create_timestamp(input);
191+
try {
192+
expectLogicalType(duckdb.get_value_type(timestamp_value), TIMESTAMP);
193+
expect(duckdb.get_timestamp(timestamp_value)).toStrictEqual(input);
194+
} finally {
195+
duckdb.destroy_value(timestamp_value);
196+
}
197+
});
198+
test('interval', () => {
199+
const input = { months: 999, days: 999, micros: 999999999n };
200+
const interval_value = duckdb.create_interval(input);
201+
try {
202+
expectLogicalType(duckdb.get_value_type(interval_value), INTERVAL);
203+
expect(duckdb.get_interval(interval_value)).toStrictEqual(input);
204+
} finally {
205+
duckdb.destroy_value(interval_value);
206+
}
207+
});
208+
test('blob', () => {
209+
const input = Buffer.from('thisisalongblob\x00withnullbytes');
210+
const blob_value = duckdb.create_blob(input);
211+
try {
212+
expectLogicalType(duckdb.get_value_type(blob_value), BLOB);
213+
expect(duckdb.get_blob(blob_value)).toStrictEqual(input);
214+
} finally {
215+
duckdb.destroy_value(blob_value);
216+
}
217+
});
218+
test('varchar', () => {
219+
const input = 'varchar_text';
220+
const varchar_value = duckdb.create_varchar(input);
221+
try {
222+
expectLogicalType(duckdb.get_value_type(varchar_value), VARCHAR);
223+
expect(duckdb.get_varchar(varchar_value)).toBe(input);
224+
} finally {
225+
duckdb.destroy_value(varchar_value);
226+
}
227+
});
23228
});

0 commit comments

Comments
 (0)