@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
@@ -2,125 +2,4 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
- import type {ScaleColor, ScaleColorEncoding} from './scale-types';
6
-
7
- type Primaries = ScaleColorEncoding['primaries'];
8
- type Transfer = ScaleColorEncoding['transfer'];
9
-
10
- /** D65 linear-RGB to XYZ (CIE 1931) matrices. No output gamut clipping. */
11
- const TO_XYZ: Record<Primaries, readonly number[]> = {
12
- srgb: [
13
- 0.4124564, 0.3575761, 0.1804375, 0.2126729, 0.7151522, 0.072175, 0.0193339, 0.119192, 0.9503041
14
- ],
15
- 'display-p3': [
16
- 0.48657095, 0.26566769, 0.19821729, 0.22897456, 0.69173852, 0.07928691, 0, 0.04511338,
17
- 1.04394437
18
- ],
19
- rec2020: [
20
- 0.63695805, 0.1446169, 0.16888098, 0.26270021, 0.67799807, 0.05930172, 0, 0.02807269, 1.06098506
21
- ]
22
- };
23
- const FROM_XYZ: Record<Primaries, readonly number[]> = {
24
- srgb: [
25
- 3.2404542, -1.5371385, -0.4985314, -0.969266, 1.8760108, 0.041556, 0.0556434, -0.2040259,
26
- 1.0572252
27
- ],
28
- 'display-p3': [
29
- 2.49349691, -0.93138362, -0.40271078, -0.82948897, 1.76266406, 0.02362469, 0.03584583,
30
- -0.07617239, 0.95688452
31
- ],
32
- rec2020: [
33
- 1.71665119, -0.35567078, -0.25336628, -0.66668435, 1.61648124, 0.01576855, 0.01763986,
34
- -0.04277061, 0.94210312
35
- ]
36
- };
37
-
38
- function transform(m: readonly number[], [r, g, b]: readonly number[]): [number, number, number] {
39
- return [
40
- m[0]! * r + m[1]! * g + m[2]! * b,
41
- m[3]! * r + m[4]! * g + m[5]! * b,
42
- m[6]! * r + m[7]! * g + m[8]! * b
43
- ];
44
- }
45
-
46
- /** Signed extension of the sRGB transfer curve, preserving extended-range RGB values. */
47
- export function decodeSRGB(v: number): number {
48
- const x = Math.abs(v);
49
- return Math.sign(v) * (x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4);
50
- }
51
- export function encodeSRGB(v: number): number {
52
- const x = Math.abs(v);
53
- return Math.sign(v) * (x <= 0.0031308 ? 12.92 * x : 1.055 * x ** (1 / 2.4) - 0.055);
54
- }
55
-
56
- function decode(v: number, transfer: Transfer): number {
57
- if (transfer === 'linear') return v;
58
- if (transfer === 'srgb') return decodeSRGB(v);
59
- throw new Error(`Transfer ${transfer} requires an explicit HDR display-referred conversion`);
60
- }
61
- function encode(v: number, transfer: Transfer): number {
62
- if (transfer === 'linear') return v;
63
- if (transfer === 'srgb') return encodeSRGB(v);
64
- throw new Error(`Transfer ${transfer} requires an explicit HDR display-referred conversion`);
65
- }
66
-
67
- /** Converts RGB primaries and sRGB/linear transfer without tone mapping or clipping. */
68
- export function convertScaleColor(
69
- color: ScaleColor,
70
- from: ScaleColorEncoding,
71
- to: ScaleColorEncoding
72
- ): ScaleColor {
73
- if (color.some(channel => !Number.isFinite(channel))) {
74
- throw new Error('Color channels must be finite');
75
- }
76
- if (
77
- from.transfer === 'pq' ||
78
- from.transfer === 'hlg' ||
79
- to.transfer === 'pq' ||
80
- to.transfer === 'hlg'
81
- ) {
82
- throw new Error('PQ/HLG conversions require explicit luminance and display assumptions');
83
- }
84
- const linear = color.slice(0, 3).map(v => decode(v, from.transfer));
85
- const converted =
86
- from.primaries === to.primaries
87
- ? linear
88
- : transform(FROM_XYZ[to.primaries], transform(TO_XYZ[from.primaries], linear));
89
- return [
90
- encode(converted[0]!, to.transfer),
91
- encode(converted[1]!, to.transfer),
92
- encode(converted[2]!, to.transfer),
93
- color[3]
94
- ];
95
- }
96
-
97
- /** IEEE-754 binary16 packing with round-to-nearest, ties-to-even. */
98
- export function packFloat16(value: number): number {
99
- const sign = value < 0 || Object.is(value, -0) ? 0x8000 : 0;
100
- const magnitude = Math.abs(value);
101
- if (Number.isNaN(magnitude)) return 0x7e00;
102
- if (magnitude >= 65520) return sign | 0x7c00;
103
- if (magnitude <= 2 ** -25) return sign;
104
-
105
- const exponent = Math.max(-14, Math.floor(Math.log2(magnitude)));
106
- const scaled = magnitude * 2 ** (10 - exponent);
107
- const lower = Math.floor(scaled);
108
- const fraction = scaled - lower;
109
- const significand = lower + (fraction > 0.5 || (fraction === 0.5 && lower % 2) ? 1 : 0);
110
- return sign | (((exponent + 14) << 10) + significand);
111
- }
112
-
113
- /** IEEE-754 binary16 decode into a JavaScript number. */
114
- export function unpackFloat16(bits: number): number {
115
- const sign = bits & 0x8000 ? -1 : 1;
116
- const exp = (bits >>> 10) & 31;
117
- const mantissa = bits & 1023;
118
- if (exp === 0) return sign * mantissa * 2 ** -24;
119
- if (exp === 31) return mantissa ? NaN : sign * Infinity;
120
- return sign * (1 + mantissa / 1024) * 2 ** (exp - 15);
121
- }
122
-
123
- /** Float16 storage words for one unpremultiplied scene-linear RGBA color. */
124
- export function packRGBA16F(color: ScaleColor): Uint16Array {
125
- return Uint16Array.from(color, packFloat16);
126
- }
5
+ export * from '@math.gl/core/color';
@@ -17,7 +17,7 @@ export type {
17
17
  ResolvedScale
18
18
  } from './scale-types';
19
19
 
20
- export {parseSQLScaleExpression} from './parse-sql-scale';
20
+ export {parseSQLScaleExpression, parseSQLScaleAST} from './parse-sql-scale';
21
21
  export type {ParsedSQLScale} from './parse-sql-scale';
22
22
 
23
23
  export {compileResolvedScale} from './compile-scale';
@@ -33,3 +33,5 @@ export {
33
33
  unpackFloat16,
34
34
  packRGBA16F
35
35
  } from './hdr-colors';
36
+
37
+ export * from './arrow-scales';
@@ -2,85 +2,4 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
- import type {ScaleColor, ScaleRange} from './scale-types';
6
-
7
- const rgba = (hex: string): ScaleColor => {
8
- const hexValue = hex.slice(1);
9
- return [
10
- parseInt(hexValue.slice(0, 2), 16) / 255,
11
- parseInt(hexValue.slice(2, 4), 16) / 255,
12
- parseInt(hexValue.slice(4, 6), 16) / 255,
13
- 1
14
- ];
15
- };
16
-
17
- /**
18
- * Compact built-in palettes. Stops are representative of established map palettes;
19
- * consumers may instead provide exact explicit color stops.
20
- * No D3 runtime dependency is required.
21
- */
22
- const PALETTES: Record<string, readonly string[]> = {
23
- viridis: ['#440154', '#414487', '#2a788e', '#22a884', '#7ad151', '#fde725'],
24
- plasma: ['#0d0887', '#7e03a8', '#cc4778', '#f89540', '#f0f921'],
25
- inferno: ['#000004', '#420a68', '#932667', '#dd513a', '#fca50a', '#fcffa4'],
26
- cividis: ['#00204c', '#414d6b', '#7a7b78', '#bcae6c', '#fee838'],
27
- blues: ['#eff3ff', '#bdd7e7', '#6baed6', '#3182bd', '#08519c'],
28
- rdbu: [
29
- '#b2182b',
30
- '#d6604d',
31
- '#f4a582',
32
- '#fddbc7',
33
- '#f7f7f7',
34
- '#d1e5f0',
35
- '#92c5de',
36
- '#4393c3',
37
- '#2166ac'
38
- ],
39
- ylorrd: ['#ffffb2', '#fecc5c', '#fd8d3c', '#f03b20', '#bd0026'],
40
- tableau10: [
41
- '#4e79a7',
42
- '#f28e2b',
43
- '#e15759',
44
- '#76b7b2',
45
- '#59a14f',
46
- '#edc948',
47
- '#b07aa1',
48
- '#ff9da7',
49
- '#9c755f',
50
- '#bab0ab'
51
- ]
52
- };
53
-
54
- function interpolate(a: ScaleColor, b: ScaleColor, t: number): ScaleColor {
55
- return [
56
- a[0] + (b[0] - a[0]) * t,
57
- a[1] + (b[1] - a[1]) * t,
58
- a[2] + (b[2] - a[2]) * t,
59
- a[3] + (b[3] - a[3]) * t
60
- ];
61
- }
62
-
63
- /** Obtain normalized color stops without clamping HDR explicit ranges. */
64
- export function resolvePalette(range: ScaleRange, count?: number): readonly ScaleColor[] {
65
- if (range.type === 'numeric') throw new Error('A numeric range is not a color palette');
66
- if (range.type === 'colors') return range.values;
67
- const key = range.name.toLowerCase();
68
- const colors = Object.prototype.hasOwnProperty.call(PALETTES, key) ? PALETTES[key] : undefined;
69
- if (!colors) throw new Error(`Unknown color palette: ${range.name}`);
70
- const target = count ?? range.count;
71
- if (target === undefined) return colors.map(rgba);
72
- if (!Number.isSafeInteger(target) || target < 2 || target > 256) {
73
- throw new Error('Palette count must be between 2 and 256');
74
- }
75
- if (target === colors.length) return colors.map(rgba);
76
- const decoded = colors.map(rgba);
77
- return Array.from({length: target}, (_, i) => {
78
- const position = (i * (decoded.length - 1)) / (target - 1);
79
- const index = Math.min(Math.floor(position), decoded.length - 2);
80
- return interpolate(decoded[index]!, decoded[index + 1]!, position - index);
81
- });
82
- }
83
-
84
- export function listScalePalettes(): string[] {
85
- return Object.keys(PALETTES);
86
- }
5
+ export * from '@math.gl/core/color';
@@ -2,8 +2,9 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
- import type {Expression} from '../expressions/index';
6
- import {parseSQLExpression} from '../sql/parse-sql-expression';
5
+ import type {SQLParameterOptions} from '../parser/sql-parameters';
6
+ import type {Expression} from '../expressions';
7
+ import {parseSQLExpression} from '../parser/parse-sql-expression';
7
8
  import {
8
9
  validateScaleSpec,
9
10
  type ScaleColor,
@@ -19,6 +20,7 @@ type Call = {
19
20
  arguments: Expression[];
20
21
  };
21
22
 
23
+ /** A row expression and its validated, unresolved visualization scale. */
22
24
  export type ParsedSQLScale = Readonly<{
23
25
  /** Row-preserving value expression, evaluated by an Arrow/GPU backend. */
24
26
  value: Expression;
@@ -45,6 +47,13 @@ function name(node: Expression): string {
45
47
  return candidate.name;
46
48
  }
47
49
 
50
+ /** Uses an existing column directly or records a computed numeric domain. */
51
+ function quantileDomain(value: Expression): ScaleDomain {
52
+ return value.type === 'Identifier'
53
+ ? {type: 'extent', column: name(value)}
54
+ : {type: 'expression-extent', expression: value};
55
+ }
56
+
48
57
  function literal(node: Expression): string | number {
49
58
  const value = node as {type: string; value?: unknown};
50
59
  if (
@@ -86,10 +95,11 @@ function color(hex: string): ScaleColor {
86
95
  function parseDomain(node: Expression): ScaleDomain {
87
96
  const fn = call(node);
88
97
  const functionName = fn.callee.name.toUpperCase();
89
- const args = fn.arguments;
98
+ const args = arrayArguments(fn.arguments);
90
99
  switch (functionName) {
91
100
  case 'DOMAIN':
92
- if (args.length === 1) return {type: 'extent', column: name(args[0]!)};
101
+ if (args.length === 1 && args[0]!.type === 'Identifier')
102
+ return {type: 'extent', column: name(args[0]!)};
93
103
  if (args.length >= 2) return {type: 'fixed', values: args.map(number)};
94
104
  break;
95
105
  case 'DOMAIN_ZERO':
@@ -113,14 +123,60 @@ function parseDomain(node: Expression): ScaleDomain {
113
123
  throw new Error(`Invalid ${functionName} domain arguments`);
114
124
  }
115
125
 
126
+ /** Parses an ordered list of unique fixed category keys. */
127
+ function parseCategories(node: Expression): ScaleDomain {
128
+ const values = arrayArguments(call(node, 'CATEGORIES').arguments).map(argument =>
129
+ argument.type === 'Literal' ? literal(argument) : number(argument)
130
+ );
131
+ if (!values.length || new Set(values).size !== values.length) {
132
+ throw new Error('CATEGORIES requires at least one category and no duplicates');
133
+ }
134
+ return {type: 'fixed', values};
135
+ }
136
+
137
+ /** Accepts a single bound list wherever a scale expects an explicit list of stops. */
138
+ function arrayArguments(args: Expression[]): Expression[] {
139
+ return args.length === 1 && args[0]!.type === 'ArrayExpression'
140
+ ? (args[0]!.elements as Expression[])
141
+ : args;
142
+ }
143
+
144
+ /** Converts explicit hex colors or normalized numeric RGB/RGBA tuples. */
145
+ function colorStop(node: Expression): ScaleColor {
146
+ if (node.type !== 'ArrayExpression') {
147
+ const value = literal(node);
148
+ if (typeof value !== 'string')
149
+ throw new Error('Palette colors require hex strings or RGB/RGBA tuples');
150
+ return color(value);
151
+ }
152
+ if (node.elements.length !== 3 && node.elements.length !== 4) {
153
+ throw new Error('Palette colors require RGB or RGBA tuples');
154
+ }
155
+ const components = (node.elements as Expression[]).map(number);
156
+ return [components[0]!, components[1]!, components[2]!, components[3] ?? 1];
157
+ }
158
+
116
159
  function parseRange(node: Expression): ScaleRange {
117
160
  const fn = call(node);
118
- const args = fn.arguments;
161
+ const args = arrayArguments(fn.arguments);
162
+ if (
163
+ fn.callee.name.toUpperCase() === 'PALETTE' &&
164
+ fn.arguments.length === 1 &&
165
+ fn.arguments[0]?.type === 'ArrayExpression'
166
+ ) {
167
+ return {type: 'colors', values: args.map(colorStop)};
168
+ }
119
169
  switch (fn.callee.name.toUpperCase()) {
120
170
  case 'RANGE':
121
171
  if (args.length >= 2) return {type: 'numeric', values: args.map(number)};
122
172
  break;
123
173
  case 'PALETTE': {
174
+ if (args.length === 1) {
175
+ const stop = literal(args[0]!);
176
+ if (typeof stop === 'string' && stop.startsWith('#')) {
177
+ return {type: 'colors', values: [color(stop)]};
178
+ }
179
+ }
124
180
  if (
125
181
  args.length === 1 ||
126
182
  (args.length === 2 &&
@@ -145,8 +201,25 @@ function parseRange(node: Expression): ScaleRange {
145
201
  * Parse one scale expression into a row-value AST and a JSON-safe scale spec.
146
202
  * This does not evaluate domain statistics, produce colors, or modify row count.
147
203
  */
148
- export function parseSQLScaleExpression(sql: string): ParsedSQLScale {
149
- const root = call(parseSQLExpression(sql));
204
+ export function parseSQLScaleExpression(
205
+ sql: string,
206
+ options: SQLParameterOptions = {}
207
+ ): ParsedSQLScale {
208
+ return parseSQLScaleAST(parseSQLExpression(sql, options));
209
+ }
210
+
211
+ /**
212
+ * Extracts a scale from a parsed SQL expression without reparsing or evaluating it.
213
+ *
214
+ * Pass a scale call from `parseSQLExpression` or a SELECT projection. The returned
215
+ * value retains the input row-expression identity; this function does not mutate
216
+ * the AST, resolve domain statistics, or compile backend-specific code.
217
+ * @param expression - A scale call in the public portable expression AST.
218
+ * @returns The row-value expression and validated scale specification.
219
+ * @throws If the root is not a supported scale call or its scale arguments are invalid.
220
+ */
221
+ export function parseSQLScaleAST(expression: Expression): ParsedSQLScale {
222
+ const root = call(expression);
150
223
  const fn = root.callee.name.toUpperCase();
151
224
  const args = root.arguments;
152
225
  const names: Record<string, ScaleSpec['type']> = {
@@ -157,33 +230,65 @@ export function parseSQLScaleExpression(sql: string): ParsedSQLScale {
157
230
  SCALE_DIVERGING: 'diverging',
158
231
  SCALE_ORDINAL: 'ordinal',
159
232
  SCALE_QUANTILE: 'quantile',
160
- SCALE_QUANTIZE: 'quantize'
233
+ SCALE_QUANTIZE: 'quantize',
234
+ SCALE_THRESHOLD: 'threshold'
161
235
  };
162
236
  const type = names[fn];
163
237
  if (!type) throw new Error(`Unsupported SQL scale function: ${fn}`);
164
- if (args.length !== (fn === 'SCALE_ORDINAL' ? 2 : 3)) {
165
- throw new Error(`${fn} expects ${fn === 'SCALE_ORDINAL' ? 'two' : 'three'} arguments`);
238
+ const inferredOrdinal = fn === 'SCALE_ORDINAL' && args.length === 2;
239
+ if (!inferredOrdinal && args.length !== 3) {
240
+ throw new Error(`${fn} expects ${fn === 'SCALE_ORDINAL' ? 'two or three' : 'three'} arguments`);
166
241
  }
167
242
  const value = args[0]!;
168
- const domain =
169
- fn === 'SCALE_ORDINAL'
170
- ? {type: 'categories' as const, column: name(value)}
171
- : fn === 'SCALE_QUANTILE' || fn === 'SCALE_QUANTIZE'
172
- ? {type: 'extent' as const, column: name(value)}
173
- : parseDomain(args[1]!);
174
- let range = parseRange(args[fn === 'SCALE_ORDINAL' ? 1 : 2]!);
243
+ const domain = inferredOrdinal
244
+ ? {type: 'categories' as const, column: name(value)}
245
+ : fn === 'SCALE_ORDINAL'
246
+ ? parseCategories(args[1]!)
247
+ : fn === 'SCALE_QUANTILE'
248
+ ? quantileDomain(value)
249
+ : fn === 'SCALE_QUANTIZE'
250
+ ? {type: 'extent' as const, column: name(value)}
251
+ : fn === 'SCALE_THRESHOLD'
252
+ ? {type: 'fixed' as const, values: call(args[1]!, 'ARRAY').arguments.map(number)}
253
+ : parseDomain(args[1]!);
254
+ let range = parseRange(args[inferredOrdinal ? 1 : 2]!);
175
255
  if (fn === 'SCALE_QUANTILE' || fn === 'SCALE_QUANTIZE') {
176
256
  const count = number(args[1]!);
177
257
  if (!Number.isSafeInteger(count) || count < 2) {
178
258
  throw new Error(`${fn} class count must be an integer >= 2`);
179
259
  }
180
- if (range.type !== 'palette') throw new Error(`${fn} requires a named palette`);
181
- range = {...range, count};
260
+ if (range.type === 'palette') {
261
+ range = {...range, count};
262
+ } else if (fn !== 'SCALE_QUANTILE' || range.type !== 'colors') {
263
+ throw new Error(`${fn} requires a named palette`);
264
+ } else if (range.values.length !== count) {
265
+ throw new Error('SCALE_QUANTILE requires one color per class');
266
+ }
182
267
  }
183
- const colors = ['sequential', 'diverging', 'ordinal', 'quantile', 'quantize'].includes(type);
268
+ const colors = [
269
+ 'sequential',
270
+ 'diverging',
271
+ 'ordinal',
272
+ 'quantile',
273
+ 'quantize',
274
+ 'threshold'
275
+ ].includes(type);
184
276
  if (colors && range.type === 'numeric') throw new Error(`${fn} requires a color range`);
185
277
  if (!colors && range.type !== 'numeric') throw new Error(`${fn} requires a numeric RANGE`);
186
- if (
278
+ if (type === 'threshold' && domain.type === 'fixed') {
279
+ const breaks = domain.values as readonly number[];
280
+ if (!breaks.length || breaks.some((value, index) => index > 0 && value <= breaks[index - 1]!)) {
281
+ throw new Error('SCALE_THRESHOLD breaks must be nonempty and strictly increasing');
282
+ }
283
+ const count = breaks.length + 1;
284
+ if (range.type === 'palette' && range.count === undefined) range = {...range, count};
285
+ if (
286
+ (range.type === 'colors' && range.values.length !== count) ||
287
+ (range.type === 'palette' && range.count !== count)
288
+ ) {
289
+ throw new Error('SCALE_THRESHOLD requires one more color than breaks');
290
+ }
291
+ } else if (
187
292
  domain.type === 'fixed' &&
188
293
  range.type === 'colors' &&
189
294
  domain.values.length !== range.values.length
@@ -2,105 +2,4 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
- /**
6
- * Deterministic, bounded-memory weighted compactor for approximate quantiles.
7
- * Compaction preserves total weight and varies the retained parity to avoid
8
- * persistent low/high bias. This is a practical sketch, not an exact error-bounded GK estimate.
9
- */
10
- export class ScaleQuantileSketch {
11
- readonly capacity: number;
12
- private samples: {value: number; weight: number}[] = [];
13
- private parity = 0;
14
- private count = 0;
15
- private min = Infinity;
16
- private max = -Infinity;
17
-
18
- constructor(capacity = 256) {
19
- if (!Number.isSafeInteger(capacity) || capacity < 16 || capacity > 65536) {
20
- throw new Error('Quantile sketch capacity must be an integer between 16 and 65536');
21
- }
22
- this.capacity = capacity;
23
- }
24
-
25
- get length(): number {
26
- return this.count;
27
- }
28
- get storedSamples(): number {
29
- return this.samples.length;
30
- }
31
-
32
- add(value: number): void {
33
- if (!Number.isFinite(value)) throw new Error('Quantile sketch requires finite values');
34
- this.samples.push({value, weight: 1});
35
- this.count++;
36
- this.min = Math.min(this.min, value);
37
- this.max = Math.max(this.max, value);
38
- if (this.samples.length >= this.capacity * 2) this.reduce();
39
- }
40
-
41
- merge(other: ScaleQuantileSketch): void {
42
- if (other === this) throw new Error('Cannot merge a quantile sketch with itself');
43
- for (const item of other.samples) this.samples.push({...item});
44
- this.count += other.count;
45
- this.min = Math.min(this.min, other.min);
46
- this.max = Math.max(this.max, other.max);
47
- this.reduce();
48
- }
49
-
50
- quantile(probability: number): number | null {
51
- if (!Number.isFinite(probability) || probability < 0 || probability > 1) {
52
- throw new Error('Quantile probability must be in [0, 1]');
53
- }
54
- if (!this.count) return null;
55
- const sorted = [...this.samples].sort((a, b) => a.value - b.value);
56
- if (probability === 0) return this.min;
57
- if (probability === 1) return this.max;
58
- const index = probability * (this.count - 1);
59
- const lowerRank = Math.floor(index);
60
- const upperRank = Math.ceil(index);
61
- const fraction = index - lowerRank;
62
- let cumulative = 0;
63
- let lower: number | undefined;
64
- for (const sample of sorted) {
65
- cumulative += sample.weight;
66
- if (lowerRank < cumulative) {
67
- lower ??= sample.value;
68
- if (upperRank < cumulative) return lower * (1 - fraction) + sample.value * fraction;
69
- }
70
- }
71
- return this.max;
72
- }
73
-
74
- /** Compact comparable weights first so recent rows do not absorb older heavy samples. */
75
- private reduce(): void {
76
- while (this.samples.length > this.capacity) {
77
- const levels = new Map<number, {value: number; weight: number}[]>();
78
- for (const sample of this.samples) {
79
- let level = levels.get(sample.weight);
80
- if (!level) {
81
- level = [];
82
- levels.set(sample.weight, level);
83
- }
84
- level.push(sample);
85
- }
86
- const weights = [...levels.keys()].sort((a, b) => a - b);
87
- const weight = weights.find(candidate => levels.get(candidate)!.length > 1);
88
- if (weight === undefined) {
89
- // Fall back to coalescing the two lightest samples if all weights differ.
90
- const a = levels.get(weights[0]!)!.pop()!;
91
- const b = levels.get(weights[1]!)!.pop()!;
92
- this.samples = this.samples.filter(item => item !== a && item !== b);
93
- this.samples.push({value: b.value, weight: a.weight + b.weight});
94
- continue;
95
- }
96
- const level = levels.get(weight)!.sort((a, b) => a.value - b.value);
97
- const offset = this.parity++ % 2;
98
- const compacted = [];
99
- for (let i = 0; i + 1 < level.length; i += 2) {
100
- compacted.push({value: level[i + offset]!.value, weight: weight * 2});
101
- }
102
- if (level.length % 2) compacted.push(level[level.length - 1]!);
103
- this.samples = this.samples.filter(sample => sample.weight !== weight).concat(compacted);
104
- }
105
- }
106
- }
5
+ export * from '@math.gl/core/color';