@math.gl/sql 5.0.0-beta.11 → 5.0.0-beta.12

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 (256) hide show
  1. package/dist/arrow/aggregates.js +1 -1
  2. package/dist/arrow/aggregates.js.map +1 -1
  3. package/dist/arrow/buffers.js.map +1 -1
  4. package/dist/arrow/cast.d.ts +1 -1
  5. package/dist/arrow/cast.js +1 -1
  6. package/dist/arrow/cast.js.map +1 -1
  7. package/dist/arrow/compiler.d.ts.map +1 -1
  8. package/dist/arrow/compiler.js +146 -29
  9. package/dist/arrow/compiler.js.map +1 -1
  10. package/dist/arrow/index.cjs +124 -23
  11. package/dist/arrow/index.cjs.map +1 -1
  12. package/dist/arrow/index.d.ts.map +1 -1
  13. package/dist/arrow/index.js.map +1 -1
  14. package/dist/arrow/list-values.d.ts.map +1 -1
  15. package/dist/arrow/list-values.js +27 -6
  16. package/dist/arrow/list-values.js.map +1 -1
  17. package/dist/arrow/predicate.d.ts.map +1 -1
  18. package/dist/arrow/predicate.js.map +1 -1
  19. package/dist/arrow/query-arrow-sql.d.ts +2 -2
  20. package/dist/arrow/query-arrow-sql.d.ts.map +1 -1
  21. package/dist/arrow/query-arrow-sql.js +40 -8
  22. package/dist/arrow/query-arrow-sql.js.map +1 -1
  23. package/dist/arrow/query-arrow-table.d.ts +1 -1
  24. package/dist/arrow/query-arrow-table.d.ts.map +1 -1
  25. package/dist/arrow/query-arrow-table.js +1 -1
  26. package/dist/arrow/query-arrow-table.js.map +1 -1
  27. package/dist/arrow/query-worker.d.ts +1 -1
  28. package/dist/arrow/query-worker.d.ts.map +1 -1
  29. package/dist/arrow/query-worker.js +1 -1
  30. package/dist/arrow/query-worker.js.map +1 -1
  31. package/dist/arrow/registry.d.ts.map +1 -1
  32. package/dist/arrow/registry.js +1 -0
  33. package/dist/arrow/registry.js.map +1 -1
  34. package/dist/arrow/relational-query.js +2 -2
  35. package/dist/arrow/relational-query.js.map +1 -1
  36. package/dist/arrow/temporal-cast.js.map +1 -1
  37. package/dist/arrow/types.d.ts.map +1 -1
  38. package/dist/arrow/unnest-arrow-column.d.ts +22 -0
  39. package/dist/arrow/unnest-arrow-column.d.ts.map +1 -0
  40. package/dist/arrow/unnest-arrow-column.js +112 -0
  41. package/dist/arrow/unnest-arrow-column.js.map +1 -0
  42. package/dist/arrow/worker.cjs +130 -29
  43. package/dist/arrow/worker.cjs.map +1 -1
  44. package/dist/arrow/worker.d.ts +1 -1
  45. package/dist/arrow/worker.d.ts.map +1 -1
  46. package/dist/arrow/worker.js +1 -1
  47. package/dist/arrow/worker.js.map +1 -1
  48. package/dist/{color → color-functions}/index.cjs +2 -2
  49. package/dist/{color → color-functions}/index.cjs.map +1 -1
  50. package/dist/color-functions/index.d.ts.map +1 -0
  51. package/dist/color-functions/index.js.map +1 -0
  52. package/dist/expressions/ast.js +3 -1
  53. package/dist/expressions/ast.js.map +1 -1
  54. package/dist/expressions/expression-eval.d.ts +10 -1
  55. package/dist/expressions/expression-eval.d.ts.map +1 -1
  56. package/dist/expressions/expression-eval.js.map +1 -1
  57. package/dist/expressions/index.cjs +1 -1
  58. package/dist/expressions/index.cjs.map +1 -1
  59. package/dist/index.cjs +2792 -2363
  60. package/dist/index.cjs.map +3 -3
  61. package/dist/index.d.ts +2 -0
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +1 -0
  64. package/dist/index.js.map +1 -1
  65. package/dist/{math → math-functions}/index.cjs +2 -2
  66. package/dist/{math → math-functions}/index.cjs.map +1 -1
  67. package/dist/math-functions/index.d.ts.map +1 -0
  68. package/dist/math-functions/index.js.map +1 -0
  69. package/dist/parser/bind-sql-predicate.d.ts.map +1 -0
  70. package/dist/parser/bind-sql-predicate.js.map +1 -0
  71. package/dist/{sql → parser}/index.cjs +348 -129
  72. package/dist/parser/index.cjs.map +6 -0
  73. package/dist/{sql → parser}/index.d.ts +2 -0
  74. package/dist/parser/index.d.ts.map +1 -0
  75. package/dist/{sql → parser}/index.js +1 -0
  76. package/dist/parser/index.js.map +1 -0
  77. package/dist/parser/parameter-snapshots.d.ts +21 -0
  78. package/dist/parser/parameter-snapshots.d.ts.map +1 -0
  79. package/dist/parser/parameter-snapshots.js +85 -0
  80. package/dist/parser/parameter-snapshots.js.map +1 -0
  81. package/dist/{sql → parser}/parse-sql-expression.d.ts +8 -2
  82. package/dist/parser/parse-sql-expression.d.ts.map +1 -0
  83. package/dist/{sql → parser}/parse-sql-expression.js +51 -5
  84. package/dist/parser/parse-sql-expression.js.map +1 -0
  85. package/dist/parser/parse-sql-predicate.d.ts.map +1 -0
  86. package/dist/parser/parse-sql-predicate.js.map +1 -0
  87. package/dist/{sql → parser}/parse-sql-query.d.ts +2 -1
  88. package/dist/parser/parse-sql-query.d.ts.map +1 -0
  89. package/dist/{sql → parser}/parse-sql-query.js +21 -6
  90. package/dist/parser/parse-sql-query.js.map +1 -0
  91. package/dist/{sql → parser}/parse-sql-select.d.ts +4 -2
  92. package/dist/parser/parse-sql-select.d.ts.map +1 -0
  93. package/dist/{sql → parser}/parse-sql-select.js +6 -3
  94. package/dist/parser/parse-sql-select.js.map +1 -0
  95. package/dist/{sql → parser}/parse-sql-statement.d.ts +9 -5
  96. package/dist/parser/parse-sql-statement.d.ts.map +1 -0
  97. package/dist/{sql → parser}/parse-sql-statement.js +39 -14
  98. package/dist/parser/parse-sql-statement.js.map +1 -0
  99. package/dist/parser/query-types.d.ts.map +1 -0
  100. package/dist/parser/query-types.js.map +1 -0
  101. package/dist/parser/sql-parameters.d.ts +29 -0
  102. package/dist/parser/sql-parameters.d.ts.map +1 -0
  103. package/dist/parser/sql-parameters.js +110 -0
  104. package/dist/parser/sql-parameters.js.map +1 -0
  105. package/dist/parser/sql-parse-error.d.ts.map +1 -0
  106. package/dist/parser/sql-parse-error.js.map +1 -0
  107. package/dist/parser/sql-predicate-schema.d.ts.map +1 -0
  108. package/dist/parser/sql-predicate-schema.js.map +1 -0
  109. package/dist/parser/sql-predicate-types.d.ts.map +1 -0
  110. package/dist/parser/sql-predicate-types.js.map +1 -0
  111. package/dist/parser/sql-predicate.d.ts.map +1 -0
  112. package/dist/parser/sql-predicate.js.map +1 -0
  113. package/dist/scales/arrow-domains.d.ts +8 -1
  114. package/dist/scales/arrow-domains.d.ts.map +1 -1
  115. package/dist/scales/arrow-domains.js +48 -6
  116. package/dist/scales/arrow-domains.js.map +1 -1
  117. package/dist/scales/arrow-scales.d.ts +28 -4
  118. package/dist/scales/arrow-scales.d.ts.map +1 -1
  119. package/dist/scales/arrow-scales.js +158 -12
  120. package/dist/scales/arrow-scales.js.map +1 -1
  121. package/dist/scales/compile-scale.d.ts +1 -6
  122. package/dist/scales/compile-scale.d.ts.map +1 -1
  123. package/dist/scales/compile-scale.js +1 -191
  124. package/dist/scales/compile-scale.js.map +1 -1
  125. package/dist/scales/conformance.cjs +180 -0
  126. package/dist/scales/conformance.cjs.map +6 -0
  127. package/dist/scales/conformance.d.ts +26 -0
  128. package/dist/scales/conformance.d.ts.map +1 -0
  129. package/dist/scales/conformance.js +164 -0
  130. package/dist/scales/conformance.js.map +1 -0
  131. package/dist/scales/hdr-colors.d.ts +1 -12
  132. package/dist/scales/hdr-colors.d.ts.map +1 -1
  133. package/dist/scales/hdr-colors.js +1 -111
  134. package/dist/scales/hdr-colors.js.map +1 -1
  135. package/dist/scales/index.cjs +3738 -618
  136. package/dist/scales/index.cjs.map +3 -3
  137. package/dist/scales/index.d.ts +2 -1
  138. package/dist/scales/index.d.ts.map +1 -1
  139. package/dist/scales/index.js +2 -1
  140. package/dist/scales/index.js.map +1 -1
  141. package/dist/scales/palettes.d.ts +1 -4
  142. package/dist/scales/palettes.d.ts.map +1 -1
  143. package/dist/scales/palettes.js +1 -81
  144. package/dist/scales/palettes.js.map +1 -1
  145. package/dist/scales/parse-sql-scale.d.ts +14 -1
  146. package/dist/scales/parse-sql-scale.d.ts.map +1 -1
  147. package/dist/scales/parse-sql-scale.js +110 -18
  148. package/dist/scales/parse-sql-scale.js.map +1 -1
  149. package/dist/scales/quantile-sketch.d.ts +1 -21
  150. package/dist/scales/quantile-sketch.d.ts.map +1 -1
  151. package/dist/scales/quantile-sketch.js +1 -104
  152. package/dist/scales/quantile-sketch.js.map +1 -1
  153. package/dist/scales/scale-types.d.ts +1 -91
  154. package/dist/scales/scale-types.d.ts.map +1 -1
  155. package/dist/scales/scale-types.js +1 -115
  156. package/dist/scales/scale-types.js.map +1 -1
  157. package/dist/scales/streaming-domains.d.ts.map +1 -1
  158. package/dist/scales/streaming-domains.js +8 -2
  159. package/dist/scales/streaming-domains.js.map +1 -1
  160. package/package.json +19 -19
  161. package/src/arrow/aggregates.ts +1 -1
  162. package/src/arrow/buffers.ts +1 -1
  163. package/src/arrow/cast.ts +3 -3
  164. package/src/arrow/compiler.ts +155 -36
  165. package/src/arrow/index.ts +1 -1
  166. package/src/arrow/list-values.ts +28 -7
  167. package/src/arrow/predicate.ts +1 -1
  168. package/src/arrow/query-arrow-sql.ts +38 -10
  169. package/src/arrow/query-arrow-table.ts +3 -3
  170. package/src/arrow/query-worker.ts +5 -1
  171. package/src/arrow/registry.ts +1 -0
  172. package/src/arrow/relational-query.ts +2 -2
  173. package/src/arrow/temporal-cast.ts +1 -1
  174. package/src/arrow/types.ts +1 -1
  175. package/src/arrow/unnest-arrow-column.ts +145 -0
  176. package/src/arrow/worker.ts +6 -2
  177. package/src/expressions/ast.ts +3 -1
  178. package/src/expressions/expression-eval.ts +10 -1
  179. package/src/index.ts +14 -0
  180. package/src/{math → math-functions}/index.ts +2 -2
  181. package/src/{sql → parser}/index.ts +15 -0
  182. package/src/parser/parameter-snapshots.ts +103 -0
  183. package/src/{sql → parser}/parse-sql-expression.ts +49 -6
  184. package/src/{sql → parser}/parse-sql-query.ts +24 -6
  185. package/src/{sql → parser}/parse-sql-select.ts +14 -8
  186. package/src/{sql → parser}/parse-sql-statement.ts +42 -24
  187. package/src/{sql → parser}/query-types.ts +1 -1
  188. package/src/parser/sql-parameters.ts +174 -0
  189. package/src/scales/arrow-domains.ts +66 -7
  190. package/src/scales/arrow-scales.ts +221 -14
  191. package/src/scales/compile-scale.ts +1 -213
  192. package/src/scales/conformance.ts +186 -0
  193. package/src/scales/hdr-colors.ts +1 -122
  194. package/src/scales/index.ts +3 -1
  195. package/src/scales/palettes.ts +1 -82
  196. package/src/scales/parse-sql-scale.ts +126 -21
  197. package/src/scales/quantile-sketch.ts +1 -102
  198. package/src/scales/scale-types.ts +1 -220
  199. package/src/scales/streaming-domains.ts +8 -2
  200. package/dist/color/index.d.ts.map +0 -1
  201. package/dist/color/index.js.map +0 -1
  202. package/dist/math/index.d.ts.map +0 -1
  203. package/dist/math/index.js.map +0 -1
  204. package/dist/scales/arrow-scales.cjs +0 -3837
  205. package/dist/scales/arrow-scales.cjs.map +0 -6
  206. package/dist/sql/bind-sql-predicate.d.ts.map +0 -1
  207. package/dist/sql/bind-sql-predicate.js.map +0 -1
  208. package/dist/sql/index.cjs.map +0 -6
  209. package/dist/sql/index.d.ts.map +0 -1
  210. package/dist/sql/index.js.map +0 -1
  211. package/dist/sql/parse-sql-expression.d.ts.map +0 -1
  212. package/dist/sql/parse-sql-expression.js.map +0 -1
  213. package/dist/sql/parse-sql-predicate.d.ts.map +0 -1
  214. package/dist/sql/parse-sql-predicate.js.map +0 -1
  215. package/dist/sql/parse-sql-query.d.ts.map +0 -1
  216. package/dist/sql/parse-sql-query.js.map +0 -1
  217. package/dist/sql/parse-sql-select.d.ts.map +0 -1
  218. package/dist/sql/parse-sql-select.js.map +0 -1
  219. package/dist/sql/parse-sql-statement.d.ts.map +0 -1
  220. package/dist/sql/parse-sql-statement.js.map +0 -1
  221. package/dist/sql/query-types.d.ts.map +0 -1
  222. package/dist/sql/query-types.js.map +0 -1
  223. package/dist/sql/sql-parse-error.d.ts.map +0 -1
  224. package/dist/sql/sql-parse-error.js.map +0 -1
  225. package/dist/sql/sql-predicate-schema.d.ts.map +0 -1
  226. package/dist/sql/sql-predicate-schema.js.map +0 -1
  227. package/dist/sql/sql-predicate-types.d.ts.map +0 -1
  228. package/dist/sql/sql-predicate-types.js.map +0 -1
  229. package/dist/sql/sql-predicate.d.ts.map +0 -1
  230. package/dist/sql/sql-predicate.js.map +0 -1
  231. package/src/expressions/vendor/jsep/README.md +0 -22
  232. /package/dist/{color → color-functions}/index.d.ts +0 -0
  233. /package/dist/{color → color-functions}/index.js +0 -0
  234. /package/dist/{math → math-functions}/index.d.ts +0 -0
  235. /package/dist/{math → math-functions}/index.js +0 -0
  236. /package/dist/{sql → parser}/bind-sql-predicate.d.ts +0 -0
  237. /package/dist/{sql → parser}/bind-sql-predicate.js +0 -0
  238. /package/dist/{sql → parser}/parse-sql-predicate.d.ts +0 -0
  239. /package/dist/{sql → parser}/parse-sql-predicate.js +0 -0
  240. /package/dist/{sql → parser}/query-types.d.ts +0 -0
  241. /package/dist/{sql → parser}/query-types.js +0 -0
  242. /package/dist/{sql → parser}/sql-parse-error.d.ts +0 -0
  243. /package/dist/{sql → parser}/sql-parse-error.js +0 -0
  244. /package/dist/{sql → parser}/sql-predicate-schema.d.ts +0 -0
  245. /package/dist/{sql → parser}/sql-predicate-schema.js +0 -0
  246. /package/dist/{sql → parser}/sql-predicate-types.d.ts +0 -0
  247. /package/dist/{sql → parser}/sql-predicate-types.js +0 -0
  248. /package/dist/{sql → parser}/sql-predicate.d.ts +0 -0
  249. /package/dist/{sql → parser}/sql-predicate.js +0 -0
  250. /package/src/{color → color-functions}/index.ts +0 -0
  251. /package/src/{sql → parser}/bind-sql-predicate.ts +0 -0
  252. /package/src/{sql → parser}/parse-sql-predicate.ts +0 -0
  253. /package/src/{sql → parser}/sql-parse-error.ts +0 -0
  254. /package/src/{sql → parser}/sql-predicate-schema.ts +0 -0
  255. /package/src/{sql → parser}/sql-predicate-types.ts +0 -0
  256. /package/src/{sql → parser}/sql-predicate.ts +0 -0
@@ -14,138 +14,165 @@ var __copyProps = (to, from, except, desc) => {
14
14
  }
15
15
  return to;
16
16
  };
17
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
17
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
19
 
19
20
  // dist/scales/index.js
20
21
  var index_exports = {};
21
22
  __export(index_exports, {
22
- ScaleQuantileSketch: () => ScaleQuantileSketch,
23
- compileResolvedScale: () => compileResolvedScale,
24
- convertScaleColor: () => convertScaleColor,
25
- createResolvedScale: () => createResolvedScale,
26
- decodeSRGB: () => decodeSRGB,
27
- encodeSRGB: () => encodeSRGB,
28
- listScalePalettes: () => listScalePalettes,
29
- packFloat16: () => packFloat16,
30
- packRGBA16F: () => packRGBA16F,
23
+ ScaleQuantileSketch: () => quantile_sketch_exports.ScaleQuantileSketch,
24
+ bindArrowScale: () => bindArrowScale,
25
+ compileArrowScale: () => compileArrowScale,
26
+ compileArrowScaleExpression: () => compileArrowScaleExpression,
27
+ compileArrowScales: () => compileArrowScales,
28
+ compileResolvedScale: () => compile_scale_exports.compileResolvedScale,
29
+ convertScaleColor: () => hdr_colors_exports.convertScaleColor,
30
+ createResolvedScale: () => scale_types_exports.createResolvedScale,
31
+ createStreamingDomainAccumulator: () => createStreamingDomainAccumulator,
32
+ decodeSRGB: () => hdr_colors_exports.decodeSRGB,
33
+ encodeSRGB: () => hdr_colors_exports.encodeSRGB,
34
+ listScalePalettes: () => palettes_exports.listScalePalettes,
35
+ packFloat16: () => hdr_colors_exports.packFloat16,
36
+ packRGBA16F: () => hdr_colors_exports.packRGBA16F,
37
+ parseSQLScaleAST: () => parseSQLScaleAST,
31
38
  parseSQLScaleExpression: () => parseSQLScaleExpression,
32
- resolvePalette: () => resolvePalette,
33
- unpackFloat16: () => unpackFloat16,
34
- validateScaleSpec: () => validateScaleSpec
39
+ resolvePalette: () => palettes_exports.resolvePalette,
40
+ unpackFloat16: () => hdr_colors_exports.unpackFloat16,
41
+ validateScaleSpec: () => scale_types_exports.validateScaleSpec
35
42
  });
36
43
  module.exports = __toCommonJS(index_exports);
37
44
 
38
45
  // dist/scales/scale-types.js
39
- function assertFinite(value, description) {
40
- if (!Number.isFinite(value))
41
- throw new Error(`${description} must be finite`);
42
- }
43
- function checkColor(color2, label) {
44
- if (!Array.isArray(color2) || color2.length !== 4) {
45
- throw new Error(`${label} must contain four channels`);
46
- }
47
- color2.forEach((channel, index) => assertFinite(channel, `${label}[${index}]`));
48
- }
49
- function validateScaleSpec(spec) {
50
- var _a;
51
- if (!spec || typeof spec !== "object")
52
- throw new Error("Scale specification is required");
53
- const kinds = [
54
- "linear",
55
- "sqrt",
56
- "log",
57
- "symlog",
58
- "sequential",
59
- "diverging",
60
- "ordinal",
61
- "quantize",
62
- "quantile",
63
- "threshold",
64
- "piecewise"
65
- ];
66
- if (!kinds.includes(spec.type))
67
- throw new Error(`Unsupported scale type: ${spec.type}`);
68
- const minimumStops = spec.type === "ordinal" ? 1 : 2;
69
- const domain = spec.domain;
70
- if (!domain || typeof domain !== "object")
71
- throw new Error("Scale domain is required");
72
- switch (domain.type) {
73
- case "fixed":
74
- if (!Array.isArray(domain.values) || domain.values.length < minimumStops || domain.values.some((value) => typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value))))
75
- throw new Error(`Fixed scale domain requires at least ${minimumStops} finite or string values`);
76
- break;
77
- case "extent":
78
- case "categories":
79
- if (typeof domain.column !== "string" || !domain.column) {
80
- throw new Error("Scale domain requires a column");
81
- }
82
- break;
83
- case "symmetric":
84
- if (!domain.column)
85
- throw new Error("Symmetric domain requires a column");
86
- assertFinite(domain.pivot, "Scale pivot");
87
- break;
88
- case "percentile":
89
- if (!domain.column || !Number.isFinite(domain.lower) || !Number.isFinite(domain.upper) || domain.lower < 0 || domain.upper > 1 || domain.lower >= domain.upper) {
90
- throw new Error("Percentile domain requires 0 <= lower < upper <= 1");
91
- }
92
- break;
93
- default:
94
- throw new Error("Unsupported scale domain");
95
- }
96
- if (!spec.range)
97
- throw new Error("Scale range is required");
98
- switch (spec.range.type) {
99
- case "numeric":
100
- if (spec.range.values.length < minimumStops)
101
- throw new Error(`Numeric range requires at least ${minimumStops} values`);
102
- spec.range.values.forEach((value) => assertFinite(value, "Numeric range"));
103
- break;
104
- case "colors":
105
- if (spec.range.values.length < minimumStops)
106
- throw new Error(`Color range requires at least ${minimumStops} colors`);
107
- spec.range.values.forEach((value, index) => checkColor(value, `Color stop ${index}`));
108
- break;
109
- case "palette":
110
- if (!spec.range.name || spec.range.count !== void 0 && (!Number.isSafeInteger(spec.range.count) || spec.range.count < 2)) {
111
- throw new Error("Palette requires a name and optional positive count >= 2");
46
+ var scale_types_exports = {};
47
+ __reExport(scale_types_exports, require("@math.gl/core/color"));
48
+
49
+ // dist/parser/parameter-snapshots.js
50
+ var snapshots = /* @__PURE__ */ new WeakMap();
51
+ function isSQLParameterSnapshot(value) {
52
+ return Boolean(value && typeof value === "object" && snapshots.has(value));
53
+ }
54
+ function snapshotParameter(snapshot, name2) {
55
+ const expression = storage(snapshot).get(name2);
56
+ if (!expression)
57
+ throw new Error(`SQL parameter ":${name2}" requires a value`);
58
+ return structuredClone(expression);
59
+ }
60
+ function storage(snapshot) {
61
+ const values = snapshots.get(snapshot);
62
+ if (!values)
63
+ throw new Error("Invalid SQL parameter snapshot");
64
+ return values;
65
+ }
66
+
67
+ // dist/expressions/scalar-type.js
68
+ var COLUMNAR_SCALAR_TYPES = Object.freeze([
69
+ "int8",
70
+ "int16",
71
+ "int32",
72
+ "int64",
73
+ "uint8",
74
+ "uint16",
75
+ "uint32",
76
+ "uint64",
77
+ "float16",
78
+ "float32",
79
+ "float64",
80
+ "bool",
81
+ "utf8"
82
+ ]);
83
+ function isColumnarScalarType(value) {
84
+ return typeof value === "string" && COLUMNAR_SCALAR_TYPES.includes(value);
85
+ }
86
+
87
+ // dist/expressions/columnar-predicate.js
88
+ function isColumnarPredicateValue(value) {
89
+ return typeof value === "boolean" || typeof value === "bigint" || typeof value === "string" || typeof value === "number" && Number.isFinite(value) || value instanceof Date && Number.isFinite(value.getTime()) || value instanceof Uint8Array;
90
+ }
91
+
92
+ // dist/parser/sql-parameters.js
93
+ function validateSQLParameterOptions(options) {
94
+ if (options.parameters !== void 0 && options.parseParameter !== void 0) {
95
+ throw new Error("SQL parameters and parseParameter are mutually exclusive");
96
+ }
97
+ }
98
+ function bindSQLParameter(name2, options) {
99
+ if (options.parseParameter)
100
+ return options.parseParameter(name2);
101
+ if (isSQLParameterSnapshot(options.parameters))
102
+ return snapshotParameter(options.parameters, name2);
103
+ if (!Object.prototype.hasOwnProperty.call(options.parameters ?? {}, name2)) {
104
+ throw new Error(`SQL parameter ":${name2}" requires a value`);
105
+ }
106
+ const value = options.parameters[name2];
107
+ if (isNumericArray(value) && !(value instanceof Uint8Array))
108
+ return typedArrayLiteral(value, name2);
109
+ if (value && typeof value === "object" && "type" in value) {
110
+ if (value.type === "array" && isNumericArray(value.value))
111
+ return typedArrayLiteral(value.value, name2);
112
+ if ((value.type === "utf8" || value.type === "binary") && value.value instanceof Uint8Array) {
113
+ const data = new Uint8Array(value.value);
114
+ if (value.type === "binary")
115
+ return literal(data);
116
+ try {
117
+ return {
118
+ type: "Literal",
119
+ value: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(data),
120
+ utf8: data
121
+ };
122
+ } catch {
123
+ throw new Error(`SQL parameter ":${name2}" requires valid UTF-8 bytes`);
112
124
  }
113
- break;
114
- default:
115
- throw new Error("Unsupported scale range");
125
+ }
126
+ throw new Error(`SQL parameter ":${name2}" has an unsupported binary format`);
116
127
  }
117
- if (spec.pivot !== void 0)
118
- assertFinite(spec.pivot, "Scale pivot");
119
- if (((_a = spec.encoding) == null ? void 0 : _a.referenceWhiteNits) !== void 0 && (!Number.isFinite(spec.encoding.referenceWhiteNits) || spec.encoding.referenceWhiteNits <= 0)) {
120
- throw new Error("referenceWhiteNits must be a positive finite number");
128
+ if (Array.isArray(value)) {
129
+ if (value.length > 4096 || !(Array.from(value).every((item) => typeof item === "number" && Number.isFinite(item)) || Array.from(value).every((item) => typeof item === "string") || Array.from(value).every((item) => (Array.isArray(item) || isNumericArray(item)) && item.length <= 4096 && Array.from(item).every((component) => typeof component === "number" && Number.isFinite(component)))))
130
+ throw new Error(`SQL parameter ":${name2}" has an unsupported array value`);
131
+ return arrayLiteral(value, name2);
121
132
  }
122
- for (const key of ["nullColor", "underColor", "overColor"]) {
123
- if (spec[key])
124
- checkColor(spec[key], key);
133
+ if (value !== null && !isColumnarPredicateValue(value)) {
134
+ throw new Error(`SQL parameter ":${name2}" has an unsupported value`);
125
135
  }
126
- return spec;
136
+ return literal(value instanceof Date ? new Date(value.getTime()) : value instanceof Uint8Array ? new Uint8Array(value) : value);
127
137
  }
128
- function createResolvedScale(descriptor, sample) {
129
- validateScaleSpec({
130
- ...descriptor,
131
- domain: { type: "fixed", values: descriptor.domain }
132
- });
133
- if (descriptor.thresholds) {
134
- if (descriptor.thresholds.some((value, i) => !Number.isFinite(value) || i > 0 && value <= descriptor.thresholds[i - 1])) {
135
- throw new Error("Thresholds must be finite and strictly ordered");
136
- }
138
+ function arrayLiteral(values, name2) {
139
+ return {
140
+ type: "ArrayExpression",
141
+ elements: Array.from(values, (value) => Array.isArray(value) ? arrayLiteral(value, name2) : isNumericArray(value) ? typedArrayLiteral(value, name2) : literal(value))
142
+ };
143
+ }
144
+ function literal(value) {
145
+ if (value instanceof Uint8Array || value instanceof Date)
146
+ return { type: "Literal", value };
147
+ return {
148
+ type: "Literal",
149
+ value,
150
+ raw: typeof value === "bigint" ? String(value) : JSON.stringify(value)
151
+ };
152
+ }
153
+ function isNumericArray(value) {
154
+ return value instanceof Float32Array || value instanceof Float64Array || value instanceof Int8Array || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int16Array || value instanceof Uint16Array || value instanceof Int32Array || value instanceof Uint32Array;
155
+ }
156
+ function typedArrayLiteral(value, name2) {
157
+ if (value.length > 4096 || !value.every(Number.isFinite)) {
158
+ throw new Error(`SQL parameter ":${name2}" has an unsupported numeric array`);
137
159
  }
138
- return Object.freeze({ descriptor, sample });
160
+ const typedValues = value instanceof Uint8Array ? new Uint8Array(value) : value.slice();
161
+ return {
162
+ type: "ArrayExpression",
163
+ elements: Array.from(typedValues, (item) => literal(item)),
164
+ typedValues
165
+ };
139
166
  }
140
167
 
141
- // dist/sql/sql-parse-error.js
168
+ // dist/parser/sql-parse-error.js
142
169
  var SQLParseError = class extends SyntaxError {
143
170
  constructor(reason, source, offset) {
144
- var _a;
171
+ var _a2;
145
172
  const lines = source.slice(0, offset).split(/\r\n|\r|\n/);
146
173
  const line = lines.length;
147
174
  const column = lines[line - 1].length + 1;
148
- const token = ((_a = source.slice(offset, offset + 32).match(/^:?\w+|^\S/u)) == null ? void 0 : _a[0]) ?? null;
175
+ const token = ((_a2 = source.slice(offset, offset + 32).match(/^:?\w+|^\S/u)) == null ? void 0 : _a2[0]) ?? null;
149
176
  super(`${reason} (line ${line}, column ${column}${token ? `, near ${JSON.stringify(token)}` : ""})`);
150
177
  this.reason = reason;
151
178
  this.name = "SQLParseError";
@@ -156,7 +183,7 @@ var SQLParseError = class extends SyntaxError {
156
183
  }
157
184
  };
158
185
 
159
- // dist/sql/parse-sql-expression.js
186
+ // dist/parser/parse-sql-expression.js
160
187
  var SQL_RESERVED_WORDS = /* @__PURE__ */ new Set([
161
188
  "SELECT",
162
189
  "ARRAY",
@@ -208,8 +235,8 @@ function sqlCastType(name2, source = name2, offset = 0) {
208
235
  throw new SQLParseError(`Unsupported SQL CAST type: ${name2}`, source, offset);
209
236
  }
210
237
  }
211
- function parseSQLExpression(source) {
212
- const reader = new SQLExpressionReader(source);
238
+ function parseSQLExpression(source, options = {}) {
239
+ const reader = new SQLExpressionReader(source, 0, options);
213
240
  const expression = reader.expression();
214
241
  reader.space();
215
242
  if (reader.position !== source.length)
@@ -217,10 +244,12 @@ function parseSQLExpression(source) {
217
244
  return expression;
218
245
  }
219
246
  var SQLExpressionReader = class {
220
- constructor(source, start = 0) {
247
+ constructor(source, start = 0, parameterOptions = {}) {
221
248
  this.source = source;
249
+ this.parameterOptions = parameterOptions;
222
250
  this.tokens = 0;
223
251
  this.depths = /* @__PURE__ */ new WeakMap();
252
+ validateSQLParameterOptions(parameterOptions);
224
253
  this.position = start;
225
254
  if (start === 0 && (typeof source !== "string" || !source.trim() || source.length > 65536))
226
255
  throw new SQLParseError("SQL expression requires non-empty input of at most 65536 characters", "", 0);
@@ -306,6 +335,28 @@ var SQLExpressionReader = class {
306
335
  }
307
336
  if (minimum <= 3) {
308
337
  const negate = this.word("NOT");
338
+ if (this.word("IN")) {
339
+ this.symbol("(");
340
+ const values = [];
341
+ do {
342
+ const item = this.expression(depth + 1);
343
+ if (item.type === "ArrayExpression") {
344
+ for (const element of item.elements) {
345
+ if (element.type !== "Literal")
346
+ throw this.error("SQL IN requires scalar values");
347
+ values.push(element);
348
+ }
349
+ } else
350
+ values.push(item);
351
+ if (values.length > 4096)
352
+ throw this.error("SQL IN exceeds 4096 values");
353
+ } while (this.take(","));
354
+ this.symbol(")");
355
+ left = values.length ? this.call("in", [left, ...values]) : this.node({ type: "Literal", value: false, raw: "false" });
356
+ if (negate)
357
+ left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
358
+ continue;
359
+ }
309
360
  const insensitive = this.word("ILIKE");
310
361
  if (insensitive || this.word("LIKE")) {
311
362
  const pattern = this.expression(depth + 1, 4);
@@ -363,7 +414,7 @@ var SQLExpressionReader = class {
363
414
  this.position += width[0].length;
364
415
  this.token();
365
416
  this.symbol("]");
366
- const array = argument;
417
+ const array = argument.type === "ArrayExpression" ? { type: "CallExpression", callee: { name: "array" }, arguments: argument.elements } : argument;
367
418
  if (array.type !== "CallExpression" || !/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(array.callee.name) || Number(width[0]) !== array.arguments.length)
368
419
  throw this.error("ARRAY cast length must match the number of components");
369
420
  const type = sqlCastType(match[0], this.source, start);
@@ -412,7 +463,7 @@ var SQLExpressionReader = class {
412
463
  }
413
464
  /** Parses binary aliases and optional positive byte widths. */
414
465
  binaryCastType(name2) {
415
- var _a;
466
+ var _a2;
416
467
  if (name2 === "blob")
417
468
  return "binary";
418
469
  if (!this.take("(")) {
@@ -421,7 +472,7 @@ var SQLExpressionReader = class {
421
472
  return "binary";
422
473
  }
423
474
  this.space();
424
- const width = ((_a = /^\d+/.exec(this.source.slice(this.position))) == null ? void 0 : _a[0]) ?? "";
475
+ const width = ((_a2 = /^\d+/.exec(this.source.slice(this.position))) == null ? void 0 : _a2[0]) ?? "";
425
476
  const byteWidth = Number(width);
426
477
  if (byteWidth < 1 || byteWidth > 2147483647)
427
478
  throw this.error("Binary CAST requires a positive 32-bit byte width");
@@ -509,11 +560,11 @@ var SQLExpressionReader = class {
509
560
  }
510
561
  /** Reads an identifier in projections, source references, and field access. */
511
562
  readIdentifier(member = false) {
512
- var _a;
563
+ var _a2;
513
564
  this.space();
514
565
  if (this.source[this.position] === '"')
515
566
  return this.quoted('"');
516
- const identifier = (_a = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.source.slice(this.position))) == null ? void 0 : _a[0];
567
+ const identifier = (_a2 = /^[A-Za-z_][A-Za-z0-9_]*/.exec(this.source.slice(this.position))) == null ? void 0 : _a2[0];
517
568
  if (!identifier || !member && (SQL_RESERVED_WORDS.has(identifier.toUpperCase()) || /^(WHEN|THEN|ELSE|END)$/i.test(identifier)))
518
569
  throw this.error("SQL expression requires a column, literal or function");
519
570
  this.position += identifier.length;
@@ -540,11 +591,11 @@ var SQLExpressionReader = class {
540
591
  }
541
592
  throw this.error("Unterminated SQL expression quote", start);
542
593
  }
543
- identifier(name2, depth, fold) {
594
+ identifier(name2, depth, fold2) {
544
595
  if (!this.take("("))
545
596
  return this.node({ type: "Identifier", name: name2 });
546
597
  const args = [];
547
- const wildcard = fold && this.wildcardArgument(name2);
598
+ const wildcard = fold2 && this.wildcardArgument(name2);
548
599
  if (wildcard) {
549
600
  this.symbol(")");
550
601
  return this.call(name2, [wildcard], true);
@@ -555,10 +606,23 @@ var SQLExpressionReader = class {
555
606
  } while (this.take(","));
556
607
  this.symbol(")");
557
608
  }
558
- return this.call(name2, args, fold);
609
+ return this.call(name2, args, fold2);
559
610
  }
611
+ /** Reads named value placeholders; quotes and identifiers use separate productions. */
560
612
  extendedPrimary() {
561
- return void 0;
613
+ if (this.source[this.position] !== ":")
614
+ return void 0;
615
+ const start = this.position;
616
+ const parameter = /^:([A-Za-z_][A-Za-z0-9_]*)/.exec(this.source.slice(start));
617
+ if (!parameter)
618
+ throw this.error("SQL named parameters require a name");
619
+ this.position += parameter[0].length;
620
+ this.token();
621
+ try {
622
+ return this.node(bindSQLParameter(parameter[1], this.parameterOptions));
623
+ } catch (error) {
624
+ throw this.error(error instanceof Error ? error.message : String(error), start);
625
+ }
562
626
  }
563
627
  /** Lowers field access to a portable expression call for Arrow binding. */
564
628
  memberAccess(object, name2) {
@@ -576,9 +640,9 @@ var SQLExpressionReader = class {
576
640
 
577
641
  // dist/scales/parse-sql-scale.js
578
642
  function call(node, expected) {
579
- var _a;
643
+ var _a2;
580
644
  const candidate = node;
581
- if (candidate.type !== "CallExpression" || ((_a = candidate.callee) == null ? void 0 : _a.type) !== "Identifier") {
645
+ if (candidate.type !== "CallExpression" || ((_a2 = candidate.callee) == null ? void 0 : _a2.type) !== "Identifier") {
582
646
  throw new Error(expected ? `${expected} expects a function call` : "Expected a scale function");
583
647
  }
584
648
  if (expected && candidate.callee.name.toUpperCase() !== expected) {
@@ -593,7 +657,10 @@ function name(node) {
593
657
  }
594
658
  return candidate.name;
595
659
  }
596
- function literal(node) {
660
+ function quantileDomain(value) {
661
+ return value.type === "Identifier" ? { type: "extent", column: name(value) } : { type: "expression-extent", expression: value };
662
+ }
663
+ function literal2(node) {
597
664
  const value = node;
598
665
  if (value.type !== "Literal" || typeof value.value !== "string" && typeof value.value !== "number" || typeof value.value === "number" && !Number.isFinite(value.value)) {
599
666
  throw new Error("Scale argument requires a literal number or quoted string");
@@ -606,7 +673,7 @@ function number(node) {
606
673
  const n = number(unary.argument);
607
674
  return unary.operator === "-" ? -n : n;
608
675
  }
609
- const value = literal(node);
676
+ const value = literal2(node);
610
677
  if (typeof value !== "number")
611
678
  throw new Error("Expected numeric scale argument");
612
679
  return value;
@@ -615,16 +682,16 @@ function color(hex) {
615
682
  if (!/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex)) {
616
683
  throw new Error("Explicit SQL color stops require #RRGGBB or #RRGGBBAA");
617
684
  }
618
- const bytes = hex.slice(1).match(/../g);
619
- return [0, 1, 2, 3].map((index) => parseInt(bytes[index] ?? "ff", 16) / 255);
685
+ const bytes2 = hex.slice(1).match(/../g);
686
+ return [0, 1, 2, 3].map((index) => parseInt(bytes2[index] ?? "ff", 16) / 255);
620
687
  }
621
688
  function parseDomain(node) {
622
689
  const fn = call(node);
623
690
  const functionName = fn.callee.name.toUpperCase();
624
- const args = fn.arguments;
691
+ const args = arrayArguments(fn.arguments);
625
692
  switch (functionName) {
626
693
  case "DOMAIN":
627
- if (args.length === 1)
694
+ if (args.length === 1 && args[0].type === "Identifier")
628
695
  return { type: "extent", column: name(args[0]) };
629
696
  if (args.length >= 2)
630
697
  return { type: "fixed", values: args.map(number) };
@@ -650,32 +717,68 @@ function parseDomain(node) {
650
717
  }
651
718
  throw new Error(`Invalid ${functionName} domain arguments`);
652
719
  }
720
+ function parseCategories(node) {
721
+ const values = arrayArguments(call(node, "CATEGORIES").arguments).map((argument) => argument.type === "Literal" ? literal2(argument) : number(argument));
722
+ if (!values.length || new Set(values).size !== values.length) {
723
+ throw new Error("CATEGORIES requires at least one category and no duplicates");
724
+ }
725
+ return { type: "fixed", values };
726
+ }
727
+ function arrayArguments(args) {
728
+ return args.length === 1 && args[0].type === "ArrayExpression" ? args[0].elements : args;
729
+ }
730
+ function colorStop(node) {
731
+ if (node.type !== "ArrayExpression") {
732
+ const value = literal2(node);
733
+ if (typeof value !== "string")
734
+ throw new Error("Palette colors require hex strings or RGB/RGBA tuples");
735
+ return color(value);
736
+ }
737
+ if (node.elements.length !== 3 && node.elements.length !== 4) {
738
+ throw new Error("Palette colors require RGB or RGBA tuples");
739
+ }
740
+ const components = node.elements.map(number);
741
+ return [components[0], components[1], components[2], components[3] ?? 1];
742
+ }
653
743
  function parseRange(node) {
744
+ var _a2;
654
745
  const fn = call(node);
655
- const args = fn.arguments;
746
+ const args = arrayArguments(fn.arguments);
747
+ if (fn.callee.name.toUpperCase() === "PALETTE" && fn.arguments.length === 1 && ((_a2 = fn.arguments[0]) == null ? void 0 : _a2.type) === "ArrayExpression") {
748
+ return { type: "colors", values: args.map(colorStop) };
749
+ }
656
750
  switch (fn.callee.name.toUpperCase()) {
657
751
  case "RANGE":
658
752
  if (args.length >= 2)
659
753
  return { type: "numeric", values: args.map(number) };
660
754
  break;
661
755
  case "PALETTE": {
662
- if (args.length === 1 || args.length === 2 && typeof literal(args[0]) === "string" && typeof literal(args[1]) === "number") {
663
- const paletteName = literal(args[0]);
756
+ if (args.length === 1) {
757
+ const stop = literal2(args[0]);
758
+ if (typeof stop === "string" && stop.startsWith("#")) {
759
+ return { type: "colors", values: [color(stop)] };
760
+ }
761
+ }
762
+ if (args.length === 1 || args.length === 2 && typeof literal2(args[0]) === "string" && typeof literal2(args[1]) === "number") {
763
+ const paletteName = literal2(args[0]);
664
764
  if (typeof paletteName !== "string")
665
765
  throw new Error("Palette name must be a string");
666
766
  const count = args.length === 2 ? number(args[1]) : void 0;
667
767
  return { type: "palette", name: paletteName, ...count === void 0 ? {} : { count } };
668
768
  }
669
- if (args.length >= 2 && args.every((arg) => typeof literal(arg) === "string")) {
670
- return { type: "colors", values: args.map((arg) => color(literal(arg))) };
769
+ if (args.length >= 2 && args.every((arg) => typeof literal2(arg) === "string")) {
770
+ return { type: "colors", values: args.map((arg) => color(literal2(arg))) };
671
771
  }
672
772
  break;
673
773
  }
674
774
  }
675
775
  throw new Error("Expected RANGE(numbers...) or PALETTE(name[, count] | color stops...)");
676
776
  }
677
- function parseSQLScaleExpression(sql) {
678
- const root = call(parseSQLExpression(sql));
777
+ function parseSQLScaleExpression(sql, options = {}) {
778
+ return parseSQLScaleAST(parseSQLExpression(sql, options));
779
+ }
780
+ function parseSQLScaleAST(expression) {
781
+ const root = call(expression);
679
782
  const fn = root.callee.name.toUpperCase();
680
783
  const args = root.arguments;
681
784
  const names = {
@@ -686,529 +789,3546 @@ function parseSQLScaleExpression(sql) {
686
789
  SCALE_DIVERGING: "diverging",
687
790
  SCALE_ORDINAL: "ordinal",
688
791
  SCALE_QUANTILE: "quantile",
689
- SCALE_QUANTIZE: "quantize"
792
+ SCALE_QUANTIZE: "quantize",
793
+ SCALE_THRESHOLD: "threshold"
690
794
  };
691
795
  const type = names[fn];
692
796
  if (!type)
693
797
  throw new Error(`Unsupported SQL scale function: ${fn}`);
694
- if (args.length !== (fn === "SCALE_ORDINAL" ? 2 : 3)) {
695
- throw new Error(`${fn} expects ${fn === "SCALE_ORDINAL" ? "two" : "three"} arguments`);
798
+ const inferredOrdinal = fn === "SCALE_ORDINAL" && args.length === 2;
799
+ if (!inferredOrdinal && args.length !== 3) {
800
+ throw new Error(`${fn} expects ${fn === "SCALE_ORDINAL" ? "two or three" : "three"} arguments`);
696
801
  }
697
802
  const value = args[0];
698
- const domain = fn === "SCALE_ORDINAL" ? { type: "categories", column: name(value) } : fn === "SCALE_QUANTILE" || fn === "SCALE_QUANTIZE" ? { type: "extent", column: name(value) } : parseDomain(args[1]);
699
- let range = parseRange(args[fn === "SCALE_ORDINAL" ? 1 : 2]);
803
+ const domain = inferredOrdinal ? { type: "categories", column: name(value) } : fn === "SCALE_ORDINAL" ? parseCategories(args[1]) : fn === "SCALE_QUANTILE" ? quantileDomain(value) : fn === "SCALE_QUANTIZE" ? { type: "extent", column: name(value) } : fn === "SCALE_THRESHOLD" ? { type: "fixed", values: call(args[1], "ARRAY").arguments.map(number) } : parseDomain(args[1]);
804
+ let range = parseRange(args[inferredOrdinal ? 1 : 2]);
700
805
  if (fn === "SCALE_QUANTILE" || fn === "SCALE_QUANTIZE") {
701
806
  const count = number(args[1]);
702
807
  if (!Number.isSafeInteger(count) || count < 2) {
703
808
  throw new Error(`${fn} class count must be an integer >= 2`);
704
809
  }
705
- if (range.type !== "palette")
810
+ if (range.type === "palette") {
811
+ range = { ...range, count };
812
+ } else if (fn !== "SCALE_QUANTILE" || range.type !== "colors") {
706
813
  throw new Error(`${fn} requires a named palette`);
707
- range = { ...range, count };
814
+ } else if (range.values.length !== count) {
815
+ throw new Error("SCALE_QUANTILE requires one color per class");
816
+ }
708
817
  }
709
- const colors = ["sequential", "diverging", "ordinal", "quantile", "quantize"].includes(type);
818
+ const colors = [
819
+ "sequential",
820
+ "diverging",
821
+ "ordinal",
822
+ "quantile",
823
+ "quantize",
824
+ "threshold"
825
+ ].includes(type);
710
826
  if (colors && range.type === "numeric")
711
827
  throw new Error(`${fn} requires a color range`);
712
828
  if (!colors && range.type !== "numeric")
713
829
  throw new Error(`${fn} requires a numeric RANGE`);
714
- if (domain.type === "fixed" && range.type === "colors" && domain.values.length !== range.values.length) {
830
+ if (type === "threshold" && domain.type === "fixed") {
831
+ const breaks = domain.values;
832
+ if (!breaks.length || breaks.some((value2, index) => index > 0 && value2 <= breaks[index - 1])) {
833
+ throw new Error("SCALE_THRESHOLD breaks must be nonempty and strictly increasing");
834
+ }
835
+ const count = breaks.length + 1;
836
+ if (range.type === "palette" && range.count === void 0)
837
+ range = { ...range, count };
838
+ if (range.type === "colors" && range.values.length !== count || range.type === "palette" && range.count !== count) {
839
+ throw new Error("SCALE_THRESHOLD requires one more color than breaks");
840
+ }
841
+ } else if (domain.type === "fixed" && range.type === "colors" && domain.values.length !== range.values.length) {
715
842
  throw new Error("Explicit domain and color stops must have equal lengths");
716
843
  }
717
- const scale = validateScaleSpec({ type, domain, range });
844
+ const scale = (0, scale_types_exports.validateScaleSpec)({ type, domain, range });
718
845
  return { value, scale };
719
846
  }
720
847
 
848
+ // dist/scales/compile-scale.js
849
+ var compile_scale_exports = {};
850
+ __reExport(compile_scale_exports, require("@math.gl/core/color"));
851
+
721
852
  // dist/scales/palettes.js
722
- var rgba = (hex) => {
723
- const hexValue = hex.slice(1);
724
- return [
725
- parseInt(hexValue.slice(0, 2), 16) / 255,
726
- parseInt(hexValue.slice(2, 4), 16) / 255,
727
- parseInt(hexValue.slice(4, 6), 16) / 255,
728
- 1
729
- ];
853
+ var palettes_exports = {};
854
+ __reExport(palettes_exports, require("@math.gl/core/color"));
855
+
856
+ // dist/scales/quantile-sketch.js
857
+ var quantile_sketch_exports = {};
858
+ __reExport(quantile_sketch_exports, require("@math.gl/core/color"));
859
+
860
+ // dist/scales/hdr-colors.js
861
+ var hdr_colors_exports = {};
862
+ __reExport(hdr_colors_exports, require("@math.gl/core/color"));
863
+
864
+ // dist/scales/arrow-scales.js
865
+ var import_apache_arrow7 = require("apache-arrow");
866
+
867
+ // dist/arrow/compiler.js
868
+ var import_apache_arrow6 = require("apache-arrow");
869
+ var import_utf82 = require("@math.gl/core/utf8");
870
+
871
+ // dist/diagnostic-log.js
872
+ function reportDiagnosticError(log, error) {
873
+ if (log) {
874
+ try {
875
+ log.error(error instanceof Error ? error.message : String(error), error)();
876
+ } catch {
877
+ }
878
+ }
879
+ }
880
+
881
+ // dist/expressions/vendor/jsep/runtime.js
882
+ var Hooks = class {
883
+ /**
884
+ * @callback HookCallback
885
+ * @this {*|Jsep} this
886
+ * @param {Jsep} env
887
+ * @returns: void
888
+ */
889
+ /**
890
+ * Adds the given callback to the list of callbacks for the given hook.
891
+ *
892
+ * The callback will be invoked when the hook it is registered for is run.
893
+ *
894
+ * One callback function can be registered to multiple hooks and the same hook multiple times.
895
+ *
896
+ * @param {string|object} name The name of the hook, or an object of callbacks keyed by name
897
+ * @param {HookCallback|boolean} callback The callback function which is given environment variables.
898
+ * @param {?boolean} [first=false] Will add the hook to the top of the list (defaults to the bottom)
899
+ * @public
900
+ */
901
+ add(name2, callback, first) {
902
+ if (typeof arguments[0] != "string") {
903
+ for (let name22 in arguments[0]) {
904
+ this.add(name22, arguments[0][name22], arguments[1]);
905
+ }
906
+ } else {
907
+ (Array.isArray(name2) ? name2 : [name2]).forEach(function(name22) {
908
+ this[name22] = this[name22] || [];
909
+ if (callback) {
910
+ this[name22][first ? "unshift" : "push"](callback);
911
+ }
912
+ }, this);
913
+ }
914
+ }
915
+ /**
916
+ * Runs a hook invoking all registered callbacks with the given environment variables.
917
+ *
918
+ * Callbacks will be invoked synchronously and in the order in which they were registered.
919
+ *
920
+ * @param {string} name The name of the hook.
921
+ * @param {Object<string, any>} env The environment variables of the hook passed to all callbacks registered.
922
+ * @public
923
+ */
924
+ run(name2, env) {
925
+ this[name2] = this[name2] || [];
926
+ this[name2].forEach(function(callback) {
927
+ callback.call(env && env.context ? env.context : env, env);
928
+ });
929
+ }
730
930
  };
731
- var PALETTES = {
732
- viridis: ["#440154", "#414487", "#2a788e", "#22a884", "#7ad151", "#fde725"],
733
- plasma: ["#0d0887", "#7e03a8", "#cc4778", "#f89540", "#f0f921"],
734
- inferno: ["#000004", "#420a68", "#932667", "#dd513a", "#fca50a", "#fcffa4"],
735
- cividis: ["#00204c", "#414d6b", "#7a7b78", "#bcae6c", "#fee838"],
736
- blues: ["#eff3ff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c"],
737
- rdbu: [
738
- "#b2182b",
739
- "#d6604d",
740
- "#f4a582",
741
- "#fddbc7",
742
- "#f7f7f7",
743
- "#d1e5f0",
744
- "#92c5de",
745
- "#4393c3",
746
- "#2166ac"
747
- ],
748
- ylorrd: ["#ffffb2", "#fecc5c", "#fd8d3c", "#f03b20", "#bd0026"],
749
- tableau10: [
750
- "#4e79a7",
751
- "#f28e2b",
752
- "#e15759",
753
- "#76b7b2",
754
- "#59a14f",
755
- "#edc948",
756
- "#b07aa1",
757
- "#ff9da7",
758
- "#9c755f",
759
- "#bab0ab"
760
- ]
931
+ var Plugins = class {
932
+ constructor(jsep22) {
933
+ this.jsep = jsep22;
934
+ this.registered = {};
935
+ }
936
+ /**
937
+ * @callback PluginSetup
938
+ * @this {Jsep} jsep
939
+ * @returns: void
940
+ */
941
+ /**
942
+ * Adds the given plugin(s) to the registry
943
+ *
944
+ * @param {object} plugins
945
+ * @param {string} plugins.name The name of the plugin
946
+ * @param {PluginSetup} plugins.init The init function
947
+ * @public
948
+ */
949
+ register(...plugins) {
950
+ plugins.forEach((plugin) => {
951
+ if (typeof plugin !== "object" || !plugin.name || !plugin.init) {
952
+ throw new Error("Invalid JSEP plugin format");
953
+ }
954
+ if (this.registered[plugin.name]) {
955
+ return;
956
+ }
957
+ plugin.init(this.jsep);
958
+ this.registered[plugin.name] = plugin;
959
+ });
960
+ }
761
961
  };
762
- function interpolate(a, b, t) {
763
- return [
764
- a[0] + (b[0] - a[0]) * t,
765
- a[1] + (b[1] - a[1]) * t,
766
- a[2] + (b[2] - a[2]) * t,
767
- a[3] + (b[3] - a[3]) * t
768
- ];
962
+ var Jsep = class _Jsep {
963
+ /**
964
+ * @returns {string}
965
+ */
966
+ static get version() {
967
+ return "1.4.0";
968
+ }
969
+ /**
970
+ * @returns {string}
971
+ */
972
+ static toString() {
973
+ return "JavaScript Expression Parser (JSEP) v" + _Jsep.version;
974
+ }
975
+ // ==================== CONFIG ================================
976
+ /**
977
+ * @method addUnaryOp
978
+ * @param {string} op_name The name of the unary op to add
979
+ * @returns {Jsep}
980
+ */
981
+ static addUnaryOp(op_name) {
982
+ _Jsep.max_unop_len = Math.max(op_name.length, _Jsep.max_unop_len);
983
+ _Jsep.unary_ops[op_name] = 1;
984
+ return _Jsep;
985
+ }
986
+ /**
987
+ * @method jsep.addBinaryOp
988
+ * @param {string} op_name The name of the binary op to add
989
+ * @param {number} precedence The precedence of the binary op (can be a float). Higher number = higher precedence
990
+ * @param {boolean} [isRightAssociative=false] whether operator is right-associative
991
+ * @returns {Jsep}
992
+ */
993
+ static addBinaryOp(op_name, precedence, isRightAssociative) {
994
+ _Jsep.max_binop_len = Math.max(op_name.length, _Jsep.max_binop_len);
995
+ _Jsep.binary_ops[op_name] = precedence;
996
+ if (isRightAssociative) {
997
+ _Jsep.right_associative.add(op_name);
998
+ } else {
999
+ _Jsep.right_associative.delete(op_name);
1000
+ }
1001
+ return _Jsep;
1002
+ }
1003
+ /**
1004
+ * @method addIdentifierChar
1005
+ * @param {string} char The additional character to treat as a valid part of an identifier
1006
+ * @returns {Jsep}
1007
+ */
1008
+ static addIdentifierChar(char) {
1009
+ _Jsep.additional_identifier_chars.add(char);
1010
+ return _Jsep;
1011
+ }
1012
+ /**
1013
+ * @method addLiteral
1014
+ * @param {string} literal_name The name of the literal to add
1015
+ * @param {*} literal_value The value of the literal
1016
+ * @returns {Jsep}
1017
+ */
1018
+ static addLiteral(literal_name, literal_value) {
1019
+ _Jsep.literals[literal_name] = literal_value;
1020
+ return _Jsep;
1021
+ }
1022
+ /**
1023
+ * @method removeUnaryOp
1024
+ * @param {string} op_name The name of the unary op to remove
1025
+ * @returns {Jsep}
1026
+ */
1027
+ static removeUnaryOp(op_name) {
1028
+ delete _Jsep.unary_ops[op_name];
1029
+ if (op_name.length === _Jsep.max_unop_len) {
1030
+ _Jsep.max_unop_len = _Jsep.getMaxKeyLen(_Jsep.unary_ops);
1031
+ }
1032
+ return _Jsep;
1033
+ }
1034
+ /**
1035
+ * @method removeAllUnaryOps
1036
+ * @returns {Jsep}
1037
+ */
1038
+ static removeAllUnaryOps() {
1039
+ _Jsep.unary_ops = {};
1040
+ _Jsep.max_unop_len = 0;
1041
+ return _Jsep;
1042
+ }
1043
+ /**
1044
+ * @method removeIdentifierChar
1045
+ * @param {string} char The additional character to stop treating as a valid part of an identifier
1046
+ * @returns {Jsep}
1047
+ */
1048
+ static removeIdentifierChar(char) {
1049
+ _Jsep.additional_identifier_chars.delete(char);
1050
+ return _Jsep;
1051
+ }
1052
+ /**
1053
+ * @method removeBinaryOp
1054
+ * @param {string} op_name The name of the binary op to remove
1055
+ * @returns {Jsep}
1056
+ */
1057
+ static removeBinaryOp(op_name) {
1058
+ delete _Jsep.binary_ops[op_name];
1059
+ if (op_name.length === _Jsep.max_binop_len) {
1060
+ _Jsep.max_binop_len = _Jsep.getMaxKeyLen(_Jsep.binary_ops);
1061
+ }
1062
+ _Jsep.right_associative.delete(op_name);
1063
+ return _Jsep;
1064
+ }
1065
+ /**
1066
+ * @method removeAllBinaryOps
1067
+ * @returns {Jsep}
1068
+ */
1069
+ static removeAllBinaryOps() {
1070
+ _Jsep.binary_ops = {};
1071
+ _Jsep.max_binop_len = 0;
1072
+ return _Jsep;
1073
+ }
1074
+ /**
1075
+ * @method removeLiteral
1076
+ * @param {string} literal_name The name of the literal to remove
1077
+ * @returns {Jsep}
1078
+ */
1079
+ static removeLiteral(literal_name) {
1080
+ delete _Jsep.literals[literal_name];
1081
+ return _Jsep;
1082
+ }
1083
+ /**
1084
+ * @method removeAllLiterals
1085
+ * @returns {Jsep}
1086
+ */
1087
+ static removeAllLiterals() {
1088
+ _Jsep.literals = {};
1089
+ return _Jsep;
1090
+ }
1091
+ // ==================== END CONFIG ============================
1092
+ /**
1093
+ * @returns {string}
1094
+ */
1095
+ get char() {
1096
+ return this.expr.charAt(this.index);
1097
+ }
1098
+ /**
1099
+ * @returns {number}
1100
+ */
1101
+ get code() {
1102
+ return this.expr.charCodeAt(this.index);
1103
+ }
1104
+ /**
1105
+ * @param {string} expr a string with the passed in express
1106
+ * @returns Jsep
1107
+ */
1108
+ constructor(expr) {
1109
+ this.expr = expr;
1110
+ this.index = 0;
1111
+ }
1112
+ /**
1113
+ * static top-level parser
1114
+ * @returns {jsep.Expression}
1115
+ */
1116
+ static parse(expr) {
1117
+ return new _Jsep(expr).parse();
1118
+ }
1119
+ /**
1120
+ * Get the longest key length of any object
1121
+ * @param {object} obj
1122
+ * @returns {number}
1123
+ */
1124
+ static getMaxKeyLen(obj) {
1125
+ return Math.max(0, ...Object.keys(obj).map((k) => k.length));
1126
+ }
1127
+ /**
1128
+ * `ch` is a character code in the next three functions
1129
+ * @param {number} ch
1130
+ * @returns {boolean}
1131
+ */
1132
+ static isDecimalDigit(ch) {
1133
+ return ch >= 48 && ch <= 57;
1134
+ }
1135
+ /**
1136
+ * Returns the precedence of a binary operator or `0` if it isn't a binary operator. Can be float.
1137
+ * @param {string} op_val
1138
+ * @returns {number}
1139
+ */
1140
+ static binaryPrecedence(op_val) {
1141
+ return _Jsep.binary_ops[op_val] || 0;
1142
+ }
1143
+ /**
1144
+ * Looks for start of identifier
1145
+ * @param {number} ch
1146
+ * @returns {boolean}
1147
+ */
1148
+ static isIdentifierStart(ch) {
1149
+ return ch >= 65 && ch <= 90 || // A...Z
1150
+ ch >= 97 && ch <= 122 || // a...z
1151
+ ch >= 128 && !_Jsep.binary_ops[String.fromCharCode(ch)] || // any non-ASCII that is not an operator
1152
+ _Jsep.additional_identifier_chars.has(String.fromCharCode(ch));
1153
+ }
1154
+ /**
1155
+ * @param {number} ch
1156
+ * @returns {boolean}
1157
+ */
1158
+ static isIdentifierPart(ch) {
1159
+ return _Jsep.isIdentifierStart(ch) || _Jsep.isDecimalDigit(ch);
1160
+ }
1161
+ /**
1162
+ * throw error at index of the expression
1163
+ * @param {string} message
1164
+ * @throws
1165
+ */
1166
+ throwError(message) {
1167
+ const error = new Error(message + " at character " + this.index);
1168
+ error.index = this.index;
1169
+ error.description = message;
1170
+ throw error;
1171
+ }
1172
+ /**
1173
+ * Run a given hook
1174
+ * @param {string} name
1175
+ * @param {jsep.Expression|false} [node]
1176
+ * @returns {?jsep.Expression}
1177
+ */
1178
+ runHook(name2, node) {
1179
+ if (_Jsep.hooks[name2]) {
1180
+ const env = { context: this, node };
1181
+ _Jsep.hooks.run(name2, env);
1182
+ return env.node;
1183
+ }
1184
+ return node;
1185
+ }
1186
+ /**
1187
+ * Runs a given hook until one returns a node
1188
+ * @param {string} name
1189
+ * @returns {?jsep.Expression}
1190
+ */
1191
+ searchHook(name2) {
1192
+ if (_Jsep.hooks[name2]) {
1193
+ const env = { context: this };
1194
+ _Jsep.hooks[name2].find(function(callback) {
1195
+ callback.call(env.context, env);
1196
+ return env.node;
1197
+ });
1198
+ return env.node;
1199
+ }
1200
+ }
1201
+ /**
1202
+ * Push `index` up to the next non-space character
1203
+ */
1204
+ gobbleSpaces() {
1205
+ let ch = this.code;
1206
+ while (ch === _Jsep.SPACE_CODE || ch === _Jsep.TAB_CODE || ch === _Jsep.LF_CODE || ch === _Jsep.CR_CODE) {
1207
+ ch = this.expr.charCodeAt(++this.index);
1208
+ }
1209
+ this.runHook("gobble-spaces");
1210
+ }
1211
+ /**
1212
+ * Top-level method to parse all expressions and returns compound or single node
1213
+ * @returns {jsep.Expression}
1214
+ */
1215
+ parse() {
1216
+ this.runHook("before-all");
1217
+ const nodes = this.gobbleExpressions();
1218
+ const node = nodes.length === 1 ? nodes[0] : {
1219
+ type: _Jsep.COMPOUND,
1220
+ body: nodes
1221
+ };
1222
+ return this.runHook("after-all", node);
1223
+ }
1224
+ /**
1225
+ * top-level parser (but can be reused within as well)
1226
+ * @param {number} [untilICode]
1227
+ * @returns {jsep.Expression[]}
1228
+ */
1229
+ gobbleExpressions(untilICode) {
1230
+ let nodes = [], ch_i, node;
1231
+ while (this.index < this.expr.length) {
1232
+ ch_i = this.code;
1233
+ if (ch_i === _Jsep.SEMCOL_CODE || ch_i === _Jsep.COMMA_CODE) {
1234
+ this.index++;
1235
+ } else {
1236
+ if (node = this.gobbleExpression()) {
1237
+ nodes.push(node);
1238
+ } else if (this.index < this.expr.length) {
1239
+ if (ch_i === untilICode) {
1240
+ break;
1241
+ }
1242
+ this.throwError('Unexpected "' + this.char + '"');
1243
+ }
1244
+ }
1245
+ }
1246
+ return nodes;
1247
+ }
1248
+ /**
1249
+ * The main parsing function.
1250
+ * @returns {?jsep.Expression}
1251
+ */
1252
+ gobbleExpression() {
1253
+ const node = this.searchHook("gobble-expression") || this.gobbleBinaryExpression();
1254
+ this.gobbleSpaces();
1255
+ return this.runHook("after-expression", node);
1256
+ }
1257
+ /**
1258
+ * Search for the operation portion of the string (e.g. `+`, `===`)
1259
+ * Start by taking the longest possible binary operations (3 characters: `===`, `!==`, `>>>`)
1260
+ * and move down from 3 to 2 to 1 character until a matching binary operation is found
1261
+ * then, return that binary operation
1262
+ * @returns {string|boolean}
1263
+ */
1264
+ gobbleBinaryOp() {
1265
+ this.gobbleSpaces();
1266
+ let to_check = this.expr.substr(this.index, _Jsep.max_binop_len);
1267
+ let tc_len = to_check.length;
1268
+ while (tc_len > 0) {
1269
+ if (_Jsep.binary_ops.hasOwnProperty(to_check) && (!_Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !_Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
1270
+ this.index += tc_len;
1271
+ return to_check;
1272
+ }
1273
+ to_check = to_check.substr(0, --tc_len);
1274
+ }
1275
+ return false;
1276
+ }
1277
+ /**
1278
+ * This function is responsible for gobbling an individual expression,
1279
+ * e.g. `1`, `1+2`, `a+(b*2)-Math.sqrt(2)`
1280
+ * @returns {?jsep.BinaryExpression}
1281
+ */
1282
+ gobbleBinaryExpression() {
1283
+ let node, biop, prec, stack, biop_info, left, right, i, cur_biop;
1284
+ left = this.gobbleToken();
1285
+ if (!left) {
1286
+ return left;
1287
+ }
1288
+ biop = this.gobbleBinaryOp();
1289
+ if (!biop) {
1290
+ return left;
1291
+ }
1292
+ biop_info = { value: biop, prec: _Jsep.binaryPrecedence(biop), right_a: _Jsep.right_associative.has(biop) };
1293
+ right = this.gobbleToken();
1294
+ if (!right) {
1295
+ this.throwError("Expected expression after " + biop);
1296
+ }
1297
+ stack = [left, biop_info, right];
1298
+ while (biop = this.gobbleBinaryOp()) {
1299
+ prec = _Jsep.binaryPrecedence(biop);
1300
+ if (prec === 0) {
1301
+ this.index -= biop.length;
1302
+ break;
1303
+ }
1304
+ biop_info = { value: biop, prec, right_a: _Jsep.right_associative.has(biop) };
1305
+ cur_biop = biop;
1306
+ const comparePrev = (prev) => biop_info.right_a && prev.right_a ? prec > prev.prec : prec <= prev.prec;
1307
+ while (stack.length > 2 && comparePrev(stack[stack.length - 2])) {
1308
+ right = stack.pop();
1309
+ biop = stack.pop().value;
1310
+ left = stack.pop();
1311
+ node = {
1312
+ type: _Jsep.BINARY_EXP,
1313
+ operator: biop,
1314
+ left,
1315
+ right
1316
+ };
1317
+ stack.push(node);
1318
+ }
1319
+ node = this.gobbleToken();
1320
+ if (!node) {
1321
+ this.throwError("Expected expression after " + cur_biop);
1322
+ }
1323
+ stack.push(biop_info, node);
1324
+ }
1325
+ i = stack.length - 1;
1326
+ node = stack[i];
1327
+ while (i > 1) {
1328
+ node = {
1329
+ type: _Jsep.BINARY_EXP,
1330
+ operator: stack[i - 1].value,
1331
+ left: stack[i - 2],
1332
+ right: node
1333
+ };
1334
+ i -= 2;
1335
+ }
1336
+ return node;
1337
+ }
1338
+ /**
1339
+ * An individual part of a binary expression:
1340
+ * e.g. `foo.bar(baz)`, `1`, `"abc"`, `(a % 2)` (because it's in parenthesis)
1341
+ * @returns {boolean|jsep.Expression}
1342
+ */
1343
+ gobbleToken() {
1344
+ let ch, to_check, tc_len, node;
1345
+ this.gobbleSpaces();
1346
+ node = this.searchHook("gobble-token");
1347
+ if (node) {
1348
+ return this.runHook("after-token", node);
1349
+ }
1350
+ ch = this.code;
1351
+ if (_Jsep.isDecimalDigit(ch) || ch === _Jsep.PERIOD_CODE) {
1352
+ return this.gobbleNumericLiteral();
1353
+ }
1354
+ if (ch === _Jsep.SQUOTE_CODE || ch === _Jsep.DQUOTE_CODE) {
1355
+ node = this.gobbleStringLiteral();
1356
+ } else if (ch === _Jsep.OBRACK_CODE) {
1357
+ node = this.gobbleArray();
1358
+ } else {
1359
+ to_check = this.expr.substr(this.index, _Jsep.max_unop_len);
1360
+ tc_len = to_check.length;
1361
+ while (tc_len > 0) {
1362
+ if (_Jsep.unary_ops.hasOwnProperty(to_check) && (!_Jsep.isIdentifierStart(this.code) || this.index + to_check.length < this.expr.length && !_Jsep.isIdentifierPart(this.expr.charCodeAt(this.index + to_check.length)))) {
1363
+ this.index += tc_len;
1364
+ const argument = this.gobbleToken();
1365
+ if (!argument) {
1366
+ this.throwError("missing unaryOp argument");
1367
+ }
1368
+ return this.runHook("after-token", {
1369
+ type: _Jsep.UNARY_EXP,
1370
+ operator: to_check,
1371
+ argument,
1372
+ prefix: true
1373
+ });
1374
+ }
1375
+ to_check = to_check.substr(0, --tc_len);
1376
+ }
1377
+ if (_Jsep.isIdentifierStart(ch)) {
1378
+ node = this.gobbleIdentifier();
1379
+ if (_Jsep.literals.hasOwnProperty(node.name)) {
1380
+ node = {
1381
+ type: _Jsep.LITERAL,
1382
+ value: _Jsep.literals[node.name],
1383
+ raw: node.name
1384
+ };
1385
+ } else if (node.name === _Jsep.this_str) {
1386
+ node = { type: _Jsep.THIS_EXP };
1387
+ }
1388
+ } else if (ch === _Jsep.OPAREN_CODE) {
1389
+ node = this.gobbleGroup();
1390
+ }
1391
+ }
1392
+ if (!node) {
1393
+ return this.runHook("after-token", false);
1394
+ }
1395
+ node = this.gobbleTokenProperty(node);
1396
+ return this.runHook("after-token", node);
1397
+ }
1398
+ /**
1399
+ * Gobble properties of of identifiers/strings/arrays/groups.
1400
+ * e.g. `foo`, `bar.baz`, `foo['bar'].baz`
1401
+ * It also gobbles function calls:
1402
+ * e.g. `Math.acos(obj.angle)`
1403
+ * @param {jsep.Expression} node
1404
+ * @returns {jsep.Expression}
1405
+ */
1406
+ gobbleTokenProperty(node) {
1407
+ this.gobbleSpaces();
1408
+ let ch = this.code;
1409
+ while (ch === _Jsep.PERIOD_CODE || ch === _Jsep.OBRACK_CODE || ch === _Jsep.OPAREN_CODE || ch === _Jsep.QUMARK_CODE) {
1410
+ let optional;
1411
+ if (ch === _Jsep.QUMARK_CODE) {
1412
+ if (this.expr.charCodeAt(this.index + 1) !== _Jsep.PERIOD_CODE) {
1413
+ break;
1414
+ }
1415
+ optional = true;
1416
+ this.index += 2;
1417
+ this.gobbleSpaces();
1418
+ ch = this.code;
1419
+ }
1420
+ this.index++;
1421
+ if (ch === _Jsep.OBRACK_CODE) {
1422
+ node = {
1423
+ type: _Jsep.MEMBER_EXP,
1424
+ computed: true,
1425
+ object: node,
1426
+ property: this.gobbleExpression()
1427
+ };
1428
+ if (!node.property) {
1429
+ this.throwError('Unexpected "' + this.char + '"');
1430
+ }
1431
+ this.gobbleSpaces();
1432
+ ch = this.code;
1433
+ if (ch !== _Jsep.CBRACK_CODE) {
1434
+ this.throwError("Unclosed [");
1435
+ }
1436
+ this.index++;
1437
+ } else if (ch === _Jsep.OPAREN_CODE) {
1438
+ node = {
1439
+ type: _Jsep.CALL_EXP,
1440
+ "arguments": this.gobbleArguments(_Jsep.CPAREN_CODE),
1441
+ callee: node
1442
+ };
1443
+ } else if (ch === _Jsep.PERIOD_CODE || optional) {
1444
+ if (optional) {
1445
+ this.index--;
1446
+ }
1447
+ this.gobbleSpaces();
1448
+ node = {
1449
+ type: _Jsep.MEMBER_EXP,
1450
+ computed: false,
1451
+ object: node,
1452
+ property: this.gobbleIdentifier()
1453
+ };
1454
+ }
1455
+ if (optional) {
1456
+ node.optional = true;
1457
+ }
1458
+ this.gobbleSpaces();
1459
+ ch = this.code;
1460
+ }
1461
+ return node;
1462
+ }
1463
+ /**
1464
+ * Parse simple numeric literals: `12`, `3.4`, `.5`. Do this by using a string to
1465
+ * keep track of everything in the numeric literal and then calling `parseFloat` on that string
1466
+ * @returns {jsep.Literal}
1467
+ */
1468
+ gobbleNumericLiteral() {
1469
+ let number2 = "", ch, chCode;
1470
+ while (_Jsep.isDecimalDigit(this.code)) {
1471
+ number2 += this.expr.charAt(this.index++);
1472
+ }
1473
+ if (this.code === _Jsep.PERIOD_CODE) {
1474
+ number2 += this.expr.charAt(this.index++);
1475
+ while (_Jsep.isDecimalDigit(this.code)) {
1476
+ number2 += this.expr.charAt(this.index++);
1477
+ }
1478
+ }
1479
+ ch = this.char;
1480
+ if (ch === "e" || ch === "E") {
1481
+ number2 += this.expr.charAt(this.index++);
1482
+ ch = this.char;
1483
+ if (ch === "+" || ch === "-") {
1484
+ number2 += this.expr.charAt(this.index++);
1485
+ }
1486
+ while (_Jsep.isDecimalDigit(this.code)) {
1487
+ number2 += this.expr.charAt(this.index++);
1488
+ }
1489
+ if (!_Jsep.isDecimalDigit(this.expr.charCodeAt(this.index - 1))) {
1490
+ this.throwError("Expected exponent (" + number2 + this.char + ")");
1491
+ }
1492
+ }
1493
+ chCode = this.code;
1494
+ if (_Jsep.isIdentifierStart(chCode)) {
1495
+ this.throwError("Variable names cannot start with a number (" + number2 + this.char + ")");
1496
+ } else if (chCode === _Jsep.PERIOD_CODE || number2.length === 1 && number2.charCodeAt(0) === _Jsep.PERIOD_CODE) {
1497
+ this.throwError("Unexpected period");
1498
+ }
1499
+ return {
1500
+ type: _Jsep.LITERAL,
1501
+ value: parseFloat(number2),
1502
+ raw: number2
1503
+ };
1504
+ }
1505
+ /**
1506
+ * Parses a string literal, staring with single or double quotes with basic support for escape codes
1507
+ * e.g. `"hello world"`, `'this is\nJSEP'`
1508
+ * @returns {jsep.Literal}
1509
+ */
1510
+ gobbleStringLiteral() {
1511
+ let str = "";
1512
+ const startIndex = this.index;
1513
+ const quote = this.expr.charAt(this.index++);
1514
+ let closed = false;
1515
+ while (this.index < this.expr.length) {
1516
+ let ch = this.expr.charAt(this.index++);
1517
+ if (ch === quote) {
1518
+ closed = true;
1519
+ break;
1520
+ } else if (ch === "\\") {
1521
+ ch = this.expr.charAt(this.index++);
1522
+ switch (ch) {
1523
+ case "n":
1524
+ str += "\n";
1525
+ break;
1526
+ case "r":
1527
+ str += "\r";
1528
+ break;
1529
+ case "t":
1530
+ str += " ";
1531
+ break;
1532
+ case "b":
1533
+ str += "\b";
1534
+ break;
1535
+ case "f":
1536
+ str += "\f";
1537
+ break;
1538
+ case "v":
1539
+ str += "\v";
1540
+ break;
1541
+ default:
1542
+ str += ch;
1543
+ }
1544
+ } else {
1545
+ str += ch;
1546
+ }
1547
+ }
1548
+ if (!closed) {
1549
+ this.throwError('Unclosed quote after "' + str + '"');
1550
+ }
1551
+ return {
1552
+ type: _Jsep.LITERAL,
1553
+ value: str,
1554
+ raw: this.expr.substring(startIndex, this.index)
1555
+ };
1556
+ }
1557
+ /**
1558
+ * Gobbles only identifiers
1559
+ * e.g.: `foo`, `_value`, `$x1`
1560
+ * Also, this function checks if that identifier is a literal:
1561
+ * (e.g. `true`, `false`, `null`) or `this`
1562
+ * @returns {jsep.Identifier}
1563
+ */
1564
+ gobbleIdentifier() {
1565
+ let ch = this.code, start = this.index;
1566
+ if (_Jsep.isIdentifierStart(ch)) {
1567
+ this.index++;
1568
+ } else {
1569
+ this.throwError("Unexpected " + this.char);
1570
+ }
1571
+ while (this.index < this.expr.length) {
1572
+ ch = this.code;
1573
+ if (_Jsep.isIdentifierPart(ch)) {
1574
+ this.index++;
1575
+ } else {
1576
+ break;
1577
+ }
1578
+ }
1579
+ return {
1580
+ type: _Jsep.IDENTIFIER,
1581
+ name: this.expr.slice(start, this.index)
1582
+ };
1583
+ }
1584
+ /**
1585
+ * Gobbles a list of arguments within the context of a function call
1586
+ * or array literal. This function also assumes that the opening character
1587
+ * `(` or `[` has already been gobbled, and gobbles expressions and commas
1588
+ * until the terminator character `)` or `]` is encountered.
1589
+ * e.g. `foo(bar, baz)`, `my_func()`, or `[bar, baz]`
1590
+ * @param {number} termination
1591
+ * @returns {jsep.Expression[]}
1592
+ */
1593
+ gobbleArguments(termination) {
1594
+ const args = [];
1595
+ let closed = false;
1596
+ let separator_count = 0;
1597
+ while (this.index < this.expr.length) {
1598
+ this.gobbleSpaces();
1599
+ let ch_i = this.code;
1600
+ if (ch_i === termination) {
1601
+ closed = true;
1602
+ this.index++;
1603
+ if (termination === _Jsep.CPAREN_CODE && separator_count && separator_count >= args.length) {
1604
+ this.throwError("Unexpected token " + String.fromCharCode(termination));
1605
+ }
1606
+ break;
1607
+ } else if (ch_i === _Jsep.COMMA_CODE) {
1608
+ this.index++;
1609
+ separator_count++;
1610
+ if (separator_count !== args.length) {
1611
+ if (termination === _Jsep.CPAREN_CODE) {
1612
+ this.throwError("Unexpected token ,");
1613
+ } else if (termination === _Jsep.CBRACK_CODE) {
1614
+ for (let arg = args.length; arg < separator_count; arg++) {
1615
+ args.push(null);
1616
+ }
1617
+ }
1618
+ }
1619
+ } else if (args.length !== separator_count && separator_count !== 0) {
1620
+ this.throwError("Expected comma");
1621
+ } else {
1622
+ const node = this.gobbleExpression();
1623
+ if (!node || node.type === _Jsep.COMPOUND) {
1624
+ this.throwError("Expected comma");
1625
+ }
1626
+ args.push(node);
1627
+ }
1628
+ }
1629
+ if (!closed) {
1630
+ this.throwError("Expected " + String.fromCharCode(termination));
1631
+ }
1632
+ return args;
1633
+ }
1634
+ /**
1635
+ * Responsible for parsing a group of things within parentheses `()`
1636
+ * that have no identifier in front (so not a function call)
1637
+ * This function assumes that it needs to gobble the opening parenthesis
1638
+ * and then tries to gobble everything within that parenthesis, assuming
1639
+ * that the next thing it should see is the close parenthesis. If not,
1640
+ * then the expression probably doesn't have a `)`
1641
+ * @returns {boolean|jsep.Expression}
1642
+ */
1643
+ gobbleGroup() {
1644
+ this.index++;
1645
+ let nodes = this.gobbleExpressions(_Jsep.CPAREN_CODE);
1646
+ if (this.code === _Jsep.CPAREN_CODE) {
1647
+ this.index++;
1648
+ if (nodes.length === 1) {
1649
+ return nodes[0];
1650
+ } else if (!nodes.length) {
1651
+ return false;
1652
+ } else {
1653
+ return {
1654
+ type: _Jsep.SEQUENCE_EXP,
1655
+ expressions: nodes
1656
+ };
1657
+ }
1658
+ } else {
1659
+ this.throwError("Unclosed (");
1660
+ }
1661
+ }
1662
+ /**
1663
+ * Responsible for parsing Array literals `[1, 2, 3]`
1664
+ * This function assumes that it needs to gobble the opening bracket
1665
+ * and then tries to gobble the expressions as arguments.
1666
+ * @returns {jsep.ArrayExpression}
1667
+ */
1668
+ gobbleArray() {
1669
+ this.index++;
1670
+ return {
1671
+ type: _Jsep.ARRAY_EXP,
1672
+ elements: this.gobbleArguments(_Jsep.CBRACK_CODE)
1673
+ };
1674
+ }
1675
+ };
1676
+ var hooks = new Hooks();
1677
+ Object.assign(Jsep, {
1678
+ hooks,
1679
+ plugins: new Plugins(Jsep),
1680
+ // Node Types
1681
+ // ----------
1682
+ // This is the full set of types that any JSEP node can be.
1683
+ // Store them here to save space when minified
1684
+ COMPOUND: "Compound",
1685
+ SEQUENCE_EXP: "SequenceExpression",
1686
+ IDENTIFIER: "Identifier",
1687
+ MEMBER_EXP: "MemberExpression",
1688
+ LITERAL: "Literal",
1689
+ THIS_EXP: "ThisExpression",
1690
+ CALL_EXP: "CallExpression",
1691
+ UNARY_EXP: "UnaryExpression",
1692
+ BINARY_EXP: "BinaryExpression",
1693
+ ARRAY_EXP: "ArrayExpression",
1694
+ TAB_CODE: 9,
1695
+ LF_CODE: 10,
1696
+ CR_CODE: 13,
1697
+ SPACE_CODE: 32,
1698
+ PERIOD_CODE: 46,
1699
+ // '.'
1700
+ COMMA_CODE: 44,
1701
+ // ','
1702
+ SQUOTE_CODE: 39,
1703
+ // single quote
1704
+ DQUOTE_CODE: 34,
1705
+ // double quotes
1706
+ OPAREN_CODE: 40,
1707
+ // (
1708
+ CPAREN_CODE: 41,
1709
+ // )
1710
+ OBRACK_CODE: 91,
1711
+ // [
1712
+ CBRACK_CODE: 93,
1713
+ // ]
1714
+ QUMARK_CODE: 63,
1715
+ // ?
1716
+ SEMCOL_CODE: 59,
1717
+ // ;
1718
+ COLON_CODE: 58,
1719
+ // :
1720
+ // Operations
1721
+ // ----------
1722
+ // Use a quickly-accessible map to store all of the unary operators
1723
+ // Values are set to `1` (it really doesn't matter)
1724
+ unary_ops: {
1725
+ "-": 1,
1726
+ "!": 1,
1727
+ "~": 1,
1728
+ "+": 1
1729
+ },
1730
+ // Also use a map for the binary operations but set their values to their
1731
+ // binary precedence for quick reference (higher number = higher precedence)
1732
+ // see [Order of operations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)
1733
+ binary_ops: {
1734
+ "||": 1,
1735
+ "??": 1,
1736
+ "&&": 2,
1737
+ "|": 3,
1738
+ "^": 4,
1739
+ "&": 5,
1740
+ "==": 6,
1741
+ "!=": 6,
1742
+ "===": 6,
1743
+ "!==": 6,
1744
+ "<": 7,
1745
+ ">": 7,
1746
+ "<=": 7,
1747
+ ">=": 7,
1748
+ "<<": 8,
1749
+ ">>": 8,
1750
+ ">>>": 8,
1751
+ "+": 9,
1752
+ "-": 9,
1753
+ "*": 10,
1754
+ "/": 10,
1755
+ "%": 10,
1756
+ "**": 11
1757
+ },
1758
+ // sets specific binary_ops as right-associative
1759
+ right_associative: /* @__PURE__ */ new Set(["**"]),
1760
+ // Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char)
1761
+ additional_identifier_chars: /* @__PURE__ */ new Set(["$", "_"]),
1762
+ // Literals
1763
+ // ----------
1764
+ // Store the values to return for the various literals we may encounter
1765
+ literals: {
1766
+ "true": true,
1767
+ "false": false,
1768
+ "null": null
1769
+ },
1770
+ // Except for `this`, which is special. This could be changed to something like `'self'` as well
1771
+ this_str: "this"
1772
+ });
1773
+ Jsep.max_unop_len = Jsep.getMaxKeyLen(Jsep.unary_ops);
1774
+ Jsep.max_binop_len = Jsep.getMaxKeyLen(Jsep.binary_ops);
1775
+ var jsep = (expr) => new Jsep(expr).parse();
1776
+ var stdClassProps = Object.getOwnPropertyNames(class Test {
1777
+ });
1778
+ Object.getOwnPropertyNames(Jsep).filter((prop) => !stdClassProps.includes(prop) && jsep[prop] === void 0).forEach((m) => {
1779
+ jsep[m] = Jsep[m];
1780
+ });
1781
+ jsep.Jsep = Jsep;
1782
+ var CONDITIONAL_EXP = "ConditionalExpression";
1783
+ var ternary = {
1784
+ name: "ternary",
1785
+ init(jsep22) {
1786
+ jsep22.hooks.add("after-expression", function gobbleTernary(env) {
1787
+ if (env.node && this.code === jsep22.QUMARK_CODE) {
1788
+ this.index++;
1789
+ const test = env.node;
1790
+ const consequent = this.gobbleExpression();
1791
+ if (!consequent) {
1792
+ this.throwError("Expected expression");
1793
+ }
1794
+ this.gobbleSpaces();
1795
+ if (this.code === jsep22.COLON_CODE) {
1796
+ this.index++;
1797
+ const alternate = this.gobbleExpression();
1798
+ if (!alternate) {
1799
+ this.throwError("Expected expression");
1800
+ }
1801
+ env.node = {
1802
+ type: CONDITIONAL_EXP,
1803
+ test,
1804
+ consequent,
1805
+ alternate
1806
+ };
1807
+ if (test.operator && jsep22.binary_ops[test.operator] <= 0.9) {
1808
+ let newTest = test;
1809
+ while (newTest.right.operator && jsep22.binary_ops[newTest.right.operator] <= 0.9) {
1810
+ newTest = newTest.right;
1811
+ }
1812
+ env.node.test = newTest.right;
1813
+ newTest.right = env.node;
1814
+ env.node = test;
1815
+ }
1816
+ } else {
1817
+ this.throwError("Expected :");
1818
+ }
1819
+ }
1820
+ });
1821
+ }
1822
+ };
1823
+ jsep.plugins.register(ternary);
1824
+ var runtime_default = jsep;
1825
+
1826
+ // dist/expressions/expression-eval.js
1827
+ var jsep2 = runtime_default;
1828
+ function parse(expression, options) {
1829
+ try {
1830
+ return typeof expression === "string" ? jsep2(expression) : expression;
1831
+ } catch (error) {
1832
+ reportDiagnosticError(options == null ? void 0 : options.log, error);
1833
+ throw error;
1834
+ }
769
1835
  }
770
- function resolvePalette(range, count) {
771
- if (range.type === "numeric")
772
- throw new Error("A numeric range is not a color palette");
773
- if (range.type === "colors")
774
- return range.values;
775
- const key = range.name.toLowerCase();
776
- const colors = Object.prototype.hasOwnProperty.call(PALETTES, key) ? PALETTES[key] : void 0;
777
- if (!colors)
778
- throw new Error(`Unknown color palette: ${range.name}`);
779
- const target = count ?? range.count;
780
- if (target === void 0)
781
- return colors.map(rgba);
782
- if (!Number.isSafeInteger(target) || target < 2 || target > 256) {
783
- throw new Error("Palette count must be between 2 and 256");
784
- }
785
- if (target === colors.length)
786
- return colors.map(rgba);
787
- const decoded = colors.map(rgba);
788
- return Array.from({ length: target }, (_, i) => {
789
- const position = i * (decoded.length - 1) / (target - 1);
790
- const index = Math.min(Math.floor(position), decoded.length - 2);
791
- return interpolate(decoded[index], decoded[index + 1], position - index);
792
- });
1836
+
1837
+ // dist/expressions/ast.js
1838
+ var MAXIMUM_EXPRESSION_NODES = 16384;
1839
+ var MAXIMUM_EXPRESSION_DEPTH = 192;
1840
+ function validateExpressionAST(value, options = {}) {
1841
+ visitExpressionAST(value, () => {
1842
+ }, options);
793
1843
  }
794
- function listScalePalettes() {
795
- return Object.keys(PALETTES);
1844
+ function visitExpressionAST(value, visit, options) {
1845
+ var _a2;
1846
+ const maxNodes = options.maxNodes ?? MAXIMUM_EXPRESSION_NODES, maxDepth = options.maxDepth ?? MAXIMUM_EXPRESSION_DEPTH;
1847
+ if (!Number.isSafeInteger(maxNodes) || maxNodes < 1 || maxNodes > MAXIMUM_EXPRESSION_NODES || !Number.isSafeInteger(maxDepth) || maxDepth < 0 || maxDepth > MAXIMUM_EXPRESSION_DEPTH)
1848
+ throw new Error(`Expression AST limits must not exceed ${MAXIMUM_EXPRESSION_NODES} nodes and ${MAXIMUM_EXPRESSION_DEPTH} levels`);
1849
+ const stack = [{ node: value, depth: 0 }];
1850
+ const ancestors = /* @__PURE__ */ new Set();
1851
+ let count = 0;
1852
+ while (stack.length) {
1853
+ const { node: input, depth, exit } = stack.pop();
1854
+ if (!input || typeof input !== "object" || Array.isArray(input))
1855
+ throw new Error("Invalid expression AST node");
1856
+ if (exit) {
1857
+ ancestors.delete(input);
1858
+ continue;
1859
+ }
1860
+ if (++count > maxNodes || depth > maxDepth)
1861
+ throw new Error("Expression AST exceeds node or depth limit");
1862
+ if (ancestors.has(input))
1863
+ throw new Error("Expression AST contains a cycle");
1864
+ ancestors.add(input);
1865
+ stack.push({ node: input, depth, exit: true });
1866
+ const node = input;
1867
+ const children = [];
1868
+ switch (node.type) {
1869
+ case "Identifier":
1870
+ if (typeof node.name !== "string" || !node.name.length || node.name.length > 65536)
1871
+ throw new Error("Invalid expression identifier");
1872
+ break;
1873
+ case "Literal":
1874
+ if (node.value !== null && !["number", "bigint", "boolean", "string"].includes(typeof node.value) && !(node.value instanceof Uint8Array) && !(node.value instanceof Date && Number.isFinite(node.value.getTime())))
1875
+ throw new Error("Unsupported expression literal");
1876
+ if (node.raw !== void 0 && typeof node.raw !== "string")
1877
+ throw new Error("Invalid literal annotation");
1878
+ break;
1879
+ case "UnaryExpression":
1880
+ if (typeof node.operator !== "string" || !node.operator.length || node.operator.length > 32)
1881
+ throw new Error("Invalid expression operator");
1882
+ if (node.prefix !== void 0 && typeof node.prefix !== "boolean")
1883
+ throw new Error("Invalid unary annotation");
1884
+ children.push(node.argument);
1885
+ break;
1886
+ case "LogicalExpression":
1887
+ if (node.operator !== "&&" && node.operator !== "||")
1888
+ throw new Error("Invalid logical operator");
1889
+ children.push(node.left, node.right);
1890
+ break;
1891
+ case "BinaryExpression":
1892
+ if (typeof node.operator !== "string" || !node.operator.length || node.operator.length > 32)
1893
+ throw new Error("Invalid expression operator");
1894
+ children.push(node.left, node.right);
1895
+ break;
1896
+ case "ConditionalExpression":
1897
+ children.push(node.test, node.consequent, node.alternate);
1898
+ break;
1899
+ case "ArrayExpression":
1900
+ if (!Array.isArray(node.elements))
1901
+ throw new Error("Invalid expression array");
1902
+ if (node.elements.length + count > maxNodes)
1903
+ throw new Error("Expression AST exceeds node limit");
1904
+ for (const element of node.elements) {
1905
+ if (element == null)
1906
+ throw new Error("Expression array contains holes");
1907
+ children.push(element);
1908
+ }
1909
+ break;
1910
+ case "CallExpression":
1911
+ if (((_a2 = node.callee) == null ? void 0 : _a2.type) !== "Identifier" || typeof node.callee.name !== "string" || !node.callee.name.length || node.callee.name.length > 65536 || !Array.isArray(node.arguments) || node.sqlFunction !== void 0 && node.sqlFunction !== true)
1912
+ throw new Error("Expressions require named function calls");
1913
+ if (node.arguments.length + count > maxNodes)
1914
+ throw new Error("Expression AST exceeds node limit");
1915
+ children.push(...node.arguments);
1916
+ break;
1917
+ default:
1918
+ throw new Error(`Unsupported portable expression AST node: ${node.type}`);
1919
+ }
1920
+ if (children.length + count + stack.length - ancestors.size > maxNodes)
1921
+ throw new Error("Expression AST exceeds node limit");
1922
+ visit(node);
1923
+ for (let index = children.length - 1; index >= 0; index--)
1924
+ stack.push({ node: children[index], depth: depth + 1 });
1925
+ }
796
1926
  }
797
1927
 
798
- // dist/scales/hdr-colors.js
799
- var TO_XYZ = {
800
- srgb: [
801
- 0.4124564,
802
- 0.3575761,
803
- 0.1804375,
804
- 0.2126729,
805
- 0.7151522,
806
- 0.072175,
807
- 0.0193339,
808
- 0.119192,
809
- 0.9503041
810
- ],
811
- "display-p3": [
812
- 0.48657095,
813
- 0.26566769,
814
- 0.19821729,
815
- 0.22897456,
816
- 0.69173852,
817
- 0.07928691,
818
- 0,
819
- 0.04511338,
820
- 1.04394437
821
- ],
822
- rec2020: [
823
- 0.63695805,
824
- 0.1446169,
825
- 0.16888098,
826
- 0.26270021,
827
- 0.67799807,
828
- 0.05930172,
829
- 0,
830
- 0.02807269,
831
- 1.06098506
832
- ]
833
- };
834
- var FROM_XYZ = {
835
- srgb: [
836
- 3.2404542,
837
- -1.5371385,
838
- -0.4985314,
839
- -0.969266,
840
- 1.8760108,
841
- 0.041556,
842
- 0.0556434,
843
- -0.2040259,
844
- 1.0572252
845
- ],
846
- "display-p3": [
847
- 2.49349691,
848
- -0.93138362,
849
- -0.40271078,
850
- -0.82948897,
851
- 1.76266406,
852
- 0.02362469,
853
- 0.03584583,
854
- -0.07617239,
855
- 0.95688452
856
- ],
857
- rec2020: [
858
- 1.71665119,
859
- -0.35567078,
860
- -0.25336628,
861
- -0.66668435,
862
- 1.61648124,
863
- 0.01576855,
864
- 0.01763986,
865
- -0.04277061,
866
- 0.94210312
867
- ]
1928
+ // dist/expressions/evaluation-error.js
1929
+ var _a;
1930
+ var brand = /* @__PURE__ */ Symbol.for("@math.gl/core/ExpressionEvaluationError");
1931
+ var codes = ["domain", "conversion", "overflow", "divisionByZero"];
1932
+ var ExpressionEvaluationError = class extends Error {
1933
+ constructor(code, message) {
1934
+ super(message);
1935
+ this.code = code;
1936
+ this.name = "ExpressionEvaluationError";
1937
+ this[_a] = true;
1938
+ if (!codes.includes(code))
1939
+ throw new TypeError("Unknown Arrow evaluation error code");
1940
+ }
868
1941
  };
869
- function transform(m, [r, g, b]) {
870
- return [
871
- m[0] * r + m[1] * g + m[2] * b,
872
- m[3] * r + m[4] * g + m[5] * b,
873
- m[6] * r + m[7] * g + m[8] * b
874
- ];
875
- }
876
- function decodeSRGB(v) {
877
- const x = Math.abs(v);
878
- return Math.sign(v) * (x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4);
879
- }
880
- function encodeSRGB(v) {
881
- const x = Math.abs(v);
882
- return Math.sign(v) * (x <= 31308e-7 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055);
883
- }
884
- function decode(v, transfer) {
885
- if (transfer === "linear")
886
- return v;
887
- if (transfer === "srgb")
888
- return decodeSRGB(v);
889
- throw new Error(`Transfer ${transfer} requires an explicit HDR display-referred conversion`);
890
- }
891
- function encode(v, transfer) {
892
- if (transfer === "linear")
893
- return v;
894
- if (transfer === "srgb")
895
- return encodeSRGB(v);
896
- throw new Error(`Transfer ${transfer} requires an explicit HDR display-referred conversion`);
897
- }
898
- function convertScaleColor(color2, from, to) {
899
- if (color2.some((channel) => !Number.isFinite(channel))) {
900
- throw new Error("Color channels must be finite");
901
- }
902
- if (from.transfer === "pq" || from.transfer === "hlg" || to.transfer === "pq" || to.transfer === "hlg") {
903
- throw new Error("PQ/HLG conversions require explicit luminance and display assumptions");
904
- }
905
- const linear = color2.slice(0, 3).map((v) => decode(v, from.transfer));
906
- const converted = from.primaries === to.primaries ? linear : transform(FROM_XYZ[to.primaries], transform(TO_XYZ[from.primaries], linear));
907
- return [
908
- encode(converted[0], to.transfer),
909
- encode(converted[1], to.transfer),
910
- encode(converted[2], to.transfer),
911
- color2[3]
912
- ];
913
- }
914
- function packFloat16(value) {
915
- const sign = value < 0 || Object.is(value, -0) ? 32768 : 0;
916
- const magnitude = Math.abs(value);
917
- if (Number.isNaN(magnitude))
918
- return 32256;
919
- if (magnitude >= 65520)
920
- return sign | 31744;
921
- if (magnitude <= 2 ** -25)
922
- return sign;
923
- const exponent = Math.max(-14, Math.floor(Math.log2(magnitude)));
924
- const scaled = magnitude * 2 ** (10 - exponent);
925
- const lower = Math.floor(scaled);
926
- const fraction = scaled - lower;
927
- const significand = lower + (fraction > 0.5 || fraction === 0.5 && lower % 2 ? 1 : 0);
928
- return sign | (exponent + 14 << 10) + significand;
929
- }
930
- function unpackFloat16(bits) {
931
- const sign = bits & 32768 ? -1 : 1;
932
- const exp = bits >>> 10 & 31;
933
- const mantissa = bits & 1023;
934
- if (exp === 0)
935
- return sign * mantissa * 2 ** -24;
936
- if (exp === 31)
937
- return mantissa ? NaN : sign * Infinity;
938
- return sign * (1 + mantissa / 1024) * 2 ** (exp - 15);
939
- }
940
- function packRGBA16F(color2) {
941
- return Uint16Array.from(color2, packFloat16);
1942
+ _a = brand;
1943
+ function isExpressionEvaluationError(error) {
1944
+ return typeof error === "object" && error !== null && error[brand] === true && codes.includes(error.code);
942
1945
  }
943
1946
 
944
- // dist/scales/compile-scale.js
945
- var SRGB = { primaries: "srgb", transfer: "srgb", format: "rgba32float" };
946
- var clamp01 = (value) => Math.min(1, Math.max(0, value));
947
- function toOklab(color2) {
948
- const [r, g, b] = color2.slice(0, 3).map(decodeSRGB);
949
- const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
950
- const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
951
- const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
952
- return [
953
- 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
954
- 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
955
- 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s
956
- ];
1947
+ // dist/arrow/buffers.js
1948
+ var import_apache_arrow = require("apache-arrow");
1949
+
1950
+ // dist/arrow/float16.js
1951
+ var import_core = require("@math.gl/core");
1952
+ function requireFloat16() {
1953
+ if (!(0, import_core.isFloat16ArrayConstructor)((0, import_core.getFloat16ArrayConstructor)()))
1954
+ throw new Error("Arrow Float16 expressions require native Float16Array support");
957
1955
  }
958
- function fromOklab([L, a, b]) {
959
- const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
960
- const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
961
- const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
962
- return [
963
- encodeSRGB(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
964
- encodeSRGB(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
965
- encodeSRGB(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s)
966
- ];
1956
+ function float16Values(values) {
1957
+ if ((0, import_core.isFloat16Array)(values))
1958
+ return values;
1959
+ requireFloat16();
1960
+ const Constructor = (0, import_core.getFloat16ArrayConstructor)();
1961
+ return new Constructor(values.buffer, values.byteOffset, values.length);
967
1962
  }
968
- function interpolateColor(a, b, t, space = "srgb") {
969
- if (space === "jzazbz") {
970
- throw new Error("Jzazbz interpolation is not implemented yet");
971
- }
972
- const mix = (x, y) => x + (y - x) * t;
973
- const alpha = mix(a[3], b[3]);
974
- if (space === "oklab") {
975
- const lhs = toOklab(a), rhs = toOklab(b);
976
- const rgb = fromOklab(lhs.map((v, i) => mix(v, rhs[i])));
977
- return [rgb[0], rgb[1], rgb[2], alpha];
978
- }
979
- if (space === "linear-srgb") {
980
- return [
981
- encodeSRGB(mix(decodeSRGB(a[0]), decodeSRGB(b[0]))),
982
- encodeSRGB(mix(decodeSRGB(a[1]), decodeSRGB(b[1]))),
983
- encodeSRGB(mix(decodeSRGB(a[2]), decodeSRGB(b[2]))),
984
- alpha
985
- ];
986
- }
987
- return [mix(a[0], b[0]), mix(a[1], b[1]), mix(a[2], b[2]), alpha];
988
- }
989
- function normalize(value, a, b, type) {
990
- if (a === b)
991
- return 0.5;
992
- if (type === "log") {
993
- if (a <= 0 || b <= 0 || value <= 0)
994
- return NaN;
995
- return Math.log(value / a) / Math.log(b / a);
996
- }
997
- if (type === "sqrt") {
998
- if (a < 0 || b < 0 || value < 0)
999
- return NaN;
1000
- return (Math.sqrt(value) - Math.sqrt(a)) / (Math.sqrt(b) - Math.sqrt(a));
1001
- }
1002
- if (type === "symlog") {
1003
- const symlog = (v) => Math.sign(v) * Math.log1p(Math.abs(v));
1004
- return (symlog(value) - symlog(a)) / (symlog(b) - symlog(a));
1005
- }
1006
- return (value - a) / (b - a);
1007
- }
1008
- function numericValues(domain) {
1009
- if (!domain.length || domain.some((v) => typeof v !== "number" || !Number.isFinite(v))) {
1010
- throw new Error("Numeric scale requires a finite numeric domain");
1011
- }
1012
- return domain;
1013
- }
1014
- function compileResolvedScale(spec, domain, thresholds) {
1015
- const colorType = [
1016
- "sequential",
1017
- "diverging",
1018
- "ordinal",
1019
- "quantize",
1020
- "quantile",
1021
- "threshold",
1022
- "piecewise"
1023
- ].includes(spec.type);
1024
- if (colorType && spec.range.type === "numeric")
1025
- throw new Error("Color scale requires colors");
1026
- if (!colorType && spec.range.type !== "numeric")
1027
- throw new Error("Numeric scale requires numeric range");
1028
- const classes = spec.range.type === "palette" ? spec.range.count : void 0;
1029
- let palette = colorType ? resolvePalette(spec.range, classes) : void 0;
1030
- if ((thresholds == null ? void 0 : thresholds.length) && thresholds.length + 1 !== ((palette == null ? void 0 : palette.length) ?? 0))
1031
- throw new Error("Scale class count must match palette color count");
1032
- if (spec.type === "quantile" && thresholds) {
1033
- const original = thresholds;
1034
- thresholds = original.filter((value, i) => i === 0 || value !== original[i - 1]);
1035
- palette = palette.filter((_, i) => i === 0 || i === original.length || original[i - 1] !== original[i]);
1036
- }
1037
- const encoding = spec.encoding ?? SRGB;
1038
- const paletteStops = palette && spec.range.type === "palette" && spec.encoding ? palette.map((color2) => convertScaleColor(color2, SRGB, encoding)) : palette;
1039
- const space = spec.interpolation ?? "srgb";
1040
- const workingStops = paletteStops && (space === "linear-srgb" || space === "oklab") ? paletteStops.map((color2) => convertScaleColor(color2, encoding, SRGB)) : paletteStops;
1041
- if (spec.type === "ordinal" && domain.length > ((paletteStops == null ? void 0 : paletteStops.length) ?? 0)) {
1042
- throw new Error("Ordinal palette must have at least one color per category");
1043
- }
1044
- const resolvedRange = spec.range.type === "numeric" ? spec.range : { type: "colors", values: paletteStops };
1045
- const descriptor = {
1046
- ...spec,
1047
- domain: [...domain],
1048
- range: resolvedRange,
1049
- ...spec.range.type === "palette" ? { palette: spec.range.name } : {},
1050
- ...thresholds ? { thresholds: [...thresholds] } : {}
1051
- };
1052
- const isOrdinal = spec.type === "ordinal";
1053
- const values = isOrdinal ? domain : numericValues(domain);
1054
- const numerical = isOrdinal ? [] : numericValues(domain);
1055
- if (!isOrdinal && numerical.length < 2)
1056
- throw new Error("Numeric domain requires two endpoints");
1057
- const pivot = spec.type === "diverging" ? spec.pivot : void 0;
1058
- if (pivot !== void 0 && !(numerical[0] < pivot && pivot < numerical[numerical.length - 1]))
1059
- throw new Error("Diverging scale pivot must be between the domain endpoints");
1060
- const breaks = thresholds ?? [];
1061
- if (["quantile", "quantize", "threshold"].includes(spec.type) && !thresholds) {
1062
- throw new Error(`${spec.type} scale requires resolved class breaks`);
1063
- }
1064
- const sample = (value) => {
1065
- if (value === null)
1066
- return spec.nullColor ?? null;
1067
- if (isOrdinal) {
1068
- const index = values.indexOf(value);
1069
- return index === -1 ? spec.nullColor ?? null : paletteStops[index];
1070
- }
1071
- if (typeof value !== "number" || !Number.isFinite(value))
1072
- return spec.nullColor ?? null;
1073
- const min = numerical[0], max = numerical[numerical.length - 1];
1074
- if (value < min && spec.underColor)
1075
- return spec.underColor;
1076
- if (value > max && spec.overColor)
1077
- return spec.overColor;
1078
- if (spec.type === "quantile" || spec.type === "quantize" || spec.type === "threshold") {
1079
- const index = breaks.findIndex((boundary) => value < boundary);
1080
- const colorIndex = index < 0 ? breaks.length : index;
1081
- return paletteStops[Math.min(colorIndex, paletteStops.length - 1)];
1082
- }
1083
- const stopCount = colorType ? paletteStops.length : resolvedRange.values.length;
1084
- if (numerical.length !== 2 && numerical.length !== stopCount) {
1085
- throw new Error("Piecewise domain and range stops must have equal lengths");
1086
- }
1087
- let segment = 0;
1088
- let t;
1089
- if (numerical.length === 2) {
1090
- t = pivot === void 0 ? normalize(value, min, max, spec.type) : value <= pivot ? normalize(value, min, pivot, spec.type) / 2 : 0.5 + normalize(value, pivot, max, spec.type) / 2;
1091
- if (spec.clamp ?? colorType)
1092
- t = clamp01(t);
1093
- const position = t * (stopCount - 1);
1094
- segment = Math.min(stopCount - 2, Math.max(0, Math.floor(position)));
1095
- t = position - segment;
1096
- } else {
1097
- while (segment < numerical.length - 2 && value > numerical[segment + 1])
1098
- segment++;
1099
- t = normalize(value, numerical[segment], numerical[segment + 1], spec.type);
1100
- if (spec.clamp ?? colorType)
1101
- t = clamp01(t);
1102
- }
1103
- if (!Number.isFinite(t))
1104
- return spec.nullColor ?? null;
1105
- if (colorType) {
1106
- const color2 = interpolateColor(workingStops[segment], workingStops[segment + 1], t, space);
1107
- return workingStops === paletteStops ? color2 : convertScaleColor(color2, SRGB, encoding);
1108
- }
1109
- const numbers = resolvedRange.values;
1110
- return numbers[segment] + (numbers[segment + 1] - numbers[segment]) * t;
1963
+ function roundFloat16() {
1964
+ const cell = float16Values(new Uint16Array(1));
1965
+ return (value) => {
1966
+ cell[0] = value;
1967
+ return cell[0];
1111
1968
  };
1112
- return createResolvedScale(descriptor, sample);
1113
1969
  }
1114
1970
 
1115
- // dist/scales/quantile-sketch.js
1116
- var ScaleQuantileSketch = class {
1117
- constructor(capacity = 256) {
1118
- this.samples = [];
1119
- this.parity = 0;
1120
- this.count = 0;
1121
- this.min = Infinity;
1122
- this.max = -Infinity;
1123
- if (!Number.isSafeInteger(capacity) || capacity < 16 || capacity > 65536) {
1124
- throw new Error("Quantile sketch capacity must be an integer between 16 and 65536");
1125
- }
1126
- this.capacity = capacity;
1127
- }
1128
- get length() {
1129
- return this.count;
1130
- }
1131
- get storedSamples() {
1132
- return this.samples.length;
1133
- }
1134
- add(value) {
1135
- if (!Number.isFinite(value))
1136
- throw new Error("Quantile sketch requires finite values");
1137
- this.samples.push({ value, weight: 1 });
1138
- this.count++;
1139
- this.min = Math.min(this.min, value);
1140
- this.max = Math.max(this.max, value);
1141
- if (this.samples.length >= this.capacity * 2)
1142
- this.reduce();
1143
- }
1144
- merge(other) {
1145
- if (other === this)
1146
- throw new Error("Cannot merge a quantile sketch with itself");
1147
- for (const item of other.samples)
1148
- this.samples.push({ ...item });
1149
- this.count += other.count;
1150
- this.min = Math.min(this.min, other.min);
1151
- this.max = Math.max(this.max, other.max);
1152
- this.reduce();
1153
- }
1154
- quantile(probability) {
1155
- if (!Number.isFinite(probability) || probability < 0 || probability > 1) {
1156
- throw new Error("Quantile probability must be in [0, 1]");
1157
- }
1158
- if (!this.count)
1971
+ // dist/arrow/buffers.js
1972
+ function valueType(type) {
1973
+ return import_apache_arrow.DataType.isDictionary(type) ? valueType(type.dictionary) : type;
1974
+ }
1975
+ function typeKey(type) {
1976
+ type = valueType(type);
1977
+ if (import_apache_arrow.DataType.isInt(type))
1978
+ return `${type.isSigned ? "i" : "u"}${type.bitWidth}`;
1979
+ if (import_apache_arrow.DataType.isFloat(type)) {
1980
+ if (type.precision === import_apache_arrow.Precision.HALF)
1981
+ requireFloat16();
1982
+ return `f${type.precision}`;
1983
+ }
1984
+ if (import_apache_arrow.DataType.isBool(type))
1985
+ return "bool";
1986
+ if (import_apache_arrow.DataType.isUtf8(type))
1987
+ return "utf8";
1988
+ if (import_apache_arrow.DataType.isDate(type) && type.unit === import_apache_arrow.DateUnit.DAY)
1989
+ return "date32";
1990
+ if (import_apache_arrow.DataType.isTimestamp(type) && type.unit === import_apache_arrow.TimeUnit.MICROSECOND)
1991
+ return `timestamp_us:${type.timezone ?? ""}`;
1992
+ if (import_apache_arrow.DataType.isBinary(type))
1993
+ return "binary";
1994
+ if (import_apache_arrow.DataType.isFixedSizeBinary(type))
1995
+ return `fixedsizebinary:${type.byteWidth}`;
1996
+ if (import_apache_arrow.DataType.isNull(type))
1997
+ return "null";
1998
+ throw new Error(`Unsupported Arrow expression type: ${type}`);
1999
+ }
2000
+ var ColumnReader = class _ColumnReader {
2001
+ constructor(vector) {
2002
+ this.vector = vector;
2003
+ this.ends = [];
2004
+ this.location = [null, 0];
2005
+ this.range = [null, 0, 0];
2006
+ this.half = /* @__PURE__ */ new Map();
2007
+ this.dictionaries = /* @__PURE__ */ new Map();
2008
+ this.decoder = new TextDecoder();
2009
+ let end = 0;
2010
+ for (const data of vector.data) {
2011
+ end += data.length;
2012
+ this.ends.push(end);
2013
+ if (import_apache_arrow.DataType.isFloat(data.type) && data.type.precision === import_apache_arrow.Precision.HALF)
2014
+ this.half.set(data, float16Values(data.values));
2015
+ if (import_apache_arrow.DataType.isDictionary(data.type))
2016
+ this.dictionaries.set(data, new _ColumnReader(data.dictionary));
2017
+ }
2018
+ }
2019
+ /** Locates a chunk without constructing row objects. */
2020
+ chunk(row) {
2021
+ let index = 0;
2022
+ while (row >= this.ends[index])
2023
+ index++;
2024
+ this.location[0] = this.vector.data[index];
2025
+ this.location[1] = row - (index ? this.ends[index - 1] : 0);
2026
+ return this.location;
2027
+ }
2028
+ /** Tests validity with the physical bit offset. */
2029
+ isValid(row) {
2030
+ const [data, index] = this.chunk(row);
2031
+ if (import_apache_arrow.DataType.isNull(data.type))
2032
+ return false;
2033
+ const bit = data.offset + index;
2034
+ if (data.nullCount && !(data.nullBitmap[bit >> 3] & 1 << (bit & 7)))
2035
+ return false;
2036
+ return !import_apache_arrow.DataType.isDictionary(data.type) || this.dictionaries.get(data).isValid(Number(data.values[index]));
2037
+ }
2038
+ /** Reads a primitive; UTF-8 is decoded only for scalar callbacks or output materialization. */
2039
+ get(row) {
2040
+ if (!this.isValid(row))
1159
2041
  return null;
1160
- const sorted = [...this.samples].sort((a, b) => a.value - b.value);
1161
- if (probability === 0)
1162
- return this.min;
1163
- if (probability === 1)
1164
- return this.max;
1165
- const index = probability * (this.count - 1);
1166
- const lowerRank = Math.floor(index);
1167
- const upperRank = Math.ceil(index);
1168
- const fraction = index - lowerRank;
1169
- let cumulative = 0;
1170
- let lower;
1171
- for (const sample of sorted) {
1172
- cumulative += sample.weight;
1173
- if (lowerRank < cumulative) {
1174
- lower ?? (lower = sample.value);
1175
- if (upperRank < cumulative)
1176
- return lower * (1 - fraction) + sample.value * fraction;
1177
- }
1178
- }
1179
- return this.max;
1180
- }
1181
- /** Compact comparable weights first so recent rows do not absorb older heavy samples. */
1182
- reduce() {
1183
- while (this.samples.length > this.capacity) {
1184
- const levels = /* @__PURE__ */ new Map();
1185
- for (const sample of this.samples) {
1186
- let level2 = levels.get(sample.weight);
1187
- if (!level2) {
1188
- level2 = [];
1189
- levels.set(sample.weight, level2);
2042
+ const [data, index] = this.chunk(row);
2043
+ if (import_apache_arrow.DataType.isDictionary(data.type))
2044
+ return this.dictionaries.get(data).get(Number(data.values[index]));
2045
+ if (import_apache_arrow.DataType.isUtf8(data.type))
2046
+ return this.decoder.decode(data.values.subarray(data.valueOffsets[index], data.valueOffsets[index + 1]));
2047
+ if (import_apache_arrow.DataType.isBinary(data.type))
2048
+ return data.values.subarray(data.valueOffsets[index], data.valueOffsets[index + 1]);
2049
+ if (import_apache_arrow.DataType.isFixedSizeBinary(data.type))
2050
+ return data.values.subarray(data.stride * index, data.stride * (index + 1));
2051
+ if (import_apache_arrow.DataType.isBool(data.type)) {
2052
+ const bit = data.offset + index;
2053
+ return Boolean(data.values[bit >> 3] & 1 << (bit & 7));
2054
+ }
2055
+ return this.half.has(data) ? this.half.get(data)[index] : data.values[index];
2056
+ }
2057
+ /** Resolves UTF-8 buffers, including dictionary values, without decoding. */
2058
+ utf8(row) {
2059
+ const [data, index] = this.chunk(row);
2060
+ if (import_apache_arrow.DataType.isDictionary(data.type))
2061
+ return this.dictionaries.get(data).utf8(Number(data.values[index]));
2062
+ this.range[0] = data.values;
2063
+ this.range[1] = Number(data.valueOffsets[index]);
2064
+ this.range[2] = Number(data.valueOffsets[index + 1]);
2065
+ return this.range;
2066
+ }
2067
+ };
2068
+ function createValueValidator(inputType) {
2069
+ const type = valueType(inputType), key = typeKey(type);
2070
+ if (import_apache_arrow.DataType.isInt(type)) {
2071
+ if (type.bitWidth === 64) {
2072
+ const bits = BigInt(type.bitWidth), min2 = type.isSigned ? -(1n << bits - 1n) : 0n;
2073
+ const max2 = type.isSigned ? (1n << bits - 1n) - 1n : (1n << bits) - 1n;
2074
+ return (value) => {
2075
+ if (value === null)
2076
+ return;
2077
+ if (typeof value !== "bigint")
2078
+ throw new Error("64-bit integers require bigint values");
2079
+ if (value < min2 || value > max2)
2080
+ throw new ExpressionEvaluationError("overflow", "Integer expression overflow");
2081
+ };
2082
+ }
2083
+ const min = type.isSigned ? -(2 ** (type.bitWidth - 1)) : 0;
2084
+ const max = type.isSigned ? 2 ** (type.bitWidth - 1) - 1 : 2 ** type.bitWidth - 1;
2085
+ return (value) => {
2086
+ if (value === null)
2087
+ return;
2088
+ if (typeof value !== "number")
2089
+ throw new TypeError("Integer expressions require numeric values");
2090
+ if (!Number.isInteger(value))
2091
+ throw new ExpressionEvaluationError("conversion", "Integer expression requires integral numbers");
2092
+ if (value < min || value > max)
2093
+ throw new ExpressionEvaluationError("overflow", "Integer expression overflow");
2094
+ };
2095
+ }
2096
+ if (key === "date32") {
2097
+ return (value) => {
2098
+ if (value === null)
2099
+ return;
2100
+ if (typeof value !== "number" || !Number.isInteger(value) || value < -2147483648 || value > 2147483647)
2101
+ throw new ExpressionEvaluationError("overflow", "Invalid Date32 epoch day");
2102
+ };
2103
+ }
2104
+ if (key.startsWith("timestamp_us:")) {
2105
+ return (value) => {
2106
+ if (value === null)
2107
+ return;
2108
+ if (typeof value !== "bigint" || value !== BigInt.asIntN(64, value))
2109
+ throw new ExpressionEvaluationError("overflow", "Invalid timestamp epoch microseconds");
2110
+ };
2111
+ }
2112
+ if (import_apache_arrow.DataType.isBinary(type) || import_apache_arrow.DataType.isFixedSizeBinary(type)) {
2113
+ return (value) => {
2114
+ if (value === null)
2115
+ return;
2116
+ if (!(value instanceof Uint8Array) || import_apache_arrow.DataType.isFixedSizeBinary(type) && value.byteLength !== type.byteWidth)
2117
+ throw new Error(`Invalid value for ${type}`);
2118
+ };
2119
+ }
2120
+ const expected = import_apache_arrow.DataType.isFloat(type) ? "number" : key === "bool" ? "boolean" : "string";
2121
+ return (value) => {
2122
+ if (value !== null && (key === "null" || typeof value !== expected))
2123
+ throw new Error(`Invalid value for ${type}`);
2124
+ };
2125
+ }
2126
+ function validateValue(value, type) {
2127
+ createValueValidator(type)(value);
2128
+ }
2129
+
2130
+ // dist/arrow/cast.js
2131
+ var import_apache_arrow3 = require("apache-arrow");
2132
+ var import_utf8 = require("@math.gl/core/utf8");
2133
+
2134
+ // dist/arrow/temporal-cast.js
2135
+ var import_apache_arrow2 = require("apache-arrow");
2136
+ var DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
2137
+ var TIMESTAMP = /^(\d{4})-(\d{2})-(\d{2})[T ](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,6}))?(Z|[+-]\d{2}:\d{2})?$/;
2138
+ function bindTemporalCast(source, target) {
2139
+ const dateOnly = target === "date";
2140
+ const zoned = target === "timestamp_utc";
2141
+ const type = dateOnly ? new import_apache_arrow2.DateDay() : new import_apache_arrow2.TimestampMicrosecond(zoned ? "UTC" : void 0);
2142
+ const sourceKey = typeKey(source);
2143
+ if (sourceKey !== "utf8" && sourceKey !== "null" && sourceKey !== typeKey(type))
2144
+ throw new Error("Temporal CAST requires UTF8 or an input of the requested Arrow type");
2145
+ return {
2146
+ type,
2147
+ textInput: false,
2148
+ evaluate(value) {
2149
+ return typeof value === "string" ? parseTemporalValue(value, dateOnly, zoned) : value;
2150
+ }
2151
+ };
2152
+ }
2153
+ function parseTemporalValue(text, dateOnly, zoned) {
2154
+ const match = (dateOnly ? DATE : TIMESTAMP).exec(text.trim());
2155
+ const invalid = () => new ExpressionEvaluationError("conversion", "Invalid text for temporal CAST");
2156
+ if (!match)
2157
+ throw invalid();
2158
+ const [, yearText, monthText, dayText, hourText, minuteText, secondText, fraction, zone] = match;
2159
+ const year = Number(yearText);
2160
+ const month = Number(monthText);
2161
+ const day = Number(dayText);
2162
+ const calendar = /* @__PURE__ */ new Date(0);
2163
+ calendar.setUTCFullYear(year, month - 1, day);
2164
+ if (calendar.getUTCFullYear() !== year || calendar.getUTCMonth() !== month - 1 || calendar.getUTCDate() !== day)
2165
+ throw invalid();
2166
+ if (dateOnly)
2167
+ return calendar.getTime() / 864e5;
2168
+ const hour = Number(hourText);
2169
+ const minute = Number(minuteText);
2170
+ const second = Number(secondText);
2171
+ if (hour > 23 || minute > 59 || second > 59 || zoned !== Boolean(zone))
2172
+ throw invalid();
2173
+ let offsetMinutes = 0;
2174
+ if (zone && zone !== "Z") {
2175
+ const offsetHour = Number(zone.slice(1, 3));
2176
+ const offsetMinute = Number(zone.slice(4));
2177
+ if (offsetHour > 23 || offsetMinute > 59)
2178
+ throw invalid();
2179
+ offsetMinutes = (zone.startsWith("-") ? -1 : 1) * (offsetHour * 60 + offsetMinute);
2180
+ }
2181
+ const seconds = hour * 3600 + minute * 60 + second - offsetMinutes * 60;
2182
+ return BigInt(calendar.getTime() + seconds * 1e3) * 1000n + BigInt((fraction ?? "").padEnd(6, "0"));
2183
+ }
2184
+
2185
+ // dist/arrow/cast.js
2186
+ var CAST_TYPES = {
2187
+ int8: import_apache_arrow3.Int8,
2188
+ int16: import_apache_arrow3.Int16,
2189
+ int32: import_apache_arrow3.Int32,
2190
+ int64: import_apache_arrow3.Int64,
2191
+ uint8: import_apache_arrow3.Uint8,
2192
+ uint16: import_apache_arrow3.Uint16,
2193
+ uint32: import_apache_arrow3.Uint32,
2194
+ uint64: import_apache_arrow3.Uint64,
2195
+ float16: import_apache_arrow3.Float16,
2196
+ float32: import_apache_arrow3.Float32,
2197
+ float64: import_apache_arrow3.Float64,
2198
+ bool: import_apache_arrow3.Bool,
2199
+ utf8: import_apache_arrow3.Utf8
2200
+ };
2201
+ function bindCast(source, target) {
2202
+ if (target === "date" || target === "timestamp" || target === "timestamp_utc")
2203
+ return bindTemporalCast(source, target);
2204
+ const binaryTarget = typeof target === "string" && /^(varbinary|fixedsizebinary):([1-9]\d*)$/.exec(target);
2205
+ if (target === "binary" || binaryTarget) {
2206
+ const byteWidth = binaryTarget ? Number(binaryTarget[2]) : void 0;
2207
+ if (byteWidth !== void 0 && byteWidth > 2147483647)
2208
+ throw new Error("Binary CAST requires a positive 32-bit byte width");
2209
+ const type2 = binaryTarget && binaryTarget[1] === "fixedsizebinary" ? new import_apache_arrow3.FixedSizeBinary(byteWidth) : new import_apache_arrow3.Binary();
2210
+ const sourceKey2 = typeKey(source);
2211
+ const textInput2 = sourceKey2 === "utf8";
2212
+ if (!textInput2 && sourceKey2 !== "null" && sourceKey2 !== "binary" && !sourceKey2.startsWith("fixedsizebinary:"))
2213
+ throw new Error("Binary CAST requires binary or UTF8 input");
2214
+ return {
2215
+ type: type2,
2216
+ textInput: textInput2,
2217
+ evaluate(value, range) {
2218
+ const bytes2 = range ? range[0].subarray(range[1], range[2]) : value;
2219
+ if (bytes2 === null || byteWidth === void 0)
2220
+ return bytes2;
2221
+ if (bytes2.byteLength > byteWidth)
2222
+ throw new ExpressionEvaluationError("overflow", `Binary CAST exceeds ${byteWidth} bytes`);
2223
+ if (import_apache_arrow3.DataType.isFixedSizeBinary(type2) && bytes2.byteLength < byteWidth) {
2224
+ const padded = new Uint8Array(byteWidth);
2225
+ padded.set(bytes2);
2226
+ return padded;
1190
2227
  }
1191
- level2.push(sample);
1192
- }
1193
- const weights = [...levels.keys()].sort((a, b) => a - b);
1194
- const weight = weights.find((candidate) => levels.get(candidate).length > 1);
1195
- if (weight === void 0) {
1196
- const a = levels.get(weights[0]).pop();
1197
- const b = levels.get(weights[1]).pop();
1198
- this.samples = this.samples.filter((item) => item !== a && item !== b);
1199
- this.samples.push({ value: b.value, weight: a.weight + b.weight });
2228
+ return bytes2;
2229
+ }
2230
+ };
2231
+ }
2232
+ const dictionary = target === "dictionary_utf8_int32";
2233
+ if (dictionary)
2234
+ target = "utf8";
2235
+ if (!isColumnarScalarType(target))
2236
+ throw new Error("Unsupported scalar CAST target");
2237
+ const type = new CAST_TYPES[target]();
2238
+ const integer = import_apache_arrow3.DataType.isInt(type);
2239
+ const wideInteger = integer && type.bitWidth === 64;
2240
+ const sourceKey = typeKey(source);
2241
+ const textInput = sourceKey === "utf8";
2242
+ if (sourceKey === "date32" || sourceKey.startsWith("timestamp_us:"))
2243
+ throw new Error("Temporal CAST requires a matching temporal target");
2244
+ if (sourceKey === "binary" || sourceKey.startsWith("fixedsizebinary:"))
2245
+ throw new Error("CAST from binary requires a binary target");
2246
+ if (sourceKey !== "null" && (sourceKey === "bool" && !["bool", "utf8"].includes(target) || target === "bool" && !["bool", "utf8"].includes(sourceKey)))
2247
+ throw new Error("CAST between Boolean and numeric values is not supported");
2248
+ const validate = createValueValidator(type);
2249
+ const halfRound = target === "float16" ? roundFloat16() : void 0;
2250
+ const encoder2 = new TextEncoder();
2251
+ return {
2252
+ type: dictionary ? new import_apache_arrow3.Dictionary(new import_apache_arrow3.Utf8(), new import_apache_arrow3.Int32()) : type,
2253
+ textInput,
2254
+ evaluate(value, range) {
2255
+ if (value === null && !range)
2256
+ return null;
2257
+ let result;
2258
+ if (target === "utf8") {
2259
+ if (typeof value === "boolean")
2260
+ return value ? "TRUE" : "FALSE";
2261
+ return String(value);
2262
+ }
2263
+ if (textInput) {
2264
+ const input = range ?? (() => {
2265
+ const bytes3 = encoder2.encode(value);
2266
+ return [bytes3, 0, bytes3.length];
2267
+ })();
2268
+ const [bytes2, start, end] = input;
2269
+ result = target === "bool" ? (0, import_utf8.parseUTF8Boolean)(bytes2, start, end) ?? null : integer ? (0, import_utf8.parseUTF8BigInt)(bytes2, start, end) ?? null : (0, import_utf8.parseUTF8Number)(bytes2, start, end) ?? null;
2270
+ if (result === null)
2271
+ throw new ExpressionEvaluationError("conversion", `Invalid text for CAST to ${target}`);
2272
+ if (integer && !wideInteger)
2273
+ result = Number(result);
2274
+ } else if (target === "bool")
2275
+ result = value;
2276
+ else if (wideInteger) {
2277
+ if (typeof value === "number" && !Number.isFinite(value))
2278
+ throw new ExpressionEvaluationError("conversion", "Non-finite integer CAST");
2279
+ result = typeof value === "bigint" ? value : BigInt(Math.trunc(value));
2280
+ } else if (integer)
2281
+ result = Math.trunc(Number(value));
2282
+ else
2283
+ result = Number(value);
2284
+ if (halfRound)
2285
+ result = halfRound(result);
2286
+ if (target === "float32")
2287
+ result = Math.fround(result);
2288
+ if ((target === "float16" || target === "float32" || target === "float64") && !Number.isFinite(result))
2289
+ throw new ExpressionEvaluationError("overflow", "Floating CAST overflow or non-finite value");
2290
+ validate(result);
2291
+ return result;
2292
+ }
2293
+ };
2294
+ }
2295
+
2296
+ // dist/arrow/like.js
2297
+ function compileUTF8Like(pattern, insensitive, escape) {
2298
+ if (escape !== void 0 && [...escape].length !== 1) {
2299
+ throw new Error("LIKE ESCAPE requires one character");
2300
+ }
2301
+ const tokens = [];
2302
+ let escaped = false;
2303
+ for (const character of pattern) {
2304
+ if (!escaped && character === escape) {
2305
+ escaped = true;
2306
+ continue;
2307
+ }
2308
+ tokens.push(!escaped && character === "%" ? -1 : !escaped && character === "_" ? -2 : fold(character.codePointAt(0), insensitive));
2309
+ escaped = false;
2310
+ }
2311
+ if (escaped)
2312
+ throw new Error("LIKE pattern ends with ESCAPE");
2313
+ return (bytes2, start, end) => {
2314
+ let offset = start;
2315
+ let token = 0;
2316
+ let wildcard = -1;
2317
+ let retry = start;
2318
+ while (offset < end) {
2319
+ if (tokens[token] === -1) {
2320
+ wildcard = token++;
2321
+ retry = offset;
1200
2322
  continue;
1201
2323
  }
1202
- const level = levels.get(weight).sort((a, b) => a.value - b.value);
1203
- const offset = this.parity++ % 2;
1204
- const compacted = [];
1205
- for (let i = 0; i + 1 < level.length; i += 2) {
1206
- compacted.push({ value: level[i + offset].value, weight: weight * 2 });
2324
+ const [point, next] = readCodePoint(bytes2, offset);
2325
+ if (tokens[token] === -2 || tokens[token] === fold(point, insensitive)) {
2326
+ token++;
2327
+ offset = next;
2328
+ } else if (wildcard >= 0) {
2329
+ retry = readCodePoint(bytes2, retry)[1];
2330
+ offset = retry;
2331
+ token = wildcard + 1;
2332
+ } else {
2333
+ return false;
1207
2334
  }
1208
- if (level.length % 2)
1209
- compacted.push(level[level.length - 1]);
1210
- this.samples = this.samples.filter((sample) => sample.weight !== weight).concat(compacted);
1211
2335
  }
2336
+ while (tokens[token] === -1)
2337
+ token++;
2338
+ return token === tokens.length;
2339
+ };
2340
+ }
2341
+ function fold(point, insensitive) {
2342
+ if (!insensitive)
2343
+ return point;
2344
+ if (point < 128)
2345
+ return point >= 65 && point <= 90 ? point + 32 : point;
2346
+ const folded = String.fromCodePoint(point).toLowerCase();
2347
+ return folded.length === 1 && folded.charCodeAt(0) < 128 ? folded.charCodeAt(0) : folded;
2348
+ }
2349
+ function readCodePoint(bytes2, offset) {
2350
+ const first = bytes2[offset++];
2351
+ if (first < 128)
2352
+ return [first, offset];
2353
+ const count = first < 224 ? 1 : first < 240 ? 2 : 3;
2354
+ let point = first & (count === 1 ? 31 : count === 2 ? 15 : 7);
2355
+ for (let index = 0; index < count; index++)
2356
+ point = point << 6 | bytes2[offset++] & 63;
2357
+ return [point, offset];
2358
+ }
2359
+
2360
+ // dist/arrow/signature.js
2361
+ var import_apache_arrow4 = require("apache-arrow");
2362
+ function physicalKey(type) {
2363
+ var _a2;
2364
+ try {
2365
+ return import_apache_arrow4.DataType.isDictionary(type) ? `dictionary:${typeKey(type.indices)}:${typeKey(type.dictionary)}:${type.isOrdered}` : typeKey(type);
2366
+ } catch {
2367
+ const properties = [
2368
+ "bitWidth",
2369
+ "isSigned",
2370
+ "precision",
2371
+ "scale",
2372
+ "unit",
2373
+ "timezone",
2374
+ "byteWidth",
2375
+ "listSize",
2376
+ "keysSorted",
2377
+ "isOrdered",
2378
+ "mode"
2379
+ ].map((key) => type[key]);
2380
+ return JSON.stringify([
2381
+ type.typeId,
2382
+ properties,
2383
+ import_apache_arrow4.DataType.isUnion(type) ? Array.from(type.typeIds) : null,
2384
+ import_apache_arrow4.DataType.isDictionary(type) ? [physicalKey(type.indices), physicalKey(type.dictionary), type.isOrdered] : null,
2385
+ (_a2 = type.children) == null ? void 0 : _a2.map((field) => [
2386
+ field.name,
2387
+ physicalKey(field.type),
2388
+ field.nullable,
2389
+ extensionKey(field)
2390
+ ])
2391
+ ]);
1212
2392
  }
1213
- };
2393
+ }
2394
+ function extensionKey(field) {
2395
+ return JSON.stringify([
2396
+ field.metadata.get("ARROW:extension:name"),
2397
+ field.metadata.get("ARROW:extension:metadata")
2398
+ ]);
2399
+ }
2400
+
2401
+ // dist/arrow/registry.js
2402
+ function createFunctionLookup(declarations = []) {
2403
+ var _a2;
2404
+ const functions = /* @__PURE__ */ new Map();
2405
+ for (const declaration of declarations) {
2406
+ const name2 = declaration == null ? void 0 : declaration.name;
2407
+ if (typeof name2 !== "string" || !/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name2))
2408
+ throw new Error("Invalid Arrow function name");
2409
+ if ([
2410
+ "TRY",
2411
+ "COALESCE",
2412
+ "array",
2413
+ "array_float16",
2414
+ "array_float32",
2415
+ "array_float64",
2416
+ "array_uint8",
2417
+ "PACK_UNORM8",
2418
+ "EPOCH_MS",
2419
+ "LIST_EXTRACT",
2420
+ "STRUCT_EXTRACT",
2421
+ "cast",
2422
+ "in",
2423
+ "isNull",
2424
+ "isNotNull",
2425
+ "parseNumber",
2426
+ "parseBigInt",
2427
+ "parseBoolean",
2428
+ "toFloat64",
2429
+ "toInt32",
2430
+ "toInt64"
2431
+ ].includes(name2))
2432
+ throw new Error(`Reserved Arrow function: ${name2}`);
2433
+ if (!Array.isArray(declaration.prototypes) || !declaration.prototypes.length)
2434
+ throw new Error("Arrow functions require one or more prototypes");
2435
+ const prototypes = [...((_a2 = functions.get(name2)) == null ? void 0 : _a2.prototypes) ?? []];
2436
+ for (const prototype of declaration.prototypes) {
2437
+ if (!prototype || typeof prototype !== "object" || !Array.isArray(prototype.arguments) || !prototype.result || typeof prototype.evaluate !== "function")
2438
+ throw new Error("Arrow prototypes require argument fields, a result field and evaluate");
2439
+ if (prototype.nullHandling !== void 0 && !["propagate", "call"].includes(prototype.nullHandling))
2440
+ throw new Error("Unsupported null handling");
2441
+ if (prototype.evaluateScalar !== void 0 && typeof prototype.evaluateScalar !== "function")
2442
+ throw new Error("Arrow function evaluateScalar must be a function");
2443
+ const key = JSON.stringify(prototype.arguments.map((field) => [
2444
+ physicalKey(field.type),
2445
+ [...field.metadata].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
2446
+ ]));
2447
+ if (prototypes.some((existing) => JSON.stringify(existing.arguments.map((field) => [
2448
+ physicalKey(field.type),
2449
+ [...field.metadata].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
2450
+ ])) === key))
2451
+ throw new Error(`Duplicate Arrow function signature: ${name2}`);
2452
+ prototypes.push(Object.freeze({ ...prototype, arguments: Object.freeze([...prototype.arguments]) }));
2453
+ }
2454
+ functions.set(name2, Object.freeze({ name: name2, prototypes: Object.freeze(prototypes) }));
2455
+ }
2456
+ return functions;
2457
+ }
2458
+
2459
+ // dist/arrow/list-values.js
2460
+ var import_apache_arrow5 = require("apache-arrow");
2461
+ function isTupleType(type) {
2462
+ if (!import_apache_arrow5.DataType.isFixedSizeList(type))
2463
+ return false;
2464
+ const child = type.children[0];
2465
+ if (!Number.isSafeInteger(type.listSize) || type.listSize <= 0 || !child || child.nullable)
2466
+ return false;
2467
+ if (!import_apache_arrow5.DataType.isInt(child.type) && !import_apache_arrow5.DataType.isFloat(child.type))
2468
+ return false;
2469
+ typeKey(child.type);
2470
+ return true;
2471
+ }
2472
+ function nullTupleVector(type, length) {
2473
+ const childType = type.children[0].type;
2474
+ const child = (0, import_apache_arrow5.makeData)({
2475
+ type: childType,
2476
+ length: length * type.listSize,
2477
+ data: new childType.ArrayType(length * type.listSize)
2478
+ });
2479
+ return (0, import_apache_arrow5.makeVector)((0, import_apache_arrow5.makeData)({
2480
+ type,
2481
+ length,
2482
+ child,
2483
+ nullCount: length,
2484
+ nullBitmap: new Uint8Array(Math.ceil(length / 8))
2485
+ }));
2486
+ }
2487
+ function maskTupleVector(vector, active) {
2488
+ if (!active.some((value) => !value))
2489
+ return vector;
2490
+ let start = 0;
2491
+ const chunks = vector.data.map((data) => {
2492
+ const bitmap = new Uint8Array(Math.ceil((data.offset + data.length) / 8));
2493
+ let nullCount = 0;
2494
+ for (let row = 0; row < data.length; row++) {
2495
+ if (active[start + row] && data.getValid(row)) {
2496
+ const bit = data.offset + row;
2497
+ bitmap[bit >> 3] |= 1 << (bit & 7);
2498
+ } else
2499
+ nullCount++;
2500
+ }
2501
+ start += data.length;
2502
+ const buffers = data.buffers;
2503
+ buffers[2] = bitmap;
2504
+ return data.clone(data.type, data.offset, data.length, nullCount, buffers);
2505
+ });
2506
+ return new import_apache_arrow5.Vector(chunks);
2507
+ }
2508
+ function validateTupleVector(vector, field, active) {
2509
+ const type = field.type;
2510
+ let start = 0;
2511
+ for (const data of vector.data) {
2512
+ const child = data.children[0];
2513
+ if (!child || child.length < data.length * type.listSize)
2514
+ throw new Error("Invalid fixed-size list child length");
2515
+ for (let row = 0; row < data.length; row++) {
2516
+ if (!active[start + row])
2517
+ continue;
2518
+ if (!data.getValid(row)) {
2519
+ if (!field.nullable)
2520
+ throw new Error(`Function ${field.name} returned null for a non-nullable result`);
2521
+ continue;
2522
+ }
2523
+ for (let component = 0; component < type.listSize; component++) {
2524
+ if (!child.getValid(row * type.listSize + component))
2525
+ throw new Error("Fixed-size list result contains a null non-nullable component");
2526
+ }
2527
+ }
2528
+ start += data.length;
2529
+ }
2530
+ }
2531
+ function selectTupleVectors(type, yes, no, selected, active) {
2532
+ const result = nullTupleVector(type, active.length);
2533
+ const data = result.data[0];
2534
+ const child = data.children[0];
2535
+ const values = import_apache_arrow5.DataType.isFloat(child.type) && child.type.precision === import_apache_arrow5.Precision.HALF ? float16Values(child.values) : child.values;
2536
+ const a = new ColumnReader(yes.getChildAt(0));
2537
+ const b = new ColumnReader(no.getChildAt(0));
2538
+ const aValid = new ColumnReader(yes);
2539
+ const bValid = new ColumnReader(no);
2540
+ for (let row = 0; row < active.length; row++) {
2541
+ if (!active[row])
2542
+ continue;
2543
+ const reader = selected[row] ? a : b;
2544
+ if (!(selected[row] ? aValid : bValid).isValid(row))
2545
+ continue;
2546
+ data.setValid(row, true);
2547
+ for (let component = 0; component < type.listSize; component++) {
2548
+ const index = row * type.listSize + component;
2549
+ values[index] = reader.get(index);
2550
+ }
2551
+ }
2552
+ return result;
2553
+ }
2554
+ function compileArrayLiteral(ast, type) {
2555
+ if (!import_apache_arrow5.DataType.isList(type) && !import_apache_arrow5.DataType.isFixedSizeList(type))
2556
+ throw new Error("Array literals require a declared List or FixedSizeList argument");
2557
+ const listType = type;
2558
+ const child = listType.children[0];
2559
+ if (import_apache_arrow5.DataType.isDictionary(child.type))
2560
+ throw new Error("Array literal elements require primitive types");
2561
+ typeKey(child.type);
2562
+ if (ast.elements.length > 4096)
2563
+ throw new Error("Array literal exceeds 4096 elements");
2564
+ if (import_apache_arrow5.DataType.isFixedSizeList(type) && ast.elements.length !== type.listSize)
2565
+ throw new Error("Array literal length must match its FixedSizeList type");
2566
+ if (import_apache_arrow5.DataType.isFixedSizeList(type) && !isTupleType(type))
2567
+ throw new Error("Fixed-size array literals require non-nullable numeric elements");
2568
+ const round = import_apache_arrow5.DataType.isFloat(child.type) ? child.type.precision === import_apache_arrow5.Precision.HALF ? roundFloat16() : child.type.precision === import_apache_arrow5.Precision.SINGLE ? Math.fround : void 0 : void 0;
2569
+ const values = Array.from(ast.elements, (element) => {
2570
+ var _a2;
2571
+ let value;
2572
+ if ((element == null ? void 0 : element.type) === "Literal")
2573
+ value = element.value;
2574
+ else if ((element == null ? void 0 : element.type) === "UnaryExpression" && ["+", "-"].includes(element.operator) && ((_a2 = element.argument) == null ? void 0 : _a2.type) === "Literal" && typeof element.argument.value === "number")
2575
+ value = element.operator === "-" ? -element.argument.value : element.argument.value;
2576
+ else
2577
+ throw new Error("Array elements must be scalar literals");
2578
+ if (value === null && !child.nullable)
2579
+ throw new Error("Array literal contains a null non-nullable element");
2580
+ validateValue(value, child.type);
2581
+ if (round && typeof value === "number") {
2582
+ const rounded = round(value);
2583
+ if (Number.isFinite(value) && !Number.isFinite(rounded))
2584
+ throw new Error(`Numeric literal overflow for ${child.type}`);
2585
+ return rounded;
2586
+ }
2587
+ return value;
2588
+ });
2589
+ const typed = ast.typedValues;
2590
+ const matchingStorage = typed && typed.constructor === child.type.ArrayType && typed.length === values.length && values.every((value, index) => Object.is(value, typed[index]));
2591
+ const literal3 = matchingStorage ? new import_apache_arrow5.Vector([
2592
+ import_apache_arrow5.DataType.isList(listType) ? (0, import_apache_arrow5.makeData)({
2593
+ type: listType,
2594
+ length: 1,
2595
+ valueOffsets: Int32Array.of(0, values.length),
2596
+ child: (0, import_apache_arrow5.makeData)({ type: child.type, length: values.length, data: typed.slice() })
2597
+ }) : (0, import_apache_arrow5.makeData)({
2598
+ type: listType,
2599
+ length: 1,
2600
+ child: (0, import_apache_arrow5.makeData)({ type: child.type, length: values.length, data: typed.slice() })
2601
+ })
2602
+ ]) : (0, import_apache_arrow5.vectorFromArray)([values], listType);
2603
+ const dictionaryType = new import_apache_arrow5.Dictionary(listType, new import_apache_arrow5.Uint8());
2604
+ let cached;
2605
+ return (length) => {
2606
+ if (!import_apache_arrow5.DataType.isFixedSizeList(listType) && (cached == null ? void 0 : cached.length) === length)
2607
+ return cached;
2608
+ if (import_apache_arrow5.DataType.isFixedSizeList(listType)) {
2609
+ const result = nullTupleVector(listType, length);
2610
+ const data = result.data[0];
2611
+ const childData = data.children[0];
2612
+ const components = literal3.data[0].children[0].values.subarray(0, listType.listSize);
2613
+ for (let row = 0; row < length; row++) {
2614
+ data.setValid(row, true);
2615
+ childData.values.set(components, row * listType.listSize);
2616
+ }
2617
+ cached = result;
2618
+ } else {
2619
+ cached = (0, import_apache_arrow5.makeVector)((0, import_apache_arrow5.makeData)({ type: dictionaryType, length, data: new Uint8Array(length), dictionary: literal3 }));
2620
+ }
2621
+ return cached;
2622
+ };
2623
+ }
2624
+
2625
+ // dist/arrow/compiler.js
2626
+ var encoder = new TextEncoder();
2627
+ var bool = new import_apache_arrow6.Bool();
2628
+ function compileArrowExpression(expression, options) {
2629
+ const fields = [...options.inputSchema.fields];
2630
+ if (new Set(fields.map((field) => field.name)).size !== fields.length)
2631
+ throw new Error("Expression input field names must be unique");
2632
+ const ast = parse(expression);
2633
+ validateExpressionAST(ast);
2634
+ const root = bind(ast, {
2635
+ ...options,
2636
+ functionLookup: createFunctionLookup(options.functions)
2637
+ });
2638
+ const keys = fields.map((field) => ({
2639
+ name: field.name,
2640
+ type: physicalKey(field.type),
2641
+ nullable: field.nullable,
2642
+ extension: extensionKey(field)
2643
+ }));
2644
+ const validateSchema = (schema) => {
2645
+ if (schema.fields.length !== keys.length || schema.fields.some((field, index) => {
2646
+ const expected = keys[index];
2647
+ return field.name !== expected.name || physicalKey(field.type) !== expected.type || field.nullable !== expected.nullable || extensionKey(field) !== expected.extension;
2648
+ }))
2649
+ throw new Error("Arrow expression input schema mismatch");
2650
+ };
2651
+ const validateMask = (mask, length) => {
2652
+ if (mask && (mask.length !== length || mask.some((value) => value !== 0 && value !== 1)))
2653
+ throw new Error("activeRows must contain one 0 or 1 per input row");
2654
+ };
2655
+ const evaluate = (batch, evaluation = {}) => {
2656
+ var _a2, _b;
2657
+ (_a2 = evaluation.signal) == null ? void 0 : _a2.throwIfAborted();
2658
+ validateSchema(batch.schema);
2659
+ validateMask(evaluation.activeRows, batch.numRows);
2660
+ const active = evaluation.activeRows ? evaluation.activeRows.slice() : new Uint8Array(batch.numRows).fill(1);
2661
+ const prepared = root.prepare(batch, active, {});
2662
+ const result = isTupleType(root.field.type) ? maskTupleVector(prepared.vector, active) : isNestedType(root.field.type) ? nestedVector(prepared.vector, root.field.type, active) : prepared.vector && (root.constant || !import_apache_arrow6.DataType.isDictionary(prepared.vector.type) && !active.some((value) => !value)) ? prepared.vector : output(root.field, active, prepared.get);
2663
+ (_b = evaluation.signal) == null ? void 0 : _b.throwIfAborted();
2664
+ return result;
2665
+ };
2666
+ return Object.freeze({
2667
+ outputField: root.field,
2668
+ evaluate,
2669
+ evaluateTable(arrowTable, evaluation = {}) {
2670
+ var _a2;
2671
+ (_a2 = evaluation.signal) == null ? void 0 : _a2.throwIfAborted();
2672
+ validateSchema(arrowTable.schema);
2673
+ validateMask(evaluation.activeRows, arrowTable.numRows);
2674
+ let offset = 0;
2675
+ const chunks = arrowTable.batches.flatMap((batch) => {
2676
+ var _a3;
2677
+ const activeRows = (_a3 = evaluation.activeRows) == null ? void 0 : _a3.subarray(offset, offset + batch.numRows);
2678
+ offset += batch.numRows;
2679
+ return evaluate(batch, { ...evaluation, activeRows }).data;
2680
+ });
2681
+ return chunks.length ? new import_apache_arrow6.Vector(chunks) : isTupleType(root.field.type) ? nullTupleVector(root.field.type, 0) : isNestedType(root.field.type) ? (0, import_apache_arrow6.vectorFromArray)([], root.field.type) : output(root.field, new Uint8Array(0), () => null);
2682
+ }
2683
+ });
2684
+ }
2685
+ function output(field, active, evaluate) {
2686
+ const type = valueType(field.type);
2687
+ const validate = createValueValidator(type);
2688
+ if (import_apache_arrow6.DataType.isUtf8(type) || import_apache_arrow6.DataType.isBinary(type) || import_apache_arrow6.DataType.isFixedSizeBinary(type)) {
2689
+ const values = new Array(active.length).fill(null);
2690
+ for (let row = 0; row < active.length; row++)
2691
+ if (active[row]) {
2692
+ const value = evaluate(row);
2693
+ if (value === null && !field.nullable)
2694
+ throw new Error(`Non-nullable expression ${field.name} returned null`);
2695
+ validate(value);
2696
+ values[row] = value;
2697
+ }
2698
+ return (0, import_apache_arrow6.vectorFromArray)(values, field.type);
2699
+ }
2700
+ if (import_apache_arrow6.DataType.isNull(type)) {
2701
+ for (let row = 0; row < active.length; row++)
2702
+ if (active[row])
2703
+ validate(evaluate(row));
2704
+ return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({ type, length: active.length }));
2705
+ }
2706
+ const isBoolean = import_apache_arrow6.DataType.isBool(type);
2707
+ const data = isBoolean ? new Uint8Array(Math.ceil(active.length / 8)) : new type.ArrayType(active.length);
2708
+ const numericData = import_apache_arrow6.DataType.isFloat(type) && type.precision === import_apache_arrow6.Precision.HALF ? float16Values(data) : data;
2709
+ const validity = new Uint8Array(Math.ceil(active.length / 8));
2710
+ let nullCount = 0;
2711
+ for (let row = 0; row < active.length; row++) {
2712
+ if (!active[row]) {
2713
+ nullCount++;
2714
+ continue;
2715
+ }
2716
+ const value = evaluate(row);
2717
+ if (value === null) {
2718
+ if (!field.nullable)
2719
+ throw new Error(`Non-nullable expression ${field.name} returned null`);
2720
+ nullCount++;
2721
+ continue;
2722
+ }
2723
+ validate(value);
2724
+ validity[row >> 3] |= 1 << (row & 7);
2725
+ if (isBoolean) {
2726
+ if (value)
2727
+ data[row >> 3] |= 1 << (row & 7);
2728
+ } else
2729
+ numericData[row] = value;
2730
+ }
2731
+ return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
2732
+ type,
2733
+ data,
2734
+ length: active.length,
2735
+ nullBitmap: validity,
2736
+ nullCount
2737
+ }));
2738
+ }
2739
+ function sourceReader(vector) {
2740
+ const data = vector.data[0];
2741
+ if (vector.data.length === 1 && (import_apache_arrow6.DataType.isInt(data.type) || import_apache_arrow6.DataType.isFloat(data.type) && data.type.precision !== import_apache_arrow6.Precision.HALF)) {
2742
+ const values = data.values, bitmap = data.nullBitmap, offset = data.offset;
2743
+ const valid = data.nullCount ? (row) => Boolean(bitmap[offset + row >> 3] & 1 << (offset + row & 7)) : () => true;
2744
+ return {
2745
+ vector,
2746
+ get: (row) => valid(row) ? values[row] : null,
2747
+ isNull: (row) => !valid(row)
2748
+ };
2749
+ }
2750
+ const reader = new ColumnReader(vector);
2751
+ return {
2752
+ vector,
2753
+ get: (row) => reader.get(row),
2754
+ isNull: (row) => !reader.isValid(row),
2755
+ bytes: typeKey(vector.type) === "utf8" ? (row) => reader.isValid(row) ? reader.utf8(row) : null : void 0
2756
+ };
2757
+ }
2758
+ function vectorReader(vector) {
2759
+ try {
2760
+ typeKey(vector.type);
2761
+ return sourceReader(vector);
2762
+ } catch {
2763
+ }
2764
+ const reader = new ColumnReader(vector);
2765
+ return {
2766
+ vector,
2767
+ isNull: (row) => !reader.isValid(row),
2768
+ get: () => {
2769
+ throw new Error("Nested values require a vector kernel");
2770
+ }
2771
+ };
2772
+ }
2773
+ function bytes(value, row) {
2774
+ if (value.bytes)
2775
+ return value.bytes(row);
2776
+ const text = value.get(row);
2777
+ if (text === null)
2778
+ return null;
2779
+ const encoded = encoder.encode(text);
2780
+ return [encoded, 0, encoded.length];
2781
+ }
2782
+ function bind(ast, options, expected, vectorInput = false) {
2783
+ const node = bindNode(ast, options, expected, vectorInput);
2784
+ return {
2785
+ ...node,
2786
+ prepare(batch, active, context) {
2787
+ const mask = context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active;
2788
+ const prepared = node.prepare(batch, mask, context);
2789
+ if (!context.failed)
2790
+ return prepared;
2791
+ const guard = (row, read) => {
2792
+ if (!mask[row] || context.failed[row])
2793
+ return null;
2794
+ try {
2795
+ const value = read();
2796
+ return context.failed[row] ? null : value;
2797
+ } catch (error) {
2798
+ if (!isExpressionEvaluationError(error))
2799
+ throw error;
2800
+ context.failed[row] = 1;
2801
+ return null;
2802
+ }
2803
+ };
2804
+ return {
2805
+ ...prepared,
2806
+ get: (row) => guard(row, () => prepared.get(row)),
2807
+ bytes: prepared.bytes ? (row) => guard(row, () => prepared.bytes(row)) : void 0,
2808
+ isNull: (row) => guard(row, () => prepared.isNull ? prepared.isNull(row) : prepared.get(row) === null) ?? true
2809
+ };
2810
+ }
2811
+ };
2812
+ }
2813
+ function prepareInput(node, batch, active, context, force) {
2814
+ const prepared = node.prepare(batch, active, context);
2815
+ if (!context.failed || !force || prepared.vector)
2816
+ return prepared;
2817
+ const mask = active.map((value, row) => value && !context.failed[row] ? 1 : 0);
2818
+ const field = new import_apache_arrow6.Field(node.field.name, node.field.type, true, node.field.metadata);
2819
+ return sourceReader(output(field, mask, prepared.get));
2820
+ }
2821
+ function bindNode(ast, options, expected, vectorInput = false) {
2822
+ if (isSignedLiteral(ast)) {
2823
+ const value = ast.operator === "-" ? -ast.argument.value : ast.argument.value;
2824
+ return bind({ type: "Literal", value }, options, expected, vectorInput);
2825
+ }
2826
+ if (ast.type === "ArrayExpression") {
2827
+ if (!expected)
2828
+ throw new Error("Array literals require a declared list type");
2829
+ const materialize = compileArrayLiteral(ast, expected);
2830
+ return {
2831
+ field: new import_apache_arrow6.Field("literal", expected, false),
2832
+ hasKernel: false,
2833
+ prepare: (_batch, active) => vectorReader(materialize(active.length))
2834
+ };
2835
+ }
2836
+ if (ast.type === "Identifier") {
2837
+ const index = options.inputSchema.fields.findIndex((field) => field.name === ast.name);
2838
+ if (index < 0)
2839
+ throw new Error(`Unknown Arrow expression field: ${ast.name}`);
2840
+ const source = options.inputSchema.fields[index];
2841
+ const tuple = isTupleType(source.type);
2842
+ if (!vectorInput && !tuple)
2843
+ typeKey(source.type);
2844
+ return {
2845
+ field: new import_apache_arrow6.Field(source.name, vectorInput ? source.type : valueType(source.type), source.nullable, source.metadata),
2846
+ hasKernel: false,
2847
+ prepare: (batch) => {
2848
+ const vector = batch.getChildAt(index);
2849
+ return vectorInput || tuple ? vectorReader(vector) : sourceReader(vector);
2850
+ }
2851
+ };
2852
+ }
2853
+ if (ast.type === "Literal") {
2854
+ if (vectorInput && expected && import_apache_arrow6.DataType.isDictionary(expected))
2855
+ throw new Error("Dictionary function arguments require a dictionary-encoded source column");
2856
+ const date = ast.value instanceof Date ? ast.value : void 0;
2857
+ let value = date ? expected && import_apache_arrow6.DataType.isDate(expected) ? Math.floor(date.getTime() / 864e5) : BigInt(date.getTime()) * 1000n : ast.value;
2858
+ if (value instanceof Uint8Array)
2859
+ value = new Uint8Array(value);
2860
+ if (expected && import_apache_arrow6.DataType.isInt(expected) && expected.bitWidth === 64 && typeof value === "number" && Number.isSafeInteger(value))
2861
+ value = BigInt(value);
2862
+ const type = expected ?? (date ? new import_apache_arrow6.TimestampMicrosecond("UTC") : value instanceof Uint8Array ? new import_apache_arrow6.Binary() : value === null ? new import_apache_arrow6.Null() : typeof value === "boolean" ? bool : typeof value === "number" ? new import_apache_arrow6.Float64() : typeof value === "bigint" ? new import_apache_arrow6.Int64() : new import_apache_arrow6.Utf8());
2863
+ if (value === null && isTupleType(type))
2864
+ return {
2865
+ field: new import_apache_arrow6.Field("literal", type, true),
2866
+ hasKernel: false,
2867
+ prepare: (_batch, active) => vectorReader(nullTupleVector(type, active.length))
2868
+ };
2869
+ if (value === null && import_apache_arrow6.DataType.isList(type)) {
2870
+ const dictionary = (0, import_apache_arrow6.vectorFromArray)([null], type);
2871
+ return {
2872
+ field: new import_apache_arrow6.Field("literal", type, true),
2873
+ hasKernel: false,
2874
+ prepare: (_batch, active) => vectorReader((0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
2875
+ type: new import_apache_arrow6.Dictionary(type, new import_apache_arrow6.Int32()),
2876
+ length: active.length,
2877
+ data: new Int32Array(active.length),
2878
+ dictionary
2879
+ })))
2880
+ };
2881
+ }
2882
+ if (value === null && isNestedType(type))
2883
+ return {
2884
+ field: new import_apache_arrow6.Field("literal", type, true),
2885
+ hasKernel: false,
2886
+ prepare: (_batch, active) => vectorReader((0, import_apache_arrow6.vectorFromArray)(Array(active.length).fill(null), type))
2887
+ };
2888
+ validateValue(value, type);
2889
+ const rounded = roundToType(type)(value);
2890
+ if (typeof value === "number" && Number.isFinite(value) && !Number.isFinite(rounded))
2891
+ throw new Error(`Numeric literal overflow for ${type}`);
2892
+ const encoded = typeof value === "string" ? ast.utf8 instanceof Uint8Array && new TextDecoder("utf-8", { ignoreBOM: true }).decode(ast.utf8) === value ? new Uint8Array(ast.utf8) : encoder.encode(value) : void 0;
2893
+ const range = encoded ? [encoded, 0, encoded.length] : void 0;
2894
+ const constant = {
2895
+ get: () => rounded,
2896
+ isNull: () => value === null,
2897
+ bytes: range ? () => range : void 0
2898
+ };
2899
+ return {
2900
+ field: new import_apache_arrow6.Field("literal", type, value === null),
2901
+ hasKernel: false,
2902
+ constant,
2903
+ prepare: (_batch, active) => {
2904
+ if (!encoded)
2905
+ return constant;
2906
+ let vector;
2907
+ return {
2908
+ ...constant,
2909
+ // Comparisons only read bytes. Materialize physical UTF-8 only when a
2910
+ // projection or vector function actually requests a batch-sized vector.
2911
+ get vector() {
2912
+ if (!vector)
2913
+ vector = repeatUTF8(encoded, active);
2914
+ return vector;
2915
+ }
2916
+ };
2917
+ }
2918
+ };
2919
+ }
2920
+ if (ast.type === "CallExpression")
2921
+ return bindCall(ast, options, expected);
2922
+ if (ast.type === "ConditionalExpression") {
2923
+ const condition = bind(ast.test, options, bool);
2924
+ requireType(condition, "bool");
2925
+ let yes, no;
2926
+ if (isContextualLiteral(ast.consequent) && !isContextualLiteral(ast.alternate)) {
2927
+ no = bind(ast.alternate, options, expected);
2928
+ yes = bind(ast.consequent, options, no.field.type);
2929
+ } else {
2930
+ yes = bind(ast.consequent, options, expected);
2931
+ no = bind(ast.alternate, options, isContextualLiteral(ast.alternate) && !import_apache_arrow6.DataType.isNull(yes.field.type) ? yes.field.type : expected);
2932
+ if (isContextualLiteral(ast.consequent) && !import_apache_arrow6.DataType.isNull(no.field.type))
2933
+ yes = bind(ast.consequent, options, no.field.type);
2934
+ }
2935
+ const tuple = isTupleType(yes.field.type);
2936
+ const list = import_apache_arrow6.DataType.isList(yes.field.type);
2937
+ const nested = isNestedType(yes.field.type);
2938
+ if (nested) {
2939
+ if (physicalKey(yes.field.type) !== physicalKey(no.field.type))
2940
+ throw new Error("Arrow expression operand types must match");
2941
+ if (!isContextualLiteral(ast.consequent) && !isContextualLiteral(ast.alternate) && extensionKey(yes.field) !== extensionKey(no.field))
2942
+ throw new Error("Arrow expression branch extension metadata must match");
2943
+ } else
2944
+ sameType(yes, no);
2945
+ const metadata = isContextualLiteral(ast.consequent) ? no.field.metadata : yes.field.metadata;
2946
+ const field = new import_apache_arrow6.Field("conditional", yes.field.type, yes.field.nullable || no.field.nullable, metadata);
2947
+ return {
2948
+ field,
2949
+ hasKernel: nested || condition.hasKernel || yes.hasKernel || no.hasKernel,
2950
+ prepare(batch, active, context) {
2951
+ const test = condition.prepare(batch, active, context);
2952
+ const selected = nested || yes.hasKernel || no.hasKernel ? active.map((value, row) => value && test.get(row) === true ? 1 : 0) : void 0;
2953
+ const a = yes.prepare(batch, selected ?? active, context);
2954
+ const b = no.prepare(batch, selected ? active.map((value, row) => value && !selected[row] ? 1 : 0) : active, context);
2955
+ if (list && import_apache_arrow6.DataType.isDictionary(a.vector.type) && import_apache_arrow6.DataType.isDictionary(b.vector.type))
2956
+ return vectorReader(selectListVectors(field.type, a.vector, b.vector, selected, active));
2957
+ if (tuple) {
2958
+ const validActive = context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active;
2959
+ const yesMask = validActive.map((value, row) => value && selected[row] ? 1 : 0);
2960
+ const noMask = validActive.map((value, row) => value && !selected[row] ? 1 : 0);
2961
+ validateTupleVector(a.vector, yes.field, yesMask);
2962
+ validateTupleVector(b.vector, no.field, noMask);
2963
+ return vectorReader(selectTupleVectors(field.type, a.vector, b.vector, yesMask, validActive));
2964
+ }
2965
+ if (nested) {
2966
+ const values = Array.from(active, (valid, row) => {
2967
+ var _a2;
2968
+ return valid && !((_a2 = context.failed) == null ? void 0 : _a2[row]) ? nestedValue((selected[row] ? a : b).vector.get(row), field.type) : null;
2969
+ });
2970
+ return vectorReader((0, import_apache_arrow6.vectorFromArray)(values, field.type));
2971
+ }
2972
+ const choose = (row) => (selected ? selected[row] : test.get(row) === true) ? a : b;
2973
+ return {
2974
+ get: (row) => choose(row).get(row),
2975
+ isNull: (row) => {
2976
+ const branch = choose(row);
2977
+ return branch.isNull ? branch.isNull(row) : branch.get(row) === null;
2978
+ },
2979
+ bytes: typeKey(field.type) === "utf8" ? (row) => bytes(choose(row), row) : void 0
2980
+ };
2981
+ }
2982
+ };
2983
+ }
2984
+ if (ast.type === "UnaryExpression") {
2985
+ const argument = bind(ast.argument, options, ast.operator === "!" ? bool : void 0);
2986
+ if (ast.operator === "!")
2987
+ requireType(argument, "bool");
2988
+ else if (ast.operator === "+" || ast.operator === "-")
2989
+ requireNumeric(argument);
2990
+ else
2991
+ throw new Error(`Unsupported unary operator: ${ast.operator}`);
2992
+ const validate = createValueValidator(argument.field.type);
2993
+ const round = roundToType(argument.field.type);
2994
+ return {
2995
+ field: new import_apache_arrow6.Field("unary", argument.field.type, argument.field.nullable),
2996
+ hasKernel: argument.hasKernel,
2997
+ prepare(batch, active, context) {
2998
+ const child = argument.prepare(batch, active, context);
2999
+ return {
3000
+ get: (row) => {
3001
+ const value = child.get(row);
3002
+ if (value === null)
3003
+ return null;
3004
+ const result = ast.operator === "!" ? !value : ast.operator === "+" ? value : -value;
3005
+ validate(result);
3006
+ return round(result);
3007
+ }
3008
+ };
3009
+ }
3010
+ };
3011
+ }
3012
+ if (ast.type === "BinaryExpression" || ast.type === "LogicalExpression")
3013
+ return bindBinary(ast, options);
3014
+ throw new Error(`Unsupported Arrow expression syntax: ${ast.type}`);
3015
+ }
3016
+ function sameType(a, b) {
3017
+ if (typeKey(a.field.type) !== typeKey(b.field.type))
3018
+ throw new Error("Arrow expression operand types must match");
3019
+ }
3020
+ function requireType(node, key) {
3021
+ if (typeKey(node.field.type) !== key)
3022
+ throw new Error(`Arrow expression requires ${key}`);
3023
+ }
3024
+ function requireNumeric(node) {
3025
+ if (!import_apache_arrow6.DataType.isInt(node.field.type) && !import_apache_arrow6.DataType.isFloat(node.field.type))
3026
+ throw new Error("Arrow expression requires numeric operands");
3027
+ }
3028
+ function bindBinary(ast, options) {
3029
+ const op = ast.operator, logical = op === "&&" || op === "||";
3030
+ let left = bind(ast.left, options, logical ? bool : void 0), right = bind(ast.right, options, logical ? bool : void 0);
3031
+ if (!logical) {
3032
+ if (isContextualLiteral(ast.left) && !import_apache_arrow6.DataType.isNull(right.field.type))
3033
+ left = bind(ast.left, options, right.field.type);
3034
+ if (isContextualLiteral(ast.right))
3035
+ right = bind(ast.right, options, left.field.type);
3036
+ }
3037
+ sameType(left, right);
3038
+ const comparison = ["==", "===", "!=", "!==", "<", "<=", ">", ">="].includes(op);
3039
+ if (logical)
3040
+ requireType(left, "bool");
3041
+ else if (!comparison) {
3042
+ if (!["+", "-", "*", "/", "%"].includes(op))
3043
+ throw new Error(`Unsupported binary operator: ${op}`);
3044
+ requireNumeric(left);
3045
+ }
3046
+ const type = left.field.type, utf8 = typeKey(type) === "utf8", binary = import_apache_arrow6.DataType.isBinary(type) || import_apache_arrow6.DataType.isFixedSizeBinary(type), integer = import_apache_arrow6.DataType.isInt(type);
3047
+ const validate = createValueValidator(type);
3048
+ const round = roundToType(type);
3049
+ const field = new import_apache_arrow6.Field("expression", logical || comparison ? bool : type, left.field.nullable || right.field.nullable);
3050
+ return {
3051
+ field,
3052
+ hasKernel: left.hasKernel || right.hasKernel,
3053
+ prepare(batch, active, context) {
3054
+ const a = prepareInput(left, batch, active, context, left.hasKernel || right.hasKernel);
3055
+ let truth;
3056
+ let mask = active;
3057
+ if (logical && right.hasKernel) {
3058
+ truth = new Uint8Array(active.length);
3059
+ mask = active.map((value, row) => {
3060
+ if (!value)
3061
+ return 0;
3062
+ const x = a.get(row);
3063
+ truth[row] = x === null ? 2 : x ? 1 : 0;
3064
+ return (op === "&&" ? x !== false : x !== true) ? 1 : 0;
3065
+ });
3066
+ }
3067
+ const b = right.prepare(batch, mask, context);
3068
+ return {
3069
+ get: (row) => {
3070
+ if (logical) {
3071
+ const x2 = truth ? truth[row] === 2 ? null : truth[row] === 1 : a.get(row);
3072
+ if (op === "&&" && x2 === false)
3073
+ return false;
3074
+ if (op === "||" && x2 === true)
3075
+ return true;
3076
+ const y2 = b.get(row);
3077
+ return op === "&&" ? y2 === false ? false : x2 === null || y2 === null ? null : true : y2 === true ? true : x2 === null || y2 === null ? null : false;
3078
+ }
3079
+ if (utf8) {
3080
+ const x2 = bytes(a, row), y2 = bytes(b, row);
3081
+ if (!x2 || !y2)
3082
+ return null;
3083
+ return compare(op, (0, import_utf82.compareUTF8)(x2[0], x2[1], x2[2], y2[0], y2[1], y2[2]), 0);
3084
+ }
3085
+ const x = a.get(row), y = b.get(row);
3086
+ if (x === null || y === null)
3087
+ return null;
3088
+ if (binary)
3089
+ return compare(op, (0, import_utf82.compareUTF8)(x, 0, x.length, y, 0, y.length), 0);
3090
+ if (comparison)
3091
+ return compare(op, x, y);
3092
+ if ((op === "/" || op === "%") && (y === 0 || y === 0n))
3093
+ throw new ExpressionEvaluationError("divisionByZero", "Division by zero");
3094
+ const result = op === "+" ? x + y : op === "-" ? x - y : op === "*" ? x * y : op === "/" ? integer && typeof x === "number" ? Math.trunc(x / y) : x / y : x % y;
3095
+ validate(result);
3096
+ const rounded = round(result);
3097
+ if (!integer && Number.isFinite(x) && Number.isFinite(y) && !Number.isFinite(rounded))
3098
+ throw new ExpressionEvaluationError("overflow", "Floating expression overflow");
3099
+ return rounded;
3100
+ }
3101
+ };
3102
+ }
3103
+ };
3104
+ }
3105
+ function compare(op, a, b) {
3106
+ switch (op) {
3107
+ case "==":
3108
+ case "===":
3109
+ return a === b;
3110
+ case "!=":
3111
+ case "!==":
3112
+ return a !== b;
3113
+ case "<":
3114
+ return a < b;
3115
+ case "<=":
3116
+ return a <= b;
3117
+ case ">":
3118
+ return a > b;
3119
+ case ">=":
3120
+ return a >= b;
3121
+ default:
3122
+ throw new Error("Invalid comparison");
3123
+ }
3124
+ }
3125
+ function selectListVectors(type, left, right, selected, active) {
3126
+ if (!import_apache_arrow6.DataType.isDictionary(left.type) || !import_apache_arrow6.DataType.isDictionary(right.type))
3127
+ throw new Error("Computed List arguments require array literal branches");
3128
+ const a = left.data[0], b = right.data[0];
3129
+ const leftDictionary = a.dictionary, rightDictionary = b.dictionary;
3130
+ const indices = new Int32Array(active.length);
3131
+ const valid = new Uint8Array(active.length);
3132
+ for (let row = 0; row < active.length; row++) {
3133
+ if (!active[row])
3134
+ continue;
3135
+ if (selected[row]) {
3136
+ indices[row] = Number(a.values[row]);
3137
+ valid[row] = a.getValid(row) ? 1 : 0;
3138
+ } else {
3139
+ indices[row] = leftDictionary.length + Number(b.values[row]);
3140
+ valid[row] = b.getValid(row) ? 1 : 0;
3141
+ }
3142
+ }
3143
+ return maskTupleVector((0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
3144
+ type: new import_apache_arrow6.Dictionary(type, new import_apache_arrow6.Int32()),
3145
+ length: active.length,
3146
+ data: indices,
3147
+ dictionary: leftDictionary.concat(rightDictionary)
3148
+ })), valid);
3149
+ }
3150
+ function bindRecovery(name2, expressions, options, expected) {
3151
+ if (expected && import_apache_arrow6.DataType.isDictionary(expected))
3152
+ throw new Error("Dictionary function arguments require a dictionary-encoded source column");
3153
+ if (name2 === "TRY" ? expressions.length < 1 || expressions.length > 2 : !expressions.length)
3154
+ throw new Error(`${name2} has an invalid argument count`);
3155
+ const indices = expressions.map((_, index) => index);
3156
+ const ordered = [
3157
+ ...indices.filter((index) => !isContextualLiteral(expressions[index])),
3158
+ ...indices.filter((index) => isContextualLiteral(expressions[index]))
3159
+ ];
3160
+ let anchor = 0;
3161
+ let candidate;
3162
+ for (const index of ordered) {
3163
+ const expression = expressions[index];
3164
+ if (!expected && isContextualLiteral(expression) && containsContextualArray(expression))
3165
+ continue;
3166
+ const node = bind(expression, options, expected);
3167
+ if (!candidate || !import_apache_arrow6.DataType.isNull(node.field.type)) {
3168
+ anchor = index;
3169
+ candidate = node;
3170
+ }
3171
+ if (!import_apache_arrow6.DataType.isNull(node.field.type))
3172
+ break;
3173
+ }
3174
+ const first = candidate ?? bind(expressions[anchor], options, expected);
3175
+ const nodes = expressions.map((expression, index) => index === anchor ? first : bind(expression, options, first.field.type));
3176
+ for (const [index, node] of nodes.entries()) {
3177
+ if (physicalKey(node.field.type) !== physicalKey(first.field.type) || !isContextualLiteral(expressions[index]) && extensionKey(node.field) !== extensionKey(first.field))
3178
+ throw new Error(`${name2} arguments must have matching types and metadata`);
3179
+ }
3180
+ const field = new import_apache_arrow6.Field(name2.toLowerCase(), first.field.type, true, first.field.metadata);
3181
+ const tuple = isTupleType(field.type);
3182
+ const list = Boolean(expected && import_apache_arrow6.DataType.isList(expected) && import_apache_arrow6.DataType.isList(field.type));
3183
+ if (!tuple && !list)
3184
+ typeKey(field.type);
3185
+ return {
3186
+ field,
3187
+ hasKernel: true,
3188
+ prepare(batch, active, context) {
3189
+ const failed = new Uint8Array(active.length);
3190
+ const childContext = name2 === "TRY" ? { failed } : context;
3191
+ const child = nodes[0].prepare(batch, active, childContext);
3192
+ const nullable = (node) => new import_apache_arrow6.Field(node.field.name, node.field.type, true, node.field.metadata);
3193
+ const materialize = (node, prepared, mask) => tuple || list ? maskTupleVector(prepared.vector, mask) : output(nullable(node), mask, prepared.get);
3194
+ const original = materialize(nodes[0], child, active);
3195
+ const reader = tuple ? void 0 : new ColumnReader(original);
3196
+ let result = original;
3197
+ let pending = active.map((value, row) => value && (name2 === "TRY" ? failed[row] : tuple ? !original.isValid(row) : !reader.isValid(row)) ? 1 : 0);
3198
+ if ((tuple || list) && name2 === "TRY")
3199
+ result = maskTupleVector(result, active.map((value, row) => value && !failed[row] ? 1 : 0));
3200
+ for (let index = 1; index < nodes.length && pending.some(Boolean); index++) {
3201
+ const node = nodes[index];
3202
+ const fallback = node.prepare(batch, pending, context);
3203
+ const vector = materialize(node, fallback, pending);
3204
+ const keep = active.map((value, row) => value && !pending[row] ? 1 : 0);
3205
+ if (tuple)
3206
+ result = selectTupleVectors(field.type, result, vector, keep, active);
3207
+ else if (list)
3208
+ result = selectListVectors(field.type, result, vector, keep, active);
3209
+ else {
3210
+ const a = new ColumnReader(result), b = new ColumnReader(vector);
3211
+ result = output(field, active, (row) => pending[row] ? b.get(row) : a.get(row));
3212
+ }
3213
+ const next = tuple ? void 0 : new ColumnReader(vector);
3214
+ pending = pending.map((value, row) => value && (tuple ? !vector.isValid(row) : !next.isValid(row)) ? 1 : 0);
3215
+ }
3216
+ return vectorReader(result);
3217
+ }
3218
+ };
3219
+ }
3220
+ function bindGeometryCast(argument) {
3221
+ const source = valueType(argument.field.type);
3222
+ const metadata = new Map(argument.field.metadata);
3223
+ const extension = metadata.get("ARROW:extension:name");
3224
+ if (!import_apache_arrow6.DataType.isBinary(source) && !import_apache_arrow6.DataType.isNull(source) || extension && extension !== "geoarrow.wkb")
3225
+ throw new Error("GEOMETRY CAST requires Binary WKB input");
3226
+ metadata.set("ARROW:extension:name", "geoarrow.wkb");
3227
+ return {
3228
+ ...argument,
3229
+ field: new import_apache_arrow6.Field("cast", new import_apache_arrow6.Binary(), argument.field.nullable, metadata),
3230
+ prepare(batch, active, context) {
3231
+ const child = argument.prepare(batch, active, context);
3232
+ return { get: (row) => child.get(row) };
3233
+ }
3234
+ };
3235
+ }
3236
+ function bindMembership(arguments_, options) {
3237
+ if (arguments_.length < 2 || arguments_.length > 4097 || arguments_.slice(1).some((value) => value.type !== "Literal" && !isSignedLiteral(value)))
3238
+ throw new Error("IN requires a value and 1 to 4096 scalar literals");
3239
+ const argument = bind(arguments_[0], options);
3240
+ const utf8 = typeKey(argument.field.type) === "utf8";
3241
+ const binary = import_apache_arrow6.DataType.isBinary(argument.field.type) || import_apache_arrow6.DataType.isFixedSizeBinary(argument.field.type);
3242
+ const literals = arguments_.slice(1).map((value) => bind(value, options, argument.field.type));
3243
+ const prepared = literals.map((value) => value.constant);
3244
+ const values = new Set(prepared.map((value) => value.get(0)));
3245
+ const ranges = utf8 ? prepared.map((value) => bytes(value, 0)).filter((range) => range !== null) : binary ? prepared.flatMap((value) => {
3246
+ const data = value.get(0);
3247
+ return data === null ? [] : [[data, 0, data.length]];
3248
+ }) : [];
3249
+ ranges.sort((a, b) => (0, import_utf82.compareUTF8)(a[0], a[1], a[2], b[0], b[1], b[2]));
3250
+ return {
3251
+ field: new import_apache_arrow6.Field("in", bool, argument.field.nullable || literals.some((value) => value.field.nullable)),
3252
+ hasKernel: argument.hasKernel,
3253
+ prepare(batch, active, context) {
3254
+ const source = argument.prepare(batch, active, context);
3255
+ return {
3256
+ get(row) {
3257
+ if (utf8 || binary) {
3258
+ const data = binary ? source.get(row) : null;
3259
+ const value = utf8 ? bytes(source, row) : data === null ? null : [data, 0, data.length];
3260
+ if (!value)
3261
+ return null;
3262
+ if (containsByteRange(ranges, value))
3263
+ return true;
3264
+ } else {
3265
+ const value = source.get(row);
3266
+ if (value === null)
3267
+ return null;
3268
+ if (!Number.isNaN(value) && values.has(value))
3269
+ return true;
3270
+ }
3271
+ return values.has(null) ? null : false;
3272
+ }
3273
+ };
3274
+ }
3275
+ };
3276
+ }
3277
+ function repeatUTF8(encoded, active) {
3278
+ const count = active.reduce((sum, value) => sum + value, 0);
3279
+ const data = new Uint8Array(count * encoded.length);
3280
+ const offsets = new Int32Array(active.length + 1);
3281
+ const validity = new Uint8Array(Math.ceil(active.length / 8));
3282
+ let position = 0;
3283
+ for (let row = 0; row < active.length; row++) {
3284
+ if (active[row]) {
3285
+ data.set(encoded, position);
3286
+ position += encoded.length;
3287
+ validity[row >> 3] |= 1 << (row & 7);
3288
+ }
3289
+ offsets[row + 1] = position;
3290
+ }
3291
+ return (0, import_apache_arrow6.makeVector)((0, import_apache_arrow6.makeData)({
3292
+ type: new import_apache_arrow6.Utf8(),
3293
+ length: active.length,
3294
+ data,
3295
+ valueOffsets: offsets,
3296
+ nullBitmap: validity,
3297
+ nullCount: active.length - count
3298
+ }));
3299
+ }
3300
+ function containsByteRange(ranges, value) {
3301
+ let low = 0;
3302
+ let high = ranges.length - 1;
3303
+ while (low <= high) {
3304
+ const middle = low + high >>> 1;
3305
+ const candidate = ranges[middle];
3306
+ const order = (0, import_utf82.compareUTF8)(value[0], value[1], value[2], candidate[0], candidate[1], candidate[2]);
3307
+ if (order === 0)
3308
+ return true;
3309
+ if (order < 0)
3310
+ high = middle - 1;
3311
+ else
3312
+ low = middle + 1;
3313
+ }
3314
+ return false;
3315
+ }
3316
+ function bindLike(args, options, insensitive) {
3317
+ if (args.length < 2 || args.length > 3)
3318
+ throw new Error("LIKE requires two or three arguments");
3319
+ const value = bind(args[0], options, new import_apache_arrow6.Utf8());
3320
+ const pattern = bind(args[1], options, new import_apache_arrow6.Utf8());
3321
+ requireType(value, "utf8");
3322
+ requireType(pattern, "utf8");
3323
+ const escape = args[2];
3324
+ if (escape && (escape.type !== "Literal" || typeof escape.value !== "string")) {
3325
+ throw new Error("LIKE ESCAPE requires a literal character");
3326
+ }
3327
+ const compile2 = (text) => compileUTF8Like(text, insensitive, escape == null ? void 0 : escape.value);
3328
+ const literal3 = args[1].type === "Literal" && typeof args[1].value === "string";
3329
+ const matcher = literal3 ? compile2(args[1].value) : void 0;
3330
+ return {
3331
+ field: new import_apache_arrow6.Field("like", bool, value.field.nullable || pattern.field.nullable),
3332
+ hasKernel: value.hasKernel || pattern.hasKernel,
3333
+ prepare(batch, active, context) {
3334
+ const source = value.prepare(batch, active, context);
3335
+ const patterns = pattern.prepare(batch, active, context);
3336
+ return {
3337
+ get(row) {
3338
+ const range = bytes(source, row);
3339
+ const text = patterns.get(row);
3340
+ if (!range || text === null)
3341
+ return null;
3342
+ return (matcher || compile2(text))(...range);
3343
+ }
3344
+ };
3345
+ }
3346
+ };
3347
+ }
3348
+ function convertByte(value, normalized) {
3349
+ if (!Number.isFinite(value))
3350
+ throw new ExpressionEvaluationError("conversion", "Byte conversion requires a finite number");
3351
+ if (normalized) {
3352
+ if (value < 0 || value > 1)
3353
+ throw new ExpressionEvaluationError("domain", "PACK_UNORM8 requires values in [0, 1]");
3354
+ return Math.round(value * 255);
3355
+ }
3356
+ const integer = Math.trunc(value);
3357
+ if (integer < 0 || integer > 255)
3358
+ throw new ExpressionEvaluationError("overflow", "UINT8 cast overflow");
3359
+ return integer;
3360
+ }
3361
+ function bindPackUnorm8(arguments_, options) {
3362
+ if (arguments_.length !== 1)
3363
+ throw new Error("PACK_UNORM8 requires one fixed-size numeric array");
3364
+ const argument = bind(arguments_[0], options, void 0, true);
3365
+ const inputType = argument.field.type;
3366
+ if (!import_apache_arrow6.DataType.isFixedSizeList(inputType) || !inputType.listSize || inputType.listSize > 4096)
3367
+ throw new Error("PACK_UNORM8 requires a fixed-size numeric array of 1 to 4096 components");
3368
+ const width = inputType.listSize;
3369
+ const child = inputType.children[0];
3370
+ if (!import_apache_arrow6.DataType.isInt(valueType(child.type)) && !import_apache_arrow6.DataType.isFloat(valueType(child.type)))
3371
+ throw new Error("PACK_UNORM8 requires numeric components");
3372
+ const type = new import_apache_arrow6.FixedSizeList(width, new import_apache_arrow6.Field("value", new import_apache_arrow6.Uint8(), false));
3373
+ const field = new import_apache_arrow6.Field("pack_unorm8", type, argument.field.nullable || child.nullable);
3374
+ return {
3375
+ field,
3376
+ hasKernel: true,
3377
+ prepare(batch, active, context) {
3378
+ var _a2;
3379
+ const input = argument.prepare(batch, active, context).vector;
3380
+ const parent = new ColumnReader(input);
3381
+ const components = new ColumnReader(input.getChildAt(0));
3382
+ const result = nullTupleVector(type, active.length);
3383
+ const data = result.data[0];
3384
+ const bytes2 = data.children[0].values;
3385
+ const rowValues = new Float64Array(width);
3386
+ for (let row = 0; row < active.length; row++) {
3387
+ if (!active[row] || ((_a2 = context.failed) == null ? void 0 : _a2[row]) || !parent.isValid(row))
3388
+ continue;
3389
+ const start = row * width;
3390
+ let valid = true;
3391
+ for (let component = 0; component < width; component++) {
3392
+ const value = components.get(start + component);
3393
+ if (value === null) {
3394
+ valid = false;
3395
+ break;
3396
+ }
3397
+ rowValues[component] = Number(value);
3398
+ }
3399
+ if (!valid)
3400
+ continue;
3401
+ try {
3402
+ for (let component = 0; component < width; component++)
3403
+ bytes2[start + component] = convertByte(rowValues[component], true);
3404
+ } catch (error) {
3405
+ if (!context.failed || !isExpressionEvaluationError(error))
3406
+ throw error;
3407
+ context.failed[row] = 1;
3408
+ bytes2.fill(0, start, start + width);
3409
+ continue;
3410
+ }
3411
+ data.setValid(row, true);
3412
+ }
3413
+ return vectorReader(result);
3414
+ }
3415
+ };
3416
+ }
3417
+ function isNestedType(type) {
3418
+ return import_apache_arrow6.DataType.isStruct(type) || import_apache_arrow6.DataType.isList(type) || import_apache_arrow6.DataType.isFixedSizeList(type);
3419
+ }
3420
+ function nestedValue(value, type) {
3421
+ if (value == null)
3422
+ return null;
3423
+ if (import_apache_arrow6.DataType.isList(type) || import_apache_arrow6.DataType.isFixedSizeList(type))
3424
+ return Array.from(value, (item) => nestedValue(item, type.valueType));
3425
+ if (import_apache_arrow6.DataType.isStruct(type))
3426
+ return Object.fromEntries(type.children.map((field) => [field.name, nestedValue(value[field.name], field.type)]));
3427
+ return value;
3428
+ }
3429
+ function nestedVector(vector, type, active) {
3430
+ if (!active.includes(0))
3431
+ return vector;
3432
+ return (0, import_apache_arrow6.vectorFromArray)(Array.from(active, (valid, row) => valid ? nestedValue(vector.get(row), type) : null), type);
3433
+ }
3434
+ function bindNestedExtract(name2, args, options) {
3435
+ if (args.length !== 2)
3436
+ throw new Error(`${name2} requires two arguments`);
3437
+ const input = bind(args[0], options, void 0, true);
3438
+ const inputType = input.field.type;
3439
+ const selector = args[1];
3440
+ let child;
3441
+ let index;
3442
+ if (name2 === "LIST_EXTRACT") {
3443
+ if (!import_apache_arrow6.DataType.isList(inputType) && !import_apache_arrow6.DataType.isFixedSizeList(inputType))
3444
+ throw new Error("LIST_EXTRACT requires an Arrow List or FixedSizeList");
3445
+ child = inputType.children[0];
3446
+ index = bind(selector, options);
3447
+ if (!import_apache_arrow6.DataType.isInt(index.field.type) && !import_apache_arrow6.DataType.isFloat(index.field.type) && !import_apache_arrow6.DataType.isNull(index.field.type))
3448
+ throw new Error("LIST_EXTRACT requires an integer index");
3449
+ } else {
3450
+ if (!import_apache_arrow6.DataType.isStruct(inputType))
3451
+ throw new Error("STRUCT_EXTRACT requires an Arrow Struct");
3452
+ if (selector.type !== "Literal" || typeof selector.value !== "string")
3453
+ throw new Error("STRUCT_EXTRACT requires a literal field name");
3454
+ const field2 = inputType.children.find((candidate) => candidate.name === selector.value);
3455
+ if (!field2)
3456
+ throw new Error(`Unknown Arrow struct field: ${selector.value}`);
3457
+ child = field2;
3458
+ }
3459
+ const field = new import_apache_arrow6.Field(child.name, valueType(child.type), true, child.metadata);
3460
+ return {
3461
+ field,
3462
+ hasKernel: true,
3463
+ prepare(batch, active, context) {
3464
+ const inputValues = input.prepare(batch, active, context).vector;
3465
+ const positions = index == null ? void 0 : index.prepare(batch, active, context);
3466
+ const childValues = index ? void 0 : inputValues.getChild(child.name);
3467
+ const values = Array.from(active, (valid, row) => {
3468
+ if (!valid || !inputValues.isValid(row))
3469
+ return null;
3470
+ if (!positions)
3471
+ return nestedValue(childValues.get(row), field.type);
3472
+ const position = positions.get(row);
3473
+ if (position === null)
3474
+ return null;
3475
+ const offset = Number(position);
3476
+ if (!Number.isSafeInteger(offset))
3477
+ return null;
3478
+ const list = inputValues.get(row);
3479
+ const element = offset < 0 ? list.length + offset : offset - 1;
3480
+ return element >= 0 && element < list.length ? nestedValue(list.get(element), field.type) : null;
3481
+ });
3482
+ return vectorReader((0, import_apache_arrow6.vectorFromArray)(values, field.type));
3483
+ }
3484
+ };
3485
+ }
3486
+ function bindCall(ast, options, expected) {
3487
+ var _a2, _b;
3488
+ if (ast.callee.type !== "Identifier")
3489
+ throw new Error("Only registered named function calls are supported");
3490
+ let name2 = ast.callee.name;
3491
+ const recovery = recoveryName(ast);
3492
+ if (recovery)
3493
+ return bindRecovery(recovery, ast.arguments, options, expected);
3494
+ if (ast.sqlFunction) {
3495
+ const matches = [
3496
+ ...options.functionLookup.keys(),
3497
+ "array",
3498
+ "array_float16",
3499
+ "array_float32",
3500
+ "array_float64",
3501
+ "array_uint8",
3502
+ "PACK_UNORM8",
3503
+ "EPOCH_MS",
3504
+ "LIST_EXTRACT",
3505
+ "STRUCT_EXTRACT",
3506
+ "cast",
3507
+ "like",
3508
+ "ilike",
3509
+ "concat",
3510
+ "concatStrict",
3511
+ "upper",
3512
+ "lower",
3513
+ "capitalize",
3514
+ "isNull",
3515
+ "isNotNull",
3516
+ "parseNumber",
3517
+ "parseBigInt",
3518
+ "parseBoolean",
3519
+ "toFloat64",
3520
+ "toInt32",
3521
+ "toInt64"
3522
+ ].filter((key) => key.toUpperCase() === name2.toUpperCase());
3523
+ if (matches.length > 1)
3524
+ throw new Error(`Ambiguous SQL function: ${name2}`);
3525
+ name2 = matches[0] ?? name2;
3526
+ }
3527
+ if (name2 === "LIST_EXTRACT" || name2 === "STRUCT_EXTRACT")
3528
+ return bindNestedExtract(name2, ast.arguments, options);
3529
+ if (name2 === "PACK_UNORM8")
3530
+ return bindPackUnorm8(ast.arguments, options);
3531
+ if (["array", "array_float16", "array_float32", "array_float64", "array_uint8"].includes(name2)) {
3532
+ if (!ast.arguments.length || ast.arguments.length > 4096)
3533
+ throw new Error("ARRAY requires 1 to 4096 numeric expressions");
3534
+ const arguments_2 = ast.arguments.map((value) => bind(value, options));
3535
+ if (arguments_2.some((value) => !import_apache_arrow6.DataType.isFloat(valueType(value.field.type)) && !import_apache_arrow6.DataType.isInt(valueType(value.field.type))))
3536
+ throw new Error("ARRAY requires numeric expressions");
3537
+ const component = name2 === "array_uint8" ? new import_apache_arrow6.Uint8() : name2 === "array_float16" ? new import_apache_arrow6.Float16() : name2 === "array_float32" ? new import_apache_arrow6.Float32() : name2 === "array_float64" || arguments_2.every((value) => import_apache_arrow6.DataType.isInt(valueType(value.field.type))) || arguments_2.some((value) => typeKey(value.field.type) === "f2") ? new import_apache_arrow6.Float64() : arguments_2.some((value) => typeKey(value.field.type) === "f1") ? new import_apache_arrow6.Float32() : new import_apache_arrow6.Float16();
3538
+ const type = new import_apache_arrow6.FixedSizeList(arguments_2.length, new import_apache_arrow6.Field("value", component, false));
3539
+ const field2 = new import_apache_arrow6.Field("array", type, arguments_2.some((value) => value.field.nullable));
3540
+ return {
3541
+ field: field2,
3542
+ hasKernel: true,
3543
+ prepare(batch, active, context) {
3544
+ const values = arguments_2.map((value) => prepareInput(value, batch, active, context, arguments_2.some((argument) => argument.hasKernel)));
3545
+ const vector = nullTupleVector(type, active.length), data = vector.data[0], output2 = import_apache_arrow6.DataType.isFloat(component) && component.precision === import_apache_arrow6.Precision.HALF ? float16Values(data.children[0].values) : data.children[0].values;
3546
+ const rowValues = new Array(values.length);
3547
+ for (let row = 0; row < active.length; row++) {
3548
+ if (!active[row])
3549
+ continue;
3550
+ for (let column = 0; column < values.length; column++)
3551
+ rowValues[column] = values[column].get(row);
3552
+ if (rowValues.some((value) => value === null))
3553
+ continue;
3554
+ try {
3555
+ for (let column = 0; column < values.length; column++) {
3556
+ const index = row * values.length + column;
3557
+ const value = Number(rowValues[column]);
3558
+ output2[index] = name2 === "array_uint8" ? convertByte(value, false) : value;
3559
+ if (Number.isFinite(value) && !Number.isFinite(output2[index]))
3560
+ throw new ExpressionEvaluationError("overflow", "ARRAY cast overflow");
3561
+ }
3562
+ } catch (error) {
3563
+ if (!context.failed || !isExpressionEvaluationError(error))
3564
+ throw error;
3565
+ context.failed[row] = 1;
3566
+ output2.fill(0, row * values.length, (row + 1) * values.length);
3567
+ continue;
3568
+ }
3569
+ data.setValid(row, true);
3570
+ }
3571
+ return vectorReader(vector);
3572
+ }
3573
+ };
3574
+ }
3575
+ if (name2 === "like" || name2 === "ilike")
3576
+ return bindLike(ast.arguments, options, name2 === "ilike");
3577
+ if (name2 === "upper" || name2 === "lower" || name2 === "capitalize") {
3578
+ if (ast.arguments.length !== 1)
3579
+ throw new Error("Case conversion requires one argument");
3580
+ const arg = bind(ast.arguments[0], options, new import_apache_arrow6.Utf8());
3581
+ requireType(arg, "utf8");
3582
+ return {
3583
+ field: new import_apache_arrow6.Field(name2, new import_apache_arrow6.Utf8(), arg.field.nullable),
3584
+ hasKernel: arg.hasKernel,
3585
+ prepare(batch, active, context) {
3586
+ const input = arg.prepare(batch, active, context);
3587
+ return {
3588
+ get(row) {
3589
+ const value = input.get(row);
3590
+ if (!value)
3591
+ return value;
3592
+ if (name2 === "upper")
3593
+ return value.toUpperCase();
3594
+ if (name2 === "lower")
3595
+ return value.toLowerCase();
3596
+ const first = String.fromCodePoint(value.codePointAt(0));
3597
+ return first.toUpperCase() + value.slice(first.length);
3598
+ }
3599
+ };
3600
+ }
3601
+ };
3602
+ }
3603
+ if (name2 === "concat" || name2 === "concatStrict") {
3604
+ if (!ast.arguments.length)
3605
+ throw new Error("CONCAT requires at least one argument");
3606
+ const args = ast.arguments.map((arg) => bind(arg, options, new import_apache_arrow6.Utf8()));
3607
+ for (const arg of args)
3608
+ requireType(arg, "utf8");
3609
+ const strict = name2 === "concatStrict";
3610
+ return {
3611
+ field: new import_apache_arrow6.Field("concat", new import_apache_arrow6.Utf8(), strict && args.some((arg) => arg.field.nullable)),
3612
+ hasKernel: args.some((arg) => arg.hasKernel),
3613
+ prepare(batch, active, context) {
3614
+ const inputs = args.map((arg) => arg.prepare(batch, active, context));
3615
+ return {
3616
+ get(row) {
3617
+ let result2 = "";
3618
+ for (const input of inputs) {
3619
+ const value = input.get(row);
3620
+ if (value === null && strict)
3621
+ return null;
3622
+ if (value !== null)
3623
+ result2 += value;
3624
+ }
3625
+ return result2;
3626
+ }
3627
+ };
3628
+ }
3629
+ };
3630
+ }
3631
+ if (name2 === "in")
3632
+ return bindMembership(ast.arguments, options);
3633
+ if (name2 === "EPOCH_MS") {
3634
+ if (ast.arguments.length !== 1)
3635
+ throw new Error("EPOCH_MS requires one argument");
3636
+ const argument = bind(ast.arguments[0], options);
3637
+ const key = typeKey(argument.field.type);
3638
+ if (key !== "date32" && !key.startsWith("timestamp_us:")) {
3639
+ throw new Error("EPOCH_MS requires DATE or microsecond TIMESTAMP");
3640
+ }
3641
+ return {
3642
+ field: new import_apache_arrow6.Field("epoch_ms", new import_apache_arrow6.Float64(), argument.field.nullable),
3643
+ hasKernel: argument.hasKernel,
3644
+ prepare(batch, active, context) {
3645
+ const input = argument.prepare(batch, active, context);
3646
+ return {
3647
+ get(row) {
3648
+ const value = input.get(row);
3649
+ if (value === null)
3650
+ return null;
3651
+ return key === "date32" ? Number(value) * 864e5 : Number(value / 1000n) + Number(value % 1000n) / 1e3;
3652
+ }
3653
+ };
3654
+ }
3655
+ };
3656
+ }
3657
+ if (name2 === "cast") {
3658
+ if (ast.arguments.length !== 2 || ((_a2 = ast.arguments[1]) == null ? void 0 : _a2.type) !== "Literal")
3659
+ throw new Error("cast requires a value and a literal scalar target");
3660
+ const argument = bind(ast.arguments[0], options);
3661
+ const target = ast.arguments[1].value;
3662
+ if (target === "geometry")
3663
+ return bindGeometryCast(argument);
3664
+ const cast = bindCast(argument.field.type, target);
3665
+ return {
3666
+ field: new import_apache_arrow6.Field("cast", cast.type, argument.field.nullable),
3667
+ hasKernel: argument.hasKernel,
3668
+ prepare(batch, active, context) {
3669
+ const child = argument.prepare(batch, active, context);
3670
+ return {
3671
+ get: (row) => cast.textInput && !import_apache_arrow6.DataType.isUtf8(valueType(cast.type)) ? cast.evaluate(null, bytes(child, row)) : cast.evaluate(child.get(row))
3672
+ };
3673
+ }
3674
+ };
3675
+ }
3676
+ const conversion = ["toFloat64", "toInt32", "toInt64"].includes(name2);
3677
+ const nullTest = name2 === "isNull" || name2 === "isNotNull";
3678
+ if (conversion || nullTest || ["parseNumber", "parseBigInt", "parseBoolean"].includes(name2)) {
3679
+ if (ast.arguments.length !== 1)
3680
+ throw new Error(`${name2} requires one argument`);
3681
+ const argument = bind(ast.arguments[0], options);
3682
+ if (conversion)
3683
+ requireNumeric(argument);
3684
+ else if (!nullTest)
3685
+ requireType(argument, "utf8");
3686
+ const type = name2 === "parseNumber" || name2 === "toFloat64" ? new import_apache_arrow6.Float64() : name2 === "toInt32" ? new import_apache_arrow6.Int32() : name2 === "parseBigInt" || name2 === "toInt64" ? new import_apache_arrow6.Int64() : bool;
3687
+ const validate2 = createValueValidator(type);
3688
+ const round2 = roundToType(type);
3689
+ const field2 = new import_apache_arrow6.Field(name2, type, conversion ? argument.field.nullable : !nullTest);
3690
+ return {
3691
+ field: field2,
3692
+ hasKernel: argument.hasKernel,
3693
+ prepare(batch, active, context) {
3694
+ const child = argument.prepare(batch, active, context);
3695
+ return {
3696
+ get: (row) => {
3697
+ if (nullTest) {
3698
+ const missing = child.isNull ? child.isNull(row) : child.get(row) === null;
3699
+ return name2 === "isNull" ? missing : !missing;
3700
+ }
3701
+ if (conversion) {
3702
+ const value = child.get(row);
3703
+ if (value === null)
3704
+ return null;
3705
+ if (name2 === "toInt64" && typeof value === "number" && !Number.isInteger(value))
3706
+ throw new ExpressionEvaluationError("conversion", "Int64 conversion requires a finite integer");
3707
+ const result3 = name2 === "toInt64" ? BigInt(value) : Number(value);
3708
+ validate2(result3);
3709
+ return result3;
3710
+ }
3711
+ const range = bytes(child, row);
3712
+ if (!range)
3713
+ return null;
3714
+ const [buffer, start, end] = range;
3715
+ const result2 = (name2 === "parseNumber" ? (0, import_utf82.parseUTF8Number)(buffer, start, end) : name2 === "parseBigInt" ? (0, import_utf82.parseUTF8BigInt)(buffer, start, end) : (0, import_utf82.parseUTF8Boolean)(buffer, start, end)) ?? null;
3716
+ validate2(result2);
3717
+ return round2(result2);
3718
+ }
3719
+ };
3720
+ }
3721
+ };
3722
+ }
3723
+ const group = options.functionLookup.get(name2);
3724
+ if (!group)
3725
+ throw new Error(`Unknown Arrow function: ${name2}`);
3726
+ const inferred = ast.arguments.map((argument) => needsArgumentContext(argument) ? void 0 : bind(argument, options, void 0, true));
3727
+ const candidates = [];
3728
+ const allLiteralArguments = ast.arguments.every(isContextualLiteral);
3729
+ let contextualError;
3730
+ for (const prototype of group.prototypes) {
3731
+ if (ast.arguments.length !== prototype.arguments.length)
3732
+ continue;
3733
+ let cost = 0;
3734
+ const arguments_2 = [];
3735
+ let compatible = true;
3736
+ for (let index = 0; index < ast.arguments.length; index++) {
3737
+ const argument = ast.arguments[index], field2 = prototype.arguments[index], source = inferred[index];
3738
+ let bound;
3739
+ if (isContextualLiteral(argument) || !source) {
3740
+ try {
3741
+ bound = bind(argument, options, field2.type, !prototype.evaluateScalar);
3742
+ } catch (error) {
3743
+ contextualError = error;
3744
+ compatible = false;
3745
+ break;
3746
+ }
3747
+ if (source && allLiteralArguments && physicalKey(valueType(source.field.type)) !== physicalKey(field2.type))
3748
+ cost += 4;
3749
+ } else {
3750
+ bound = source;
3751
+ }
3752
+ const sourceType = valueType(bound.field.type);
3753
+ if (import_apache_arrow6.DataType.isDictionary(bound.field.type) && !import_apache_arrow6.DataType.isDictionary(field2.type))
3754
+ cost += 1;
3755
+ if (physicalKey(import_apache_arrow6.DataType.isDictionary(field2.type) ? bound.field.type : sourceType) !== physicalKey(field2.type)) {
3756
+ if (import_apache_arrow6.DataType.isFloat(sourceType) && import_apache_arrow6.DataType.isFloat(field2.type) && sourceType.precision < field2.type.precision) {
3757
+ cost += field2.type.precision - sourceType.precision;
3758
+ const child = bound, round2 = roundToType(field2.type);
3759
+ bound = {
3760
+ field: new import_apache_arrow6.Field(child.field.name, field2.type, child.field.nullable, child.field.metadata),
3761
+ hasKernel: child.hasKernel,
3762
+ prepare(batch, active, context) {
3763
+ const prepared = child.prepare(batch, active, context);
3764
+ return { get: (row) => round2(prepared.get(row)) };
3765
+ }
3766
+ };
3767
+ } else {
3768
+ compatible = false;
3769
+ break;
3770
+ }
3771
+ }
3772
+ if ([...field2.metadata].some(([key, value]) => bound.field.metadata.get(key) !== value) || prototype.nullHandling === "call" && bound.field.nullable && !field2.nullable) {
3773
+ compatible = false;
3774
+ break;
3775
+ }
3776
+ arguments_2.push(bound);
3777
+ }
3778
+ if (compatible)
3779
+ candidates.push({ prototype, arguments: arguments_2, cost });
3780
+ }
3781
+ const untypedNumeric = (value) => value.type === "Literal" && (value.value === null || typeof value.value === "number") || isSignedLiteral(value) || Boolean(recoveryName(value) && value.arguments.every(untypedNumeric)) || value.type === "ConditionalExpression" && untypedNumeric(value.consequent) && untypedNumeric(value.alternate);
3782
+ const allFloat64 = (prototype) => prototype.arguments.every((field2) => import_apache_arrow6.DataType.isFloat(field2.type) && field2.type.precision === import_apache_arrow6.Precision.DOUBLE);
3783
+ if (ast.arguments.every(untypedNumeric) && candidates.every((candidate) => candidate.prototype.arguments.every((field2) => import_apache_arrow6.DataType.isInt(field2.type) || import_apache_arrow6.DataType.isFloat(field2.type))) && candidates.some((candidate) => allFloat64(candidate.prototype))) {
3784
+ for (const candidate of candidates)
3785
+ if (!allFloat64(candidate.prototype))
3786
+ candidate.cost++;
3787
+ }
3788
+ candidates.sort((a, b) => a.cost - b.cost);
3789
+ if (!candidates.length && group.prototypes.length === 1 && contextualError)
3790
+ throw contextualError;
3791
+ if (!candidates.length)
3792
+ throw new Error(`Argument type mismatch or wrong argument count for ${name2}`);
3793
+ if (((_b = candidates[1]) == null ? void 0 : _b.cost) === candidates[0].cost)
3794
+ throw new Error(`Ambiguous Arrow function call: ${name2}`);
3795
+ const declaration = candidates[0].prototype;
3796
+ const arguments_ = candidates[0].arguments;
3797
+ const evaluateVector = declaration.evaluateScalar ? void 0 : declaration.evaluate;
3798
+ const result = declaration.result;
3799
+ const tuple = isTupleType(result.type);
3800
+ if (tuple && declaration.evaluateScalar)
3801
+ throw new Error("Fixed-size list results require a vector-only function");
3802
+ if (declaration.evaluateScalar)
3803
+ for (const argument of arguments_)
3804
+ typeKey(argument.field.type);
3805
+ if (!tuple)
3806
+ typeKey(result.type);
3807
+ const validate = tuple ? void 0 : createValueValidator(result.type);
3808
+ const round = tuple ? void 0 : roundToType(result.type);
3809
+ const field = new import_apache_arrow6.Field(result.name, valueType(result.type), result.nullable || declaration.nullHandling !== "call" && arguments_.some((argument) => argument.field.nullable), result.metadata);
3810
+ return {
3811
+ field,
3812
+ hasKernel: Boolean(evaluateVector) || arguments_.some((argument) => argument.hasKernel),
3813
+ prepare(batch, active, context) {
3814
+ const children = arguments_.map((argument) => prepareInput(argument, batch, active, context, Boolean(evaluateVector) || arguments_.some((value) => value.hasKernel)));
3815
+ let kernel;
3816
+ let mask = active;
3817
+ if (evaluateVector && active.some(Boolean)) {
3818
+ const vectors = children.map((child, index) => child.vector ?? output(arguments_[index].field, context.failed ? active.map((value, row) => value && !context.failed[row] ? 1 : 0) : active, child.get));
3819
+ const readers = vectors.map((vector) => new ColumnReader(vector));
3820
+ mask = active.map((value, row) => {
3821
+ var _a3;
3822
+ return value && !((_a3 = context.failed) == null ? void 0 : _a3[row]) && (declaration.nullHandling === "call" || readers.every((reader) => reader.isValid(row))) ? 1 : 0;
3823
+ });
3824
+ if (mask.some(Boolean)) {
3825
+ const vector = evaluateVector(vectors, {
3826
+ activeRows: mask,
3827
+ length: active.length,
3828
+ reportError(row, error) {
3829
+ if (!Number.isInteger(row) || row < 0 || row >= active.length || !mask[row])
3830
+ throw new Error("Kernel reported an inactive or invalid row");
3831
+ if (!context.failed || !isExpressionEvaluationError(error))
3832
+ throw error;
3833
+ context.failed[row] = 1;
3834
+ mask[row] = 0;
3835
+ }
3836
+ });
3837
+ if (vector.length !== active.length || (tuple ? physicalKey(vector.type) !== physicalKey(result.type) : typeKey(vector.type) !== typeKey(result.type)))
3838
+ throw new Error(`Invalid kernel result for ${name2}`);
3839
+ if (tuple) {
3840
+ validateTupleVector(vector, result, mask);
3841
+ return vectorReader(maskTupleVector(vector, mask));
3842
+ }
3843
+ kernel = sourceReader(vector);
3844
+ }
3845
+ }
3846
+ if (isTupleType(result.type))
3847
+ return vectorReader(nullTupleVector(result.type, active.length));
3848
+ return {
3849
+ get: (row) => {
3850
+ var _a3;
3851
+ let value;
3852
+ if (evaluateVector) {
3853
+ if (!mask[row] || !kernel)
3854
+ return null;
3855
+ value = kernel.get(row);
3856
+ } else {
3857
+ const args = children.map((child) => child.get(row));
3858
+ if ((_a3 = context.failed) == null ? void 0 : _a3[row])
3859
+ return null;
3860
+ if (declaration.nullHandling !== "call" && args.some((value2) => value2 === null))
3861
+ return null;
3862
+ value = declaration.evaluateScalar(...args);
3863
+ }
3864
+ if (value === null && !result.nullable)
3865
+ throw new Error(`Function ${name2} returned null for a non-nullable result`);
3866
+ validate(value);
3867
+ return round(value);
3868
+ }
3869
+ };
3870
+ }
3871
+ };
3872
+ }
3873
+ function roundToType(type) {
3874
+ if (typeKey(type) === "f0") {
3875
+ const round = roundFloat16();
3876
+ return (value) => value === null ? null : round(value);
3877
+ }
3878
+ return typeKey(type) === "f1" ? (value) => value === null ? null : Math.fround(value) : (value) => value;
3879
+ }
3880
+ function isSignedLiteral(ast) {
3881
+ return ast.type === "UnaryExpression" && (ast.operator === "+" || ast.operator === "-") && ast.argument.type === "Literal" && typeof ast.argument.value === "number";
3882
+ }
3883
+ function recoveryName(ast) {
3884
+ if (ast.type !== "CallExpression" || ast.callee.type !== "Identifier")
3885
+ return void 0;
3886
+ const name2 = ast.sqlFunction ? ast.callee.name.toUpperCase() : ast.callee.name;
3887
+ return name2 === "TRY" || name2 === "COALESCE" ? name2 : void 0;
3888
+ }
3889
+ function containsContextualArray(ast) {
3890
+ return ast.type === "ArrayExpression" || ast.type === "ConditionalExpression" && (containsContextualArray(ast.consequent) || containsContextualArray(ast.alternate)) || Boolean(recoveryName(ast) && ast.arguments.some(containsContextualArray));
3891
+ }
3892
+ function isContextualLiteral(ast) {
3893
+ return ast.type === "Literal" || ast.type === "ArrayExpression" || isSignedLiteral(ast) || Boolean(recoveryName(ast) && ast.arguments.every(isContextualLiteral)) || ast.type === "ConditionalExpression" && isContextualLiteral(ast.consequent) && isContextualLiteral(ast.alternate);
3894
+ }
3895
+ function needsArgumentContext(ast) {
3896
+ return ast.type === "ArrayExpression" || ast.type === "Literal" && ast.value === null || Boolean(recoveryName(ast) && ast.arguments.some(containsContextualArray)) || ast.type === "ConditionalExpression" && (needsArgumentContext(ast.consequent) || needsArgumentContext(ast.alternate));
3897
+ }
3898
+
3899
+ // dist/scales/arrow-domains.js
3900
+ function arrowScaleValue(value) {
3901
+ if (typeof value !== "bigint")
3902
+ return value;
3903
+ const numeric = Number(value);
3904
+ if (!Number.isSafeInteger(numeric))
3905
+ throw new RangeError("Scale integer exceeds the safe numeric range");
3906
+ return numeric;
3907
+ }
3908
+ function quantileSorted(sorted, probability) {
3909
+ if (!sorted.length || !Number.isFinite(probability) || probability < 0 || probability > 1) {
3910
+ throw new Error("Quantile requires nonempty finite samples and a probability in [0, 1]");
3911
+ }
3912
+ const index = (sorted.length - 1) * probability;
3913
+ const lower = Math.floor(index);
3914
+ const fraction = index - lower;
3915
+ return sorted[lower] + fraction * (sorted[Math.ceil(index)] - sorted[lower]);
3916
+ }
3917
+ function resolveArrowDomains(table, domains, functions) {
3918
+ const columns = /* @__PURE__ */ new Map();
3919
+ const needsSamples = /* @__PURE__ */ new Set();
3920
+ const needsCategories = /* @__PURE__ */ new Set();
3921
+ for (const domain of domains) {
3922
+ if (domain.type === "expression-extent")
3923
+ continue;
3924
+ if (domain.type === "percentile")
3925
+ needsSamples.add(domain.column);
3926
+ if (domain.type === "categories")
3927
+ needsCategories.add(domain.column);
3928
+ if (domain.type !== "fixed" && !table.getChild(domain.column)) {
3929
+ throw new Error(`Unknown scale domain column: ${domain.column}`);
3930
+ }
3931
+ }
3932
+ for (const domain of domains) {
3933
+ if (domain.type === "fixed" || domain.type === "expression-extent" || columns.has(domain.column))
3934
+ continue;
3935
+ const vector = table.getChild(domain.column);
3936
+ const state = {
3937
+ values: [],
3938
+ categories: /* @__PURE__ */ new Set(),
3939
+ min: Infinity,
3940
+ max: -Infinity
3941
+ };
3942
+ const samples = needsSamples.has(domain.column);
3943
+ for (let row = 0; row < vector.length; row++) {
3944
+ const value = arrowScaleValue(vector.get(row));
3945
+ if (value === null || value === void 0)
3946
+ continue;
3947
+ if (typeof value === "string") {
3948
+ if (needsCategories.has(domain.column))
3949
+ state.categories.add(value);
3950
+ continue;
3951
+ }
3952
+ if (typeof value !== "number" || !Number.isFinite(value)) {
3953
+ throw new Error(`Unsupported scale domain value in ${domain.column} at row ${row}`);
3954
+ }
3955
+ if (needsCategories.has(domain.column))
3956
+ state.categories.add(value);
3957
+ if (value < state.min)
3958
+ state.min = value;
3959
+ if (value > state.max)
3960
+ state.max = value;
3961
+ if (samples)
3962
+ state.values.push(value);
3963
+ }
3964
+ if (samples)
3965
+ state.values.sort((a, b) => a - b);
3966
+ columns.set(domain.column, state);
3967
+ }
3968
+ return domains.map((domain) => {
3969
+ if (domain.type === "fixed")
3970
+ return [...domain.values];
3971
+ if (domain.type === "expression-extent") {
3972
+ return resolveArrowExpressionStatistics(table, domain.expression, void 0, functions).domain;
3973
+ }
3974
+ const data = columns.get(domain.column);
3975
+ if (domain.type === "categories") {
3976
+ const values = [...data.categories];
3977
+ if (domain.order !== "first-seen")
3978
+ values.sort((a, b) => typeof a === "number" && typeof b === "number" ? a - b : a < b ? -1 : a > b ? 1 : 0);
3979
+ if (!values.length)
3980
+ throw new Error(`Empty categorical scale domain: ${domain.column}`);
3981
+ return values;
3982
+ }
3983
+ if (!Number.isFinite(data.min) || !Number.isFinite(data.max)) {
3984
+ throw new Error(`Empty numeric scale domain: ${domain.column}`);
3985
+ }
3986
+ if (domain.type === "percentile") {
3987
+ return [quantileSorted(data.values, domain.lower), quantileSorted(data.values, domain.upper)];
3988
+ }
3989
+ if (domain.type === "symmetric") {
3990
+ const radius = Math.max(Math.abs(data.min - domain.pivot), Math.abs(data.max - domain.pivot));
3991
+ return [domain.pivot - radius, domain.pivot + radius];
3992
+ }
3993
+ if (domain.includeZero)
3994
+ return [Math.min(0, data.min), Math.max(0, data.max)];
3995
+ return [data.min, data.max];
3996
+ });
3997
+ }
3998
+ function resolveArrowExpressionStatistics(table, expression, classes, functions) {
3999
+ const compiled = compileArrowExpression(expression, {
4000
+ inputSchema: table.schema,
4001
+ functions
4002
+ });
4003
+ const values = readNumericSamples(compiled.evaluateTable(table), "Quantile expression");
4004
+ if (!values.length)
4005
+ throw new Error("Empty numeric scale expression domain");
4006
+ if (classes === void 0) {
4007
+ let min = Infinity;
4008
+ let max = -Infinity;
4009
+ for (const value of values) {
4010
+ min = Math.min(min, value);
4011
+ max = Math.max(max, value);
4012
+ }
4013
+ return { domain: [min, max] };
4014
+ }
4015
+ values.sort((a, b) => a - b);
4016
+ return {
4017
+ domain: [values[0], values[values.length - 1]],
4018
+ quantileBreaks: computeQuantileBreaks(values, classes)
4019
+ };
4020
+ }
4021
+ function resolveArrowQuantiles(table, column, classes) {
4022
+ const vector = table.getChild(column);
4023
+ if (!vector)
4024
+ throw new Error(`Unknown quantile column: ${column}`);
4025
+ const values = readNumericSamples(vector, `Quantile column ${column}`);
4026
+ values.sort((a, b) => a - b);
4027
+ return computeQuantileBreaks(values, classes);
4028
+ }
4029
+ function readNumericSamples(vector, label) {
4030
+ const values = [];
4031
+ for (let row = 0; row < vector.length; row++) {
4032
+ const value = arrowScaleValue(vector.get(row));
4033
+ if (value === null || value === void 0)
4034
+ continue;
4035
+ if (typeof value !== "number" || !Number.isFinite(value)) {
4036
+ throw new Error(`${label} must be finite numeric`);
4037
+ }
4038
+ values.push(value);
4039
+ }
4040
+ return values;
4041
+ }
4042
+ function computeQuantileBreaks(values, classes) {
4043
+ if (!Number.isSafeInteger(classes) || classes < 2) {
4044
+ throw new Error("Quantile classes must be >= 2");
4045
+ }
4046
+ return Array.from({ length: classes - 1 }, (_, i) => quantileSorted(values, (i + 1) / classes));
4047
+ }
4048
+
4049
+ // dist/scales/streaming-domains.js
4050
+ function createStreamingDomainAccumulator(domains, options = {}) {
4051
+ const columns = /* @__PURE__ */ new Map();
4052
+ const quantileColumns = new Set(options.quantileColumns ?? []);
4053
+ const streamingDomains = domains.map((domain) => {
4054
+ if (domain.type === "expression-extent") {
4055
+ throw new Error("Streaming scale domains require computed values in a projected column");
4056
+ }
4057
+ return domain;
4058
+ });
4059
+ for (const domain of streamingDomains) {
4060
+ if (domain.type === "fixed")
4061
+ continue;
4062
+ const previous = columns.get(domain.column);
4063
+ if (previous) {
4064
+ if ((domain.type === "percentile" || quantileColumns.has(domain.column)) && !previous.sketch) {
4065
+ previous.sketch = new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity);
4066
+ }
4067
+ if (domain.type === "categories")
4068
+ previous.categories ?? (previous.categories = /* @__PURE__ */ new Set());
4069
+ continue;
4070
+ }
4071
+ columns.set(domain.column, {
4072
+ min: Infinity,
4073
+ max: -Infinity,
4074
+ seen: 0,
4075
+ ...domain.type === "categories" ? { categories: /* @__PURE__ */ new Set() } : {},
4076
+ ...domain.type === "percentile" || quantileColumns.has(domain.column) ? { sketch: new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity) } : {}
4077
+ });
4078
+ }
4079
+ for (const name2 of quantileColumns) {
4080
+ if (!columns.has(name2))
4081
+ columns.set(name2, {
4082
+ min: Infinity,
4083
+ max: -Infinity,
4084
+ seen: 0,
4085
+ sketch: new quantile_sketch_exports.ScaleQuantileSketch(options.quantileCapacity)
4086
+ });
4087
+ }
4088
+ return {
4089
+ update(batch) {
4090
+ var _a2, _b, _c;
4091
+ for (const [name2, state] of columns) {
4092
+ const vector = batch.getChild(name2);
4093
+ if (!vector)
4094
+ throw new Error(`Missing scale column: ${name2}`);
4095
+ for (let row = 0; row < vector.length; row++) {
4096
+ const value = arrowScaleValue(vector.get(row));
4097
+ if (value === null || value === void 0)
4098
+ continue;
4099
+ if (typeof value === "string") {
4100
+ (_a2 = state.categories) == null ? void 0 : _a2.add(value);
4101
+ } else if (typeof value === "number" && Number.isFinite(value)) {
4102
+ (_b = state.categories) == null ? void 0 : _b.add(value);
4103
+ state.seen++;
4104
+ state.min = Math.min(state.min, value);
4105
+ state.max = Math.max(state.max, value);
4106
+ (_c = state.sketch) == null ? void 0 : _c.add(value);
4107
+ } else {
4108
+ throw new Error(`Scale column ${name2} has unsupported value at row ${row}`);
4109
+ }
4110
+ }
4111
+ }
4112
+ },
4113
+ result() {
4114
+ return streamingDomains.map((domain) => {
4115
+ if (domain.type === "fixed")
4116
+ return [...domain.values];
4117
+ const state = columns.get(domain.column);
4118
+ if (domain.type === "categories") {
4119
+ const labels = [...state.categories];
4120
+ return domain.order === "first-seen" ? labels : labels.sort((a, b) => typeof a === "number" && typeof b === "number" ? a - b : a < b ? -1 : a > b ? 1 : 0);
4121
+ }
4122
+ if (!state.seen)
4123
+ return [];
4124
+ if (domain.type === "percentile") {
4125
+ return [state.sketch.quantile(domain.lower), state.sketch.quantile(domain.upper)];
4126
+ }
4127
+ if (domain.type === "symmetric") {
4128
+ const radius = Math.max(Math.abs(state.min - domain.pivot), Math.abs(state.max - domain.pivot));
4129
+ return [domain.pivot - radius, domain.pivot + radius];
4130
+ }
4131
+ if (domain.includeZero)
4132
+ return [Math.min(state.min, 0), Math.max(state.max, 0)];
4133
+ return [state.min, state.max];
4134
+ });
4135
+ },
4136
+ quantileBreaks(column, classes) {
4137
+ var _a2;
4138
+ if (!Number.isSafeInteger(classes) || classes < 2) {
4139
+ throw new Error("Scale class count must be at least two");
4140
+ }
4141
+ const sketch = (_a2 = columns.get(column)) == null ? void 0 : _a2.sketch;
4142
+ if (!sketch)
4143
+ throw new Error(`Column ${column} has no quantile sketch`);
4144
+ if (!sketch.length)
4145
+ return [];
4146
+ return Array.from({ length: classes - 1 }, (_, i) => sketch.quantile((i + 1) / classes));
4147
+ }
4148
+ };
4149
+ }
4150
+
4151
+ // dist/scales/arrow-scales.js
4152
+ function asColor(value) {
4153
+ if (value === null)
4154
+ return null;
4155
+ if (typeof value === "number")
4156
+ throw new Error("Expected a color-valued scale");
4157
+ return value;
4158
+ }
4159
+ function toVector(rows, spec) {
4160
+ var _a2;
4161
+ const numeric = spec.range.type === "numeric";
4162
+ if (numeric) {
4163
+ return (0, import_apache_arrow7.vectorFromArray)(rows, new import_apache_arrow7.Float64());
4164
+ }
4165
+ const format = ((_a2 = spec.encoding) == null ? void 0 : _a2.format) ?? "rgba8unorm";
4166
+ const type = format === "rgba8unorm" ? new import_apache_arrow7.Uint8() : format === "rgba16unorm" ? new import_apache_arrow7.Uint16() : format === "rgba16float" ? new import_apache_arrow7.Float16() : new import_apache_arrow7.Float32();
4167
+ const factor = format === "rgba8unorm" ? 255 : format === "rgba16unorm" ? 65535 : void 0;
4168
+ const vector = nullTupleVector(new import_apache_arrow7.FixedSizeList(4, new import_apache_arrow7.Field("item", type, false)), rows.length);
4169
+ const channels = vector.getChildAt(0);
4170
+ for (let row = 0; row < rows.length; row++) {
4171
+ const color2 = asColor(rows[row]);
4172
+ if (!color2)
4173
+ continue;
4174
+ for (let component = 0; component < 4; component++) {
4175
+ const channel = color2[component];
4176
+ if (!Number.isFinite(channel) || format === "rgba16float" && Math.abs(channel) >= 65520 || format === "rgba32float" && !Number.isFinite(Math.fround(channel)))
4177
+ throw new Error("Scale color cannot be represented in the requested format");
4178
+ channels.set(row * 4 + component, factor === void 0 ? channel : Math.round(Math.max(0, Math.min(1, channel)) * factor));
4179
+ }
4180
+ vector.data[0].setValid(row, true);
4181
+ }
4182
+ return vector;
4183
+ }
4184
+ function getClassCount(spec) {
4185
+ if (spec.range.type === "palette")
4186
+ return spec.range.count;
4187
+ return spec.type === "quantile" && spec.range.type === "colors" ? spec.range.values.length : void 0;
4188
+ }
4189
+ function compileArrowScale(table, parsed, resolvedDomain, functions) {
4190
+ const classCount = getClassCount(parsed.scale);
4191
+ if (parsed.scale.type === "quantile" && (!classCount || parsed.scale.domain.type === "fixed")) {
4192
+ throw new Error("Quantile scale requires a class count and a data-derived domain");
4193
+ }
4194
+ if (parsed.scale.domain.type === "expression-extent") {
4195
+ const resolution2 = resolveArrowExpressionStatistics(table, parsed.scale.domain.expression, parsed.scale.type === "quantile" ? classCount : void 0, functions);
4196
+ return bindArrowScale(table.schema, parsed, { ...resolution2, domain: resolvedDomain ?? resolution2.domain }, functions);
4197
+ }
4198
+ const resolution = {
4199
+ domain: resolvedDomain ?? resolveArrowDomains(table, [parsed.scale.domain])[0],
4200
+ ...parsed.scale.type === "quantile" && parsed.scale.domain.type !== "fixed" ? { quantileBreaks: resolveArrowQuantiles(table, parsed.scale.domain.column, classCount) } : {}
4201
+ };
4202
+ return bindArrowScale(table.schema, parsed, resolution, functions);
4203
+ }
4204
+ function bindArrowScale(inputSchema, parsed, { domain, quantileBreaks }, functions) {
4205
+ let thresholds;
4206
+ const kind = parsed.scale.type;
4207
+ const classCount = getClassCount(parsed.scale);
4208
+ if (kind === "quantile") {
4209
+ if (classCount === void 0 || !Number.isSafeInteger(classCount) || classCount < 2 || (quantileBreaks == null ? void 0 : quantileBreaks.length) !== classCount - 1) {
4210
+ throw new Error("Quantile scale requires one resolved break per class boundary");
4211
+ }
4212
+ if (quantileBreaks.some((value, index) => !Number.isFinite(value) || index > 0 && value < quantileBreaks[index - 1])) {
4213
+ throw new Error("Quantile scale breaks must be finite and nondecreasing");
4214
+ }
4215
+ thresholds = quantileBreaks;
4216
+ } else if (kind === "quantize") {
4217
+ if (!classCount || domain.length !== 2 || typeof domain[0] !== "number" || typeof domain[1] !== "number") {
4218
+ throw new Error("Quantize scale requires a numeric extent and a class count");
4219
+ }
4220
+ thresholds = Array.from({ length: classCount - 1 }, (_, i) => domain[0] + (domain[1] - domain[0]) * (i + 1) / classCount);
4221
+ } else if (kind === "threshold") {
4222
+ if (domain.some((value) => typeof value !== "number")) {
4223
+ throw new Error("Thresholds require numeric domain values");
4224
+ }
4225
+ thresholds = domain;
4226
+ }
4227
+ const scale = (0, compile_scale_exports.compileResolvedScale)(parsed.scale, domain, thresholds);
4228
+ const compiled = compileArrowExpression(parsed.value, {
4229
+ inputSchema,
4230
+ functions
4231
+ });
4232
+ const evaluateVector = (input) => {
4233
+ const result = Array.from({ length: input.length }, (_, i) => {
4234
+ const value = arrowScaleValue(input.get(i));
4235
+ if (value === null || value === void 0)
4236
+ return scale.sample(null);
4237
+ if (typeof value !== "number" && typeof value !== "string") {
4238
+ throw new Error("Scale expression must produce scalar numeric or categorical values");
4239
+ }
4240
+ return scale.sample(value);
4241
+ });
4242
+ return toVector(result, parsed.scale);
4243
+ };
4244
+ return {
4245
+ scale,
4246
+ evaluate: (input) => evaluateVector(compiled.evaluateTable(input)),
4247
+ evaluateBatch: (batch) => evaluateVector(compiled.evaluate(batch))
4248
+ };
4249
+ }
4250
+ function compileArrowScales(table, expressions, functions) {
4251
+ const entries = Object.entries(expressions);
4252
+ const columnEntries = entries.filter(([, parsed]) => parsed.scale.domain.type !== "expression-extent");
4253
+ const domains = resolveArrowDomains(table, columnEntries.map(([, parsed]) => parsed.scale.domain));
4254
+ const domainsByKey = new Map(columnEntries.map(([key], index) => [key, domains[index]]));
4255
+ return Object.fromEntries(entries.map(([key, parsed]) => [
4256
+ key,
4257
+ compileArrowScale(table, parsed, domainsByKey.get(key), functions)
4258
+ ]));
4259
+ }
4260
+ function compileArrowScaleExpression(table, expression, functions = []) {
4261
+ const ast = typeof expression === "string" ? parseSQLExpression(expression) : expression;
4262
+ const names = new Set(functions.map((fn) => fn.name.toUpperCase()));
4263
+ const scales = [];
4264
+ const declarations = [...functions];
4265
+ function reserve(node) {
4266
+ if (!node || typeof node !== "object")
4267
+ return;
4268
+ if ("type" in node && node.type === "Identifier" && "name" in node)
4269
+ names.add(String(node.name).toUpperCase());
4270
+ for (const value of Object.values(node))
4271
+ reserve(value);
4272
+ }
4273
+ reserve(ast);
4274
+ let nextName = 0;
4275
+ function visit(node) {
4276
+ var _a2;
4277
+ if (!node || typeof node !== "object")
4278
+ return node;
4279
+ if (Array.isArray(node))
4280
+ return node.map(visit);
4281
+ if (node.type === "CallExpression" && ((_a2 = node.callee) == null ? void 0 : _a2.type) === "Identifier" && /^SCALE_(LINEAR|SQRT|LOG|COLOR|DIVERGING|ORDINAL|QUANTILE|QUANTIZE|THRESHOLD)$/i.test(node.callee.name)) {
4282
+ const scaleIndex = scales.length;
4283
+ scales.length++;
4284
+ const valueExpression = visit(node.arguments[0]);
4285
+ const parsed = parseSQLScaleAST({
4286
+ ...node,
4287
+ arguments: [valueExpression, ...node.arguments.slice(1)]
4288
+ });
4289
+ const { scale } = compileArrowScale(table, parsed, void 0, declarations);
4290
+ scales[scaleIndex] = scale;
4291
+ const input = compileArrowExpression(parsed.value, {
4292
+ inputSchema: table.schema,
4293
+ functions: declarations
4294
+ });
4295
+ let name2;
4296
+ do {
4297
+ name2 = "__scale_" + nextName++;
4298
+ } while (names.has(name2.toUpperCase()));
4299
+ names.add(name2.toUpperCase());
4300
+ declarations.push({
4301
+ name: name2,
4302
+ prototypes: [
4303
+ {
4304
+ arguments: [input.outputField],
4305
+ result: new import_apache_arrow7.Field(name2, toVector([], parsed.scale).type, true),
4306
+ nullHandling: "call",
4307
+ evaluate: ([values], { activeRows, length }) => toVector(Array.from({ length }, (_, row) => {
4308
+ if (!activeRows[row])
4309
+ return null;
4310
+ const value = arrowScaleValue(values.get(row));
4311
+ if (value !== null && value !== void 0 && typeof value !== "number" && typeof value !== "string") {
4312
+ throw new Error("Scale input must be numeric or categorical");
4313
+ }
4314
+ return scale.sample(value ?? null);
4315
+ }), parsed.scale)
4316
+ }
4317
+ ]
4318
+ });
4319
+ return {
4320
+ type: "CallExpression",
4321
+ callee: { type: "Identifier", name: name2 },
4322
+ arguments: [parsed.value]
4323
+ };
4324
+ }
4325
+ return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, visit(value)]));
4326
+ }
4327
+ const rewritten = visit(ast);
4328
+ const compiled = compileArrowExpression(rewritten, {
4329
+ inputSchema: table.schema,
4330
+ functions: declarations
4331
+ });
4332
+ return { ...compiled, scales };
4333
+ }
1214
4334
  //# sourceMappingURL=index.cjs.map