@aaqu/fromcubes-db 0.0.0-stage → 0.1.0

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 (54) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +24 -0
  3. package/README.md +442 -2
  4. package/SECURITY.md +75 -0
  5. package/THIRD-PARTY-NOTICES.md +151 -0
  6. package/examples/Quick Start/All Databases.json +1719 -0
  7. package/examples/Quick Start/ClickHouse.json +278 -0
  8. package/examples/Quick Start/CockroachDB.json +280 -0
  9. package/examples/Quick Start/Firebird.json +278 -0
  10. package/examples/Quick Start/MariaDB.json +281 -0
  11. package/examples/Quick Start/MySQL.json +281 -0
  12. package/examples/Quick Start/Oracle.json +280 -0
  13. package/examples/Quick Start/PostgreSQL.json +280 -0
  14. package/examples/Quick Start/SQL Server.json +282 -0
  15. package/examples/Quick Start/SQLite.json +278 -0
  16. package/examples/Quick Start/TiDB.json +281 -0
  17. package/examples/Quick Start/YugabyteDB.json +280 -0
  18. package/examples/SQLite Sensor Log With Portal.json +391 -0
  19. package/examples/Select With Parameters.json +130 -0
  20. package/index.js +32 -0
  21. package/lib/adapters/clickhouse.js +254 -0
  22. package/lib/adapters/firebird.js +416 -0
  23. package/lib/adapters/index.js +37 -0
  24. package/lib/adapters/mssql.js +178 -0
  25. package/lib/adapters/mysql.js +171 -0
  26. package/lib/adapters/oracle.js +272 -0
  27. package/lib/adapters/postgres.js +161 -0
  28. package/lib/adapters/read.js +57 -0
  29. package/lib/adapters/sqlite.js +193 -0
  30. package/lib/adapters/time.js +89 -0
  31. package/lib/bound.js +29 -0
  32. package/lib/editor-api.js +134 -0
  33. package/lib/editor-preview.js +191 -0
  34. package/lib/errors.js +66 -0
  35. package/lib/params.js +966 -0
  36. package/lib/pool.js +391 -0
  37. package/lib/settings.js +302 -0
  38. package/lib/sql/compile.js +160 -0
  39. package/lib/sql/dialect.js +194 -0
  40. package/lib/sql/parse.js +202 -0
  41. package/lib/sql/types.js +1071 -0
  42. package/lib/sqlite-files.js +137 -0
  43. package/nodes/connection.html +728 -0
  44. package/nodes/connection.js +179 -0
  45. package/nodes/editor/ark-pixel.LICENSE.txt +96 -0
  46. package/nodes/editor/ark-pixel.woff2 +0 -0
  47. package/nodes/editor/mark.svg +5 -0
  48. package/nodes/editor/sql-editor.css +604 -0
  49. package/nodes/editor/sql-editor.js +1490 -0
  50. package/nodes/editor/sql-model.js +203 -0
  51. package/nodes/icons/fromcubes-db.svg +24 -0
  52. package/nodes/query.html +662 -0
  53. package/nodes/query.js +195 -0
  54. package/package.json +81 -4
@@ -0,0 +1,1071 @@
1
+ "use strict";
2
+
3
+ const { byDatabase } = require("./dialect");
4
+
5
+ /**
6
+ * The column types each database offers, with the limits its own reference
7
+ * gives. `docs/column-types.md` is the source material: every number here can
8
+ * be traced back to a row in that file, and the page it came from is named
9
+ * there rather than repeated in a comment on each line.
10
+ *
11
+ * This is what turns "expected a string" into "expected at most 50 characters",
12
+ * and what lets a typo like `varchar2` on PostgreSQL fail in the editor's own
13
+ * error rather than as a driver error about a column nobody mentioned.
14
+ *
15
+ * Read it through `byDatabase`, never by indexing directly: that is what lets
16
+ * MariaDB carry its own limits while still living on the MySQL engine.
17
+ *
18
+ * @typedef {Object} ColumnType
19
+ * @property {string} category which JS value the parameter has to be —
20
+ * string|integer|number|boolean|date|binary|json|other
21
+ * @property {'none'|'length'|'precision'|'type'} args what the parentheses hold ('type': a type or list the server reads, ClickHouse's Array(Int32))
22
+ * @property {number} [maxLength] largest n the database accepts in `type(n)`
23
+ * @property {'chars'|'bytes'|'utf16units'} [lengthIn] what n counts
24
+ * @property {boolean} [allowsMax] `type(max)` is legal (SQL Server only)
25
+ * @property {bigint} [min] smallest value, for integers
26
+ * @property {bigint} [max] largest value, for integers
27
+ * @property {number} [maxPrecision]
28
+ * @property {number} [minScale]
29
+ * @property {number} [maxScale]
30
+ * @property {string[]} [aliases] other spellings of the same type
31
+ */
32
+
33
+ const CATEGORIES = [
34
+ "string",
35
+ "integer",
36
+ "number",
37
+ "boolean",
38
+ "date",
39
+ "binary",
40
+ "json",
41
+ // A list of numbers for a VECTOR column: an array is the value itself.
42
+ "vector",
43
+ // Text the database parses itself — a time of day, an interval, a bit string.
44
+ // Checked for shape and handed to the driver exactly as it arrived.
45
+ "literal",
46
+ "other",
47
+ ];
48
+
49
+ /**
50
+ * Integer bounds are BigInt because the 64-bit ones are not representable as a
51
+ * double: `2**63 - 1` rounds up to 2^63 and would wave through the one value
52
+ * the check exists to catch.
53
+ */
54
+ const int = (min, max, aliases) => ({ category: "integer", args: "none", min, max, aliases });
55
+ /** MySQL and MariaDB only: the same type name means a different range with the
56
+ * `unsigned` modifier, so both ranges have to travel together. */
57
+ const uint = (min, max, unsignedMax, aliases) => ({
58
+ category: "integer",
59
+ args: "none",
60
+ min,
61
+ max,
62
+ unsignedMin: 0n,
63
+ unsignedMax,
64
+ aliases,
65
+ });
66
+ const dec = (maxPrecision, maxScale, minScale, aliases, extra) => ({
67
+ category: "number",
68
+ args: "precision",
69
+ maxPrecision,
70
+ maxScale,
71
+ minScale,
72
+ aliases,
73
+ scaleAtMostPrecision: true,
74
+ exact: true,
75
+ ...extra,
76
+ });
77
+ const text = (maxLength, lengthIn, aliases, extra) => ({
78
+ category: "string",
79
+ args: maxLength === undefined ? "none" : "length",
80
+ maxLength,
81
+ lengthIn,
82
+ aliases,
83
+ ...extra,
84
+ });
85
+ const bin = (maxLength, aliases, extra) => ({
86
+ category: "binary",
87
+ args: maxLength === undefined ? "none" : "length",
88
+ maxLength,
89
+ lengthIn: "bytes",
90
+ aliases,
91
+ ...extra,
92
+ });
93
+ const plain = (category, aliases) => ({ category, args: "none", aliases });
94
+ /** A type whose length is part of the type: `uuid` is 36 characters, always. */
95
+ const fixed = (category, maxLength, lengthIn, aliases) => ({
96
+ category,
97
+ args: "none",
98
+ maxLength,
99
+ lengthIn,
100
+ aliases,
101
+ });
102
+ // A timestamp that keeps more than milliseconds keeps a value written with more
103
+ // as text: a JS Date would silently drop the digits the column was asked for.
104
+ const stamp = (maxPrecision, aliases, extra) => ({
105
+ category: "date",
106
+ args: "precision",
107
+ maxPrecision,
108
+ aliases,
109
+ keepsText: maxPrecision > 3,
110
+ ...extra,
111
+ });
112
+ /** A vector of n numbers. The JS value is an array; how it binds is the adapter's business. */
113
+ const vec = (maxLength, extra) => ({
114
+ category: "vector",
115
+ args: "length",
116
+ lengthIn: "elements",
117
+ maxLength,
118
+ minLength: 1,
119
+ ...extra,
120
+ });
121
+ /** Range and multirange types: text the server parses, checked for its brackets only. */
122
+ const RANGE = /^\s*(?:empty|[[(][^,]*,[^,]*[\])])\s*$/i;
123
+ const MULTIRANGE = /^\s*\{[\s\S]*\}\s*$/;
124
+ const range = () => ({ category: "other", args: "none", pattern: RANGE, patternName: "a range such as [1,10)" });
125
+ const multirange = () => ({
126
+ category: "other",
127
+ args: "none",
128
+ pattern: MULTIRANGE,
129
+ patternName: "a multirange such as {[1,3),[5,7)}",
130
+ });
131
+ const FLOAT32_MAX = 3.4028234663852886e38;
132
+ /** A value the database reads as text and we only check the shape of: a time of
133
+ * day, an interval, a bit string. Passed to the driver exactly as it arrived. */
134
+ const literal = (pattern, patternName, extra) => ({
135
+ category: "literal",
136
+ args: "none",
137
+ pattern,
138
+ patternName,
139
+ ...extra,
140
+ });
141
+
142
+ // Shapes shared by more than one product. Each is sourced in docs/column-types.md.
143
+ const UUID_HEX = /^[0-9a-f]{32}$/i;
144
+ const BITS = /^[01]*$/;
145
+ const MACADDR6 = /^(?:[0-9a-f]{2}([:.-]?)){5}[0-9a-f]{2}$|^[0-9a-f]{12}$/i;
146
+ const MACADDR8 =
147
+ /^(?:[0-9a-f]{2}([:.-]?)){7}[0-9a-f]{2}$|^[0-9a-f]{16}$|^(?:[0-9a-f]{2}([:.-]?)){5}[0-9a-f]{2}$|^[0-9a-f]{12}$/i;
148
+ // 24:00:00 is a real time of day on PostgreSQL and CockroachDB.
149
+ const TIME_OF_DAY = /^(?:(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d{1,6})?)?|24:00(?::00(?:\.0+)?)?)$/;
150
+ const TIME_OF_DAY_TZ =
151
+ /^(?:(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d{1,6})?)?|24:00(?::00(?:\.0+)?)?)(?:[+-]\d{2}(?::?\d{2})?|Z)?$/;
152
+ // ISO 8601 duration, or the "1 day 2 hours" form, or a bare clock interval.
153
+ const PG_INTERVAL =
154
+ /^[-+]?(?:P(?!$)(?:\d+(?:\.\d+)?[YMWD])*(?:T(?!$)(?:\d+(?:\.\d+)?[HMS])*)?|(?:\s*[-+]?\d+(?:\.\d+)?\s*(?:microsecond|millisecond|second|minute|hour|day|week|month|year|decade|century|centuries|millennium|millennia)s?)+(?:\s+[-+]?\d{1,2}:\d{2}(?::\d{2}(?:\.\d{1,6})?)?)?(?:\s+ago)?|\d{1,9}:\d{2}(?::\d{2}(?:\.\d{1,6})?)?)$/i;
155
+ // numeric and the float types accept these spellings as values in their own right.
156
+ const NUMERIC_SPECIALS = ["NaN", "Infinity", "-Infinity", "inf", "-inf"];
157
+ // The manual's table says 4713 BC, but the server takes everything from Julian
158
+ // day zero, 4714-11-24 BC (astronomical -4713) — measured by the contract
159
+ // suite, which also checks that the day before is refused.
160
+ const PG_EPOCH_START = "-004713-11-24T00:00:00.000Z";
161
+ // SQL Server's date, datetime2 and datetimeoffset all run 0001 to 9999.
162
+ // Oracle: January 1, 4712 BC to December 31, 9999 AD (Table 2-1). 4712 BC is
163
+ // astronomical year -4711 — there is no year zero — which the probe of the
164
+ // real server confirmed by refusing -4712.
165
+ const ORACLE_START = "-004711-01-01T00:00:00.000Z";
166
+ const ORACLE_END = "9999-12-31T23:59:59.999Z";
167
+ // CockroachDB's own reference: 4714-11-24 BC (Julian day zero) for both DATE
168
+ // and TIMESTAMP — astronomical year -4713; the server refuses a day earlier.
169
+ const CRDB_EPOCH_START = "-004713-11-24T00:00:00.000Z";
170
+ // The SQL standard form '1-2 3 4:5:6' as well as the two PostgreSQL ones.
171
+ const CRDB_INTERVAL =
172
+ /^[-+]?(?:P(?!$)(?:\d+(?:\.\d+)?[YMWD])*(?:T(?!$)(?:\d+(?:\.\d+)?[HMS])*)?|\d+-\d+(?:\s+\d+)?(?:\s+\d{1,2}:\d{1,2}(?::\d{1,2}(?:\.\d+)?)?)?|(?:\s*[-+]?\d+(?:\.\d+)?\s*(?:microsecond|us|millisecond|ms|second|sec|s|minute|min|m|hour|hr|h|day|d|week|w|month|mon|year|yr)s?)+(?:\s+\d{1,2}:\d{1,2}(?::\d{1,2}(?:\.\d+)?)?)?|\d{1,9}:\d{1,2}(?::\d{1,2}(?:\.\d+)?)?)$/i;
173
+ const MSSQL_YEAR_ONE = "0001-01-01T00:00:00.000Z";
174
+ const MSSQL_YEAR_END = "9999-12-31T23:59:59.999Z";
175
+ // Unlike a uuid, SQL Server writes the hyphens or not at all.
176
+ const MSSQL_TIME = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d{1,7})?)?$/;
177
+ const GUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
178
+ // MySQL and MariaDB share these; TIMESTAMP is narrower and lives per product.
179
+ const MYSQL_DATE_START = "1000-01-01T00:00:00.000Z";
180
+ const MYSQL_DATE_END = "9999-12-31T23:59:59.999Z";
181
+ const MYSQL_TIME = /^[-+]?\d{1,3}:[0-5]\d:[0-5]\d(?:\.\d{1,6})?$/;
182
+
183
+ const INT16 = [-32768n, 32767n];
184
+ const INT32 = [-2147483648n, 2147483647n];
185
+ const INT64 = [-9223372036854775808n, 9223372036854775807n];
186
+
187
+ /** @type {Record<string, Record<string, ColumnType>>} */
188
+ const TYPES = {
189
+ postgres: {
190
+ // n counts characters here, not bytes — the one thing most length checks
191
+ // get wrong, and the reason `lengthIn` exists at all.
192
+ varchar: text(10485760, "chars", ["character varying"], { unboundedWhenBare: true }),
193
+ // Bare `char` is `char(1)`; bare `varchar` is unlimited. The two bare forms
194
+ // mean opposite things, which is why the default is a field of its own and
195
+ // not the type's ceiling reused.
196
+ char: text(10485760, "chars", ["character", "bpchar"], { defaultLength: 1 }),
197
+ text: text(),
198
+ smallint: int(...INT16, ["int2"]),
199
+ integer: int(...INT32, ["int", "int4"]),
200
+ bigint: int(...INT64, ["int8"]),
201
+ // A serial column is an int with a sequence attached to its default. The
202
+ // sequence starts at 1; the column itself takes the whole signed range.
203
+ smallserial: int(...INT16, ["serial2"]),
204
+ serial: int(...INT32, ["serial4"]),
205
+ bigserial: int(...INT64, ["serial8"]),
206
+ numeric: dec(1000, 1000, -1000, ["decimal"], { exact: true, special: NUMERIC_SPECIALS }),
207
+ real: { ...plain("number", ["float4"]), maxAbs: 3.4028235e38, special: NUMERIC_SPECIALS },
208
+ "double precision": {
209
+ ...plain("number", ["float8"]),
210
+ maxAbs: 1.7976931348623157e308,
211
+ special: NUMERIC_SPECIALS,
212
+ },
213
+ boolean: plain("boolean", ["bool"]),
214
+ date: { ...plain("date"), minDate: PG_EPOCH_START },
215
+ // A time of day and an interval are not points in time: `new Date('10:30')`
216
+ // is either invalid or, worse, silently a different value. They are read as
217
+ // literals and handed to the driver untouched.
218
+ time: literal(TIME_OF_DAY, "a time of day", {
219
+ args: "precision",
220
+ maxPrecision: 6,
221
+ aliases: ["time without time zone"],
222
+ }),
223
+ "time with time zone": literal(TIME_OF_DAY_TZ, "a time of day", {
224
+ args: "precision",
225
+ maxPrecision: 6,
226
+ aliases: ["timetz"],
227
+ }),
228
+ timestamp: stamp(6, ["timestamp without time zone"], { minDate: PG_EPOCH_START }),
229
+ "timestamp with time zone": stamp(6, ["timestamptz"], { minDate: PG_EPOCH_START }),
230
+ interval: literal(PG_INTERVAL, "an interval", { args: "precision", maxPrecision: 6 }),
231
+ bytea: bin(),
232
+ json: plain("json"),
233
+ jsonb: plain("json"),
234
+ uuid: {
235
+ category: "string",
236
+ args: "none",
237
+ normalise: "uuid",
238
+ pattern: UUID_HEX,
239
+ patternName: "a uuid",
240
+ },
241
+ xml: text(),
242
+ money: {
243
+ category: "other",
244
+ args: "none",
245
+ check: "money",
246
+ min: "-92233720368547758.08",
247
+ max: "92233720368547758.07",
248
+ },
249
+ inet: { category: "other", args: "none", check: "inet" },
250
+ cidr: { category: "other", args: "none", check: "cidr" },
251
+ macaddr: { category: "other", args: "none", pattern: MACADDR6, patternName: "a MAC address" },
252
+ macaddr8: {
253
+ category: "other",
254
+ args: "none",
255
+ pattern: MACADDR8,
256
+ patternName: "an 8-byte MAC address",
257
+ },
258
+ // VARBITMAXLEN in PostgreSQL's own source; the manual states no maximum.
259
+ bit: {
260
+ category: "other",
261
+ args: "length",
262
+ maxLength: 83886080,
263
+ lengthIn: "chars",
264
+ defaultLength: 1,
265
+ exactLength: true,
266
+ pattern: BITS,
267
+ patternName: "a bit string",
268
+ },
269
+ "bit varying": {
270
+ category: "other",
271
+ args: "length",
272
+ maxLength: 83886080,
273
+ lengthIn: "chars",
274
+ aliases: ["varbit"],
275
+ pattern: BITS,
276
+ patternName: "a bit string",
277
+ },
278
+ tsvector: plain("other"),
279
+ tsquery: plain("other"),
280
+ point: plain("other"),
281
+ line: plain("other"),
282
+ lseg: plain("other"),
283
+ box: plain("other"),
284
+ path: plain("other"),
285
+ polygon: plain("other"),
286
+ circle: plain("other"),
287
+ pg_lsn: plain("other"),
288
+ jsonpath: plain("other"),
289
+ int4range: range(),
290
+ int8range: range(),
291
+ numrange: range(),
292
+ tsrange: range(),
293
+ tstzrange: range(),
294
+ daterange: range(),
295
+ int4multirange: multirange(),
296
+ int8multirange: multirange(),
297
+ nummultirange: multirange(),
298
+ tsmultirange: multirange(),
299
+ tstzmultirange: multirange(),
300
+ datemultirange: multirange(),
301
+ },
302
+
303
+ mysql: {
304
+ tinyint: { ...uint(-128n, 127n, 255n), displayWidth: true },
305
+ bool: plain("boolean", ["boolean"]), // BOOL is TINYINT(1), but a flow sends true/false
306
+ smallint: { ...uint(...INT16, 65535n), displayWidth: true },
307
+ mediumint: { ...uint(-8388608n, 8388607n, 16777215n), displayWidth: true },
308
+ int: { ...uint(...INT32, 4294967295n, ["integer"]), displayWidth: true },
309
+ bigint: { ...uint(...INT64, 18446744073709551615n), displayWidth: true },
310
+ decimal: dec(65, 30, 0, ["numeric", "dec", "fixed"]),
311
+ float: { ...plain("number"), maxAbs: 3.402823466e38 },
312
+ double: { ...plain("number", ["real", "double precision"]), maxAbs: 1.7976931348623157e308 },
313
+ bit: {
314
+ category: "other",
315
+ args: "length",
316
+ maxLength: 64,
317
+ minLength: 1,
318
+ lengthIn: "chars",
319
+ defaultLength: 1,
320
+ pattern: BITS,
321
+ patternName: "a bit string",
322
+ allowsNumber: true,
323
+ },
324
+ char: text(255, "chars", undefined, { defaultLength: 1 }),
325
+ varchar: text(65535, "chars"),
326
+ // The TEXT and BLOB families each have a documented ceiling; leaving them
327
+ // open let a 300 KB value pass as a TINYTEXT.
328
+ // Only TEXT and BLOB take an (M); the server rejects `tinytext(10)`
329
+ // outright, which the integration probe is what caught.
330
+ tinytext: fixed("string", 255, "chars"),
331
+ text: text(65535, "chars"),
332
+ mediumtext: fixed("string", 16777215, "chars"),
333
+ longtext: fixed("string", 4294967295, "chars"),
334
+ binary: bin(255, ["char byte"], { defaultLength: 1 }),
335
+ varbinary: bin(65535),
336
+ tinyblob: fixed("binary", 255, "bytes"),
337
+ blob: bin(65535),
338
+ mediumblob: fixed("binary", 16777215, "bytes"),
339
+ longblob: fixed("binary", 4294967295, "bytes"),
340
+ enum: { ...plain("string"), maxLength: 255, lengthIn: "chars" },
341
+ // A SET value is a comma-separated list, so an array is the value itself
342
+ // rather than a spread list — the same exception the json types make.
343
+ set: {
344
+ ...plain("string"),
345
+ maxLength: 255,
346
+ lengthIn: "chars",
347
+ joinsArrays: ",",
348
+ maxMembers: 64,
349
+ },
350
+ date: { ...plain("date"), minDate: MYSQL_DATE_START, maxDate: MYSQL_DATE_END },
351
+ // Both round a fraction they have no room for (precision 0 unless declared).
352
+ datetime: stamp(6, undefined, {
353
+ minDate: MYSQL_DATE_START,
354
+ maxDate: MYSQL_DATE_END,
355
+ roundsFraction: true,
356
+ defaultPrecision: 0,
357
+ }),
358
+ // A fifth of DATETIME's range, and the gap most likely to reach production.
359
+ timestamp: stamp(6, undefined, {
360
+ minDate: "1970-01-01T00:00:01.000Z",
361
+ maxDate: "2038-01-19T03:14:07.999Z",
362
+ roundsFraction: true,
363
+ defaultPrecision: 0,
364
+ }),
365
+ // TIME is a signed duration of up to ±838 hours, not a point in time.
366
+ time: literal(MYSQL_TIME, "a time of ±838 hours", {
367
+ args: "precision",
368
+ maxPrecision: 6,
369
+ maxSeconds: 3020399,
370
+ }),
371
+ year: { ...int(1901n, 2155n), alsoAllowed: [0n] },
372
+ json: plain("json"),
373
+ geometry: plain("other"),
374
+ point: plain("other"),
375
+ linestring: plain("other"),
376
+ polygon: plain("other"),
377
+ multipoint: plain("other"),
378
+ multilinestring: plain("other"),
379
+ multipolygon: plain("other"),
380
+ geometrycollection: plain("other", ["geomcollection"]),
381
+ // MySQL 9.0. Up to 16383 single-precision numbers.
382
+ vector: vec(16383, { maxAbs: FLOAT32_MAX, since: "9.0" }),
383
+ },
384
+
385
+ mssql: {
386
+ bit: plain("boolean"),
387
+ // Unsigned, unlike MySQL's TINYINT. Two products, one type name, different
388
+ // range — which is exactly why this table is per database.
389
+ tinyint: int(0n, 255n),
390
+ smallint: int(...INT16),
391
+ int: int(...INT32, ["integer"]),
392
+ bigint: int(...INT64),
393
+ decimal: dec(38, 38, 0, ["dec"], { defaultPrecision: 18, defaultScale: 0 }),
394
+ // Functionally identical to decimal, but a type of its own in sys.types
395
+ // (measured: a numeric column is stored as numeric), so not an alias.
396
+ numeric: dec(38, 38, 0, undefined, { defaultPrecision: 18, defaultScale: 0 }),
397
+ // The whole definition of these two is a fixed range, and neither end fits
398
+ // a double — so the bounds are text and the comparison is on the digits.
399
+ money: {
400
+ category: "other",
401
+ args: "none",
402
+ check: "money",
403
+ min: "-922337203685477.5808",
404
+ max: "922337203685477.5807",
405
+ },
406
+ smallmoney: {
407
+ category: "other",
408
+ args: "none",
409
+ check: "money",
410
+ min: "-214748.3648",
411
+ max: "214748.3647",
412
+ },
413
+ // n is a mantissa bit width, not a digit budget: read as digits, float(1)
414
+ // rejected 123.5 with a message about digits nobody had asked for.
415
+ float: {
416
+ category: "number",
417
+ args: "precision",
418
+ maxPrecision: 53,
419
+ precisionIn: "binary",
420
+ maxAbs: 1.79e308,
421
+ },
422
+ real: { ...plain("number"), maxAbs: 3.4e38 },
423
+ date: { ...plain("date"), minDate: MSSQL_YEAR_ONE, maxDate: MSSQL_YEAR_END },
424
+ // A time of day, not a point in time — the same reasoning as PostgreSQL's,
425
+ // with the seven fractional digits SQL Server keeps.
426
+ time: literal(MSSQL_TIME, "a time of day", { args: "precision", maxPrecision: 7 }),
427
+ datetime2: stamp(7, undefined, { minDate: MSSQL_YEAR_ONE, maxDate: MSSQL_YEAR_END }),
428
+ // Stores the offset it was given; a Date would keep only the instant.
429
+ datetimeoffset: stamp(7, undefined, { minDate: MSSQL_YEAR_ONE, maxDate: MSSQL_YEAR_END, keepsOffset: true }),
430
+ // Two ranges that catch real flows: datetime cannot hold 1752, and
431
+ // smalldatetime stops in 2079.
432
+ datetime: {
433
+ ...plain("date"),
434
+ minDate: "1753-01-01T00:00:00.000Z",
435
+ maxDate: "9999-12-31T23:59:59.997Z",
436
+ },
437
+ // Minutes only, rounded: 23:59:29.998 is the last value that stays on 6 June
438
+ // 2079, and the server refuses anything that rounds past it.
439
+ smalldatetime: {
440
+ ...plain("date"),
441
+ minDate: "1900-01-01T00:00:00.000Z",
442
+ maxDate: "2079-06-06T23:59:29.998Z",
443
+ },
444
+ // n is bytes here, and byte-pairs for the n-prefixed types. Microsoft's own
445
+ // reference says it outright: "n never defines numbers of characters".
446
+ char: text(8000, "bytes", ["character"]),
447
+ varchar: text(8000, "bytes", ["char varying", "character varying"], { allowsMax: true }),
448
+ text: text(),
449
+ nchar: text(4000, "utf16units", ["national char", "national character"]),
450
+ nvarchar: text(4000, "utf16units", ["national char varying", "national character varying"], {
451
+ allowsMax: true,
452
+ }),
453
+ ntext: text(),
454
+ sysname: fixed("string", 128, "utf16units"),
455
+ binary: bin(8000),
456
+ varbinary: bin(8000, undefined, { allowsMax: true }),
457
+ image: bin(),
458
+ uniqueidentifier: {
459
+ category: "string",
460
+ args: "none",
461
+ pattern: GUID,
462
+ patternName: "a uniqueidentifier",
463
+ },
464
+ xml: text(),
465
+ // SQL Server 2025 (17.x) added the native json and vector types.
466
+ json: { ...plain("json"), since: "17.0", sinceName: "2025" },
467
+ sql_variant: plain("other"),
468
+ rowversion: plain("other", ["timestamp"]),
469
+ hierarchyid: plain("other"),
470
+ geography: plain("other"),
471
+ geometry: plain("other"),
472
+ // SQL Server 2025: VECTOR(n) of float32 (float16 in preview), n up to 1998.
473
+ vector: vec(1998, { formats: ["float32", "float16"], maxAbs: FLOAT32_MAX, since: "17.0", sinceName: "2025" }),
474
+ },
475
+
476
+ oracle: {
477
+ // 4000 bytes on a standard database, 32767 with MAX_STRING_SIZE=EXTENDED.
478
+ // The looser bound is the right one to hold: it only bounds what the user
479
+ // may declare, and rejecting a legal extended column would be worse than
480
+ // letting the server give the error.
481
+ varchar2: text(32767, "bytes", ["varchar"], { defaultLength: 1 }),
482
+ // The same EXTENDED ceiling varchar2 and raw already hold, rather than the
483
+ // 4000 a standard database stops at.
484
+ nvarchar2: text(32767, "chars"),
485
+ char: text(2000, "bytes", undefined, { defaultLength: 1 }),
486
+ nchar: text(2000, "chars", undefined, { defaultLength: 1 }),
487
+ // Scale runs -84 to 127, and it may exceed the precision — the one product
488
+ // where a scale larger than the precision is legal.
489
+ number: dec(38, 127, -84, ["dec", "decimal", "numeric"], {
490
+ scaleAtMostPrecision: false,
491
+ maxAbs: 1e126,
492
+ magnitudeExclusive: true,
493
+ }),
494
+ // REAL is FLOAT(63) and DOUBLE PRECISION FLOAT(126): stored as FLOAT (measured).
495
+ float: {
496
+ category: "number",
497
+ args: "precision",
498
+ maxPrecision: 126,
499
+ precisionIn: "binary",
500
+ aliases: ["real", "double precision"],
501
+ },
502
+ binary_float: { ...plain("number"), maxAbs: 3.40282e38 },
503
+ binary_double: { ...plain("number"), maxAbs: 1.79769313486231e308 },
504
+ // INTEGER, INT and SMALLINT are all NUMBER(38) here — 38 digits wide, not
505
+ // 64 bits. SMALLINT being wider than a JS number is the cross-product trap.
506
+ integer: {
507
+ category: "integer",
508
+ args: "none",
509
+ aliases: ["int", "smallint"],
510
+ min: -99999999999999999999999999999999999999n,
511
+ max: 99999999999999999999999999999999999999n,
512
+ },
513
+ date: { ...plain("date"), minDate: ORACLE_START, maxDate: ORACLE_END },
514
+ timestamp: stamp(9, undefined, { minDate: ORACLE_START, maxDate: ORACLE_END, keepsText: true }),
515
+ "timestamp with time zone": stamp(9, undefined, {
516
+ minDate: ORACLE_START,
517
+ maxDate: ORACLE_END,
518
+ keepsText: true,
519
+ keepsOffset: true,
520
+ }),
521
+ "timestamp with local time zone": stamp(9, undefined, {
522
+ minDate: ORACLE_START,
523
+ maxDate: ORACLE_END,
524
+ keepsText: true,
525
+ }),
526
+ "interval year to month": literal(/^[+-]?\d{1,9}(-\d{1,2})?$/, "an interval such as '4-2'"),
527
+ "interval day to second": literal(
528
+ /^[+-]?\d{1,9}( \d{1,2}(:\d{1,2}(:\d{1,2}(\.\d{1,9})?)?)?)?$/,
529
+ "an interval such as '4 5:12:10.222'",
530
+ ),
531
+ raw: bin(32767),
532
+ "long raw": bin(),
533
+ long: text(),
534
+ blob: bin(),
535
+ clob: text(),
536
+ nclob: text(),
537
+ bfile: plain("other"),
538
+ rowid: {
539
+ category: "string",
540
+ args: "none",
541
+ pattern: /^[A-Za-z0-9+/]{18}$/,
542
+ patternName: "an 18-character rowid",
543
+ },
544
+ urowid: {
545
+ category: "string",
546
+ args: "length",
547
+ maxLength: 4000,
548
+ lengthIn: "bytes",
549
+ pattern: /^\*?[A-Za-z0-9+/]{1,4000}$/,
550
+ patternName: "a base-64 rowid",
551
+ },
552
+ json: { ...plain("json"), since: "21.0", sinceName: "21c" },
553
+ boolean: { ...plain("boolean"), since: "23.0", sinceName: "23ai" },
554
+ xmltype: text(),
555
+ // 23ai and later: VECTOR(n, format), n up to 65535, format INT8, FLOAT32,
556
+ // FLOAT64 or BINARY — `*` or no argument leaves either open.
557
+ vector: vec(65535, { formats: ["int8", "float32", "float64", "binary"], since: "23.4", sinceName: "23ai" }),
558
+ },
559
+
560
+ sqlite: {
561
+ // SQLite ignores every one of these limits — see docs/column-types.md. The
562
+ // types are still worth declaring: the check they give a flow is ours, and
563
+ // it is the same check the same flow would get on PostgreSQL.
564
+ integer: int(...INT64, ["int", "bigint", "smallint", "tinyint", "mediumint", "int2", "int8", "unsigned big int"]),
565
+ real: { ...plain("number", ["double", "double precision", "float"]), maxAbs: Number.MAX_VALUE },
566
+ // SQLITE_MAX_LENGTH — the one limit the engine really has, and it is in
567
+ // bytes. Our varchar(n) counts characters because that is what the same
568
+ // declaration means on every other product; the two units are deliberate.
569
+ text: text(1000000000, "bytes", ["clob"]),
570
+ blob: bin(1000000000),
571
+ // SQLite has no exact decimal — NUMERIC affinity is a double — so there is
572
+ // no text to preserve here the way there is on the other six.
573
+ numeric: dec(undefined, undefined, undefined, ["decimal"], { exact: false }),
574
+ varchar: text(1000000000, "chars", ["varying character", "nvarchar", "nchar", "character", "native character"]),
575
+ char: text(1000000000, "chars", undefined, { defaultLength: 1 }),
576
+ boolean: plain("boolean", ["bool"]),
577
+ date: plain("date"),
578
+ datetime: plain("date"),
579
+ json: plain("json"),
580
+ },
581
+ };
582
+
583
+ /**
584
+ * CockroachDB and MariaDB run on the postgres and mysql engines, but each has
585
+ * limits its engine does not. A product-keyed entry is the escape hatch
586
+ * `byDatabase` was built for, so they take one rather than silently inheriting
587
+ * a number that is wrong for them.
588
+ */
589
+ TYPES.cockroachdb = {
590
+ ...TYPES.postgres,
591
+ // No documented maximum for either. Left unbounded rather than guessed at;
592
+ // tests/integration/contract.js measures the real ceiling against a server.
593
+ // No documented maximum for n, and the contract suite measured that a
594
+ // declaration of 2^31-1 is accepted — so the length is checked, the
595
+ // declaration is not.
596
+ string: { category: "string", args: "length", lengthIn: "chars" },
597
+ varchar: {
598
+ category: "string",
599
+ args: "length",
600
+ lengthIn: "chars",
601
+ aliases: ["character varying"],
602
+ },
603
+ char: {
604
+ category: "string",
605
+ args: "length",
606
+ lengthIn: "chars",
607
+ aliases: ["character", "bpchar"],
608
+ defaultLength: 1,
609
+ },
610
+ decimal: dec(undefined, undefined, undefined, ["dec", "numeric"]),
611
+ bytes: bin(undefined, ["bytea", "blob"]),
612
+ // The INT page gives a symmetric range stopping at -9223372036854775807, but
613
+ // the server takes -9223372036854775808 — measured, not read, and the
614
+ // contract suite asks again on every run.
615
+ int: int(...INT64, ["integer", "int64", "bigint"]),
616
+ int2: int(...INT16, ["smallint"]),
617
+ int4: int(...INT32),
618
+ int8: int(...INT64),
619
+ // SERIAL is INT8 here, not PostgreSQL's INT4, so a unique_rowid() value fits.
620
+ serial: int(...INT64, ["serial8"]),
621
+ // With the default serial_normalization (rowid) every serial is INT8 — the
622
+ // server accepted 32768 into SMALLSERIAL and read it back unchanged.
623
+ serial4: int(...INT64),
624
+ smallserial: int(...INT64, ["serial2"]),
625
+ bigserial: int(...INT64),
626
+ // Every float type is stored as a 64-bit IEEE754 here, so REAL is not the
627
+ // narrower thing it is on PostgreSQL. Left deliberately unbounded.
628
+ real: { ...plain("number", ["float4"]), special: NUMERIC_SPECIALS },
629
+ date: { ...plain("date"), minDate: CRDB_EPOCH_START },
630
+ timestamp: stamp(6, ["timestamp without time zone"], { minDate: CRDB_EPOCH_START }),
631
+ "timestamp with time zone": stamp(6, ["timestamptz"], { minDate: CRDB_EPOCH_START }),
632
+ interval: literal(CRDB_INTERVAL, "an interval", { args: "precision", maxPrecision: 6 }),
633
+ // CockroachDB documents no maximum for either, and the contract suite
634
+ // measured that a declaration of 2^31-1 is accepted.
635
+ bit: {
636
+ category: "other",
637
+ args: "length",
638
+ lengthIn: "chars",
639
+ defaultLength: 1,
640
+ exactLength: true,
641
+ pattern: BITS,
642
+ patternName: "a bit string",
643
+ },
644
+ "bit varying": {
645
+ category: "other",
646
+ args: "length",
647
+ lengthIn: "chars",
648
+ aliases: ["varbit"],
649
+ pattern: BITS,
650
+ patternName: "a bit string",
651
+ },
652
+ geometry: plain("other"),
653
+ geography: plain("other"),
654
+ box2d: plain("other"),
655
+ // Case-insensitive text (24.3) and label trees (25.2).
656
+ citext: text(),
657
+ ltree: {
658
+ category: "other",
659
+ args: "none",
660
+ pattern: /^(?:[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)*)?$/,
661
+ patternName: "a label path such as top.science",
662
+ },
663
+ // VECTOR(n), n up to 16000 (24.2) — the pgvector type, built in.
664
+ vector: vec(16000, { maxAbs: FLOAT32_MAX }),
665
+ };
666
+ delete TYPES.cockroachdb.bigserial.aliases;
667
+ // CockroachDB names its integers and decimals after their width, so
668
+ // PostgreSQL's spellings become aliases of them rather than entries of their
669
+ // own. The rest of the list is types PostgreSQL has and CockroachDB does not.
670
+ for (const gone of [
671
+ "numeric",
672
+ "smallint",
673
+ "integer",
674
+ "bigint",
675
+ "bytea",
676
+ "money",
677
+ // A path expression for the jsonb functions, but not a column type here.
678
+ "jsonpath",
679
+ "int4range",
680
+ "int8range",
681
+ "numrange",
682
+ "tsrange",
683
+ "tstzrange",
684
+ "daterange",
685
+ "int4multirange",
686
+ "int8multirange",
687
+ "nummultirange",
688
+ "tsmultirange",
689
+ "tstzmultirange",
690
+ "datemultirange",
691
+ "xml",
692
+ "cidr",
693
+ "macaddr",
694
+ "macaddr8",
695
+ "point",
696
+ "line",
697
+ "lseg",
698
+ "box",
699
+ "path",
700
+ "polygon",
701
+ "circle",
702
+ ]) {
703
+ delete TYPES.cockroachdb[gone];
704
+ }
705
+
706
+ TYPES.mariadb = {
707
+ ...TYPES.mysql,
708
+ // MariaDB truncates the fraction it has no room for; MySQL rounds it.
709
+ datetime: { ...TYPES.mysql.datetime, roundsFraction: false },
710
+ varchar: text(65532, "chars"), // MySQL allows 65535
711
+ decimal: dec(65, 38, 0, ["numeric", "dec", "fixed"]), // MySQL stops at scale 30
712
+ // 11.5 moved the ceiling from 2038 to 2106. Holding the later one: refusing a
713
+ // row a modern server would take is worse than letting the server say no.
714
+ timestamp: stamp(6, undefined, {
715
+ minDate: "1970-01-01T00:00:01.000Z",
716
+ maxDate: "2106-02-07T06:28:15.999Z",
717
+ // Before 11.5 the old ceiling holds (measured on 10.11 and 11.4).
718
+ maxDateBefore: { 11.5: "2038-01-19T03:14:07.999Z" },
719
+ }),
720
+ // Hyphens are optional and may fall after any pair of digits, so the shape is
721
+ // 32 hex digits once they are stripped — never a count of characters.
722
+ uuid: {
723
+ category: "string",
724
+ args: "none",
725
+ normalise: "hyphens",
726
+ pattern: UUID_HEX,
727
+ patternName: "a uuid",
728
+ since: "10.7",
729
+ },
730
+ inet4: { category: "other", args: "none", check: "ipv4", since: "10.10" },
731
+ inet6: { category: "other", args: "none", check: "ipv6", since: "10.5" },
732
+ // 11.7. Up to 16383 single-precision numbers.
733
+ vector: vec(16383, { maxAbs: FLOAT32_MAX, since: "11.7" }),
734
+ // 12.3. XML text, no length and (so far) no validation.
735
+ xmltype: { category: "string", args: "none", since: "12.3" },
736
+ };
737
+
738
+ // MariaDB's synonyms, each one measured: declared with the synonym, the column
739
+ // is stored as the type it is filed under (tests/integration/type-inventory.js).
740
+ for (const [type, synonyms] of Object.entries({
741
+ tinyint: ["int1"],
742
+ smallint: ["int2"],
743
+ mediumint: ["int3", "middleint"],
744
+ int: ["int4"],
745
+ bigint: ["int8"],
746
+ float: ["float4"],
747
+ double: ["float8"],
748
+ char: ["character", "national char", "national character", "nchar"],
749
+ varchar: [
750
+ "char varying",
751
+ "varcharacter",
752
+ "national varchar",
753
+ "national char varying",
754
+ "national character varying",
755
+ "national varcharacter",
756
+ "nchar varchar",
757
+ "nchar varcharacter",
758
+ "nchar varying",
759
+ ],
760
+ mediumtext: ["long varchar", "long char varying", "long character varying", "long varcharacter"],
761
+ mediumblob: ["long varbinary"],
762
+ year: ["sql_tsi_year"],
763
+ })) {
764
+ const entry = TYPES.mariadb[type];
765
+ TYPES.mariadb[type] = { ...entry, aliases: [...(entry.aliases || []), ...synonyms] };
766
+ }
767
+ // MySQL's GEOMCOLLECTION spelling is refused by MariaDB (measured).
768
+ TYPES.mariadb.geometrycollection = { ...TYPES.mariadb.geometrycollection, aliases: [] };
769
+
770
+ // TiDB speaks MySQL and documents MySQL's types; what it lacks or bounds
771
+ // differently is set below from its own reference and the contract's run.
772
+ TYPES.tidb = {
773
+ ...TYPES.mysql,
774
+ // MySQL's BINARY synonym CHAR BYTE is a syntax error here (measured).
775
+ binary: { ...TYPES.mysql.binary, aliases: [] },
776
+ // 8.4+: up to 16383 float32 elements. Takes "[1,2.5]" text, not float32
777
+ // bytes ("Invalid vector text" — measured).
778
+ vector: { ...TYPES.mysql.vector, since: "8.4", bindAs: "text" },
779
+ };
780
+ // "TiDB supports all the data types in MySQL except the Spatial type."
781
+ for (const spatial of [
782
+ "geometry",
783
+ "point",
784
+ "linestring",
785
+ "polygon",
786
+ "multipoint",
787
+ "multilinestring",
788
+ "multipolygon",
789
+ "geometrycollection",
790
+ ])
791
+ delete TYPES.tidb[spatial];
792
+
793
+ // YugabyteDB's YSQL is PostgreSQL's query layer; the same, minus what it lacks.
794
+ TYPES.yugabytedb = {
795
+ ...TYPES.postgres,
796
+ // "DECIMAL does not support NaN yet" / "… Infinity yet" (measured on 2026.1):
797
+ // refused here, with the parameter's name, rather than by the server.
798
+ numeric: { ...TYPES.postgres.numeric, special: [] },
799
+ // 2025.2 takes the column but not a value: "unsupported XML feature" (built
800
+ // without libxml). 2026.1 stores it (measured on both).
801
+ xml: { ...TYPES.postgres.xml, since: "2026.1" },
802
+ };
803
+
804
+ // Firebird 5.0 Language Reference, chapter 3 "Data Types and Subtypes" (see
805
+ // docs/column-types.md). Firebird 4.0 is the floor: INT128, DECFLOAT, the
806
+ // time-zone types, BINARY/VARBINARY and 38-digit NUMERIC all arrived in 4.0.
807
+ const FB_TIME = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d{1,4})?)?$/;
808
+ // A displacement, Z or UTC. Region names (Europe/Warsaw) are left out: the
809
+ // adapter encodes the zone itself and carries no region table.
810
+ const FB_TIME_TZ = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d{1,4})?)?\s*(?:[+-]\d{2}:?\d{2}|Z|UTC)?$/i;
811
+ const FB_START = "0001-01-01T00:00:00.000Z";
812
+ const FB_END = "9999-12-31T23:59:59.999Z";
813
+ TYPES.firebird = {
814
+ smallint: int(-32768n, 32767n),
815
+ integer: int(-2147483648n, 2147483647n, ["int"]),
816
+ bigint: int(-9223372036854775808n, 9223372036854775807n),
817
+ int128: int(-(2n ** 127n), 2n ** 127n - 1n),
818
+ // NUMERIC and DECIMAL are stored apart (field sub-type 1 and 2), so neither
819
+ // is the other's alias. Precision 1..38, scale 0..precision.
820
+ numeric: dec(38, 38, 0, undefined, { defaultPrecision: 9, defaultScale: 0 }),
821
+ decimal: dec(38, 38, 0, ["dec"], { defaultPrecision: 9, defaultScale: 0 }),
822
+ // DECFLOAT(16) or DECFLOAT(34); bare is 34. Decimal floating point: exact digits.
823
+ decfloat: {
824
+ category: "number",
825
+ args: "precision",
826
+ maxPrecision: 34,
827
+ precisions: [16, 34],
828
+ defaultPrecision: 34,
829
+ exact: true,
830
+ },
831
+ // FLOAT(p): p is binary, 1..24 single, 25..53 double. REAL is single.
832
+ float: { category: "number", args: "precision", maxPrecision: 53, precisionIn: "binary", aliases: ["real"] },
833
+ "double precision": { ...plain("number"), maxAbs: 1.7976931348623157e308 },
834
+ date: { ...plain("date"), minDate: FB_START, maxDate: FB_END },
835
+ // Four fractional digits — one more than a Date carries, so text keeps them.
836
+ timestamp: {
837
+ ...plain("date", ["timestamp without time zone"]),
838
+ minDate: FB_START,
839
+ maxDate: FB_END,
840
+ keepsText: true,
841
+ },
842
+ "timestamp with time zone": {
843
+ ...plain("date"),
844
+ minDate: FB_START,
845
+ maxDate: FB_END,
846
+ keepsText: true,
847
+ keepsOffset: true,
848
+ },
849
+ time: literal(FB_TIME, "a time of day", { aliases: ["time without time zone"] }),
850
+ "time with time zone": literal(FB_TIME_TZ, "a time of day with a zone"),
851
+ // n counts characters; the byte limit (32767 / 32765) holds for a one-byte
852
+ // character set, so UTF8 columns stop at a quarter of it.
853
+ char: text(32767, "chars", ["character"]),
854
+ varchar: text(32765, "chars", ["character varying", "char varying"]),
855
+ // ISO8859_1, a different character set from the database's, so its own type.
856
+ nchar: text(32767, "chars", ["national char", "national character"]),
857
+ "nchar varying": text(32765, "chars", ["national char varying", "national character varying"]),
858
+ binary: bin(32767),
859
+ varbinary: bin(32765, ["binary varying"]),
860
+ boolean: plain("boolean"),
861
+ blob: bin(undefined, ["blob sub_type binary", "blob sub_type 0"]),
862
+ "blob sub_type text": text(undefined, undefined, ["blob sub_type 1"]),
863
+ };
864
+
865
+ // ClickHouse 26.11 reference, "Data types" (see docs/column-types.md). Names are
866
+ // case-sensitive on the server (Int32, not int32); the catalogue keeps the
867
+ // server's spelling and is looked up case-insensitively like every other one.
868
+ // The MySQL-style aliases are system.data_type_families' own `alias_to`.
869
+ const CH_DATE_END = "2149-06-06T00:00:00.000Z";
870
+ const CH_TIME = /^-?\d{1,3}:[0-5]\d(?::[0-5]\d(?:\.\d{1,9})?)?$/;
871
+ const chType = (category, extra) => ({ category, args: "type", ...extra });
872
+ TYPES.clickhouse = {
873
+ // Int8 … Int64 also carry the unsigned range: `TINYINT UNSIGNED` is UInt8.
874
+ Int8: uint(-128n, 127n, 255n, ["byte", "int1", "tinyint"]),
875
+ Int16: uint(-32768n, 32767n, 65535n, ["smallint"]),
876
+ Int32: uint(-2147483648n, 2147483647n, 4294967295n, ["int", "integer", "mediumint"]),
877
+ Int64: uint(-9223372036854775808n, 9223372036854775807n, 18446744073709551615n, ["bigint", "signed"]),
878
+ Int128: int(-(2n ** 127n), 2n ** 127n - 1n),
879
+ Int256: int(-(2n ** 255n), 2n ** 255n - 1n),
880
+ UInt8: int(0n, 255n),
881
+ UInt16: int(0n, 65535n, ["year"]),
882
+ UInt32: int(0n, 4294967295n),
883
+ UInt64: int(0n, 18446744073709551615n, ["unsigned", "bit", "set"]),
884
+ UInt128: int(0n, 2n ** 128n - 1n),
885
+ UInt256: int(0n, 2n ** 256n - 1n),
886
+ Float32: { ...plain("number", ["float", "real", "single"]), maxAbs: FLOAT32_MAX },
887
+ Float64: { ...plain("number", ["double", "double precision"]), maxAbs: 1.7976931348623157e308 },
888
+ BFloat16: { ...plain("number"), maxAbs: 3.3895313892515355e38 },
889
+ // Decimal(P, S): P 1 … 76. Decimal32(S) … Decimal256(S) fix P and take S.
890
+ Decimal: dec(76, 76, 0, ["dec", "fixed", "numeric"], { defaultPrecision: 10, defaultScale: 0 }),
891
+ Decimal32: chType("number", { exact: true, defaultPrecision: 9 }),
892
+ Decimal64: chType("number", { exact: true, defaultPrecision: 18 }),
893
+ Decimal128: chType("number", { exact: true, defaultPrecision: 38 }),
894
+ Decimal256: chType("number", { exact: true, defaultPrecision: 76 }),
895
+ Bool: plain("boolean", ["bool", "boolean"]),
896
+ // Bytes of any length. The SQL aliases take a length and ignore it, as here.
897
+ String: {
898
+ ...text(undefined, undefined, [
899
+ "text",
900
+ "tinytext",
901
+ "mediumtext",
902
+ "longtext",
903
+ "char",
904
+ "character",
905
+ "varchar",
906
+ "varchar2",
907
+ "nvarchar",
908
+ "nchar",
909
+ "national char",
910
+ "national character",
911
+ "char varying",
912
+ "character varying",
913
+ "national char varying",
914
+ "national character varying",
915
+ "nchar varying",
916
+ "char large object",
917
+ "character large object",
918
+ "national character large object",
919
+ "nchar large object",
920
+ "clob",
921
+ "blob",
922
+ "tinyblob",
923
+ "mediumblob",
924
+ "longblob",
925
+ "bytea",
926
+ "varbinary",
927
+ "binary varying",
928
+ "binary large object",
929
+ ]),
930
+ displayWidth: true,
931
+ },
932
+ FixedString: text(undefined, "bytes", ["binary"], { args: "length", minLength: 1 }),
933
+ UUID: {
934
+ category: "string",
935
+ args: "none",
936
+ pattern: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
937
+ patternName: "a UUID",
938
+ },
939
+ IPv4: plain("other", ["inet4"]),
940
+ IPv6: plain("other", ["inet6"]),
941
+ Date: { ...plain("date"), minDate: "1970-01-01T00:00:00.000Z", maxDate: CH_DATE_END },
942
+ Date32: { ...plain("date"), minDate: "1900-01-01T00:00:00.000Z", maxDate: "2299-12-31T00:00:00.000Z" },
943
+ // DateTime([zone]): whole seconds, 1970 … 2106. The zone changes display only.
944
+ DateTime: chType("date", {
945
+ aliases: ["timestamp", "datetime32"],
946
+ minDate: "1970-01-01T00:00:00.000Z",
947
+ maxDate: "2106-02-07T06:28:15.000Z",
948
+ }),
949
+ // DateTime64(p[, zone]): p 0 … 9, so text keeps what a Date would cut.
950
+ DateTime64: chType("date", {
951
+ minDate: "1900-01-01T00:00:00.000Z",
952
+ maxDate: "2299-12-31T23:59:59.999Z",
953
+ keepsText: true,
954
+ }),
955
+ // Time, Time64: on by default since 25.12 (experimental before, behind
956
+ // enable_time_time64_type).
957
+ Time: { ...literal(CH_TIME, "a time such as 10:20:30 (−999:59:59 … 999:59:59)"), since: "25.12" },
958
+ Time64: literal(CH_TIME, "a time such as 10:20:30.123", { args: "type", since: "25.12" }),
959
+ Enum8: chType("string"),
960
+ Enum16: chType("string"),
961
+ Enum: chType("string", { aliases: ["enum"] }),
962
+ JSON: { ...chType("json"), aliases: ["json"] },
963
+ Array: chType("other", { wholeValue: true }),
964
+ Tuple: chType("other", { wholeValue: true, tuple: true }),
965
+ Map: chType("other"),
966
+ Variant: chType("other"),
967
+ // Hold what the type inside holds; a declaration is checked as that type.
968
+ Nullable: chType("other", { wraps: true }),
969
+ LowCardinality: chType("other", { wraps: true }),
970
+ // QBit(element, n): a vector stored bit-sliced for vector search.
971
+ QBit: chType("vector", { since: "25.10" }),
972
+ Point: plain("other"),
973
+ Ring: plain("other"),
974
+ LineString: plain("other"),
975
+ MultiPoint: { ...plain("other"), since: "26.8" },
976
+ MultiLineString: plain("other"),
977
+ Polygon: plain("other"),
978
+ MultiPolygon: plain("other"),
979
+ // Spelled Geometry or GEOMETRY; `geometry` is refused (case-sensitive alias).
980
+ Geometry: { ...plain("other"), since: "25.11" },
981
+ };
982
+ // IntervalDay … IntervalYear: a count of that unit, stored as an Int64
983
+ // (measured: '3' into IntervalDay is 3 days).
984
+ for (const unit of [
985
+ "Nanosecond",
986
+ "Microsecond",
987
+ "Millisecond",
988
+ "Second",
989
+ "Minute",
990
+ "Hour",
991
+ "Day",
992
+ "Week",
993
+ "Month",
994
+ "Quarter",
995
+ "Year",
996
+ ]) {
997
+ TYPES.clickhouse[`Interval${unit}`] = int(-9223372036854775808n, 9223372036854775807n);
998
+ }
999
+
1000
+ /** Every type a database offers, keyed by its canonical name. */
1001
+ function typesFor(database) {
1002
+ const types = byDatabase(TYPES, database);
1003
+ if (!types) throw new Error(`Unknown database: ${database}`);
1004
+ return types;
1005
+ }
1006
+
1007
+ // One flattened name -> canonical-name map per database, built on first use.
1008
+ // Aliases are common enough in saved flows (`int` for `integer`, `dec` for
1009
+ // `numeric`) that resolving them by scan on every message would be waste.
1010
+ const lookups = new Map();
1011
+
1012
+ function lookupFor(database) {
1013
+ let lookup = lookups.get(database);
1014
+ if (lookup) return lookup;
1015
+
1016
+ lookup = new Map();
1017
+ const types = typesFor(database);
1018
+ for (const [name, spec] of Object.entries(types)) {
1019
+ // Lower case: ClickHouse's names are mixed case, every other catalogue's are not.
1020
+ lookup.set(name.toLowerCase(), name);
1021
+ for (const alias of spec.aliases || []) if (!lookup.has(alias)) lookup.set(alias, name);
1022
+ }
1023
+ lookups.set(database, lookup);
1024
+ return lookup;
1025
+ }
1026
+
1027
+ /**
1028
+ * Finds a type by any of its spellings, case- and whitespace-insensitively.
1029
+ *
1030
+ * @returns {{name: string, spec: ColumnType}|undefined}
1031
+ */
1032
+ function resolveType(database, name) {
1033
+ if (typeof name !== "string") return undefined;
1034
+ const key = name.trim().toLowerCase().replace(/\s+/g, " ");
1035
+ const canonical = lookupFor(database).get(key);
1036
+ return canonical === undefined ? undefined : { name: canonical, spec: typesFor(database)[canonical] };
1037
+ }
1038
+
1039
+ /** -1, 0 or 1 for two dotted versions, compared as numbers: 10.10 is past 10.9. */
1040
+ function compareVersions(a, b) {
1041
+ const x = String(a).split(".").map(Number);
1042
+ const y = String(b).split(".").map(Number);
1043
+ for (let i = 0; i < Math.max(x.length, y.length); i++) {
1044
+ const d = (x[i] || 0) - (y[i] || 0);
1045
+ if (d !== 0) return d < 0 ? -1 : 1;
1046
+ }
1047
+ return 0;
1048
+ }
1049
+
1050
+ /** Whether a server of this version has the type at all. */
1051
+ function availableIn(spec, version) {
1052
+ return !spec.since || !version || compareVersions(version, spec.since) >= 0;
1053
+ }
1054
+
1055
+ /** The latest date the type holds on a server of this version. */
1056
+ function maxDateIn(spec, version) {
1057
+ for (const [before, maxDate] of Object.entries(spec.maxDateBefore || {})) {
1058
+ if (version && compareVersions(version, before) < 0) return maxDate;
1059
+ }
1060
+ return spec.maxDate;
1061
+ }
1062
+
1063
+ module.exports = {
1064
+ compareVersions,
1065
+ availableIn,
1066
+ maxDateIn,
1067
+ TYPES,
1068
+ CATEGORIES,
1069
+ typesFor,
1070
+ resolveType,
1071
+ };