-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcreateValue.ts
More file actions
202 lines (201 loc) · 7.52 KB
/
createValue.ts
File metadata and controls
202 lines (201 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import duckdb, { Value } from '@duckdb/node-bindings';
import { DuckDBType } from './DuckDBType';
import { DuckDBTypeId } from './DuckDBTypeId';
import {
DuckDBArrayValue,
DuckDBBlobValue,
DuckDBDateValue,
DuckDBDecimalValue,
DuckDBIntervalValue,
DuckDBListValue,
DuckDBStructValue,
DuckDBTimestampTZValue,
DuckDBTimestampValue,
DuckDBTimeTZValue,
DuckDBTimeValue,
DuckDBValue,
} from './values';
export function createValue(type: DuckDBType, input: DuckDBValue): Value {
switch (type.typeId) {
case DuckDBTypeId.BOOLEAN:
if (typeof input === 'boolean') {
return duckdb.create_bool(input);
}
throw new Error(`input is not a boolean`);
case DuckDBTypeId.TINYINT:
if (typeof input === 'number') {
return duckdb.create_int8(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.SMALLINT:
if (typeof input === 'number') {
return duckdb.create_int16(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.INTEGER:
if (typeof input === 'number') {
return duckdb.create_int32(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.BIGINT:
if (typeof input === 'bigint') {
return duckdb.create_int64(input);
}
throw new Error(`input is not a bigint`);
case DuckDBTypeId.UTINYINT:
if (typeof input === 'number') {
return duckdb.create_uint8(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.USMALLINT:
if (typeof input === 'number') {
return duckdb.create_uint16(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.UINTEGER:
if (typeof input === 'number') {
return duckdb.create_uint32(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.UBIGINT:
if (typeof input === 'bigint') {
return duckdb.create_uint64(input);
}
throw new Error(`input is not a bigint`);
case DuckDBTypeId.FLOAT:
if (typeof input === 'number') {
return duckdb.create_float(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.DOUBLE:
if (typeof input === 'number') {
return duckdb.create_double(input);
}
throw new Error(`input is not a number`);
case DuckDBTypeId.TIMESTAMP:
if (input instanceof DuckDBTimestampValue) {
return duckdb.create_timestamp(input);
}
throw new Error(`input is not a DuckDBTimestampValue`);
case DuckDBTypeId.DATE:
if (input instanceof DuckDBDateValue) {
return duckdb.create_date(input);
}
throw new Error(`input is not a DuckDBDateValue`);
case DuckDBTypeId.TIME:
if (input instanceof DuckDBTimeValue) {
return duckdb.create_time(input);
}
throw new Error(`input is not a DuckDBTimeValue`);
case DuckDBTypeId.INTERVAL:
if (input instanceof DuckDBIntervalValue) {
return duckdb.create_interval(input);
}
throw new Error(`input is not a DuckDBIntervalValue`);
case DuckDBTypeId.HUGEINT:
if (typeof input === 'bigint') {
return duckdb.create_hugeint(input);
}
throw new Error(`input is not a bigint`);
case DuckDBTypeId.UHUGEINT:
if (typeof input === 'bigint') {
return duckdb.create_uhugeint(input);
}
throw new Error(`input is not a bigint`);
case DuckDBTypeId.VARCHAR:
if (typeof input === 'string') {
return duckdb.create_varchar(input);
}
throw new Error(`input is not a string`);
case DuckDBTypeId.BLOB:
if (input instanceof DuckDBBlobValue) {
return duckdb.create_blob(input.bytes);
}
throw new Error(`input is not a DuckDBBlobValue`);
case DuckDBTypeId.DECIMAL:
if (input instanceof DuckDBDecimalValue) {
return duckdb.create_decimal(input);
}
throw new Error(`input is not a DuckDBDecimalValue`);
case DuckDBTypeId.TIMESTAMP_S:
throw new Error(`not yet implemented for TIMESTAMP_S`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.TIMESTAMP_MS:
throw new Error(`not yet implemented for TIMESTAMP_MS`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.TIMESTAMP_NS:
throw new Error(`not yet implemented for TIMESTAMP_NS`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.ENUM:
throw new Error(`not yet implemented for ENUM`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.LIST:
if (input instanceof DuckDBListValue) {
if (type.valueType.typeId === DuckDBTypeId.ANY) {
throw new Error(
'Cannot create lists with item type of ANY. Specify a specific type.'
);
}
return duckdb.create_list_value(
type.valueType.toLogicalType().logical_type,
input.items.map((item) => createValue(type.valueType, item))
);
}
throw new Error(`input is not a DuckDBListValue`);
case DuckDBTypeId.STRUCT:
if (input instanceof DuckDBStructValue) {
if (type.entryTypes.find((type) => type.typeId === DuckDBTypeId.ANY)) {
throw new Error(
'Cannot create structs with an entry type of ANY. Specify a specific type.'
);
}
return duckdb.create_struct_value(
type.toLogicalType().logical_type,
Object.values(input.entries).map((value, i) =>
createValue(type.entryTypes[i], value)
)
);
}
throw new Error(`input is not a DuckDBStructValue`);
case DuckDBTypeId.MAP:
throw new Error(`not yet implemented for MAP`); // TODO: implement when available, hopefully in 1.2.0
case DuckDBTypeId.ARRAY:
if (input instanceof DuckDBArrayValue) {
if (type.valueType.typeId === DuckDBTypeId.ANY) {
throw new Error(
'Cannot create arrays with item type of ANY. Specify a specific type.'
);
}
return duckdb.create_array_value(
type.valueType.toLogicalType().logical_type,
input.items.map((item) => createValue(type.valueType, item))
);
}
throw new Error(`input is not a DuckDBArrayValue`);
case DuckDBTypeId.UUID:
throw new Error(`not yet implemented for UUID`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.UNION:
throw new Error(`not yet implemented for UNION`); // TODO: implement when available, hopefully in 1.2.0
case DuckDBTypeId.UNION:
throw new Error(`not yet implemented for BIT`); // TODO: implement when available in 1.2.0
case DuckDBTypeId.TIME_TZ:
if (input instanceof DuckDBTimeTZValue) {
return duckdb.create_time_tz_value(input);
}
throw new Error(`input is not a DuckDBTimeTZValue`);
case DuckDBTypeId.TIMESTAMP_TZ:
if (input instanceof DuckDBTimestampTZValue) {
return duckdb.create_timestamp(input); // TODO: change to create_timestamp_tz when available in 1.2.0
}
throw new Error(`input is not a DuckDBTimestampTZValue`);
case DuckDBTypeId.ANY:
throw new Error(
`Cannot create values of type ANY. Specify a specific type.`
);
case DuckDBTypeId.VARINT:
if (typeof input === 'bigint') {
return duckdb.create_varint(input);
}
throw new Error(`input is not a bigint`);
case DuckDBTypeId.SQLNULL:
throw new Error(`not yet implemented for SQLNUll`); // TODO: implement when available in 1.2.0
default:
throw new Error(`unrecognized type id ${type.typeId}`);
}
}