@haybarn/node-api 1.5.5-rc1 → 2.0.0-rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. package/README.md +315 -20
  2. package/lib/DuckDBAppender.d.ts +8 -2
  3. package/lib/DuckDBAppender.js +19 -1
  4. package/lib/DuckDBConnection.d.ts +2 -0
  5. package/lib/DuckDBConnection.js +4 -1
  6. package/lib/DuckDBDataChunk.d.ts +30 -0
  7. package/lib/DuckDBDataChunk.js +94 -1
  8. package/lib/DuckDBDataChunkWriter.d.ts +74 -0
  9. package/lib/DuckDBDataChunkWriter.js +109 -0
  10. package/lib/DuckDBLogicalType.d.ts +5 -1
  11. package/lib/DuckDBLogicalType.js +18 -1
  12. package/lib/DuckDBMaterializedResult.d.ts +8 -1
  13. package/lib/DuckDBMaterializedResult.js +22 -2
  14. package/lib/DuckDBPendingResult.d.ts +16 -1
  15. package/lib/DuckDBPendingResult.js +19 -2
  16. package/lib/DuckDBPreparedStatement.d.ts +4 -1
  17. package/lib/DuckDBPreparedStatement.js +17 -4
  18. package/lib/DuckDBResult.d.ts +21 -1
  19. package/lib/DuckDBResult.js +87 -5
  20. package/lib/DuckDBResultReader.d.ts +5 -0
  21. package/lib/DuckDBResultReader.js +9 -2
  22. package/lib/DuckDBScalarFunction.d.ts +9 -5
  23. package/lib/DuckDBScalarFunction.js +14 -5
  24. package/lib/DuckDBScalarFunctionBindInfo.d.ts +16 -0
  25. package/lib/{DuckDBBindInfo.js → DuckDBScalarFunctionBindInfo.js} +5 -3
  26. package/lib/DuckDBScalarFunctionInfo.d.ts +16 -0
  27. package/lib/{DuckDBFunctionInfo.js → DuckDBScalarFunctionInfo.js} +11 -3
  28. package/lib/{DuckDBBindInfo.d.ts → DuckDBScalarFunctionInitInfo.d.ts} +6 -4
  29. package/lib/DuckDBScalarFunctionInitInfo.js +39 -0
  30. package/lib/DuckDBTableFunction.d.ts +41 -0
  31. package/lib/DuckDBTableFunction.js +83 -0
  32. package/lib/DuckDBTableFunctionBindInfo.d.ts +21 -0
  33. package/lib/DuckDBTableFunctionBindInfo.js +54 -0
  34. package/lib/DuckDBTableFunctionInfo.d.ts +15 -0
  35. package/lib/DuckDBTableFunctionInfo.js +42 -0
  36. package/lib/DuckDBTableFunctionInitInfo.d.ts +22 -0
  37. package/lib/DuckDBTableFunctionInitInfo.js +58 -0
  38. package/lib/DuckDBType.d.ts +1 -373
  39. package/lib/DuckDBType.js +14 -927
  40. package/lib/DuckDBTypeId.d.ts +4 -1
  41. package/lib/DuckDBTypeId.js +3 -0
  42. package/lib/DuckDBValueConverter.d.ts +10 -0
  43. package/lib/DuckDBValueConverters.d.ts +27 -0
  44. package/lib/DuckDBValueConverters.js +128 -1
  45. package/lib/DuckDBVector.d.ts +1 -596
  46. package/lib/DuckDBVector.js +14 -2522
  47. package/lib/JSDuckDBValueConverter.d.ts +1 -2
  48. package/lib/JSDuckDBValueConverter.js +5 -2
  49. package/lib/JSInputTypeForTypeId.d.ts +104 -0
  50. package/lib/JSInputTypeForTypeId.js +3 -0
  51. package/lib/JSToDuckDBValueConverter.d.ts +30 -0
  52. package/lib/JSToDuckDBValueConverter.js +83 -0
  53. package/lib/JSTypeForTypeId.d.ts +91 -0
  54. package/lib/JSTypeForTypeId.js +3 -0
  55. package/lib/JsonDuckDBValueConverter.d.ts +1 -2
  56. package/lib/JsonDuckDBValueConverter.js +4 -1
  57. package/lib/JsonTypeForTypeId.d.ts +96 -0
  58. package/lib/JsonTypeForTypeId.js +3 -0
  59. package/lib/ToDuckDBValueConverter.d.ts +41 -0
  60. package/lib/ToDuckDBValueConverter.js +27 -0
  61. package/lib/ToDuckDBValueConverters.d.ts +54 -0
  62. package/lib/ToDuckDBValueConverters.js +199 -0
  63. package/lib/conversion/dateTimeStringConversion.d.ts +2 -2
  64. package/lib/conversion/dateTimeStringConversion.js +20 -7
  65. package/lib/conversion/varintDecode.d.ts +18 -0
  66. package/lib/conversion/varintDecode.js +42 -0
  67. package/lib/convertColumnsFromChunks.d.ts +1 -1
  68. package/lib/convertColumnsFromChunks.js +2 -2
  69. package/lib/createResult.d.ts +2 -1
  70. package/lib/createResult.js +3 -3
  71. package/lib/createValue.js +63 -5
  72. package/lib/duckdb.d.ts +15 -2
  73. package/lib/duckdb.js +15 -2
  74. package/lib/getColumnsFromChunks.d.ts +1 -1
  75. package/lib/getColumnsFromChunks.js +2 -2
  76. package/lib/getColumnsObjectFromChunks.js +3 -0
  77. package/lib/readValue.d.ts +22 -0
  78. package/lib/readValue.js +35 -0
  79. package/lib/typeForValue.js +21 -2
  80. package/lib/types/BaseDuckDBType.d.ts +11 -0
  81. package/lib/types/BaseDuckDBType.js +34 -0
  82. package/lib/types/DuckDBAnyType.d.ts +8 -0
  83. package/lib/types/DuckDBAnyType.js +16 -0
  84. package/lib/types/DuckDBArrayType.d.ts +14 -0
  85. package/lib/types/DuckDBArrayType.js +38 -0
  86. package/lib/types/DuckDBBigIntType.d.ts +12 -0
  87. package/lib/types/DuckDBBigIntType.js +24 -0
  88. package/lib/types/DuckDBBigNumType.d.ts +12 -0
  89. package/lib/types/DuckDBBigNumType.js +24 -0
  90. package/lib/types/DuckDBBitType.d.ts +8 -0
  91. package/lib/types/DuckDBBitType.js +16 -0
  92. package/lib/types/DuckDBBlobType.d.ts +8 -0
  93. package/lib/types/DuckDBBlobType.js +16 -0
  94. package/lib/types/DuckDBBooleanType.d.ts +8 -0
  95. package/lib/types/DuckDBBooleanType.js +16 -0
  96. package/lib/types/DuckDBDateType.d.ts +14 -0
  97. package/lib/types/DuckDBDateType.js +32 -0
  98. package/lib/types/DuckDBDecimalType.d.ts +14 -0
  99. package/lib/types/DuckDBDecimalType.js +45 -0
  100. package/lib/types/DuckDBDoubleType.d.ts +12 -0
  101. package/lib/types/DuckDBDoubleType.js +24 -0
  102. package/lib/types/DuckDBEnumType.d.ts +29 -0
  103. package/lib/types/DuckDBEnumType.js +100 -0
  104. package/lib/types/DuckDBFloatType.d.ts +12 -0
  105. package/lib/types/DuckDBFloatType.js +24 -0
  106. package/lib/types/DuckDBGeometryType.d.ts +19 -0
  107. package/lib/types/DuckDBGeometryType.js +38 -0
  108. package/lib/types/DuckDBHugeIntType.d.ts +12 -0
  109. package/lib/types/DuckDBHugeIntType.js +24 -0
  110. package/lib/types/DuckDBIntegerLiteralType.d.ts +8 -0
  111. package/lib/types/DuckDBIntegerLiteralType.js +16 -0
  112. package/lib/types/DuckDBIntegerType.d.ts +12 -0
  113. package/lib/types/DuckDBIntegerType.js +24 -0
  114. package/lib/types/DuckDBIntervalType.d.ts +8 -0
  115. package/lib/types/DuckDBIntervalType.js +16 -0
  116. package/lib/types/DuckDBListType.d.ts +13 -0
  117. package/lib/types/DuckDBListType.js +35 -0
  118. package/lib/types/DuckDBMapType.d.ts +14 -0
  119. package/lib/types/DuckDBMapType.js +38 -0
  120. package/lib/types/DuckDBSQLNullType.d.ts +10 -0
  121. package/lib/types/DuckDBSQLNullType.js +20 -0
  122. package/lib/types/DuckDBSmallIntType.d.ts +12 -0
  123. package/lib/types/DuckDBSmallIntType.js +24 -0
  124. package/lib/types/DuckDBStringLiteralType.d.ts +8 -0
  125. package/lib/types/DuckDBStringLiteralType.js +16 -0
  126. package/lib/types/DuckDBStructType.d.ts +29 -0
  127. package/lib/types/DuckDBStructType.js +86 -0
  128. package/lib/types/DuckDBTimeNSType.d.ts +11 -0
  129. package/lib/types/DuckDBTimeNSType.js +23 -0
  130. package/lib/types/DuckDBTimeTZType.d.ts +12 -0
  131. package/lib/types/DuckDBTimeTZType.js +26 -0
  132. package/lib/types/DuckDBTimeType.d.ts +11 -0
  133. package/lib/types/DuckDBTimeType.js +23 -0
  134. package/lib/types/DuckDBTimestampMillisecondsType.d.ts +14 -0
  135. package/lib/types/DuckDBTimestampMillisecondsType.js +32 -0
  136. package/lib/types/DuckDBTimestampNanosecondsType.d.ts +14 -0
  137. package/lib/types/DuckDBTimestampNanosecondsType.js +32 -0
  138. package/lib/types/DuckDBTimestampSecondsType.d.ts +14 -0
  139. package/lib/types/DuckDBTimestampSecondsType.js +32 -0
  140. package/lib/types/DuckDBTimestampTZNanosecondsType.d.ts +15 -0
  141. package/lib/types/DuckDBTimestampTZNanosecondsType.js +35 -0
  142. package/lib/types/DuckDBTimestampTZType.d.ts +15 -0
  143. package/lib/types/DuckDBTimestampTZType.js +35 -0
  144. package/lib/types/DuckDBTimestampType.d.ts +16 -0
  145. package/lib/types/DuckDBTimestampType.js +33 -0
  146. package/lib/types/DuckDBTinyIntType.d.ts +12 -0
  147. package/lib/types/DuckDBTinyIntType.js +24 -0
  148. package/lib/types/DuckDBType.d.ts +43 -0
  149. package/lib/types/DuckDBType.js +2 -0
  150. package/lib/types/DuckDBUBigIntType.d.ts +12 -0
  151. package/lib/types/DuckDBUBigIntType.js +24 -0
  152. package/lib/types/DuckDBUHugeIntType.d.ts +12 -0
  153. package/lib/types/DuckDBUHugeIntType.js +24 -0
  154. package/lib/types/DuckDBUIntegerType.d.ts +12 -0
  155. package/lib/types/DuckDBUIntegerType.js +24 -0
  156. package/lib/types/DuckDBUSmallIntType.d.ts +12 -0
  157. package/lib/types/DuckDBUSmallIntType.js +24 -0
  158. package/lib/types/DuckDBUTinyIntType.d.ts +12 -0
  159. package/lib/types/DuckDBUTinyIntType.js +24 -0
  160. package/lib/types/DuckDBUUIDType.d.ts +11 -0
  161. package/lib/types/DuckDBUUIDType.js +23 -0
  162. package/lib/types/DuckDBUnionType.d.ts +18 -0
  163. package/lib/types/DuckDBUnionType.js +64 -0
  164. package/lib/types/DuckDBVarCharType.d.ts +8 -0
  165. package/lib/types/DuckDBVarCharType.js +16 -0
  166. package/lib/types/DuckDBVariantType.d.ts +8 -0
  167. package/lib/types/DuckDBVariantType.js +16 -0
  168. package/lib/types/index.d.ts +44 -0
  169. package/lib/types/index.js +60 -0
  170. package/lib/values/DuckDBGeometryValue.d.ts +12 -0
  171. package/lib/values/DuckDBGeometryValue.js +30 -0
  172. package/lib/values/DuckDBTimestampTZNanosecondsValue.d.ts +14 -0
  173. package/lib/values/DuckDBTimestampTZNanosecondsValue.js +33 -0
  174. package/lib/values/DuckDBTupleValue.d.ts +11 -0
  175. package/lib/values/DuckDBTupleValue.js +22 -0
  176. package/lib/values/DuckDBUUIDValue.js +5 -1
  177. package/lib/values/DuckDBValue.d.ts +5 -1
  178. package/lib/values/DuckDBVariantValue.d.ts +19 -0
  179. package/lib/values/DuckDBVariantValue.js +30 -0
  180. package/lib/values/index.d.ts +4 -0
  181. package/lib/values/index.js +4 -0
  182. package/lib/vectors/DuckDBArrayVector.d.ts +21 -0
  183. package/lib/vectors/DuckDBArrayVector.js +79 -0
  184. package/lib/vectors/DuckDBBigIntVector.d.ts +17 -0
  185. package/lib/vectors/DuckDBBigIntVector.js +58 -0
  186. package/lib/vectors/DuckDBBigNumVector.d.ts +21 -0
  187. package/lib/vectors/DuckDBBigNumVector.js +70 -0
  188. package/lib/vectors/DuckDBBitVector.d.ts +22 -0
  189. package/lib/vectors/DuckDBBitVector.js +74 -0
  190. package/lib/vectors/DuckDBBlobVector.d.ts +32 -0
  191. package/lib/vectors/DuckDBBlobVector.js +83 -0
  192. package/lib/vectors/DuckDBBooleanVector.d.ts +18 -0
  193. package/lib/vectors/DuckDBBooleanVector.js +58 -0
  194. package/lib/vectors/DuckDBDateVector.d.ts +18 -0
  195. package/lib/vectors/DuckDBDateVector.js +64 -0
  196. package/lib/vectors/DuckDBDecimal128Vector.d.ts +21 -0
  197. package/lib/vectors/DuckDBDecimal128Vector.js +64 -0
  198. package/lib/vectors/DuckDBDecimal16Vector.d.ts +21 -0
  199. package/lib/vectors/DuckDBDecimal16Vector.js +64 -0
  200. package/lib/vectors/DuckDBDecimal32Vector.d.ts +21 -0
  201. package/lib/vectors/DuckDBDecimal32Vector.js +64 -0
  202. package/lib/vectors/DuckDBDecimal64Vector.d.ts +21 -0
  203. package/lib/vectors/DuckDBDecimal64Vector.js +64 -0
  204. package/lib/vectors/DuckDBDoubleVector.d.ts +17 -0
  205. package/lib/vectors/DuckDBDoubleVector.js +54 -0
  206. package/lib/vectors/DuckDBEnum16Vector.d.ts +18 -0
  207. package/lib/vectors/DuckDBEnum16Vector.js +57 -0
  208. package/lib/vectors/DuckDBEnum32Vector.d.ts +18 -0
  209. package/lib/vectors/DuckDBEnum32Vector.js +57 -0
  210. package/lib/vectors/DuckDBEnum8Vector.d.ts +18 -0
  211. package/lib/vectors/DuckDBEnum8Vector.js +57 -0
  212. package/lib/vectors/DuckDBFloatVector.d.ts +17 -0
  213. package/lib/vectors/DuckDBFloatVector.js +54 -0
  214. package/lib/vectors/DuckDBGeometryVector.d.ts +22 -0
  215. package/lib/vectors/DuckDBGeometryVector.js +69 -0
  216. package/lib/vectors/DuckDBHugeIntVector.d.ts +19 -0
  217. package/lib/vectors/DuckDBHugeIntVector.js +63 -0
  218. package/lib/vectors/DuckDBIntegerVector.d.ts +17 -0
  219. package/lib/vectors/DuckDBIntegerVector.js +61 -0
  220. package/lib/vectors/DuckDBIntervalVector.d.ts +19 -0
  221. package/lib/vectors/DuckDBIntervalVector.js +67 -0
  222. package/lib/vectors/DuckDBListVector.d.ts +35 -0
  223. package/lib/vectors/DuckDBListVector.js +151 -0
  224. package/lib/vectors/DuckDBMapVector.d.ts +17 -0
  225. package/lib/vectors/DuckDBMapVector.js +60 -0
  226. package/lib/vectors/DuckDBSQLNullVector.d.ts +18 -0
  227. package/lib/vectors/DuckDBSQLNullVector.js +44 -0
  228. package/lib/vectors/DuckDBSmallIntVector.d.ts +17 -0
  229. package/lib/vectors/DuckDBSmallIntVector.js +61 -0
  230. package/lib/vectors/DuckDBStructVector.d.ts +35 -0
  231. package/lib/vectors/DuckDBStructVector.js +105 -0
  232. package/lib/vectors/DuckDBTimeNSVector.d.ts +18 -0
  233. package/lib/vectors/DuckDBTimeNSVector.js +61 -0
  234. package/lib/vectors/DuckDBTimeTZVector.d.ts +18 -0
  235. package/lib/vectors/DuckDBTimeTZVector.js +61 -0
  236. package/lib/vectors/DuckDBTimeVector.d.ts +18 -0
  237. package/lib/vectors/DuckDBTimeVector.js +61 -0
  238. package/lib/vectors/DuckDBTimestampMillisecondsVector.d.ts +18 -0
  239. package/lib/vectors/DuckDBTimestampMillisecondsVector.js +61 -0
  240. package/lib/vectors/DuckDBTimestampNanosecondsVector.d.ts +18 -0
  241. package/lib/vectors/DuckDBTimestampNanosecondsVector.js +61 -0
  242. package/lib/vectors/DuckDBTimestampSecondsVector.d.ts +18 -0
  243. package/lib/vectors/DuckDBTimestampSecondsVector.js +61 -0
  244. package/lib/vectors/DuckDBTimestampTZNanosecondsVector.d.ts +18 -0
  245. package/lib/vectors/DuckDBTimestampTZNanosecondsVector.js +61 -0
  246. package/lib/vectors/DuckDBTimestampTZVector.d.ts +18 -0
  247. package/lib/vectors/DuckDBTimestampTZVector.js +61 -0
  248. package/lib/vectors/DuckDBTimestampVector.d.ts +18 -0
  249. package/lib/vectors/DuckDBTimestampVector.js +61 -0
  250. package/lib/vectors/DuckDBTinyIntVector.d.ts +17 -0
  251. package/lib/vectors/DuckDBTinyIntVector.js +61 -0
  252. package/lib/vectors/DuckDBTupleVector.d.ts +26 -0
  253. package/lib/vectors/DuckDBTupleVector.js +91 -0
  254. package/lib/vectors/DuckDBUBigIntVector.d.ts +17 -0
  255. package/lib/vectors/DuckDBUBigIntVector.js +58 -0
  256. package/lib/vectors/DuckDBUHugeIntVector.d.ts +19 -0
  257. package/lib/vectors/DuckDBUHugeIntVector.js +63 -0
  258. package/lib/vectors/DuckDBUIntegerVector.d.ts +17 -0
  259. package/lib/vectors/DuckDBUIntegerVector.js +61 -0
  260. package/lib/vectors/DuckDBUSmallIntVector.d.ts +17 -0
  261. package/lib/vectors/DuckDBUSmallIntVector.js +61 -0
  262. package/lib/vectors/DuckDBUTinyIntVector.d.ts +17 -0
  263. package/lib/vectors/DuckDBUTinyIntVector.js +61 -0
  264. package/lib/vectors/DuckDBUUIDVector.d.ts +19 -0
  265. package/lib/vectors/DuckDBUUIDVector.js +59 -0
  266. package/lib/vectors/DuckDBUnionVector.d.ts +17 -0
  267. package/lib/vectors/DuckDBUnionVector.js +70 -0
  268. package/lib/vectors/DuckDBValidity.d.ts +12 -0
  269. package/lib/vectors/DuckDBValidity.js +91 -0
  270. package/lib/vectors/DuckDBVarCharVector.d.ts +21 -0
  271. package/lib/vectors/DuckDBVarCharVector.js +74 -0
  272. package/lib/vectors/DuckDBVariantVector.d.ts +42 -0
  273. package/lib/vectors/DuckDBVariantVector.js +461 -0
  274. package/lib/vectors/DuckDBVector.d.ts +14 -0
  275. package/lib/vectors/DuckDBVector.js +27 -0
  276. package/lib/vectors/createVector.d.ts +1 -0
  277. package/lib/vectors/createVector.js +201 -0
  278. package/lib/vectors/dataAccessors.d.ts +45 -0
  279. package/lib/vectors/dataAccessors.js +344 -0
  280. package/lib/vectors/index.d.ts +47 -0
  281. package/lib/vectors/index.js +65 -0
  282. package/lib/vectors/vectorRegistry.d.ts +20 -0
  283. package/lib/vectors/vectorRegistry.js +16 -0
  284. package/package.json +3 -2
  285. package/lib/DuckDBFunctionInfo.d.ts +0 -10
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.readValue = readValue;
7
+ const node_bindings_1 = __importDefault(require("@haybarn/node-bindings"));
8
+ const DuckDBVector_1 = require("./DuckDBVector");
9
+ /**
10
+ * Converts a `duckdb.Value` into a `DuckDBValue`.
11
+ *
12
+ * Results normally arrive as vectors rather than values, so this is for the
13
+ * places that hand back a value instead: table function parameters, and
14
+ * `duckdb_get_table_names`.
15
+ *
16
+ * It works by referencing the value into a vector of its own and reading the one
17
+ * element back, which reuses the vector reading path rather than duplicating it.
18
+ * That matters for more than tidiness: the value accessors cannot reach inside an
19
+ * ARRAY or a UNION at all, because `duckdb_get_list_child` and
20
+ * `duckdb_get_struct_child` guard on the exact logical type id and return null
21
+ * for both. Going through a vector covers every type, and leaves one
22
+ * implementation of how each type is represented rather than two that can drift.
23
+ *
24
+ * The cost is roughly an extra microsecond per scalar value, which is fine here
25
+ * and would not be in a per-row path. Per-row data already arrives as vectors and
26
+ * never comes through here.
27
+ */
28
+ function readValue(value) {
29
+ // Capacity of one: the vector exists only to hold this single value. It is
30
+ // destroyed when collected, and getItem materializes what it returns, so
31
+ // nothing handed back keeps a reference to the vector's memory.
32
+ const vector = node_bindings_1.default.create_vector(node_bindings_1.default.get_value_type(value), 1);
33
+ node_bindings_1.default.vector_reference_value(vector, value);
34
+ return DuckDBVector_1.DuckDBVector.create(vector, 1).getItem(0);
35
+ }
@@ -12,8 +12,11 @@ function typeForValue(value) {
12
12
  case 'boolean':
13
13
  return DuckDBType_1.BOOLEAN;
14
14
  case 'number':
15
- if (Math.round(value) === value) {
16
- return DuckDBType_1.INTEGER;
15
+ if (Number.isInteger(value)) {
16
+ if (value >= -2_147_483_648 && value <= 2_147_483_647) {
17
+ return DuckDBType_1.INTEGER;
18
+ }
19
+ return DuckDBType_1.BIGINT;
17
20
  }
18
21
  else {
19
22
  return DuckDBType_1.DOUBLE;
@@ -45,6 +48,13 @@ function typeForValue(value) {
45
48
  return (0, DuckDBType_1.LIST)(typeForValue(value.items[0]));
46
49
  }
47
50
  else if (value instanceof values_1.DuckDBMapValue) {
51
+ if (value.entries.length === 0) {
52
+ // An empty map carries no entries to infer key/value types from.
53
+ // Mirror the empty-list path (LIST(ANY)) so createValue raises the
54
+ // same "Cannot create maps with key type of ANY" error, instead of
55
+ // a cryptic "Cannot read properties of undefined (reading 'key')".
56
+ return (0, DuckDBType_1.MAP)(DuckDBType_1.ANY, DuckDBType_1.ANY);
57
+ }
48
58
  return (0, DuckDBType_1.MAP)(typeForValue(value.entries[0].key), typeForValue(value.entries[0].value));
49
59
  }
50
60
  else if (value instanceof values_1.DuckDBStructValue) {
@@ -66,6 +76,9 @@ function typeForValue(value) {
66
76
  else if (value instanceof values_1.DuckDBTimestampTZValue) {
67
77
  return DuckDBType_1.TIMESTAMPTZ;
68
78
  }
79
+ else if (value instanceof values_1.DuckDBTimestampTZNanosecondsValue) {
80
+ return DuckDBType_1.TIMESTAMPTZ_NS;
81
+ }
69
82
  else if (value instanceof values_1.DuckDBTimestampValue) {
70
83
  return DuckDBType_1.TIMESTAMP;
71
84
  }
@@ -78,12 +91,18 @@ function typeForValue(value) {
78
91
  else if (value instanceof values_1.DuckDBTimeNSValue) {
79
92
  return DuckDBType_1.TIME_NS;
80
93
  }
94
+ else if (value instanceof values_1.DuckDBTupleValue) {
95
+ return (0, DuckDBType_1.TUPLE)(value.items.map(typeForValue));
96
+ }
81
97
  else if (value instanceof values_1.DuckDBUnionValue) {
82
98
  return (0, DuckDBType_1.UNION)({ [value.tag]: typeForValue(value.value) });
83
99
  }
84
100
  else if (value instanceof values_1.DuckDBUUIDValue) {
85
101
  return DuckDBType_1.UUID;
86
102
  }
103
+ else if (value instanceof values_1.DuckDBVariantValue) {
104
+ return DuckDBType_1.VARIANT;
105
+ }
87
106
  break;
88
107
  }
89
108
  }
@@ -0,0 +1,11 @@
1
+ import { DuckDBLogicalType } from '../DuckDBLogicalType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ import { Json } from '../Json';
4
+ export declare abstract class BaseDuckDBType<T extends DuckDBTypeId> {
5
+ readonly typeId: T;
6
+ readonly alias?: string;
7
+ protected constructor(typeId: T, alias?: string);
8
+ toString(): string;
9
+ toLogicalType(): DuckDBLogicalType;
10
+ toJson(): Json;
11
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseDuckDBType = void 0;
7
+ const node_bindings_1 = __importDefault(require("@haybarn/node-bindings"));
8
+ const DuckDBLogicalType_1 = require("../DuckDBLogicalType");
9
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
10
+ class BaseDuckDBType {
11
+ typeId;
12
+ alias;
13
+ constructor(typeId, alias) {
14
+ this.typeId = typeId;
15
+ this.alias = alias;
16
+ }
17
+ toString() {
18
+ return DuckDBTypeId_1.DuckDBTypeId[this.typeId];
19
+ }
20
+ toLogicalType() {
21
+ const logicalType = DuckDBLogicalType_1.DuckDBLogicalType.create(node_bindings_1.default.create_logical_type(this.typeId));
22
+ if (this.alias) {
23
+ logicalType.alias = this.alias;
24
+ }
25
+ return logicalType;
26
+ }
27
+ toJson() {
28
+ return {
29
+ typeId: this.typeId,
30
+ ...(this.alias ? { alias: this.alias } : {}),
31
+ };
32
+ }
33
+ }
34
+ exports.BaseDuckDBType = BaseDuckDBType;
@@ -0,0 +1,8 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBAnyType extends BaseDuckDBType<DuckDBTypeId.ANY> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBAnyType;
6
+ static create(alias?: string): DuckDBAnyType;
7
+ }
8
+ export declare const ANY: DuckDBAnyType;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ANY = exports.DuckDBAnyType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBAnyType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.ANY, alias);
9
+ }
10
+ static instance = new DuckDBAnyType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBAnyType(alias) : DuckDBAnyType.instance;
13
+ }
14
+ }
15
+ exports.DuckDBAnyType = DuckDBAnyType;
16
+ exports.ANY = DuckDBAnyType.instance;
@@ -0,0 +1,14 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import type { DuckDBType } from './DuckDBType';
3
+ import { DuckDBLogicalType } from '../DuckDBLogicalType';
4
+ import { DuckDBTypeId } from '../DuckDBTypeId';
5
+ import { Json } from '../Json';
6
+ export declare class DuckDBArrayType extends BaseDuckDBType<DuckDBTypeId.ARRAY> {
7
+ readonly valueType: DuckDBType;
8
+ readonly length: number;
9
+ constructor(valueType: DuckDBType, length: number, alias?: string);
10
+ toString(): string;
11
+ toLogicalType(): DuckDBLogicalType;
12
+ toJson(): Json;
13
+ }
14
+ export declare function ARRAY(valueType: DuckDBType, length: number, alias?: string): DuckDBArrayType;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DuckDBArrayType = void 0;
4
+ exports.ARRAY = ARRAY;
5
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
6
+ const DuckDBLogicalType_1 = require("../DuckDBLogicalType");
7
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
8
+ class DuckDBArrayType extends BaseDuckDBType_1.BaseDuckDBType {
9
+ valueType;
10
+ length;
11
+ constructor(valueType, length, alias) {
12
+ super(DuckDBTypeId_1.DuckDBTypeId.ARRAY, alias);
13
+ this.valueType = valueType;
14
+ this.length = length;
15
+ }
16
+ toString() {
17
+ return `${this.valueType}[${this.length}]`;
18
+ }
19
+ toLogicalType() {
20
+ const logicalType = DuckDBLogicalType_1.DuckDBLogicalType.createArray(this.valueType.toLogicalType(), this.length);
21
+ if (this.alias) {
22
+ logicalType.alias = this.alias;
23
+ }
24
+ return logicalType;
25
+ }
26
+ toJson() {
27
+ return {
28
+ typeId: this.typeId,
29
+ valueType: this.valueType.toJson(),
30
+ length: this.length,
31
+ ...(this.alias ? { alias: this.alias } : {}),
32
+ };
33
+ }
34
+ }
35
+ exports.DuckDBArrayType = DuckDBArrayType;
36
+ function ARRAY(valueType, length, alias) {
37
+ return new DuckDBArrayType(valueType, length, alias);
38
+ }
@@ -0,0 +1,12 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBBigIntType extends BaseDuckDBType<DuckDBTypeId.BIGINT> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBBigIntType;
6
+ static create(alias?: string): DuckDBBigIntType;
7
+ static readonly Max: bigint;
8
+ static readonly Min: bigint;
9
+ get max(): bigint;
10
+ get min(): bigint;
11
+ }
12
+ export declare const BIGINT: DuckDBBigIntType;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BIGINT = exports.DuckDBBigIntType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBBigIntType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.BIGINT, alias);
9
+ }
10
+ static instance = new DuckDBBigIntType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBBigIntType(alias) : DuckDBBigIntType.instance;
13
+ }
14
+ static Max = 2n ** 63n - 1n;
15
+ static Min = -(2n ** 63n);
16
+ get max() {
17
+ return DuckDBBigIntType.Max;
18
+ }
19
+ get min() {
20
+ return DuckDBBigIntType.Min;
21
+ }
22
+ }
23
+ exports.DuckDBBigIntType = DuckDBBigIntType;
24
+ exports.BIGINT = DuckDBBigIntType.instance;
@@ -0,0 +1,12 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBBigNumType extends BaseDuckDBType<DuckDBTypeId.BIGNUM> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBBigNumType;
6
+ static create(alias?: string): DuckDBBigNumType;
7
+ static readonly Max: bigint;
8
+ static readonly Min: bigint;
9
+ get max(): bigint;
10
+ get min(): bigint;
11
+ }
12
+ export declare const BIGNUM: DuckDBBigNumType;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BIGNUM = exports.DuckDBBigNumType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBBigNumType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.BIGNUM, alias);
9
+ }
10
+ static instance = new DuckDBBigNumType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBBigNumType(alias) : DuckDBBigNumType.instance;
13
+ }
14
+ static Max = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n;
15
+ static Min = -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368n;
16
+ get max() {
17
+ return DuckDBBigNumType.Max;
18
+ }
19
+ get min() {
20
+ return DuckDBBigNumType.Min;
21
+ }
22
+ }
23
+ exports.DuckDBBigNumType = DuckDBBigNumType;
24
+ exports.BIGNUM = DuckDBBigNumType.instance;
@@ -0,0 +1,8 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBBitType extends BaseDuckDBType<DuckDBTypeId.BIT> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBBitType;
6
+ static create(alias?: string): DuckDBBitType;
7
+ }
8
+ export declare const BIT: DuckDBBitType;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BIT = exports.DuckDBBitType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBBitType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.BIT, alias);
9
+ }
10
+ static instance = new DuckDBBitType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBBitType(alias) : DuckDBBitType.instance;
13
+ }
14
+ }
15
+ exports.DuckDBBitType = DuckDBBitType;
16
+ exports.BIT = DuckDBBitType.instance;
@@ -0,0 +1,8 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBBlobType extends BaseDuckDBType<DuckDBTypeId.BLOB> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBBlobType;
6
+ static create(alias?: string): DuckDBBlobType;
7
+ }
8
+ export declare const BLOB: DuckDBBlobType;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BLOB = exports.DuckDBBlobType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBBlobType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.BLOB, alias);
9
+ }
10
+ static instance = new DuckDBBlobType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBBlobType(alias) : DuckDBBlobType.instance;
13
+ }
14
+ }
15
+ exports.DuckDBBlobType = DuckDBBlobType;
16
+ exports.BLOB = DuckDBBlobType.instance;
@@ -0,0 +1,8 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBBooleanType extends BaseDuckDBType<DuckDBTypeId.BOOLEAN> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBBooleanType;
6
+ static create(alias?: string): DuckDBBooleanType;
7
+ }
8
+ export declare const BOOLEAN: DuckDBBooleanType;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BOOLEAN = exports.DuckDBBooleanType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBBooleanType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.BOOLEAN, alias);
9
+ }
10
+ static instance = new DuckDBBooleanType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBBooleanType(alias) : DuckDBBooleanType.instance;
13
+ }
14
+ }
15
+ exports.DuckDBBooleanType = DuckDBBooleanType;
16
+ exports.BOOLEAN = DuckDBBooleanType.instance;
@@ -0,0 +1,14 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ import { DuckDBDateValue } from '../values';
4
+ export declare class DuckDBDateType extends BaseDuckDBType<DuckDBTypeId.DATE> {
5
+ constructor(alias?: string);
6
+ static readonly instance: DuckDBDateType;
7
+ static create(alias?: string): DuckDBDateType;
8
+ get epoch(): DuckDBDateValue;
9
+ get max(): DuckDBDateValue;
10
+ get min(): DuckDBDateValue;
11
+ get posInf(): DuckDBDateValue;
12
+ get negInf(): DuckDBDateValue;
13
+ }
14
+ export declare const DATE: DuckDBDateType;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DATE = exports.DuckDBDateType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ const values_1 = require("../values");
7
+ class DuckDBDateType extends BaseDuckDBType_1.BaseDuckDBType {
8
+ constructor(alias) {
9
+ super(DuckDBTypeId_1.DuckDBTypeId.DATE, alias);
10
+ }
11
+ static instance = new DuckDBDateType();
12
+ static create(alias) {
13
+ return alias ? new DuckDBDateType(alias) : DuckDBDateType.instance;
14
+ }
15
+ get epoch() {
16
+ return values_1.DuckDBDateValue.Epoch;
17
+ }
18
+ get max() {
19
+ return values_1.DuckDBDateValue.Max;
20
+ }
21
+ get min() {
22
+ return values_1.DuckDBDateValue.Min;
23
+ }
24
+ get posInf() {
25
+ return values_1.DuckDBDateValue.PosInf;
26
+ }
27
+ get negInf() {
28
+ return values_1.DuckDBDateValue.NegInf;
29
+ }
30
+ }
31
+ exports.DuckDBDateType = DuckDBDateType;
32
+ exports.DATE = DuckDBDateType.instance;
@@ -0,0 +1,14 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBLogicalType } from '../DuckDBLogicalType';
3
+ import { DuckDBTypeId } from '../DuckDBTypeId';
4
+ import { Json } from '../Json';
5
+ export declare class DuckDBDecimalType extends BaseDuckDBType<DuckDBTypeId.DECIMAL> {
6
+ readonly width: number;
7
+ readonly scale: number;
8
+ constructor(width: number, scale: number, alias?: string);
9
+ toString(): string;
10
+ toLogicalType(): DuckDBLogicalType;
11
+ toJson(): Json;
12
+ static readonly default: DuckDBDecimalType;
13
+ }
14
+ export declare function DECIMAL(width?: number, scale?: number, alias?: string): DuckDBDecimalType;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DuckDBDecimalType = void 0;
4
+ exports.DECIMAL = DECIMAL;
5
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
6
+ const DuckDBLogicalType_1 = require("../DuckDBLogicalType");
7
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
8
+ class DuckDBDecimalType extends BaseDuckDBType_1.BaseDuckDBType {
9
+ width;
10
+ scale;
11
+ constructor(width, scale, alias) {
12
+ super(DuckDBTypeId_1.DuckDBTypeId.DECIMAL, alias);
13
+ this.width = width;
14
+ this.scale = scale;
15
+ }
16
+ toString() {
17
+ return `DECIMAL(${this.width},${this.scale})`;
18
+ }
19
+ toLogicalType() {
20
+ const logicalType = DuckDBLogicalType_1.DuckDBLogicalType.createDecimal(this.width, this.scale);
21
+ if (this.alias) {
22
+ logicalType.alias = this.alias;
23
+ }
24
+ return logicalType;
25
+ }
26
+ toJson() {
27
+ return {
28
+ typeId: this.typeId,
29
+ width: this.width,
30
+ scale: this.scale,
31
+ ...(this.alias ? { alias: this.alias } : {}),
32
+ };
33
+ }
34
+ static default = new DuckDBDecimalType(18, 3);
35
+ }
36
+ exports.DuckDBDecimalType = DuckDBDecimalType;
37
+ function DECIMAL(width, scale, alias) {
38
+ if (width === undefined) {
39
+ return DuckDBDecimalType.default;
40
+ }
41
+ if (scale === undefined) {
42
+ return new DuckDBDecimalType(width, 0);
43
+ }
44
+ return new DuckDBDecimalType(width, scale, alias);
45
+ }
@@ -0,0 +1,12 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBTypeId } from '../DuckDBTypeId';
3
+ export declare class DuckDBDoubleType extends BaseDuckDBType<DuckDBTypeId.DOUBLE> {
4
+ constructor(alias?: string);
5
+ static readonly instance: DuckDBDoubleType;
6
+ static create(alias?: string): DuckDBDoubleType;
7
+ static readonly Max: number;
8
+ static readonly Min: number;
9
+ get max(): number;
10
+ get min(): number;
11
+ }
12
+ export declare const DOUBLE: DuckDBDoubleType;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DOUBLE = exports.DuckDBDoubleType = void 0;
4
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
5
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
6
+ class DuckDBDoubleType extends BaseDuckDBType_1.BaseDuckDBType {
7
+ constructor(alias) {
8
+ super(DuckDBTypeId_1.DuckDBTypeId.DOUBLE, alias);
9
+ }
10
+ static instance = new DuckDBDoubleType();
11
+ static create(alias) {
12
+ return alias ? new DuckDBDoubleType(alias) : DuckDBDoubleType.instance;
13
+ }
14
+ static Max = Number.MAX_VALUE;
15
+ static Min = -Number.MAX_VALUE;
16
+ get max() {
17
+ return DuckDBDoubleType.Max;
18
+ }
19
+ get min() {
20
+ return DuckDBDoubleType.Min;
21
+ }
22
+ }
23
+ exports.DuckDBDoubleType = DuckDBDoubleType;
24
+ exports.DOUBLE = DuckDBDoubleType.instance;
@@ -0,0 +1,29 @@
1
+ import { BaseDuckDBType } from './BaseDuckDBType';
2
+ import { DuckDBLogicalType } from '../DuckDBLogicalType';
3
+ import { DuckDBTypeId } from '../DuckDBTypeId';
4
+ import { Json } from '../Json';
5
+ export declare class DuckDBEnumType extends BaseDuckDBType<DuckDBTypeId.ENUM> {
6
+ readonly values: readonly string[];
7
+ readonly valueIndexes: Readonly<Record<string, number>>;
8
+ readonly internalTypeId: DuckDBTypeId;
9
+ constructor(values: readonly string[], internalTypeId: DuckDBTypeId, alias?: string);
10
+ /**
11
+ * The index of the given value in this enum.
12
+ *
13
+ * Throws if the value is not a member: storing an unknown value would
14
+ * otherwise write index 0, silently substituting the enum's first member.
15
+ */
16
+ indexForValue(value: string): number;
17
+ /**
18
+ * Like toString, but bounded: an enum can have millions of members, and all of
19
+ * them in an error message would bury the part that matters.
20
+ */
21
+ private toStringForError;
22
+ toString(): string;
23
+ toLogicalType(): DuckDBLogicalType;
24
+ toJson(): Json;
25
+ }
26
+ export declare function ENUM8(values: readonly string[], alias?: string): DuckDBEnumType;
27
+ export declare function ENUM16(values: readonly string[], alias?: string): DuckDBEnumType;
28
+ export declare function ENUM32(values: readonly string[], alias?: string): DuckDBEnumType;
29
+ export declare function ENUM(values: readonly string[], alias?: string): DuckDBEnumType;
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DuckDBEnumType = void 0;
4
+ exports.ENUM8 = ENUM8;
5
+ exports.ENUM16 = ENUM16;
6
+ exports.ENUM32 = ENUM32;
7
+ exports.ENUM = ENUM;
8
+ const BaseDuckDBType_1 = require("./BaseDuckDBType");
9
+ const DuckDBLogicalType_1 = require("../DuckDBLogicalType");
10
+ const DuckDBTypeId_1 = require("../DuckDBTypeId");
11
+ const sql_1 = require("../sql");
12
+ class DuckDBEnumType extends BaseDuckDBType_1.BaseDuckDBType {
13
+ values;
14
+ valueIndexes;
15
+ internalTypeId;
16
+ constructor(values, internalTypeId, alias) {
17
+ super(DuckDBTypeId_1.DuckDBTypeId.ENUM, alias);
18
+ this.values = values;
19
+ // Null prototype: with a plain object literal, a lookup of an inherited key
20
+ // such as 'toString' or 'constructor' returns a function instead of
21
+ // undefined, so neither this map nor indexForValue could tell a member from
22
+ // an Object.prototype property.
23
+ const valueIndexes = Object.create(null);
24
+ for (let i = 0; i < values.length; i++) {
25
+ valueIndexes[values[i]] = i;
26
+ }
27
+ this.valueIndexes = valueIndexes;
28
+ this.internalTypeId = internalTypeId;
29
+ }
30
+ /**
31
+ * The index of the given value in this enum.
32
+ *
33
+ * Throws if the value is not a member: storing an unknown value would
34
+ * otherwise write index 0, silently substituting the enum's first member.
35
+ */
36
+ indexForValue(value) {
37
+ const index = this.valueIndexes[value];
38
+ if (index === undefined) {
39
+ throw new Error(`${(0, sql_1.quotedString)(value)} is not a member of ${this.toStringForError()}`);
40
+ }
41
+ return index;
42
+ }
43
+ /**
44
+ * Like toString, but bounded: an enum can have millions of members, and all of
45
+ * them in an error message would bury the part that matters.
46
+ */
47
+ toStringForError() {
48
+ if (this.alias) {
49
+ return this.alias;
50
+ }
51
+ const shown = 8;
52
+ if (this.values.length <= shown) {
53
+ return this.toString();
54
+ }
55
+ const listed = this.values.slice(0, shown).map(sql_1.quotedString).join(', ');
56
+ return `ENUM(${listed}, and ${this.values.length - shown} more)`;
57
+ }
58
+ toString() {
59
+ return `ENUM(${this.values.map(sql_1.quotedString).join(', ')})`;
60
+ }
61
+ toLogicalType() {
62
+ const logicalType = DuckDBLogicalType_1.DuckDBLogicalType.createEnum(this.values);
63
+ if (this.alias) {
64
+ logicalType.alias = this.alias;
65
+ }
66
+ return logicalType;
67
+ }
68
+ toJson() {
69
+ return {
70
+ typeId: this.typeId,
71
+ values: [...this.values],
72
+ internalTypeId: this.internalTypeId,
73
+ ...(this.alias ? { alias: this.alias } : {}),
74
+ };
75
+ }
76
+ }
77
+ exports.DuckDBEnumType = DuckDBEnumType;
78
+ function ENUM8(values, alias) {
79
+ return new DuckDBEnumType(values, DuckDBTypeId_1.DuckDBTypeId.UTINYINT, alias);
80
+ }
81
+ function ENUM16(values, alias) {
82
+ return new DuckDBEnumType(values, DuckDBTypeId_1.DuckDBTypeId.USMALLINT, alias);
83
+ }
84
+ function ENUM32(values, alias) {
85
+ return new DuckDBEnumType(values, DuckDBTypeId_1.DuckDBTypeId.UINTEGER, alias);
86
+ }
87
+ function ENUM(values, alias) {
88
+ if (values.length < 256) {
89
+ return ENUM8(values, alias);
90
+ }
91
+ else if (values.length < 65536) {
92
+ return ENUM16(values, alias);
93
+ }
94
+ else if (values.length < 4294967296) {
95
+ return ENUM32(values, alias);
96
+ }
97
+ else {
98
+ throw new Error(`ENUM types cannot have more than 4294967295 values; received ${values.length}`);
99
+ }
100
+ }