@math.gl/sql 0.0.0-stage → 5.0.0-beta.11

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 (334) hide show
  1. package/LICENSE +249 -0
  2. package/README.md +26 -2
  3. package/THIRD-PARTY-NOTICES.md +5 -0
  4. package/dist/arrow/aggregates.d.ts +46 -0
  5. package/dist/arrow/aggregates.d.ts.map +1 -0
  6. package/dist/arrow/aggregates.js +289 -0
  7. package/dist/arrow/aggregates.js.map +1 -0
  8. package/dist/arrow/buffers.d.ts +30 -0
  9. package/dist/arrow/buffers.d.ts.map +1 -0
  10. package/dist/arrow/buffers.js +177 -0
  11. package/dist/arrow/buffers.js.map +1 -0
  12. package/dist/arrow/cast.d.ts +19 -0
  13. package/dist/arrow/cast.d.ts.map +1 -0
  14. package/dist/arrow/cast.js +138 -0
  15. package/dist/arrow/cast.js.map +1 -0
  16. package/dist/arrow/compiler.d.ts +10 -0
  17. package/dist/arrow/compiler.d.ts.map +1 -0
  18. package/dist/arrow/compiler.js +1421 -0
  19. package/dist/arrow/compiler.js.map +1 -0
  20. package/dist/arrow/float16.d.ts +7 -0
  21. package/dist/arrow/float16.d.ts.map +1 -0
  22. package/dist/arrow/float16.js +26 -0
  23. package/dist/arrow/float16.js.map +1 -0
  24. package/dist/arrow/index.cjs +3220 -0
  25. package/dist/arrow/index.cjs.map +6 -0
  26. package/dist/arrow/index.d.ts +5 -0
  27. package/dist/arrow/index.d.ts.map +1 -0
  28. package/dist/arrow/index.js +7 -0
  29. package/dist/arrow/index.js.map +1 -0
  30. package/dist/arrow/like.d.ts +3 -0
  31. package/dist/arrow/like.d.ts.map +1 -0
  32. package/dist/arrow/like.js +75 -0
  33. package/dist/arrow/like.js.map +1 -0
  34. package/dist/arrow/list-values.d.ts +14 -0
  35. package/dist/arrow/list-values.d.ts.map +1 -0
  36. package/dist/arrow/list-values.js +180 -0
  37. package/dist/arrow/list-values.js.map +1 -0
  38. package/dist/arrow/predicate.d.ts +7 -0
  39. package/dist/arrow/predicate.d.ts.map +1 -0
  40. package/dist/arrow/predicate.js +10 -0
  41. package/dist/arrow/predicate.js.map +1 -0
  42. package/dist/arrow/query-arrow-aggregate-snapshots.d.ts +24 -0
  43. package/dist/arrow/query-arrow-aggregate-snapshots.d.ts.map +1 -0
  44. package/dist/arrow/query-arrow-aggregate-snapshots.js +64 -0
  45. package/dist/arrow/query-arrow-aggregate-snapshots.js.map +1 -0
  46. package/dist/arrow/query-arrow-sql.d.ts +8 -0
  47. package/dist/arrow/query-arrow-sql.d.ts.map +1 -0
  48. package/dist/arrow/query-arrow-sql.js +191 -0
  49. package/dist/arrow/query-arrow-sql.js.map +1 -0
  50. package/dist/arrow/query-arrow-table.d.ts +36 -0
  51. package/dist/arrow/query-arrow-table.d.ts.map +1 -0
  52. package/dist/arrow/query-arrow-table.js +208 -0
  53. package/dist/arrow/query-arrow-table.js.map +1 -0
  54. package/dist/arrow/query-worker-entry.d.ts +2 -0
  55. package/dist/arrow/query-worker-entry.d.ts.map +1 -0
  56. package/dist/arrow/query-worker-entry.js +7 -0
  57. package/dist/arrow/query-worker-entry.js.map +1 -0
  58. package/dist/arrow/query-worker.d.ts +65 -0
  59. package/dist/arrow/query-worker.d.ts.map +1 -0
  60. package/dist/arrow/query-worker.js +135 -0
  61. package/dist/arrow/query-worker.js.map +1 -0
  62. package/dist/arrow/registry.d.ts +4 -0
  63. package/dist/arrow/registry.d.ts.map +1 -0
  64. package/dist/arrow/registry.js +65 -0
  65. package/dist/arrow/registry.js.map +1 -0
  66. package/dist/arrow/relational-query.d.ts +36 -0
  67. package/dist/arrow/relational-query.d.ts.map +1 -0
  68. package/dist/arrow/relational-query.js +174 -0
  69. package/dist/arrow/relational-query.js.map +1 -0
  70. package/dist/arrow/replace-arrow-columns.d.ts +8 -0
  71. package/dist/arrow/replace-arrow-columns.d.ts.map +1 -0
  72. package/dist/arrow/replace-arrow-columns.js +63 -0
  73. package/dist/arrow/replace-arrow-columns.js.map +1 -0
  74. package/dist/arrow/signature.d.ts +6 -0
  75. package/dist/arrow/signature.d.ts.map +1 -0
  76. package/dist/arrow/signature.js +51 -0
  77. package/dist/arrow/signature.js.map +1 -0
  78. package/dist/arrow/temporal-cast.d.ts +9 -0
  79. package/dist/arrow/temporal-cast.d.ts.map +1 -0
  80. package/dist/arrow/temporal-cast.js +60 -0
  81. package/dist/arrow/temporal-cast.js.map +1 -0
  82. package/dist/arrow/types.d.ts +62 -0
  83. package/dist/arrow/types.d.ts.map +1 -0
  84. package/dist/arrow/types.js +5 -0
  85. package/dist/arrow/types.js.map +1 -0
  86. package/dist/arrow/worker-entry.d.ts +2 -0
  87. package/dist/arrow/worker-entry.d.ts.map +1 -0
  88. package/dist/arrow/worker-entry.js +9 -0
  89. package/dist/arrow/worker-entry.js.map +1 -0
  90. package/dist/arrow/worker.cjs +3973 -0
  91. package/dist/arrow/worker.cjs.map +6 -0
  92. package/dist/arrow/worker.d.ts +58 -0
  93. package/dist/arrow/worker.d.ts.map +1 -0
  94. package/dist/arrow/worker.js +84 -0
  95. package/dist/arrow/worker.js.map +1 -0
  96. package/dist/color/index.cjs +382 -0
  97. package/dist/color/index.cjs.map +6 -0
  98. package/dist/color/index.d.ts +26 -0
  99. package/dist/color/index.d.ts.map +1 -0
  100. package/dist/color/index.js +306 -0
  101. package/dist/color/index.js.map +1 -0
  102. package/dist/diagnostic-log.d.ts +8 -0
  103. package/dist/diagnostic-log.d.ts.map +1 -0
  104. package/dist/diagnostic-log.js +15 -0
  105. package/dist/diagnostic-log.js.map +1 -0
  106. package/dist/expressions/ast.d.ts +12 -0
  107. package/dist/expressions/ast.d.ts.map +1 -0
  108. package/dist/expressions/ast.js +112 -0
  109. package/dist/expressions/ast.js.map +1 -0
  110. package/dist/expressions/columnar-predicate.d.ts +56 -0
  111. package/dist/expressions/columnar-predicate.d.ts.map +1 -0
  112. package/dist/expressions/columnar-predicate.js +176 -0
  113. package/dist/expressions/columnar-predicate.js.map +1 -0
  114. package/dist/expressions/evaluation-error.d.ts +14 -0
  115. package/dist/expressions/evaluation-error.d.ts.map +1 -0
  116. package/dist/expressions/evaluation-error.js +26 -0
  117. package/dist/expressions/evaluation-error.js.map +1 -0
  118. package/dist/expressions/expression-eval.d.ts +96 -0
  119. package/dist/expressions/expression-eval.d.ts.map +1 -0
  120. package/dist/expressions/expression-eval.js +330 -0
  121. package/dist/expressions/expression-eval.js.map +1 -0
  122. package/dist/expressions/function-libraries.d.ts +63 -0
  123. package/dist/expressions/function-libraries.d.ts.map +1 -0
  124. package/dist/expressions/function-libraries.js +69 -0
  125. package/dist/expressions/function-libraries.js.map +1 -0
  126. package/dist/expressions/function-registry.d.ts +93 -0
  127. package/dist/expressions/function-registry.d.ts.map +1 -0
  128. package/dist/expressions/function-registry.js +139 -0
  129. package/dist/expressions/function-registry.js.map +1 -0
  130. package/dist/expressions/get.d.ts +9 -0
  131. package/dist/expressions/get.d.ts.map +1 -0
  132. package/dist/expressions/get.js +32 -0
  133. package/dist/expressions/get.js.map +1 -0
  134. package/dist/expressions/index.cjs +1888 -0
  135. package/dist/expressions/index.cjs.map +6 -0
  136. package/dist/expressions/index.d.ts +13 -0
  137. package/dist/expressions/index.d.ts.map +1 -0
  138. package/dist/expressions/index.js +14 -0
  139. package/dist/expressions/index.js.map +1 -0
  140. package/dist/expressions/numeric-functions.d.ts +277 -0
  141. package/dist/expressions/numeric-functions.d.ts.map +1 -0
  142. package/dist/expressions/numeric-functions.js +52 -0
  143. package/dist/expressions/numeric-functions.js.map +1 -0
  144. package/dist/expressions/parse-expression-string.d.ts +21 -0
  145. package/dist/expressions/parse-expression-string.d.ts.map +1 -0
  146. package/dist/expressions/parse-expression-string.js +61 -0
  147. package/dist/expressions/parse-expression-string.js.map +1 -0
  148. package/dist/expressions/predicate-expression.d.ts +7 -0
  149. package/dist/expressions/predicate-expression.d.ts.map +1 -0
  150. package/dist/expressions/predicate-expression.js +112 -0
  151. package/dist/expressions/predicate-expression.js.map +1 -0
  152. package/dist/expressions/scalar-type.d.ts +6 -0
  153. package/dist/expressions/scalar-type.d.ts.map +1 -0
  154. package/dist/expressions/scalar-type.js +23 -0
  155. package/dist/expressions/scalar-type.js.map +1 -0
  156. package/dist/expressions/vendor/jsep/runtime.d.ts +3 -0
  157. package/dist/expressions/vendor/jsep/runtime.d.ts.map +1 -0
  158. package/dist/expressions/vendor/jsep/runtime.js +972 -0
  159. package/dist/expressions/vendor/jsep/runtime.js.map +1 -0
  160. package/dist/expressions/vendor/jsep/types.d.ts +148 -0
  161. package/dist/expressions/vendor/jsep/types.d.ts.map +1 -0
  162. package/dist/expressions/vendor/jsep/types.js +6 -0
  163. package/dist/expressions/vendor/jsep/types.js.map +1 -0
  164. package/dist/index.cjs +4865 -0
  165. package/dist/index.cjs.map +6 -0
  166. package/dist/index.d.ts +9 -0
  167. package/dist/index.d.ts.map +1 -0
  168. package/dist/index.js +10 -0
  169. package/dist/index.js.map +1 -0
  170. package/dist/math/index.cjs +444 -0
  171. package/dist/math/index.cjs.map +6 -0
  172. package/dist/math/index.d.ts +44 -0
  173. package/dist/math/index.d.ts.map +1 -0
  174. package/dist/math/index.js +182 -0
  175. package/dist/math/index.js.map +1 -0
  176. package/dist/scales/arrow-domains.d.ts +15 -0
  177. package/dist/scales/arrow-domains.d.ts.map +1 -0
  178. package/dist/scales/arrow-domains.js +124 -0
  179. package/dist/scales/arrow-domains.js.map +1 -0
  180. package/dist/scales/arrow-scales.cjs +3837 -0
  181. package/dist/scales/arrow-scales.cjs.map +6 -0
  182. package/dist/scales/arrow-scales.d.ts +20 -0
  183. package/dist/scales/arrow-scales.d.ts.map +1 -0
  184. package/dist/scales/arrow-scales.js +109 -0
  185. package/dist/scales/arrow-scales.js.map +1 -0
  186. package/dist/scales/compile-scale.d.ts +7 -0
  187. package/dist/scales/compile-scale.d.ts.map +1 -0
  188. package/dist/scales/compile-scale.js +195 -0
  189. package/dist/scales/compile-scale.js.map +1 -0
  190. package/dist/scales/hdr-colors.d.ts +13 -0
  191. package/dist/scales/hdr-colors.d.ts.map +1 -0
  192. package/dist/scales/hdr-colors.js +115 -0
  193. package/dist/scales/hdr-colors.js.map +1 -0
  194. package/dist/scales/index.cjs +1214 -0
  195. package/dist/scales/index.cjs.map +6 -0
  196. package/dist/scales/index.d.ts +9 -0
  197. package/dist/scales/index.d.ts.map +1 -0
  198. package/dist/scales/index.js +10 -0
  199. package/dist/scales/index.js.map +1 -0
  200. package/dist/scales/palettes.d.ts +5 -0
  201. package/dist/scales/palettes.d.ts.map +1 -0
  202. package/dist/scales/palettes.js +85 -0
  203. package/dist/scales/palettes.js.map +1 -0
  204. package/dist/scales/parse-sql-scale.d.ts +14 -0
  205. package/dist/scales/parse-sql-scale.d.ts.map +1 -0
  206. package/dist/scales/parse-sql-scale.js +165 -0
  207. package/dist/scales/parse-sql-scale.js.map +1 -0
  208. package/dist/scales/quantile-sketch.d.ts +22 -0
  209. package/dist/scales/quantile-sketch.d.ts.map +1 -0
  210. package/dist/scales/quantile-sketch.js +108 -0
  211. package/dist/scales/quantile-sketch.js.map +1 -0
  212. package/dist/scales/scale-types.d.ts +92 -0
  213. package/dist/scales/scale-types.d.ts.map +1 -0
  214. package/dist/scales/scale-types.js +119 -0
  215. package/dist/scales/scale-types.js.map +1 -0
  216. package/dist/scales/streaming-domains.d.ts +18 -0
  217. package/dist/scales/streaming-domains.d.ts.map +1 -0
  218. package/dist/scales/streaming-domains.js +109 -0
  219. package/dist/scales/streaming-domains.js.map +1 -0
  220. package/dist/sql/bind-sql-predicate.d.ts +4 -0
  221. package/dist/sql/bind-sql-predicate.d.ts.map +1 -0
  222. package/dist/sql/bind-sql-predicate.js +11 -0
  223. package/dist/sql/bind-sql-predicate.js.map +1 -0
  224. package/dist/sql/index.cjs +1588 -0
  225. package/dist/sql/index.cjs.map +6 -0
  226. package/dist/sql/index.d.ts +10 -0
  227. package/dist/sql/index.d.ts.map +1 -0
  228. package/dist/sql/index.js +10 -0
  229. package/dist/sql/index.js.map +1 -0
  230. package/dist/sql/parse-sql-expression.d.ts +44 -0
  231. package/dist/sql/parse-sql-expression.d.ts.map +1 -0
  232. package/dist/sql/parse-sql-expression.js +455 -0
  233. package/dist/sql/parse-sql-expression.js.map +1 -0
  234. package/dist/sql/parse-sql-predicate.d.ts +13 -0
  235. package/dist/sql/parse-sql-predicate.d.ts.map +1 -0
  236. package/dist/sql/parse-sql-predicate.js +365 -0
  237. package/dist/sql/parse-sql-predicate.js.map +1 -0
  238. package/dist/sql/parse-sql-query.d.ts +8 -0
  239. package/dist/sql/parse-sql-query.d.ts.map +1 -0
  240. package/dist/sql/parse-sql-query.js +66 -0
  241. package/dist/sql/parse-sql-query.js.map +1 -0
  242. package/dist/sql/parse-sql-select.d.ts +55 -0
  243. package/dist/sql/parse-sql-select.d.ts.map +1 -0
  244. package/dist/sql/parse-sql-select.js +244 -0
  245. package/dist/sql/parse-sql-select.js.map +1 -0
  246. package/dist/sql/parse-sql-statement.d.ts +50 -0
  247. package/dist/sql/parse-sql-statement.d.ts.map +1 -0
  248. package/dist/sql/parse-sql-statement.js +182 -0
  249. package/dist/sql/parse-sql-statement.js.map +1 -0
  250. package/dist/sql/query-types.d.ts +56 -0
  251. package/dist/sql/query-types.d.ts.map +1 -0
  252. package/dist/sql/query-types.js +5 -0
  253. package/dist/sql/query-types.js.map +1 -0
  254. package/dist/sql/sql-parse-error.d.ts +13 -0
  255. package/dist/sql/sql-parse-error.d.ts.map +1 -0
  256. package/dist/sql/sql-parse-error.js +20 -0
  257. package/dist/sql/sql-parse-error.js.map +1 -0
  258. package/dist/sql/sql-predicate-schema.d.ts +164 -0
  259. package/dist/sql/sql-predicate-schema.d.ts.map +1 -0
  260. package/dist/sql/sql-predicate-schema.js +249 -0
  261. package/dist/sql/sql-predicate-schema.js.map +1 -0
  262. package/dist/sql/sql-predicate-types.d.ts +48 -0
  263. package/dist/sql/sql-predicate-types.d.ts.map +1 -0
  264. package/dist/sql/sql-predicate-types.js +9 -0
  265. package/dist/sql/sql-predicate-types.js.map +1 -0
  266. package/dist/sql/sql-predicate.d.ts +6 -0
  267. package/dist/sql/sql-predicate.d.ts.map +1 -0
  268. package/dist/sql/sql-predicate.js +8 -0
  269. package/dist/sql/sql-predicate.js.map +1 -0
  270. package/package.json +92 -4
  271. package/src/arrow/aggregates.ts +386 -0
  272. package/src/arrow/buffers.ts +177 -0
  273. package/src/arrow/cast.ts +160 -0
  274. package/src/arrow/compiler.ts +1659 -0
  275. package/src/arrow/float16.ts +25 -0
  276. package/src/arrow/index.ts +22 -0
  277. package/src/arrow/like.ts +72 -0
  278. package/src/arrow/list-values.ts +200 -0
  279. package/src/arrow/predicate.ts +19 -0
  280. package/src/arrow/query-arrow-aggregate-snapshots.ts +107 -0
  281. package/src/arrow/query-arrow-sql.ts +227 -0
  282. package/src/arrow/query-arrow-table.ts +298 -0
  283. package/src/arrow/query-worker-entry.ts +8 -0
  284. package/src/arrow/query-worker.ts +190 -0
  285. package/src/arrow/registry.ts +86 -0
  286. package/src/arrow/relational-query.ts +247 -0
  287. package/src/arrow/replace-arrow-columns.ts +71 -0
  288. package/src/arrow/signature.ts +50 -0
  289. package/src/arrow/temporal-cast.ts +66 -0
  290. package/src/arrow/types.ts +76 -0
  291. package/src/arrow/worker-entry.ts +10 -0
  292. package/src/arrow/worker.ts +139 -0
  293. package/src/color/index.ts +405 -0
  294. package/src/diagnostic-log.ts +20 -0
  295. package/src/expressions/ast.ts +141 -0
  296. package/src/expressions/columnar-predicate.ts +293 -0
  297. package/src/expressions/evaluation-error.ts +29 -0
  298. package/src/expressions/expression-eval.ts +480 -0
  299. package/src/expressions/function-libraries.ts +126 -0
  300. package/src/expressions/function-registry.ts +171 -0
  301. package/src/expressions/get.ts +37 -0
  302. package/src/expressions/index.ts +40 -0
  303. package/src/expressions/numeric-functions.ts +99 -0
  304. package/src/expressions/parse-expression-string.ts +80 -0
  305. package/src/expressions/predicate-expression.ts +128 -0
  306. package/src/expressions/scalar-type.ts +39 -0
  307. package/src/expressions/vendor/jsep/LICENSE.txt +23 -0
  308. package/src/expressions/vendor/jsep/README.md +22 -0
  309. package/src/expressions/vendor/jsep/runtime.ts +949 -0
  310. package/src/expressions/vendor/jsep/types.ts +191 -0
  311. package/src/index.ts +40 -0
  312. package/src/math/index.ts +235 -0
  313. package/src/scales/arrow-domains.ts +131 -0
  314. package/src/scales/arrow-scales.ts +156 -0
  315. package/src/scales/compile-scale.ts +217 -0
  316. package/src/scales/hdr-colors.ts +126 -0
  317. package/src/scales/index.ts +35 -0
  318. package/src/scales/palettes.ts +86 -0
  319. package/src/scales/parse-sql-scale.ts +195 -0
  320. package/src/scales/quantile-sketch.ts +106 -0
  321. package/src/scales/scale-types.ts +224 -0
  322. package/src/scales/streaming-domains.ts +133 -0
  323. package/src/sql/bind-sql-predicate.ts +21 -0
  324. package/src/sql/index.ts +28 -0
  325. package/src/sql/parse-sql-expression.ts +457 -0
  326. package/src/sql/parse-sql-predicate.ts +454 -0
  327. package/src/sql/parse-sql-query.ts +72 -0
  328. package/src/sql/parse-sql-select.ts +266 -0
  329. package/src/sql/parse-sql-statement.ts +242 -0
  330. package/src/sql/query-types.ts +71 -0
  331. package/src/sql/sql-parse-error.ts +33 -0
  332. package/src/sql/sql-predicate-schema.ts +279 -0
  333. package/src/sql/sql-predicate-types.ts +91 -0
  334. package/src/sql/sql-predicate.ts +28 -0
@@ -0,0 +1,1214 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name2 in all)
7
+ __defProp(target, name2, { get: all[name2], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // dist/scales/index.js
20
+ var index_exports = {};
21
+ __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,
31
+ parseSQLScaleExpression: () => parseSQLScaleExpression,
32
+ resolvePalette: () => resolvePalette,
33
+ unpackFloat16: () => unpackFloat16,
34
+ validateScaleSpec: () => validateScaleSpec
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+
38
+ // 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");
112
+ }
113
+ break;
114
+ default:
115
+ throw new Error("Unsupported scale range");
116
+ }
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");
121
+ }
122
+ for (const key of ["nullColor", "underColor", "overColor"]) {
123
+ if (spec[key])
124
+ checkColor(spec[key], key);
125
+ }
126
+ return spec;
127
+ }
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
+ }
137
+ }
138
+ return Object.freeze({ descriptor, sample });
139
+ }
140
+
141
+ // dist/sql/sql-parse-error.js
142
+ var SQLParseError = class extends SyntaxError {
143
+ constructor(reason, source, offset) {
144
+ var _a;
145
+ const lines = source.slice(0, offset).split(/\r\n|\r|\n/);
146
+ const line = lines.length;
147
+ 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;
149
+ super(`${reason} (line ${line}, column ${column}${token ? `, near ${JSON.stringify(token)}` : ""})`);
150
+ this.reason = reason;
151
+ this.name = "SQLParseError";
152
+ this.offset = offset;
153
+ this.line = line;
154
+ this.column = column;
155
+ this.token = token;
156
+ }
157
+ };
158
+
159
+ // dist/sql/parse-sql-expression.js
160
+ var SQL_RESERVED_WORDS = /* @__PURE__ */ new Set([
161
+ "SELECT",
162
+ "ARRAY",
163
+ "FROM",
164
+ "WHERE",
165
+ "LIMIT",
166
+ "OFFSET",
167
+ "AS",
168
+ "TRUE",
169
+ "FALSE",
170
+ "NULL",
171
+ "ALL",
172
+ "AND",
173
+ "OR",
174
+ "NOT",
175
+ "IN",
176
+ "IS",
177
+ "DISTINCT",
178
+ "ORDER",
179
+ "GROUP",
180
+ "HAVING",
181
+ "JOIN",
182
+ "UNION"
183
+ ]);
184
+ function sqlCastType(name2, source = name2, offset = 0) {
185
+ const normalized = name2.toLowerCase().replace(/\s+/g, " ").trim();
186
+ if (/^(?:u?int(?:8|16|32|64)|float(?:16|32|64))$/.test(normalized)) {
187
+ return normalized;
188
+ }
189
+ switch (normalized) {
190
+ case "smallint":
191
+ return "int16";
192
+ case "int":
193
+ case "integer":
194
+ return "int32";
195
+ case "bigint":
196
+ return "int64";
197
+ case "real":
198
+ return "float32";
199
+ case "double precision":
200
+ return "float64";
201
+ case "boolean":
202
+ return "bool";
203
+ case "utf8":
204
+ case "varchar":
205
+ case "character varying":
206
+ return "utf8";
207
+ default:
208
+ throw new SQLParseError(`Unsupported SQL CAST type: ${name2}`, source, offset);
209
+ }
210
+ }
211
+ function parseSQLExpression(source) {
212
+ const reader = new SQLExpressionReader(source);
213
+ const expression = reader.expression();
214
+ reader.space();
215
+ if (reader.position !== source.length)
216
+ throw reader.error("Unexpected token after SQL expression");
217
+ return expression;
218
+ }
219
+ var SQLExpressionReader = class {
220
+ constructor(source, start = 0) {
221
+ this.source = source;
222
+ this.tokens = 0;
223
+ this.depths = /* @__PURE__ */ new WeakMap();
224
+ this.position = start;
225
+ if (start === 0 && (typeof source !== "string" || !source.trim() || source.length > 65536))
226
+ throw new SQLParseError("SQL expression requires non-empty input of at most 65536 characters", "", 0);
227
+ }
228
+ /** Creates an error at the current expression cursor. */
229
+ error(message, offset = this.position) {
230
+ return new SQLParseError(message, this.source, offset);
231
+ }
232
+ space() {
233
+ while (this.position < this.source.length && /\s/.test(this.source[this.position]))
234
+ this.position++;
235
+ if (this.source.startsWith("--", this.position) || this.source.startsWith("/*", this.position))
236
+ throw this.error("SQL comments are not supported.");
237
+ }
238
+ word(word) {
239
+ this.space();
240
+ if (this.source.slice(this.position, this.position + word.length).toUpperCase() !== word || /[A-Za-z0-9_]/.test(this.source[this.position + word.length] || ""))
241
+ return false;
242
+ this.position += word.length;
243
+ this.token();
244
+ return true;
245
+ }
246
+ take(symbol) {
247
+ this.space();
248
+ if (!this.source.startsWith(symbol, this.position))
249
+ return false;
250
+ this.position += symbol.length;
251
+ this.token();
252
+ return true;
253
+ }
254
+ symbol(symbol) {
255
+ if (!this.take(symbol))
256
+ throw this.error(`SQL expression expected ${symbol}`);
257
+ }
258
+ node(value, children = []) {
259
+ const depth = children.length ? 1 + Math.max(...children.map((child) => this.depths.get(child) || 0)) : 0;
260
+ if (depth > 64)
261
+ throw this.error("SQL expression exceeds nesting limit");
262
+ this.depths.set(value, depth);
263
+ return value;
264
+ }
265
+ expression(depth = 0, minimum = 0) {
266
+ if (depth > 64)
267
+ throw this.error("SQL expression exceeds nesting limit");
268
+ let left;
269
+ if (this.word("NOT")) {
270
+ const argument = this.expression(depth + 1, 3);
271
+ left = this.node({ type: "UnaryExpression", operator: "!", argument }, [argument]);
272
+ } else if (/^\s*[+-](?:\d|\.\d)/.test(this.source.slice(this.position))) {
273
+ left = this.primary(depth);
274
+ } else if (this.take("+") || this.take("-")) {
275
+ const operator = this.source[this.position - 1];
276
+ const argument = this.expression(depth + 1, 6);
277
+ left = this.node({ type: "UnaryExpression", operator, argument }, [argument]);
278
+ } else
279
+ left = this.primary(depth);
280
+ while (true) {
281
+ const start = this.position;
282
+ if (this.take("[")) {
283
+ const index = this.expression(depth + 1);
284
+ this.symbol("]");
285
+ left = this.call("LIST_EXTRACT", [left, index], true);
286
+ continue;
287
+ }
288
+ if (this.take(".")) {
289
+ left = this.memberAccess(left, this.readIdentifier(true));
290
+ continue;
291
+ }
292
+ if (this.take("::")) {
293
+ if (minimum > 7) {
294
+ this.position = start;
295
+ break;
296
+ }
297
+ left = this.cast(left);
298
+ continue;
299
+ }
300
+ if (minimum <= 3 && this.word("IS")) {
301
+ const negate = this.word("NOT");
302
+ if (!this.word("NULL"))
303
+ throw this.error("SQL IS requires NULL or NOT NULL");
304
+ left = this.call(negate ? "isNotNull" : "isNull", [left]);
305
+ continue;
306
+ }
307
+ if (minimum <= 3) {
308
+ const negate = this.word("NOT");
309
+ const insensitive = this.word("ILIKE");
310
+ if (insensitive || this.word("LIKE")) {
311
+ const pattern = this.expression(depth + 1, 4);
312
+ const args = [left, pattern];
313
+ if (this.word("ESCAPE"))
314
+ args.push(this.expression(depth + 1, 4));
315
+ left = this.call(insensitive ? "ilike" : "like", args);
316
+ if (negate)
317
+ left = this.node({ type: "UnaryExpression", operator: "!", argument: left }, [left]);
318
+ continue;
319
+ }
320
+ this.position = start;
321
+ }
322
+ this.space();
323
+ const match = /^(OR\b|AND\b|\|\||<>|!=|<=|>=|=|<|>|\+|-|\*|\/)/i.exec(this.source.slice(this.position));
324
+ if (!match)
325
+ break;
326
+ const token = match[0].toUpperCase();
327
+ const precedence = token === "OR" ? 1 : token === "AND" ? 2 : ["=", "<>", "!=", "<", ">", "<=", ">="].includes(token) ? 3 : token === "+" || token === "-" || token === "||" ? 4 : 5;
328
+ if (precedence < minimum)
329
+ break;
330
+ this.position += match[0].length;
331
+ this.token();
332
+ const right = this.expression(depth + 1, precedence + 1);
333
+ if (token === "||") {
334
+ left = this.call("concatStrict", [left, right]);
335
+ continue;
336
+ }
337
+ const operator = token === "OR" ? "||" : token === "AND" ? "&&" : token === "=" ? "==" : token === "<>" ? "!=" : token;
338
+ left = this.node({ type: "BinaryExpression", operator, left, right }, [left, right]);
339
+ }
340
+ return left;
341
+ }
342
+ call(name2, args, sqlFunction = false) {
343
+ return this.node({
344
+ type: "CallExpression",
345
+ callee: { type: "Identifier", name: name2 },
346
+ arguments: args,
347
+ ...sqlFunction ? { sqlFunction: true } : {}
348
+ }, args);
349
+ }
350
+ cast(argument) {
351
+ this.space();
352
+ const start = this.position;
353
+ const match = /^[A-Za-z][A-Za-z0-9]*(?:\s+(?:PRECISION|VARYING)\b)?/i.exec(this.source.slice(this.position));
354
+ if (!match)
355
+ throw this.error("SQL cast requires a target type");
356
+ this.position += match[0].length;
357
+ this.token();
358
+ if (this.take("[")) {
359
+ this.space();
360
+ const width = /^\d+/.exec(this.source.slice(this.position));
361
+ if (!width)
362
+ throw this.error("SQL ARRAY cast requires a fixed length");
363
+ this.position += width[0].length;
364
+ this.token();
365
+ this.symbol("]");
366
+ const array = argument;
367
+ if (array.type !== "CallExpression" || !/^array(?:_(?:float(?:16|32|64)|uint8))?$/.test(array.callee.name) || Number(width[0]) !== array.arguments.length)
368
+ throw this.error("ARRAY cast length must match the number of components");
369
+ const type = sqlCastType(match[0], this.source, start);
370
+ if (!["float16", "float32", "float64", "uint8"].includes(type))
371
+ throw this.error("Unsupported ARRAY cast type");
372
+ if (array.callee.name !== "array" && (type === "uint8" || array.callee.name === "array_uint8"))
373
+ throw this.error("Chained UINT8 array casts are not supported");
374
+ return this.call(`array_${type}`, array.arguments);
375
+ }
376
+ const name2 = match[0].toLowerCase();
377
+ let target;
378
+ if (name2 === "timestamp")
379
+ target = this.timestampCastType();
380
+ else if (name2 === "date" || name2 === "geometry")
381
+ target = name2;
382
+ else if (name2 === "dictionary")
383
+ target = this.dictionaryCastType();
384
+ else if (/^(?:binary|varbinary|blob|fixedsizebinary)$/.test(name2))
385
+ target = this.binaryCastType(name2);
386
+ else
387
+ target = sqlCastType(name2, this.source, start);
388
+ return this.call("cast", [
389
+ argument,
390
+ this.node({ type: "Literal", value: target, raw: JSON.stringify(target) })
391
+ ]);
392
+ }
393
+ /** Parses the optional SQL time zone qualifier. */
394
+ timestampCastType() {
395
+ const zoned = this.word("WITH");
396
+ if (!zoned && !this.word("WITHOUT"))
397
+ return "timestamp";
398
+ if (!this.word("TIME") || !this.word("ZONE"))
399
+ throw this.error("Expected TIME ZONE");
400
+ return zoned ? "timestamp_utc" : "timestamp";
401
+ }
402
+ /** Parses the supported Arrow dictionary layout. */
403
+ dictionaryCastType() {
404
+ this.symbol("(");
405
+ if (!this.word("UTF8"))
406
+ throw this.error("Expected UTF8 dictionary values");
407
+ this.symbol(",");
408
+ if (!this.word("INT32"))
409
+ throw this.error("Expected INT32 dictionary indices");
410
+ this.symbol(")");
411
+ return "dictionary_utf8_int32";
412
+ }
413
+ /** Parses binary aliases and optional positive byte widths. */
414
+ binaryCastType(name2) {
415
+ var _a;
416
+ if (name2 === "blob")
417
+ return "binary";
418
+ if (!this.take("(")) {
419
+ if (name2 === "fixedsizebinary")
420
+ throw this.error("FIXEDSIZEBINARY requires a byte width");
421
+ return "binary";
422
+ }
423
+ this.space();
424
+ const width = ((_a = /^\d+/.exec(this.source.slice(this.position))) == null ? void 0 : _a[0]) ?? "";
425
+ const byteWidth = Number(width);
426
+ if (byteWidth < 1 || byteWidth > 2147483647)
427
+ throw this.error("Binary CAST requires a positive 32-bit byte width");
428
+ this.position += width.length;
429
+ this.token();
430
+ this.symbol(")");
431
+ return `${name2 === "varbinary" ? "varbinary" : "fixedsizebinary"}:${byteWidth}`;
432
+ }
433
+ primary(depth) {
434
+ if (depth > 64)
435
+ throw this.error("SQL expression exceeds nesting limit");
436
+ if (this.take("(")) {
437
+ const value = this.expression(depth + 1);
438
+ this.symbol(")");
439
+ return value;
440
+ }
441
+ if (this.word("CAST")) {
442
+ this.symbol("(");
443
+ const argument = this.expression(depth + 1);
444
+ if (!this.word("AS"))
445
+ throw this.error("SQL CAST requires AS");
446
+ const result = this.cast(argument);
447
+ this.symbol(")");
448
+ return result;
449
+ }
450
+ if (this.word("ARRAY")) {
451
+ this.symbol("[");
452
+ const args = [];
453
+ do {
454
+ args.push(this.expression(depth + 1));
455
+ } while (this.take(","));
456
+ this.symbol("]");
457
+ return this.call("array", args);
458
+ }
459
+ if (this.word("CASE")) {
460
+ const clauses = [];
461
+ while (this.word("WHEN")) {
462
+ const test = this.expression(depth + 1);
463
+ if (!this.word("THEN"))
464
+ throw this.error("SQL CASE requires THEN");
465
+ clauses.push({ test, consequent: this.expression(depth + 1) });
466
+ }
467
+ if (!clauses.length)
468
+ throw this.error("SQL CASE requires WHEN");
469
+ let alternate = this.word("ELSE") ? this.expression(depth + 1) : this.node({ type: "Literal", value: null, raw: "null" });
470
+ if (!this.word("END"))
471
+ throw this.error("SQL CASE requires END");
472
+ for (let index = clauses.length - 1; index >= 0; index--) {
473
+ const { test, consequent } = clauses[index];
474
+ alternate = this.node({ type: "ConditionalExpression", test, consequent, alternate }, [
475
+ test,
476
+ consequent,
477
+ alternate
478
+ ]);
479
+ }
480
+ return alternate;
481
+ }
482
+ this.space();
483
+ const start = this.position;
484
+ const extended = this.extendedPrimary();
485
+ if (extended)
486
+ return extended;
487
+ const quote = this.source[this.position];
488
+ if (quote === '"' || quote === "'") {
489
+ const value = this.quoted(quote);
490
+ return quote === '"' ? this.identifier(value, depth, false) : this.node({ type: "Literal", value, raw: JSON.stringify(value) });
491
+ }
492
+ const number2 = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(this.source.slice(this.position));
493
+ if (number2) {
494
+ this.position += number2[0].length;
495
+ this.token();
496
+ const value = Number(number2[0]);
497
+ if (!Number.isFinite(value) || /^[+-]?\d+$/.test(number2[0]) && !Number.isSafeInteger(value))
498
+ throw this.error("SQL numeric literal must be finite and exactly representable; use a quoted BIGINT value", start);
499
+ return this.node({ type: "Literal", value, raw: number2[0] });
500
+ }
501
+ for (const [keyword, value] of [
502
+ ["NULL", null],
503
+ ["TRUE", true],
504
+ ["FALSE", false]
505
+ ])
506
+ if (this.word(keyword))
507
+ return this.node({ type: "Literal", value, raw: keyword.toLowerCase() });
508
+ return this.identifier(this.readIdentifier(), depth, true);
509
+ }
510
+ /** Reads an identifier in projections, source references, and field access. */
511
+ readIdentifier(member = false) {
512
+ var _a;
513
+ this.space();
514
+ if (this.source[this.position] === '"')
515
+ 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];
517
+ if (!identifier || !member && (SQL_RESERVED_WORDS.has(identifier.toUpperCase()) || /^(WHEN|THEN|ELSE|END)$/i.test(identifier)))
518
+ throw this.error("SQL expression requires a column, literal or function");
519
+ this.position += identifier.length;
520
+ this.token();
521
+ return identifier;
522
+ }
523
+ /** Decodes a quoted token, including doubled quote escapes. */
524
+ quoted(quote) {
525
+ const start = this.position++;
526
+ this.token();
527
+ let value = "";
528
+ while (this.position < this.source.length) {
529
+ const character = this.source[this.position++];
530
+ if (character !== quote)
531
+ value += character;
532
+ else if (this.source[this.position] === quote) {
533
+ value += quote;
534
+ this.position++;
535
+ } else {
536
+ if (quote === '"' && !value)
537
+ throw this.error("SQL identifiers must not be empty", start);
538
+ return value;
539
+ }
540
+ }
541
+ throw this.error("Unterminated SQL expression quote", start);
542
+ }
543
+ identifier(name2, depth, fold) {
544
+ if (!this.take("("))
545
+ return this.node({ type: "Identifier", name: name2 });
546
+ const args = [];
547
+ const wildcard = fold && this.wildcardArgument(name2);
548
+ if (wildcard) {
549
+ this.symbol(")");
550
+ return this.call(name2, [wildcard], true);
551
+ }
552
+ if (!this.take(")")) {
553
+ do {
554
+ args.push(this.expression(depth + 1));
555
+ } while (this.take(","));
556
+ this.symbol(")");
557
+ }
558
+ return this.call(name2, args, fold);
559
+ }
560
+ extendedPrimary() {
561
+ return void 0;
562
+ }
563
+ /** Lowers field access to a portable expression call for Arrow binding. */
564
+ memberAccess(object, name2) {
565
+ const key = this.node({ type: "Literal", value: name2, raw: JSON.stringify(name2) });
566
+ return this.call("STRUCT_EXTRACT", [object, key], true);
567
+ }
568
+ wildcardArgument(_name) {
569
+ return void 0;
570
+ }
571
+ token() {
572
+ if (++this.tokens > 4096)
573
+ throw this.error("SQL expression exceeds token limit");
574
+ }
575
+ };
576
+
577
+ // dist/scales/parse-sql-scale.js
578
+ function call(node, expected) {
579
+ var _a;
580
+ const candidate = node;
581
+ if (candidate.type !== "CallExpression" || ((_a = candidate.callee) == null ? void 0 : _a.type) !== "Identifier") {
582
+ throw new Error(expected ? `${expected} expects a function call` : "Expected a scale function");
583
+ }
584
+ if (expected && candidate.callee.name.toUpperCase() !== expected) {
585
+ throw new Error(`Expected ${expected}`);
586
+ }
587
+ return candidate;
588
+ }
589
+ function name(node) {
590
+ const candidate = node;
591
+ if (candidate.type !== "Identifier" || !candidate.name) {
592
+ throw new Error("Data-derived scale domain requires one column identifier");
593
+ }
594
+ return candidate.name;
595
+ }
596
+ function literal(node) {
597
+ const value = node;
598
+ if (value.type !== "Literal" || typeof value.value !== "string" && typeof value.value !== "number" || typeof value.value === "number" && !Number.isFinite(value.value)) {
599
+ throw new Error("Scale argument requires a literal number or quoted string");
600
+ }
601
+ return value.value;
602
+ }
603
+ function number(node) {
604
+ const unary = node;
605
+ if (unary.type === "UnaryExpression" && (unary.operator === "-" || unary.operator === "+") && unary.argument) {
606
+ const n = number(unary.argument);
607
+ return unary.operator === "-" ? -n : n;
608
+ }
609
+ const value = literal(node);
610
+ if (typeof value !== "number")
611
+ throw new Error("Expected numeric scale argument");
612
+ return value;
613
+ }
614
+ function color(hex) {
615
+ if (!/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex)) {
616
+ throw new Error("Explicit SQL color stops require #RRGGBB or #RRGGBBAA");
617
+ }
618
+ const bytes = hex.slice(1).match(/../g);
619
+ return [0, 1, 2, 3].map((index) => parseInt(bytes[index] ?? "ff", 16) / 255);
620
+ }
621
+ function parseDomain(node) {
622
+ const fn = call(node);
623
+ const functionName = fn.callee.name.toUpperCase();
624
+ const args = fn.arguments;
625
+ switch (functionName) {
626
+ case "DOMAIN":
627
+ if (args.length === 1)
628
+ return { type: "extent", column: name(args[0]) };
629
+ if (args.length >= 2)
630
+ return { type: "fixed", values: args.map(number) };
631
+ break;
632
+ case "DOMAIN_ZERO":
633
+ if (args.length === 1)
634
+ return { type: "extent", column: name(args[0]), includeZero: true };
635
+ break;
636
+ case "DOMAIN_SYMMETRIC":
637
+ if (args.length === 2)
638
+ return { type: "symmetric", column: name(args[0]), pivot: number(args[1]) };
639
+ break;
640
+ case "DOMAIN_QUANTILE":
641
+ if (args.length === 3) {
642
+ return {
643
+ type: "percentile",
644
+ column: name(args[0]),
645
+ lower: number(args[1]),
646
+ upper: number(args[2])
647
+ };
648
+ }
649
+ break;
650
+ }
651
+ throw new Error(`Invalid ${functionName} domain arguments`);
652
+ }
653
+ function parseRange(node) {
654
+ const fn = call(node);
655
+ const args = fn.arguments;
656
+ switch (fn.callee.name.toUpperCase()) {
657
+ case "RANGE":
658
+ if (args.length >= 2)
659
+ return { type: "numeric", values: args.map(number) };
660
+ break;
661
+ 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]);
664
+ if (typeof paletteName !== "string")
665
+ throw new Error("Palette name must be a string");
666
+ const count = args.length === 2 ? number(args[1]) : void 0;
667
+ return { type: "palette", name: paletteName, ...count === void 0 ? {} : { count } };
668
+ }
669
+ if (args.length >= 2 && args.every((arg) => typeof literal(arg) === "string")) {
670
+ return { type: "colors", values: args.map((arg) => color(literal(arg))) };
671
+ }
672
+ break;
673
+ }
674
+ }
675
+ throw new Error("Expected RANGE(numbers...) or PALETTE(name[, count] | color stops...)");
676
+ }
677
+ function parseSQLScaleExpression(sql) {
678
+ const root = call(parseSQLExpression(sql));
679
+ const fn = root.callee.name.toUpperCase();
680
+ const args = root.arguments;
681
+ const names = {
682
+ SCALE_LINEAR: "linear",
683
+ SCALE_SQRT: "sqrt",
684
+ SCALE_LOG: "log",
685
+ SCALE_COLOR: "sequential",
686
+ SCALE_DIVERGING: "diverging",
687
+ SCALE_ORDINAL: "ordinal",
688
+ SCALE_QUANTILE: "quantile",
689
+ SCALE_QUANTIZE: "quantize"
690
+ };
691
+ const type = names[fn];
692
+ if (!type)
693
+ 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`);
696
+ }
697
+ 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]);
700
+ if (fn === "SCALE_QUANTILE" || fn === "SCALE_QUANTIZE") {
701
+ const count = number(args[1]);
702
+ if (!Number.isSafeInteger(count) || count < 2) {
703
+ throw new Error(`${fn} class count must be an integer >= 2`);
704
+ }
705
+ if (range.type !== "palette")
706
+ throw new Error(`${fn} requires a named palette`);
707
+ range = { ...range, count };
708
+ }
709
+ const colors = ["sequential", "diverging", "ordinal", "quantile", "quantize"].includes(type);
710
+ if (colors && range.type === "numeric")
711
+ throw new Error(`${fn} requires a color range`);
712
+ if (!colors && range.type !== "numeric")
713
+ throw new Error(`${fn} requires a numeric RANGE`);
714
+ if (domain.type === "fixed" && range.type === "colors" && domain.values.length !== range.values.length) {
715
+ throw new Error("Explicit domain and color stops must have equal lengths");
716
+ }
717
+ const scale = validateScaleSpec({ type, domain, range });
718
+ return { value, scale };
719
+ }
720
+
721
+ // 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
+ ];
730
+ };
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
+ ]
761
+ };
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
+ ];
769
+ }
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
+ });
793
+ }
794
+ function listScalePalettes() {
795
+ return Object.keys(PALETTES);
796
+ }
797
+
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
+ ]
868
+ };
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);
942
+ }
943
+
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
+ ];
957
+ }
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
+ ];
967
+ }
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;
1111
+ };
1112
+ return createResolvedScale(descriptor, sample);
1113
+ }
1114
+
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)
1159
+ 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);
1190
+ }
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 });
1200
+ continue;
1201
+ }
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 });
1207
+ }
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
+ }
1212
+ }
1213
+ };
1214
+ //# sourceMappingURL=index.cjs.map