@reifydb/core 0.8.1 → 0.9.1

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.
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/constant.ts
2
2
  var NONE_VALUE = "\u27EAnone\u27EB";
3
+ var ROW_NUMBER_KEY = "#rownum";
3
4
 
4
5
  // src/value/blob.ts
5
6
  var BlobValue = class _BlobValue {
@@ -461,13 +462,13 @@ var DateValue = class _DateValue {
461
462
  * Helper to parse YYYY-MM-DD format
462
463
  */
463
464
  static parseDate(str) {
464
- const match = str.match(/^(-?\d{1,4})-(\d{2})-(\d{2})$/);
465
- if (!match) {
465
+ const match2 = str.match(/^(-?\d{1,4})-(\d{2})-(\d{2})$/);
466
+ if (!match2) {
466
467
  return null;
467
468
  }
468
- const year = parseInt(match[1], 10);
469
- const month = parseInt(match[2], 10);
470
- const day = parseInt(match[3], 10);
469
+ const year = parseInt(match2[1], 10);
470
+ const month = parseInt(match2[2], 10);
471
+ const day = parseInt(match2[3], 10);
471
472
  if (month < 1 || month > 12) {
472
473
  return null;
473
474
  }
@@ -669,16 +670,16 @@ var _TimeValue = class _TimeValue {
669
670
  * Helper to parse HH:MM:SS[.nnnnnnnnn] format
670
671
  */
671
672
  static parseTime(str) {
672
- const match = str.match(/^(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?$/);
673
- if (!match) {
673
+ const match2 = str.match(/^(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?$/);
674
+ if (!match2) {
674
675
  return null;
675
676
  }
676
- const hour = parseInt(match[1], 10);
677
- const minute = parseInt(match[2], 10);
678
- const second = parseInt(match[3], 10);
677
+ const hour = parseInt(match2[1], 10);
678
+ const minute = parseInt(match2[2], 10);
679
+ const second = parseInt(match2[3], 10);
679
680
  let nano = 0;
680
- if (match[4]) {
681
- const fracStr = match[4].padEnd(9, "0").substring(0, 9);
681
+ if (match2[4]) {
682
+ const fracStr = match2[4].padEnd(9, "0").substring(0, 9);
682
683
  nano = parseInt(fracStr, 10);
683
684
  }
684
685
  if (hour < 0 || hour > 23) {
@@ -877,7 +878,7 @@ var DateTimeValue = class _DateTimeValue {
877
878
  /**
878
879
  * Get Unix timestamp in seconds
879
880
  */
880
- timestamp() {
881
+ toEpochSecs() {
881
882
  const date = this.valueOf();
882
883
  if (date === void 0) return void 0;
883
884
  return Math.floor(date.getTime() / 1e3);
@@ -995,19 +996,19 @@ var DateTimeValue = class _DateTimeValue {
995
996
  * Helper to parse ISO 8601 datetime format
996
997
  */
997
998
  static parseDateTime(str) {
998
- const match = str.match(/^(-?\d{1,4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?Z$/);
999
- if (!match) {
999
+ const match2 = str.match(/^(-?\d{1,4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?Z$/);
1000
+ if (!match2) {
1000
1001
  return null;
1001
1002
  }
1002
- const year = parseInt(match[1], 10);
1003
- const month = parseInt(match[2], 10);
1004
- const day = parseInt(match[3], 10);
1005
- const hour = parseInt(match[4], 10);
1006
- const minute = parseInt(match[5], 10);
1007
- const second = parseInt(match[6], 10);
1003
+ const year = parseInt(match2[1], 10);
1004
+ const month = parseInt(match2[2], 10);
1005
+ const day = parseInt(match2[3], 10);
1006
+ const hour = parseInt(match2[4], 10);
1007
+ const minute = parseInt(match2[5], 10);
1008
+ const second = parseInt(match2[6], 10);
1008
1009
  let nanosFraction = 0n;
1009
- if (match[7]) {
1010
- const fracStr = match[7].padEnd(9, "0").substring(0, 9);
1010
+ if (match2[7]) {
1011
+ const fracStr = match2[7].padEnd(9, "0").substring(0, 9);
1011
1012
  nanosFraction = BigInt(fracStr);
1012
1013
  }
1013
1014
  if (month < 1 || month > 12) {
@@ -1867,19 +1868,19 @@ var DurationValue = class _DurationValue {
1867
1868
  * Parse ISO 8601 duration format: P[n]Y[n]M[n]DT[n]H[n]M[n.n]S
1868
1869
  */
1869
1870
  static parseIsoDuration(str, negative) {
1870
- const match = str.match(/^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/);
1871
- if (!match) {
1871
+ const match2 = str.match(/^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/);
1872
+ if (!match2) {
1872
1873
  return null;
1873
1874
  }
1874
- if (!match[1] && !match[2] && !match[3] && !match[4] && !match[5] && !match[6]) {
1875
+ if (!match2[1] && !match2[2] && !match2[3] && !match2[4] && !match2[5] && !match2[6]) {
1875
1876
  return null;
1876
1877
  }
1877
- const years = parseInt(match[1] || "0", 10);
1878
- const months = parseInt(match[2] || "0", 10);
1879
- const days = parseInt(match[3] || "0", 10);
1880
- const hours = parseInt(match[4] || "0", 10);
1881
- const minutes = parseInt(match[5] || "0", 10);
1882
- const secondsStr = match[6] || "0";
1878
+ const years = parseInt(match2[1] || "0", 10);
1879
+ const months = parseInt(match2[2] || "0", 10);
1880
+ const days = parseInt(match2[3] || "0", 10);
1881
+ const hours = parseInt(match2[4] || "0", 10);
1882
+ const minutes = parseInt(match2[5] || "0", 10);
1883
+ const secondsStr = match2[6] || "0";
1883
1884
  const secondsParts = secondsStr.split(".");
1884
1885
  const wholeSeconds = parseInt(secondsParts[0] || "0", 10);
1885
1886
  let fracNanos = 0n;
@@ -1899,22 +1900,22 @@ var DurationValue = class _DurationValue {
1899
1900
  */
1900
1901
  static parseHumanDuration(str, negative) {
1901
1902
  const pattern = /^(?:(\d+)y)?(?:(\d+)mo)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m(?!s|o))?(?:(\d+)s(?!$)?)?(?:(\d+)ms)?(?:(\d+)us)?(?:(\d+)ns)?$/;
1902
- const match = str.match(pattern);
1903
- if (!match) {
1903
+ const match2 = str.match(pattern);
1904
+ if (!match2) {
1904
1905
  return null;
1905
1906
  }
1906
- if (!match[1] && !match[2] && !match[3] && !match[4] && !match[5] && !match[6] && !match[7] && !match[8] && !match[9]) {
1907
+ if (!match2[1] && !match2[2] && !match2[3] && !match2[4] && !match2[5] && !match2[6] && !match2[7] && !match2[8] && !match2[9]) {
1907
1908
  return null;
1908
1909
  }
1909
- const years = parseInt(match[1] || "0", 10);
1910
- const months = parseInt(match[2] || "0", 10);
1911
- const days = parseInt(match[3] || "0", 10);
1912
- const hours = parseInt(match[4] || "0", 10);
1913
- const minutes = parseInt(match[5] || "0", 10);
1914
- const seconds = parseInt(match[6] || "0", 10);
1915
- const ms = parseInt(match[7] || "0", 10);
1916
- const us = parseInt(match[8] || "0", 10);
1917
- const ns = parseInt(match[9] || "0", 10);
1910
+ const years = parseInt(match2[1] || "0", 10);
1911
+ const months = parseInt(match2[2] || "0", 10);
1912
+ const days = parseInt(match2[3] || "0", 10);
1913
+ const hours = parseInt(match2[4] || "0", 10);
1914
+ const minutes = parseInt(match2[5] || "0", 10);
1915
+ const seconds = parseInt(match2[6] || "0", 10);
1916
+ const ms = parseInt(match2[7] || "0", 10);
1917
+ const us = parseInt(match2[8] || "0", 10);
1918
+ const ns = parseInt(match2[9] || "0", 10);
1918
1919
  const totalNanos = BigInt(hours) * 3600n * 1000000000n + BigInt(minutes) * 60n * 1000000000n + BigInt(seconds) * 1000000000n + BigInt(ms) * 1000000n + BigInt(us) * 1000n + BigInt(ns);
1919
1920
  const totalMonths = years * 12 + months;
1920
1921
  if (negative) {
@@ -2818,6 +2819,148 @@ function decode(pair) {
2818
2819
  throw new Error(`Unsupported type: ${pair.type}`);
2819
2820
  }
2820
2821
  }
2822
+ function columnsToRows(columns) {
2823
+ const rowCount = columns[0]?.payload.length ?? 0;
2824
+ return Array.from({ length: rowCount }, (_, i) => {
2825
+ const row = {};
2826
+ for (const col of columns) {
2827
+ row[col.name] = decode({ type: col.type, value: col.payload[i] });
2828
+ }
2829
+ return row;
2830
+ });
2831
+ }
2832
+
2833
+ // src/encoder.ts
2834
+ function encodeValue(value) {
2835
+ if (value === null || value === void 0) {
2836
+ return { type: "None", value: NONE_VALUE };
2837
+ }
2838
+ if (value && typeof value === "object" && "encode" in value && typeof value.encode === "function") {
2839
+ return value.encode();
2840
+ }
2841
+ if (typeof value === "boolean") {
2842
+ return { type: "Boolean", value: value.toString() };
2843
+ }
2844
+ if (typeof value === "number") {
2845
+ if (Number.isInteger(value)) {
2846
+ if (value >= -128 && value <= 127) {
2847
+ return { type: "Int1", value: value.toString() };
2848
+ } else if (value >= -32768 && value <= 32767) {
2849
+ return { type: "Int2", value: value.toString() };
2850
+ } else if (value >= -2147483648 && value <= 2147483647) {
2851
+ return { type: "Int4", value: value.toString() };
2852
+ } else {
2853
+ return { type: "Int8", value: value.toString() };
2854
+ }
2855
+ } else {
2856
+ return { type: "Float8", value: value.toString() };
2857
+ }
2858
+ }
2859
+ if (typeof value === "string") {
2860
+ if (/^[0-9a-f]{8}-[0-9a-f]{4}-[47][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)) {
2861
+ const version4 = value[14];
2862
+ if (version4 === "4") {
2863
+ return { type: "Uuid4", value };
2864
+ } else if (version4 === "7") {
2865
+ return { type: "Uuid7", value };
2866
+ }
2867
+ }
2868
+ return { type: "Utf8", value };
2869
+ }
2870
+ if (typeof value === "bigint") {
2871
+ if (value >= BigInt(0)) {
2872
+ if (value <= BigInt(255)) {
2873
+ return { type: "Uint1", value: value.toString() };
2874
+ } else if (value <= BigInt(65535)) {
2875
+ return { type: "Uint2", value: value.toString() };
2876
+ } else if (value <= BigInt(4294967295)) {
2877
+ return { type: "Uint4", value: value.toString() };
2878
+ } else if (value <= BigInt("18446744073709551615")) {
2879
+ return { type: "Uint8", value: value.toString() };
2880
+ } else {
2881
+ return { type: "Uint16", value: value.toString() };
2882
+ }
2883
+ } else {
2884
+ if (value >= BigInt("-9223372036854775808")) {
2885
+ return { type: "Int8", value: value.toString() };
2886
+ } else {
2887
+ return { type: "Int16", value: value.toString() };
2888
+ }
2889
+ }
2890
+ }
2891
+ if (value instanceof Date) {
2892
+ return { type: "DateTime", value: value.toISOString() };
2893
+ }
2894
+ if (value instanceof Uint8Array) {
2895
+ const hex = Array.from(value).map((b) => b.toString(16).padStart(2, "0")).join("");
2896
+ return { type: "Blob", value: "0x" + hex };
2897
+ }
2898
+ if (value instanceof ArrayBuffer) {
2899
+ const uint8Array = new Uint8Array(value);
2900
+ const hex = Array.from(uint8Array).map((b) => b.toString(16).padStart(2, "0")).join("");
2901
+ return { type: "Blob", value: "0x" + hex };
2902
+ }
2903
+ throw new Error(`Cannot encode value of type ${typeof value}: ${value}`);
2904
+ }
2905
+ function encodeParams(params) {
2906
+ if (params === void 0 || params === null) {
2907
+ return [];
2908
+ }
2909
+ if (Array.isArray(params)) {
2910
+ return params.map((param) => encodeValue(param));
2911
+ } else if (typeof params === "object") {
2912
+ const encoded = {};
2913
+ for (const [key, value] of Object.entries(params)) {
2914
+ encoded[key] = encodeValue(value);
2915
+ }
2916
+ return encoded;
2917
+ }
2918
+ throw new Error(`Invalid parameters type: expected array or object, got ${typeof params}`);
2919
+ }
2920
+
2921
+ // src/type-code.ts
2922
+ var TYPE_CODE = {
2923
+ None: 0,
2924
+ Boolean: 1,
2925
+ Float4: 2,
2926
+ Float8: 3,
2927
+ Int1: 4,
2928
+ Int2: 5,
2929
+ Int4: 6,
2930
+ Int8: 7,
2931
+ Int16: 8,
2932
+ Utf8: 9,
2933
+ Uint1: 10,
2934
+ Uint2: 11,
2935
+ Uint4: 12,
2936
+ Uint8: 13,
2937
+ Uint16: 14,
2938
+ Date: 15,
2939
+ DateTime: 16,
2940
+ Time: 17,
2941
+ Duration: 18,
2942
+ IdentityId: 19,
2943
+ Uuid4: 20,
2944
+ Uuid7: 21,
2945
+ Blob: 22,
2946
+ Int: 23,
2947
+ Uint: 24,
2948
+ Decimal: 25,
2949
+ Any: 26,
2950
+ DictionaryId: 27,
2951
+ Type: 28,
2952
+ List: 29,
2953
+ Record: 30,
2954
+ Tuple: 31
2955
+ };
2956
+ var CODE_TO_NAME = Object.fromEntries(
2957
+ Object.entries(TYPE_CODE).map(([k, v]) => [v, k])
2958
+ );
2959
+ function typeNameFromCode(code) {
2960
+ const name = CODE_TO_NAME[code];
2961
+ if (!name) throw new Error(`Unknown type code: ${code}`);
2962
+ return name;
2963
+ }
2821
2964
 
2822
2965
  // src/shape/builder.ts
2823
2966
  var ShapeBuilder = class {
@@ -3097,11 +3240,11 @@ function parseValue(shape, value) {
3097
3240
  // src/shape/validator.ts
3098
3241
  function validateShape(shape, value) {
3099
3242
  if (shape.kind === "primitive") {
3100
- const shape_type = shape.type;
3243
+ const objectType = shape.type;
3101
3244
  if (value === null || value === void 0) {
3102
- return shape_type === "None";
3245
+ return objectType === "None";
3103
3246
  }
3104
- switch (shape_type) {
3247
+ switch (objectType) {
3105
3248
  case "Boolean":
3106
3249
  return typeof value === "boolean";
3107
3250
  case "Float4":
@@ -3171,6 +3314,692 @@ function validateShape(shape, value) {
3171
3314
  return false;
3172
3315
  }
3173
3316
 
3317
+ // src/shape/transform.ts
3318
+ function snakeToCamel(key) {
3319
+ return key.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
3320
+ }
3321
+ function transformFrames(frames, shapes) {
3322
+ const transformed = frames.map((frame, frameIndex) => {
3323
+ const frameShape = shapes[frameIndex];
3324
+ if (!frameShape) {
3325
+ return frame;
3326
+ }
3327
+ return frame.map((row) => transformResult(row, frameShape));
3328
+ });
3329
+ return transformed;
3330
+ }
3331
+ function transformResult(row, resultShape) {
3332
+ if (resultShape && resultShape.kind === "object" && resultShape.properties) {
3333
+ const transformedRow = {};
3334
+ for (const [key, value] of Object.entries(row)) {
3335
+ const propertyShape = resultShape.properties[key];
3336
+ const outputKey = snakeToCamel(key);
3337
+ if (propertyShape && propertyShape.kind === "primitive") {
3338
+ if (value && typeof value === "object" && typeof value.valueOf === "function") {
3339
+ const rawValue = value.valueOf();
3340
+ transformedRow[outputKey] = coerceToPrimitiveType(rawValue, propertyShape.type);
3341
+ } else {
3342
+ transformedRow[outputKey] = coerceToPrimitiveType(value, propertyShape.type);
3343
+ }
3344
+ } else if (propertyShape && propertyShape.kind === "value") {
3345
+ transformedRow[outputKey] = value;
3346
+ } else {
3347
+ transformedRow[outputKey] = propertyShape ? transformResult(value, propertyShape) : value;
3348
+ }
3349
+ }
3350
+ return transformedRow;
3351
+ }
3352
+ if (resultShape && resultShape.kind === "primitive") {
3353
+ if (row && typeof row === "object" && typeof row.valueOf === "function") {
3354
+ return coerceToPrimitiveType(row.valueOf(), resultShape.type);
3355
+ }
3356
+ return coerceToPrimitiveType(row, resultShape.type);
3357
+ }
3358
+ if (resultShape && resultShape.kind === "value") {
3359
+ return row;
3360
+ }
3361
+ if (resultShape && resultShape.kind === "array") {
3362
+ if (Array.isArray(row)) {
3363
+ return row.map((item) => transformResult(item, resultShape.items));
3364
+ }
3365
+ return row;
3366
+ }
3367
+ if (resultShape && resultShape.kind === "optional") {
3368
+ if (row === void 0) {
3369
+ return void 0;
3370
+ }
3371
+ if (row === null) {
3372
+ return null;
3373
+ }
3374
+ return transformResult(row, resultShape.shape);
3375
+ }
3376
+ return row;
3377
+ }
3378
+ function coerceToPrimitiveType(value, valueType) {
3379
+ if (value === void 0 || value === null) {
3380
+ return value;
3381
+ }
3382
+ const bigintTypes = ["Int8", "Int16", "Uint8", "Uint16"];
3383
+ if (bigintTypes.includes(valueType)) {
3384
+ if (typeof value === "bigint") {
3385
+ return value;
3386
+ }
3387
+ if (typeof value === "number") {
3388
+ return BigInt(Math.trunc(value));
3389
+ }
3390
+ if (typeof value === "string") {
3391
+ return BigInt(value);
3392
+ }
3393
+ }
3394
+ return value;
3395
+ }
3396
+
3397
+ // src/present/value.ts
3398
+ var NONE_PRESENTATION = {
3399
+ text: "none",
3400
+ italic: true,
3401
+ muted: true
3402
+ };
3403
+ var ROLE_BY_TYPE = {
3404
+ None: "none",
3405
+ Boolean: "boolean",
3406
+ Int1: "number",
3407
+ Int2: "number",
3408
+ Int4: "number",
3409
+ Int8: "number",
3410
+ Int16: "number",
3411
+ Uint1: "number",
3412
+ Uint2: "number",
3413
+ Uint4: "number",
3414
+ Uint8: "number",
3415
+ Uint16: "number",
3416
+ Float4: "number",
3417
+ Float8: "number",
3418
+ Decimal: "number",
3419
+ Date: "temporal",
3420
+ DateTime: "temporal",
3421
+ Time: "temporal",
3422
+ Duration: "temporal",
3423
+ Uuid4: "uuid",
3424
+ Uuid7: "uuid",
3425
+ IdentityId: "uuid",
3426
+ Utf8: "string",
3427
+ Blob: "blob"
3428
+ };
3429
+ function typeName(value) {
3430
+ if (value === null || typeof value !== "object") return void 0;
3431
+ const candidate = value.type;
3432
+ return typeof candidate === "string" ? candidate : void 0;
3433
+ }
3434
+ function valueTypeName(value) {
3435
+ if (value === null || value === void 0) return "None";
3436
+ return typeName(value);
3437
+ }
3438
+ function valueRole(value) {
3439
+ if (value === null || value === void 0) return "none";
3440
+ const name = typeName(value);
3441
+ if (name !== void 0) {
3442
+ return ROLE_BY_TYPE[name] ?? "unknown";
3443
+ }
3444
+ switch (typeof value) {
3445
+ case "number":
3446
+ case "bigint":
3447
+ return "number";
3448
+ case "boolean":
3449
+ return "boolean";
3450
+ case "string":
3451
+ return "string";
3452
+ default:
3453
+ return "unknown";
3454
+ }
3455
+ }
3456
+ function formatValue(value) {
3457
+ if (value === null || value === void 0) return NONE_PRESENTATION.text;
3458
+ if (typeof value === "bigint") return value.toString();
3459
+ if (typeof value === "object") {
3460
+ const name = typeName(value);
3461
+ if (name === "None") return NONE_PRESENTATION.text;
3462
+ if (name !== void 0) return String(value);
3463
+ return JSON.stringify(value) ?? String(value);
3464
+ }
3465
+ return String(value);
3466
+ }
3467
+
3468
+ // src/present/width.ts
3469
+ var ZERO_WIDTH = [
3470
+ [768, 879],
3471
+ [1155, 1161],
3472
+ [1425, 1469],
3473
+ [1552, 1562],
3474
+ [1611, 1631],
3475
+ [3633, 3633],
3476
+ [3636, 3642],
3477
+ [3761, 3761],
3478
+ [3764, 3769],
3479
+ [8203, 8207],
3480
+ [8400, 8447],
3481
+ [65024, 65039],
3482
+ [65056, 65071],
3483
+ [65279, 65279]
3484
+ ];
3485
+ var WIDE = [
3486
+ [4352, 4447],
3487
+ [9001, 9002],
3488
+ [11904, 12350],
3489
+ [12353, 13311],
3490
+ [13312, 19903],
3491
+ [19968, 40959],
3492
+ [40960, 42191],
3493
+ [43360, 43391],
3494
+ [44032, 55203],
3495
+ [63744, 64255],
3496
+ [65040, 65049],
3497
+ [65072, 65135],
3498
+ [65280, 65376],
3499
+ [65504, 65510],
3500
+ [127744, 129535],
3501
+ [129648, 129791],
3502
+ [131072, 262141]
3503
+ ];
3504
+ function inRanges(code, ranges) {
3505
+ let low = 0;
3506
+ let high = ranges.length - 1;
3507
+ while (low <= high) {
3508
+ const middle = low + high >> 1;
3509
+ const [start, end] = ranges[middle];
3510
+ if (code < start) high = middle - 1;
3511
+ else if (code > end) low = middle + 1;
3512
+ else return true;
3513
+ }
3514
+ return false;
3515
+ }
3516
+ function codePointWidth(code) {
3517
+ if (code === 0) return 0;
3518
+ if (code < 32 || code >= 127 && code < 160) return 0;
3519
+ if (inRanges(code, ZERO_WIDTH)) return 0;
3520
+ if (inRanges(code, WIDE)) return 2;
3521
+ return 1;
3522
+ }
3523
+ function createSegmenter() {
3524
+ if (typeof Intl === "undefined") return null;
3525
+ const factory = Intl.Segmenter;
3526
+ if (typeof factory !== "function") return null;
3527
+ return new factory(void 0, { granularity: "grapheme" });
3528
+ }
3529
+ var segmenter = createSegmenter();
3530
+ function graphemes(text) {
3531
+ if (segmenter !== null) {
3532
+ return Array.from(segmenter.segment(text), (entry) => entry.segment);
3533
+ }
3534
+ return Array.from(text);
3535
+ }
3536
+ function graphemeWidth(grapheme) {
3537
+ let width = 0;
3538
+ for (const character of grapheme) {
3539
+ width += codePointWidth(character.codePointAt(0) ?? 0);
3540
+ }
3541
+ return Math.min(width, 2);
3542
+ }
3543
+ function displayWidth(text) {
3544
+ let width = 0;
3545
+ for (const grapheme of graphemes(text)) {
3546
+ width += graphemeWidth(grapheme);
3547
+ }
3548
+ return width;
3549
+ }
3550
+ function truncateToWidth(text, width, ellipsis = "\u2026") {
3551
+ if (width <= 0) return "";
3552
+ if (displayWidth(text) <= width) return text;
3553
+ const markerWidth = displayWidth(ellipsis);
3554
+ if (width <= markerWidth) return ellipsis.slice(0, width);
3555
+ const budget = width - markerWidth;
3556
+ let used = 0;
3557
+ let result = "";
3558
+ for (const grapheme of graphemes(text)) {
3559
+ const next = graphemeWidth(grapheme);
3560
+ if (used + next > budget) break;
3561
+ result += grapheme;
3562
+ used += next;
3563
+ }
3564
+ return result + ellipsis;
3565
+ }
3566
+ function padToWidth(text, width, align) {
3567
+ const padding = width - displayWidth(text);
3568
+ if (padding <= 0) return text;
3569
+ if (align === "right") return " ".repeat(padding) + text;
3570
+ if (align === "left") return text + " ".repeat(padding);
3571
+ const left = Math.floor(padding / 2);
3572
+ return " ".repeat(left) + text + " ".repeat(padding - left);
3573
+ }
3574
+
3575
+ // src/present/table.ts
3576
+ var DEFAULT_MAX_COLUMN_WIDTH = 40;
3577
+ var DEFAULT_COLUMN_OVERHEAD = 3;
3578
+ var DEFAULT_TABLE_OVERHEAD = 1;
3579
+ function inferColumns(rows) {
3580
+ const names = [];
3581
+ const seen = /* @__PURE__ */ new Set();
3582
+ for (const row of rows) {
3583
+ for (const name of Object.keys(row)) {
3584
+ if (seen.has(name)) continue;
3585
+ seen.add(name);
3586
+ names.push(name);
3587
+ }
3588
+ }
3589
+ return names.map((name) => {
3590
+ let role = "unknown";
3591
+ let type;
3592
+ for (const row of rows) {
3593
+ const value = row[name];
3594
+ const candidate = valueRole(value);
3595
+ if (candidate === "none" || candidate === "unknown") continue;
3596
+ role = candidate;
3597
+ type = valueTypeName(value);
3598
+ break;
3599
+ }
3600
+ return { name, type, role, align: role === "number" ? "right" : "left" };
3601
+ });
3602
+ }
3603
+ function planTable(rows, columns, options = {}) {
3604
+ const maxColumnWidth = options.maxColumnWidth ?? DEFAULT_MAX_COLUMN_WIDTH;
3605
+ const columnOverhead = options.columnOverhead ?? DEFAULT_COLUMN_OVERHEAD;
3606
+ const tableOverhead = options.tableOverhead ?? DEFAULT_TABLE_OVERHEAD;
3607
+ const sample = options.sample ?? rows.length;
3608
+ const measured = rows.slice(0, Math.max(sample, 0));
3609
+ let truncated = false;
3610
+ const natural = columns.map((column) => {
3611
+ let width = displayWidth(column.name);
3612
+ for (const row of measured) {
3613
+ width = Math.max(width, displayWidth(formatValue(row[column.name])));
3614
+ }
3615
+ if (width > maxColumnWidth) truncated = true;
3616
+ return { column, width: Math.min(width, maxColumnWidth) };
3617
+ });
3618
+ if (options.width === void 0) {
3619
+ return { columns: natural, dropped: [], truncated };
3620
+ }
3621
+ const planned = [];
3622
+ const dropped = [];
3623
+ let used = tableOverhead;
3624
+ for (const entry of natural) {
3625
+ const required = entry.width + columnOverhead;
3626
+ if (planned.length > 0 && used + required > options.width) {
3627
+ dropped.push(entry.column);
3628
+ continue;
3629
+ }
3630
+ if (planned.length === 0 && used + required > options.width) {
3631
+ const available = options.width - used - columnOverhead;
3632
+ const width = Math.max(available, 1);
3633
+ if (width < entry.width) truncated = true;
3634
+ planned.push({ column: entry.column, width });
3635
+ used += width + columnOverhead;
3636
+ continue;
3637
+ }
3638
+ planned.push(entry);
3639
+ used += required;
3640
+ }
3641
+ return { columns: planned, dropped, truncated };
3642
+ }
3643
+ function cellText(value, width) {
3644
+ return truncateToWidth(formatValue(value), width);
3645
+ }
3646
+
3647
+ // src/syntax/keyword.ts
3648
+ var QUERY_TRANSFORMS = [
3649
+ "map",
3650
+ "extend",
3651
+ "by",
3652
+ "from",
3653
+ "where",
3654
+ "aggregate",
3655
+ "having",
3656
+ "sort",
3657
+ "distinct",
3658
+ "take",
3659
+ "offset"
3660
+ ];
3661
+ var JOINS_AND_SET_OPERATIONS = [
3662
+ "left",
3663
+ "inner",
3664
+ "natural",
3665
+ "join",
3666
+ "on",
3667
+ "using",
3668
+ "intersect",
3669
+ "except"
3670
+ ];
3671
+ var DML = ["insert", "into", "update", "set", "delete"];
3672
+ var CONTROL_FLOW = [
3673
+ "let",
3674
+ "if",
3675
+ "else",
3676
+ "end",
3677
+ "loop",
3678
+ "while",
3679
+ "break",
3680
+ "continue",
3681
+ "return"
3682
+ ];
3683
+ var FUNCTIONS_AND_CASTING = ["fun", "call", "apply", "cast"];
3684
+ var DDL_AND_SHAPE = [
3685
+ "describe",
3686
+ "show",
3687
+ "create",
3688
+ "alter",
3689
+ "drop",
3690
+ "filter",
3691
+ "gate",
3692
+ "flow",
3693
+ "window",
3694
+ "returning"
3695
+ ];
3696
+ var OPERATOR_KEYWORDS = ["in", "between", "like", "is", "with"];
3697
+ var OBJECT_TYPES = [
3698
+ "namespace",
3699
+ "sequence",
3700
+ "series",
3701
+ "subscription",
3702
+ "table",
3703
+ "ringbuffer",
3704
+ "column",
3705
+ "policy",
3706
+ "property",
3707
+ "view",
3708
+ "deferred",
3709
+ "transactional"
3710
+ ];
3711
+ var INDEX_AND_CONSTRAINTS = [
3712
+ "index",
3713
+ "unique",
3714
+ "primary",
3715
+ "key",
3716
+ "asc",
3717
+ "desc",
3718
+ "auto",
3719
+ "increment",
3720
+ "value"
3721
+ ];
3722
+ var MISC_OPERATIONS = [
3723
+ "exists",
3724
+ "replace",
3725
+ "cascade",
3726
+ "restrict",
3727
+ "to",
3728
+ "pause",
3729
+ "resume",
3730
+ "query",
3731
+ "rename",
3732
+ "rownum",
3733
+ "dictionary",
3734
+ "for",
3735
+ "output",
3736
+ "append",
3737
+ "assert",
3738
+ "patch"
3739
+ ];
3740
+ var ENUMS_AND_PATTERN_MATCHING = ["enum", "match"];
3741
+ var PROCEDURES_AND_EVENTS = ["procedure", "event", "handler", "dispatch", "tag"];
3742
+ var TESTING = ["test", "tests", "run"];
3743
+ var ACCESS_CONTROL = [
3744
+ "user",
3745
+ "role",
3746
+ "grant",
3747
+ "revoke",
3748
+ "password",
3749
+ "require",
3750
+ "execute",
3751
+ "access",
3752
+ "subscribe",
3753
+ "enable",
3754
+ "disable"
3755
+ ];
3756
+ var SYSTEM_OBJECTS = ["function", "session", "feature"];
3757
+ var MIGRATIONS = [
3758
+ "add",
3759
+ "migration",
3760
+ "migrate",
3761
+ "rollback",
3762
+ "diff",
3763
+ "version",
3764
+ "current",
3765
+ "pending"
3766
+ ];
3767
+ var MISC = ["authentication", "contains", "remote", "error"];
3768
+ var ADDITIONAL_CONSTRUCTS = ["derive", "group", "union", "as"];
3769
+ var WORD_OPERATORS = ["and", "or", "not", "xor"];
3770
+ var RQL_KEYWORDS = [
3771
+ ...QUERY_TRANSFORMS,
3772
+ ...JOINS_AND_SET_OPERATIONS,
3773
+ ...DML,
3774
+ ...CONTROL_FLOW,
3775
+ ...FUNCTIONS_AND_CASTING,
3776
+ ...DDL_AND_SHAPE,
3777
+ ...OPERATOR_KEYWORDS,
3778
+ ...OBJECT_TYPES,
3779
+ ...INDEX_AND_CONSTRAINTS,
3780
+ ...MISC_OPERATIONS,
3781
+ ...ENUMS_AND_PATTERN_MATCHING,
3782
+ ...PROCEDURES_AND_EVENTS,
3783
+ ...TESTING,
3784
+ ...ACCESS_CONTROL,
3785
+ ...SYSTEM_OBJECTS,
3786
+ ...MIGRATIONS,
3787
+ ...MISC,
3788
+ ...ADDITIONAL_CONSTRUCTS,
3789
+ ...WORD_OPERATORS
3790
+ ];
3791
+ var RQL_LITERALS = ["none", "true", "false"];
3792
+ var SIGNED_INTEGERS = ["int1", "int2", "int4", "int8", "int16", "int"];
3793
+ var UNSIGNED_INTEGERS = ["uint1", "uint2", "uint4", "uint8", "uint16", "uint"];
3794
+ var FLOATING_POINT = ["float4", "float8"];
3795
+ var TEXT_AND_BINARY = ["utf8", "blob", "text"];
3796
+ var BOOLEAN = ["bool", "boolean"];
3797
+ var NUMERIC = ["decimal"];
3798
+ var TEMPORAL = ["date", "datetime", "time", "duration", "interval"];
3799
+ var IDENTIFIERS_AND_UUIDS = [
3800
+ "uuid4",
3801
+ "uuid7",
3802
+ "identityid",
3803
+ "identity_id",
3804
+ "dictionaryid",
3805
+ "dictionary_id"
3806
+ ];
3807
+ var CONTAINERS = ["Option", "List", "Record", "Tuple"];
3808
+ var RQL_TYPES = [
3809
+ ...SIGNED_INTEGERS,
3810
+ ...UNSIGNED_INTEGERS,
3811
+ ...FLOATING_POINT,
3812
+ ...TEXT_AND_BINARY,
3813
+ ...BOOLEAN,
3814
+ ...NUMERIC,
3815
+ ...TEMPORAL,
3816
+ ...IDENTIFIERS_AND_UUIDS,
3817
+ ...CONTAINERS,
3818
+ "any"
3819
+ ];
3820
+ var RQL_OPERATORS = [
3821
+ "+",
3822
+ "-",
3823
+ "*",
3824
+ "/",
3825
+ "//",
3826
+ "%",
3827
+ "=",
3828
+ "==",
3829
+ "!=",
3830
+ "->",
3831
+ "=>",
3832
+ ">",
3833
+ "<",
3834
+ ">=",
3835
+ "<=",
3836
+ "~=",
3837
+ "&&",
3838
+ "||",
3839
+ "??"
3840
+ ];
3841
+ var RQL_SYSTEM_COLUMNS = ["rownum", "created_at", "updated_at"];
3842
+
3843
+ // src/syntax/tokenizer.ts
3844
+ var KEYWORD_SET = new Set(RQL_KEYWORDS.map((entry) => entry.toLowerCase()));
3845
+ var LITERAL_SET = new Set(RQL_LITERALS.map((entry) => entry.toLowerCase()));
3846
+ var TYPE_SET = new Set(RQL_TYPES.map((entry) => entry.toLowerCase()));
3847
+ var SYSTEM_COLUMN = /#(?:rownum|created_at|updated_at)\b/y;
3848
+ var COMMENT = /#.*/y;
3849
+ var NAMESPACE_HEAD = /[a-zA-Z_][\w$]*(?=\s*::)/y;
3850
+ var NAMESPACE_SEPARATOR = /::/y;
3851
+ var NAMED_ARGUMENT = /\w+\s*:(?!:)/y;
3852
+ var VARIABLE = /\$[\w$]+/y;
3853
+ var CALL_NAME = /[a-zA-Z_][\w$]*(?=\s*\()/y;
3854
+ var WORD = /[a-zA-Z_][\w$]*/y;
3855
+ var WHITESPACE = /[ \t\r\n]+/y;
3856
+ var BRACKET = /[{}()[\]]/y;
3857
+ var NUMBER = /[+-]?(?:[\d_]+(?:\.[\d_]+)?|\.[\d_]+)(?:[eE][+-]?\d+)?/y;
3858
+ var OPERATOR = /<<|>>|\.\.|==|!=|->|=>|>=|<=|~=|:=|&&|\|\||\?\?|\/\/|[+\-*/%|.=<>!&^?]/y;
3859
+ var DELIMITER = /[;,]/y;
3860
+ function match(pattern, source, position) {
3861
+ pattern.lastIndex = position;
3862
+ const result = pattern.exec(source);
3863
+ return result === null ? null : result[0];
3864
+ }
3865
+ function classifyWord(word) {
3866
+ const lowered = word.toLowerCase();
3867
+ if (KEYWORD_SET.has(lowered)) return "keyword";
3868
+ if (LITERAL_SET.has(lowered)) return "constant";
3869
+ if (TYPE_SET.has(lowered)) return "type";
3870
+ return "identifier";
3871
+ }
3872
+ function classifyCall(word) {
3873
+ const lowered = word.toLowerCase();
3874
+ if (KEYWORD_SET.has(lowered)) return "keyword";
3875
+ if (TYPE_SET.has(lowered)) return "type";
3876
+ return "function";
3877
+ }
3878
+ function scanString(source, position) {
3879
+ const quote = source[position];
3880
+ let index = position + 1;
3881
+ while (index < source.length) {
3882
+ const char = source[index];
3883
+ if (char === "\\") {
3884
+ index += 2;
3885
+ continue;
3886
+ }
3887
+ index++;
3888
+ if (char === quote) break;
3889
+ }
3890
+ return Math.min(index, source.length);
3891
+ }
3892
+ function tokenizeRql(source) {
3893
+ const tokens = [];
3894
+ let position = 0;
3895
+ let afterSeparator = false;
3896
+ const push = (kind, end) => {
3897
+ tokens.push({ kind, start: position, end });
3898
+ position = end;
3899
+ };
3900
+ while (position < source.length) {
3901
+ if (afterSeparator) {
3902
+ const white2 = match(WHITESPACE, source, position);
3903
+ if (white2 !== null) {
3904
+ push("white", position + white2.length);
3905
+ continue;
3906
+ }
3907
+ const chained = match(NAMESPACE_HEAD, source, position);
3908
+ if (chained !== null) {
3909
+ afterSeparator = false;
3910
+ push("namespace", position + chained.length);
3911
+ continue;
3912
+ }
3913
+ const called = match(CALL_NAME, source, position);
3914
+ if (called !== null) {
3915
+ afterSeparator = false;
3916
+ push("function", position + called.length);
3917
+ continue;
3918
+ }
3919
+ const leaf = match(WORD, source, position);
3920
+ if (leaf !== null) {
3921
+ afterSeparator = false;
3922
+ push("entity", position + leaf.length);
3923
+ continue;
3924
+ }
3925
+ afterSeparator = false;
3926
+ }
3927
+ const systemColumn = match(SYSTEM_COLUMN, source, position);
3928
+ if (systemColumn !== null) {
3929
+ push("variable.predefined", position + systemColumn.length);
3930
+ continue;
3931
+ }
3932
+ const comment = match(COMMENT, source, position);
3933
+ if (comment !== null) {
3934
+ push("comment", position + comment.length);
3935
+ continue;
3936
+ }
3937
+ const namespace = match(NAMESPACE_HEAD, source, position);
3938
+ if (namespace !== null) {
3939
+ push("namespace", position + namespace.length);
3940
+ continue;
3941
+ }
3942
+ const separator = match(NAMESPACE_SEPARATOR, source, position);
3943
+ if (separator !== null) {
3944
+ afterSeparator = true;
3945
+ push("operator", position + separator.length);
3946
+ continue;
3947
+ }
3948
+ const namedArgument = match(NAMED_ARGUMENT, source, position);
3949
+ if (namedArgument !== null) {
3950
+ push("key", position + namedArgument.length);
3951
+ continue;
3952
+ }
3953
+ const variable = match(VARIABLE, source, position);
3954
+ if (variable !== null) {
3955
+ push("variable", position + variable.length);
3956
+ continue;
3957
+ }
3958
+ const call = match(CALL_NAME, source, position);
3959
+ if (call !== null) {
3960
+ push(classifyCall(call), position + call.length);
3961
+ continue;
3962
+ }
3963
+ const word = match(WORD, source, position);
3964
+ if (word !== null) {
3965
+ push(classifyWord(word), position + word.length);
3966
+ continue;
3967
+ }
3968
+ const white = match(WHITESPACE, source, position);
3969
+ if (white !== null) {
3970
+ push("white", position + white.length);
3971
+ continue;
3972
+ }
3973
+ const bracket = match(BRACKET, source, position);
3974
+ if (bracket !== null) {
3975
+ push("bracket", position + bracket.length);
3976
+ continue;
3977
+ }
3978
+ const number = match(NUMBER, source, position);
3979
+ if (number !== null) {
3980
+ push("number", position + number.length);
3981
+ continue;
3982
+ }
3983
+ const char = source[position];
3984
+ if (char === '"' || char === "'") {
3985
+ push("string", scanString(source, position));
3986
+ continue;
3987
+ }
3988
+ const operator = match(OPERATOR, source, position);
3989
+ if (operator !== null) {
3990
+ push("operator", position + operator.length);
3991
+ continue;
3992
+ }
3993
+ const delimiter = match(DELIMITER, source, position);
3994
+ if (delimiter !== null) {
3995
+ push("delimiter", position + delimiter.length);
3996
+ continue;
3997
+ }
3998
+ push("identifier", position + 1);
3999
+ }
4000
+ return tokens;
4001
+ }
4002
+
3174
4003
  // src/types.ts
3175
4004
  var ReifyError = class extends Error {
3176
4005
  constructor(response) {
@@ -3217,11 +4046,19 @@ export {
3217
4046
  Int2Value,
3218
4047
  Int4Value,
3219
4048
  Int8Value,
4049
+ NONE_PRESENTATION,
3220
4050
  NONE_VALUE,
3221
4051
  NoneValue,
4052
+ ROW_NUMBER_KEY,
4053
+ RQL_KEYWORDS,
4054
+ RQL_LITERALS,
4055
+ RQL_OPERATORS,
4056
+ RQL_SYSTEM_COLUMNS,
4057
+ RQL_TYPES,
3222
4058
  ReifyError,
3223
4059
  Shape,
3224
4060
  ShapeBuilder,
4061
+ TYPE_CODE,
3225
4062
  TimeValue,
3226
4063
  Uint16Value,
3227
4064
  Uint1Value,
@@ -3233,10 +4070,27 @@ export {
3233
4070
  Uuid7Value,
3234
4071
  Value,
3235
4072
  asFrameResults,
4073
+ cellText,
4074
+ columnsToRows,
3236
4075
  decode,
4076
+ displayWidth,
4077
+ encodeParams,
4078
+ encodeValue,
4079
+ formatValue,
4080
+ graphemeWidth,
4081
+ inferColumns,
3237
4082
  isOptionType,
4083
+ padToWidth,
3238
4084
  parseValue,
4085
+ planTable,
4086
+ tokenizeRql,
4087
+ transformFrames,
4088
+ transformResult,
4089
+ truncateToWidth,
4090
+ typeNameFromCode,
3239
4091
  unwrapOptionType,
3240
- validateShape
4092
+ validateShape,
4093
+ valueRole,
4094
+ valueTypeName
3241
4095
  };
3242
4096
  //# sourceMappingURL=index.js.map