@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.
- package/LICENSE +249 -0
- package/README.md +26 -2
- package/THIRD-PARTY-NOTICES.md +5 -0
- package/dist/arrow/aggregates.d.ts +46 -0
- package/dist/arrow/aggregates.d.ts.map +1 -0
- package/dist/arrow/aggregates.js +289 -0
- package/dist/arrow/aggregates.js.map +1 -0
- package/dist/arrow/buffers.d.ts +30 -0
- package/dist/arrow/buffers.d.ts.map +1 -0
- package/dist/arrow/buffers.js +177 -0
- package/dist/arrow/buffers.js.map +1 -0
- package/dist/arrow/cast.d.ts +19 -0
- package/dist/arrow/cast.d.ts.map +1 -0
- package/dist/arrow/cast.js +138 -0
- package/dist/arrow/cast.js.map +1 -0
- package/dist/arrow/compiler.d.ts +10 -0
- package/dist/arrow/compiler.d.ts.map +1 -0
- package/dist/arrow/compiler.js +1421 -0
- package/dist/arrow/compiler.js.map +1 -0
- package/dist/arrow/float16.d.ts +7 -0
- package/dist/arrow/float16.d.ts.map +1 -0
- package/dist/arrow/float16.js +26 -0
- package/dist/arrow/float16.js.map +1 -0
- package/dist/arrow/index.cjs +3220 -0
- package/dist/arrow/index.cjs.map +6 -0
- package/dist/arrow/index.d.ts +5 -0
- package/dist/arrow/index.d.ts.map +1 -0
- package/dist/arrow/index.js +7 -0
- package/dist/arrow/index.js.map +1 -0
- package/dist/arrow/like.d.ts +3 -0
- package/dist/arrow/like.d.ts.map +1 -0
- package/dist/arrow/like.js +75 -0
- package/dist/arrow/like.js.map +1 -0
- package/dist/arrow/list-values.d.ts +14 -0
- package/dist/arrow/list-values.d.ts.map +1 -0
- package/dist/arrow/list-values.js +180 -0
- package/dist/arrow/list-values.js.map +1 -0
- package/dist/arrow/predicate.d.ts +7 -0
- package/dist/arrow/predicate.d.ts.map +1 -0
- package/dist/arrow/predicate.js +10 -0
- package/dist/arrow/predicate.js.map +1 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.d.ts +24 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.d.ts.map +1 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.js +64 -0
- package/dist/arrow/query-arrow-aggregate-snapshots.js.map +1 -0
- package/dist/arrow/query-arrow-sql.d.ts +8 -0
- package/dist/arrow/query-arrow-sql.d.ts.map +1 -0
- package/dist/arrow/query-arrow-sql.js +191 -0
- package/dist/arrow/query-arrow-sql.js.map +1 -0
- package/dist/arrow/query-arrow-table.d.ts +36 -0
- package/dist/arrow/query-arrow-table.d.ts.map +1 -0
- package/dist/arrow/query-arrow-table.js +208 -0
- package/dist/arrow/query-arrow-table.js.map +1 -0
- package/dist/arrow/query-worker-entry.d.ts +2 -0
- package/dist/arrow/query-worker-entry.d.ts.map +1 -0
- package/dist/arrow/query-worker-entry.js +7 -0
- package/dist/arrow/query-worker-entry.js.map +1 -0
- package/dist/arrow/query-worker.d.ts +65 -0
- package/dist/arrow/query-worker.d.ts.map +1 -0
- package/dist/arrow/query-worker.js +135 -0
- package/dist/arrow/query-worker.js.map +1 -0
- package/dist/arrow/registry.d.ts +4 -0
- package/dist/arrow/registry.d.ts.map +1 -0
- package/dist/arrow/registry.js +65 -0
- package/dist/arrow/registry.js.map +1 -0
- package/dist/arrow/relational-query.d.ts +36 -0
- package/dist/arrow/relational-query.d.ts.map +1 -0
- package/dist/arrow/relational-query.js +174 -0
- package/dist/arrow/relational-query.js.map +1 -0
- package/dist/arrow/replace-arrow-columns.d.ts +8 -0
- package/dist/arrow/replace-arrow-columns.d.ts.map +1 -0
- package/dist/arrow/replace-arrow-columns.js +63 -0
- package/dist/arrow/replace-arrow-columns.js.map +1 -0
- package/dist/arrow/signature.d.ts +6 -0
- package/dist/arrow/signature.d.ts.map +1 -0
- package/dist/arrow/signature.js +51 -0
- package/dist/arrow/signature.js.map +1 -0
- package/dist/arrow/temporal-cast.d.ts +9 -0
- package/dist/arrow/temporal-cast.d.ts.map +1 -0
- package/dist/arrow/temporal-cast.js +60 -0
- package/dist/arrow/temporal-cast.js.map +1 -0
- package/dist/arrow/types.d.ts +62 -0
- package/dist/arrow/types.d.ts.map +1 -0
- package/dist/arrow/types.js +5 -0
- package/dist/arrow/types.js.map +1 -0
- package/dist/arrow/worker-entry.d.ts +2 -0
- package/dist/arrow/worker-entry.d.ts.map +1 -0
- package/dist/arrow/worker-entry.js +9 -0
- package/dist/arrow/worker-entry.js.map +1 -0
- package/dist/arrow/worker.cjs +3973 -0
- package/dist/arrow/worker.cjs.map +6 -0
- package/dist/arrow/worker.d.ts +58 -0
- package/dist/arrow/worker.d.ts.map +1 -0
- package/dist/arrow/worker.js +84 -0
- package/dist/arrow/worker.js.map +1 -0
- package/dist/color/index.cjs +382 -0
- package/dist/color/index.cjs.map +6 -0
- package/dist/color/index.d.ts +26 -0
- package/dist/color/index.d.ts.map +1 -0
- package/dist/color/index.js +306 -0
- package/dist/color/index.js.map +1 -0
- package/dist/diagnostic-log.d.ts +8 -0
- package/dist/diagnostic-log.d.ts.map +1 -0
- package/dist/diagnostic-log.js +15 -0
- package/dist/diagnostic-log.js.map +1 -0
- package/dist/expressions/ast.d.ts +12 -0
- package/dist/expressions/ast.d.ts.map +1 -0
- package/dist/expressions/ast.js +112 -0
- package/dist/expressions/ast.js.map +1 -0
- package/dist/expressions/columnar-predicate.d.ts +56 -0
- package/dist/expressions/columnar-predicate.d.ts.map +1 -0
- package/dist/expressions/columnar-predicate.js +176 -0
- package/dist/expressions/columnar-predicate.js.map +1 -0
- package/dist/expressions/evaluation-error.d.ts +14 -0
- package/dist/expressions/evaluation-error.d.ts.map +1 -0
- package/dist/expressions/evaluation-error.js +26 -0
- package/dist/expressions/evaluation-error.js.map +1 -0
- package/dist/expressions/expression-eval.d.ts +96 -0
- package/dist/expressions/expression-eval.d.ts.map +1 -0
- package/dist/expressions/expression-eval.js +330 -0
- package/dist/expressions/expression-eval.js.map +1 -0
- package/dist/expressions/function-libraries.d.ts +63 -0
- package/dist/expressions/function-libraries.d.ts.map +1 -0
- package/dist/expressions/function-libraries.js +69 -0
- package/dist/expressions/function-libraries.js.map +1 -0
- package/dist/expressions/function-registry.d.ts +93 -0
- package/dist/expressions/function-registry.d.ts.map +1 -0
- package/dist/expressions/function-registry.js +139 -0
- package/dist/expressions/function-registry.js.map +1 -0
- package/dist/expressions/get.d.ts +9 -0
- package/dist/expressions/get.d.ts.map +1 -0
- package/dist/expressions/get.js +32 -0
- package/dist/expressions/get.js.map +1 -0
- package/dist/expressions/index.cjs +1888 -0
- package/dist/expressions/index.cjs.map +6 -0
- package/dist/expressions/index.d.ts +13 -0
- package/dist/expressions/index.d.ts.map +1 -0
- package/dist/expressions/index.js +14 -0
- package/dist/expressions/index.js.map +1 -0
- package/dist/expressions/numeric-functions.d.ts +277 -0
- package/dist/expressions/numeric-functions.d.ts.map +1 -0
- package/dist/expressions/numeric-functions.js +52 -0
- package/dist/expressions/numeric-functions.js.map +1 -0
- package/dist/expressions/parse-expression-string.d.ts +21 -0
- package/dist/expressions/parse-expression-string.d.ts.map +1 -0
- package/dist/expressions/parse-expression-string.js +61 -0
- package/dist/expressions/parse-expression-string.js.map +1 -0
- package/dist/expressions/predicate-expression.d.ts +7 -0
- package/dist/expressions/predicate-expression.d.ts.map +1 -0
- package/dist/expressions/predicate-expression.js +112 -0
- package/dist/expressions/predicate-expression.js.map +1 -0
- package/dist/expressions/scalar-type.d.ts +6 -0
- package/dist/expressions/scalar-type.d.ts.map +1 -0
- package/dist/expressions/scalar-type.js +23 -0
- package/dist/expressions/scalar-type.js.map +1 -0
- package/dist/expressions/vendor/jsep/runtime.d.ts +3 -0
- package/dist/expressions/vendor/jsep/runtime.d.ts.map +1 -0
- package/dist/expressions/vendor/jsep/runtime.js +972 -0
- package/dist/expressions/vendor/jsep/runtime.js.map +1 -0
- package/dist/expressions/vendor/jsep/types.d.ts +148 -0
- package/dist/expressions/vendor/jsep/types.d.ts.map +1 -0
- package/dist/expressions/vendor/jsep/types.js +6 -0
- package/dist/expressions/vendor/jsep/types.js.map +1 -0
- package/dist/index.cjs +4865 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/math/index.cjs +444 -0
- package/dist/math/index.cjs.map +6 -0
- package/dist/math/index.d.ts +44 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +182 -0
- package/dist/math/index.js.map +1 -0
- package/dist/scales/arrow-domains.d.ts +15 -0
- package/dist/scales/arrow-domains.d.ts.map +1 -0
- package/dist/scales/arrow-domains.js +124 -0
- package/dist/scales/arrow-domains.js.map +1 -0
- package/dist/scales/arrow-scales.cjs +3837 -0
- package/dist/scales/arrow-scales.cjs.map +6 -0
- package/dist/scales/arrow-scales.d.ts +20 -0
- package/dist/scales/arrow-scales.d.ts.map +1 -0
- package/dist/scales/arrow-scales.js +109 -0
- package/dist/scales/arrow-scales.js.map +1 -0
- package/dist/scales/compile-scale.d.ts +7 -0
- package/dist/scales/compile-scale.d.ts.map +1 -0
- package/dist/scales/compile-scale.js +195 -0
- package/dist/scales/compile-scale.js.map +1 -0
- package/dist/scales/hdr-colors.d.ts +13 -0
- package/dist/scales/hdr-colors.d.ts.map +1 -0
- package/dist/scales/hdr-colors.js +115 -0
- package/dist/scales/hdr-colors.js.map +1 -0
- package/dist/scales/index.cjs +1214 -0
- package/dist/scales/index.cjs.map +6 -0
- package/dist/scales/index.d.ts +9 -0
- package/dist/scales/index.d.ts.map +1 -0
- package/dist/scales/index.js +10 -0
- package/dist/scales/index.js.map +1 -0
- package/dist/scales/palettes.d.ts +5 -0
- package/dist/scales/palettes.d.ts.map +1 -0
- package/dist/scales/palettes.js +85 -0
- package/dist/scales/palettes.js.map +1 -0
- package/dist/scales/parse-sql-scale.d.ts +14 -0
- package/dist/scales/parse-sql-scale.d.ts.map +1 -0
- package/dist/scales/parse-sql-scale.js +165 -0
- package/dist/scales/parse-sql-scale.js.map +1 -0
- package/dist/scales/quantile-sketch.d.ts +22 -0
- package/dist/scales/quantile-sketch.d.ts.map +1 -0
- package/dist/scales/quantile-sketch.js +108 -0
- package/dist/scales/quantile-sketch.js.map +1 -0
- package/dist/scales/scale-types.d.ts +92 -0
- package/dist/scales/scale-types.d.ts.map +1 -0
- package/dist/scales/scale-types.js +119 -0
- package/dist/scales/scale-types.js.map +1 -0
- package/dist/scales/streaming-domains.d.ts +18 -0
- package/dist/scales/streaming-domains.d.ts.map +1 -0
- package/dist/scales/streaming-domains.js +109 -0
- package/dist/scales/streaming-domains.js.map +1 -0
- package/dist/sql/bind-sql-predicate.d.ts +4 -0
- package/dist/sql/bind-sql-predicate.d.ts.map +1 -0
- package/dist/sql/bind-sql-predicate.js +11 -0
- package/dist/sql/bind-sql-predicate.js.map +1 -0
- package/dist/sql/index.cjs +1588 -0
- package/dist/sql/index.cjs.map +6 -0
- package/dist/sql/index.d.ts +10 -0
- package/dist/sql/index.d.ts.map +1 -0
- package/dist/sql/index.js +10 -0
- package/dist/sql/index.js.map +1 -0
- package/dist/sql/parse-sql-expression.d.ts +44 -0
- package/dist/sql/parse-sql-expression.d.ts.map +1 -0
- package/dist/sql/parse-sql-expression.js +455 -0
- package/dist/sql/parse-sql-expression.js.map +1 -0
- package/dist/sql/parse-sql-predicate.d.ts +13 -0
- package/dist/sql/parse-sql-predicate.d.ts.map +1 -0
- package/dist/sql/parse-sql-predicate.js +365 -0
- package/dist/sql/parse-sql-predicate.js.map +1 -0
- package/dist/sql/parse-sql-query.d.ts +8 -0
- package/dist/sql/parse-sql-query.d.ts.map +1 -0
- package/dist/sql/parse-sql-query.js +66 -0
- package/dist/sql/parse-sql-query.js.map +1 -0
- package/dist/sql/parse-sql-select.d.ts +55 -0
- package/dist/sql/parse-sql-select.d.ts.map +1 -0
- package/dist/sql/parse-sql-select.js +244 -0
- package/dist/sql/parse-sql-select.js.map +1 -0
- package/dist/sql/parse-sql-statement.d.ts +50 -0
- package/dist/sql/parse-sql-statement.d.ts.map +1 -0
- package/dist/sql/parse-sql-statement.js +182 -0
- package/dist/sql/parse-sql-statement.js.map +1 -0
- package/dist/sql/query-types.d.ts +56 -0
- package/dist/sql/query-types.d.ts.map +1 -0
- package/dist/sql/query-types.js +5 -0
- package/dist/sql/query-types.js.map +1 -0
- package/dist/sql/sql-parse-error.d.ts +13 -0
- package/dist/sql/sql-parse-error.d.ts.map +1 -0
- package/dist/sql/sql-parse-error.js +20 -0
- package/dist/sql/sql-parse-error.js.map +1 -0
- package/dist/sql/sql-predicate-schema.d.ts +164 -0
- package/dist/sql/sql-predicate-schema.d.ts.map +1 -0
- package/dist/sql/sql-predicate-schema.js +249 -0
- package/dist/sql/sql-predicate-schema.js.map +1 -0
- package/dist/sql/sql-predicate-types.d.ts +48 -0
- package/dist/sql/sql-predicate-types.d.ts.map +1 -0
- package/dist/sql/sql-predicate-types.js +9 -0
- package/dist/sql/sql-predicate-types.js.map +1 -0
- package/dist/sql/sql-predicate.d.ts +6 -0
- package/dist/sql/sql-predicate.d.ts.map +1 -0
- package/dist/sql/sql-predicate.js +8 -0
- package/dist/sql/sql-predicate.js.map +1 -0
- package/package.json +92 -4
- package/src/arrow/aggregates.ts +386 -0
- package/src/arrow/buffers.ts +177 -0
- package/src/arrow/cast.ts +160 -0
- package/src/arrow/compiler.ts +1659 -0
- package/src/arrow/float16.ts +25 -0
- package/src/arrow/index.ts +22 -0
- package/src/arrow/like.ts +72 -0
- package/src/arrow/list-values.ts +200 -0
- package/src/arrow/predicate.ts +19 -0
- package/src/arrow/query-arrow-aggregate-snapshots.ts +107 -0
- package/src/arrow/query-arrow-sql.ts +227 -0
- package/src/arrow/query-arrow-table.ts +298 -0
- package/src/arrow/query-worker-entry.ts +8 -0
- package/src/arrow/query-worker.ts +190 -0
- package/src/arrow/registry.ts +86 -0
- package/src/arrow/relational-query.ts +247 -0
- package/src/arrow/replace-arrow-columns.ts +71 -0
- package/src/arrow/signature.ts +50 -0
- package/src/arrow/temporal-cast.ts +66 -0
- package/src/arrow/types.ts +76 -0
- package/src/arrow/worker-entry.ts +10 -0
- package/src/arrow/worker.ts +139 -0
- package/src/color/index.ts +405 -0
- package/src/diagnostic-log.ts +20 -0
- package/src/expressions/ast.ts +141 -0
- package/src/expressions/columnar-predicate.ts +293 -0
- package/src/expressions/evaluation-error.ts +29 -0
- package/src/expressions/expression-eval.ts +480 -0
- package/src/expressions/function-libraries.ts +126 -0
- package/src/expressions/function-registry.ts +171 -0
- package/src/expressions/get.ts +37 -0
- package/src/expressions/index.ts +40 -0
- package/src/expressions/numeric-functions.ts +99 -0
- package/src/expressions/parse-expression-string.ts +80 -0
- package/src/expressions/predicate-expression.ts +128 -0
- package/src/expressions/scalar-type.ts +39 -0
- package/src/expressions/vendor/jsep/LICENSE.txt +23 -0
- package/src/expressions/vendor/jsep/README.md +22 -0
- package/src/expressions/vendor/jsep/runtime.ts +949 -0
- package/src/expressions/vendor/jsep/types.ts +191 -0
- package/src/index.ts +40 -0
- package/src/math/index.ts +235 -0
- package/src/scales/arrow-domains.ts +131 -0
- package/src/scales/arrow-scales.ts +156 -0
- package/src/scales/compile-scale.ts +217 -0
- package/src/scales/hdr-colors.ts +126 -0
- package/src/scales/index.ts +35 -0
- package/src/scales/palettes.ts +86 -0
- package/src/scales/parse-sql-scale.ts +195 -0
- package/src/scales/quantile-sketch.ts +106 -0
- package/src/scales/scale-types.ts +224 -0
- package/src/scales/streaming-domains.ts +133 -0
- package/src/sql/bind-sql-predicate.ts +21 -0
- package/src/sql/index.ts +28 -0
- package/src/sql/parse-sql-expression.ts +457 -0
- package/src/sql/parse-sql-predicate.ts +454 -0
- package/src/sql/parse-sql-query.ts +72 -0
- package/src/sql/parse-sql-select.ts +266 -0
- package/src/sql/parse-sql-statement.ts +242 -0
- package/src/sql/query-types.ts +71 -0
- package/src/sql/sql-parse-error.ts +33 -0
- package/src/sql/sql-predicate-schema.ts +279 -0
- package/src/sql/sql-predicate-types.ts +91 -0
- package/src/sql/sql-predicate.ts +28 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {Expression} from '../expressions/index';
|
|
6
|
+
import {parseSQLExpression} from '../sql/parse-sql-expression';
|
|
7
|
+
import {
|
|
8
|
+
validateScaleSpec,
|
|
9
|
+
type ScaleColor,
|
|
10
|
+
type ScaleDomain,
|
|
11
|
+
type ScaleRange,
|
|
12
|
+
type ScaleSpec
|
|
13
|
+
} from './scale-types';
|
|
14
|
+
|
|
15
|
+
/** SQL call shape with arguments produced by the SQL expression parser. */
|
|
16
|
+
type Call = {
|
|
17
|
+
type: 'CallExpression';
|
|
18
|
+
callee: {type: 'Identifier'; name: string};
|
|
19
|
+
arguments: Expression[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type ParsedSQLScale = Readonly<{
|
|
23
|
+
/** Row-preserving value expression, evaluated by an Arrow/GPU backend. */
|
|
24
|
+
value: Expression;
|
|
25
|
+
/** Serializable specification, with unresolved domain dependencies. */
|
|
26
|
+
scale: ScaleSpec;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
function call(node: Expression, expected?: string): Call {
|
|
30
|
+
const candidate = node as unknown as Call;
|
|
31
|
+
if (candidate.type !== 'CallExpression' || candidate.callee?.type !== 'Identifier') {
|
|
32
|
+
throw new Error(expected ? `${expected} expects a function call` : 'Expected a scale function');
|
|
33
|
+
}
|
|
34
|
+
if (expected && candidate.callee.name.toUpperCase() !== expected) {
|
|
35
|
+
throw new Error(`Expected ${expected}`);
|
|
36
|
+
}
|
|
37
|
+
return candidate;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function name(node: Expression): string {
|
|
41
|
+
const candidate = node as {type: string; name?: string};
|
|
42
|
+
if (candidate.type !== 'Identifier' || !candidate.name) {
|
|
43
|
+
throw new Error('Data-derived scale domain requires one column identifier');
|
|
44
|
+
}
|
|
45
|
+
return candidate.name;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function literal(node: Expression): string | number {
|
|
49
|
+
const value = node as {type: string; value?: unknown};
|
|
50
|
+
if (
|
|
51
|
+
value.type !== 'Literal' ||
|
|
52
|
+
(typeof value.value !== 'string' && typeof value.value !== 'number') ||
|
|
53
|
+
(typeof value.value === 'number' && !Number.isFinite(value.value))
|
|
54
|
+
) {
|
|
55
|
+
throw new Error('Scale argument requires a literal number or quoted string');
|
|
56
|
+
}
|
|
57
|
+
return value.value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function number(node: Expression): number {
|
|
61
|
+
// SQL negative numbers are sometimes represented as unary nodes.
|
|
62
|
+
const unary = node as {type: string; operator?: string; argument?: Expression};
|
|
63
|
+
if (
|
|
64
|
+
unary.type === 'UnaryExpression' &&
|
|
65
|
+
(unary.operator === '-' || unary.operator === '+') &&
|
|
66
|
+
unary.argument
|
|
67
|
+
) {
|
|
68
|
+
const n = number(unary.argument);
|
|
69
|
+
return unary.operator === '-' ? -n : n;
|
|
70
|
+
}
|
|
71
|
+
const value = literal(node);
|
|
72
|
+
if (typeof value !== 'number') throw new Error('Expected numeric scale argument');
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function color(hex: string): ScaleColor {
|
|
77
|
+
if (!/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(hex)) {
|
|
78
|
+
throw new Error('Explicit SQL color stops require #RRGGBB or #RRGGBBAA');
|
|
79
|
+
}
|
|
80
|
+
const bytes = hex.slice(1).match(/../g)!;
|
|
81
|
+
return [0, 1, 2, 3].map(
|
|
82
|
+
index => parseInt(bytes[index] ?? 'ff', 16) / 255
|
|
83
|
+
) as unknown as ScaleColor;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function parseDomain(node: Expression): ScaleDomain {
|
|
87
|
+
const fn = call(node);
|
|
88
|
+
const functionName = fn.callee.name.toUpperCase();
|
|
89
|
+
const args = fn.arguments;
|
|
90
|
+
switch (functionName) {
|
|
91
|
+
case 'DOMAIN':
|
|
92
|
+
if (args.length === 1) return {type: 'extent', column: name(args[0]!)};
|
|
93
|
+
if (args.length >= 2) return {type: 'fixed', values: args.map(number)};
|
|
94
|
+
break;
|
|
95
|
+
case 'DOMAIN_ZERO':
|
|
96
|
+
if (args.length === 1) return {type: 'extent', column: name(args[0]!), includeZero: true};
|
|
97
|
+
break;
|
|
98
|
+
case 'DOMAIN_SYMMETRIC':
|
|
99
|
+
if (args.length === 2)
|
|
100
|
+
return {type: 'symmetric', column: name(args[0]!), pivot: number(args[1]!)};
|
|
101
|
+
break;
|
|
102
|
+
case 'DOMAIN_QUANTILE':
|
|
103
|
+
if (args.length === 3) {
|
|
104
|
+
return {
|
|
105
|
+
type: 'percentile',
|
|
106
|
+
column: name(args[0]!),
|
|
107
|
+
lower: number(args[1]!),
|
|
108
|
+
upper: number(args[2]!)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`Invalid ${functionName} domain arguments`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function parseRange(node: Expression): ScaleRange {
|
|
117
|
+
const fn = call(node);
|
|
118
|
+
const args = fn.arguments;
|
|
119
|
+
switch (fn.callee.name.toUpperCase()) {
|
|
120
|
+
case 'RANGE':
|
|
121
|
+
if (args.length >= 2) return {type: 'numeric', values: args.map(number)};
|
|
122
|
+
break;
|
|
123
|
+
case 'PALETTE': {
|
|
124
|
+
if (
|
|
125
|
+
args.length === 1 ||
|
|
126
|
+
(args.length === 2 &&
|
|
127
|
+
typeof literal(args[0]!) === 'string' &&
|
|
128
|
+
typeof literal(args[1]!) === 'number')
|
|
129
|
+
) {
|
|
130
|
+
const paletteName = literal(args[0]!);
|
|
131
|
+
if (typeof paletteName !== 'string') throw new Error('Palette name must be a string');
|
|
132
|
+
const count = args.length === 2 ? number(args[1]!) : undefined;
|
|
133
|
+
return {type: 'palette', name: paletteName, ...(count === undefined ? {} : {count})};
|
|
134
|
+
}
|
|
135
|
+
if (args.length >= 2 && args.every(arg => typeof literal(arg) === 'string')) {
|
|
136
|
+
return {type: 'colors', values: args.map(arg => color(literal(arg) as string))};
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
throw new Error('Expected RANGE(numbers...) or PALETTE(name[, count] | color stops...)');
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Parse one scale expression into a row-value AST and a JSON-safe scale spec.
|
|
146
|
+
* This does not evaluate domain statistics, produce colors, or modify row count.
|
|
147
|
+
*/
|
|
148
|
+
export function parseSQLScaleExpression(sql: string): ParsedSQLScale {
|
|
149
|
+
const root = call(parseSQLExpression(sql));
|
|
150
|
+
const fn = root.callee.name.toUpperCase();
|
|
151
|
+
const args = root.arguments;
|
|
152
|
+
const names: Record<string, ScaleSpec['type']> = {
|
|
153
|
+
SCALE_LINEAR: 'linear',
|
|
154
|
+
SCALE_SQRT: 'sqrt',
|
|
155
|
+
SCALE_LOG: 'log',
|
|
156
|
+
SCALE_COLOR: 'sequential',
|
|
157
|
+
SCALE_DIVERGING: 'diverging',
|
|
158
|
+
SCALE_ORDINAL: 'ordinal',
|
|
159
|
+
SCALE_QUANTILE: 'quantile',
|
|
160
|
+
SCALE_QUANTIZE: 'quantize'
|
|
161
|
+
};
|
|
162
|
+
const type = names[fn];
|
|
163
|
+
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`);
|
|
166
|
+
}
|
|
167
|
+
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]!);
|
|
175
|
+
if (fn === 'SCALE_QUANTILE' || fn === 'SCALE_QUANTIZE') {
|
|
176
|
+
const count = number(args[1]!);
|
|
177
|
+
if (!Number.isSafeInteger(count) || count < 2) {
|
|
178
|
+
throw new Error(`${fn} class count must be an integer >= 2`);
|
|
179
|
+
}
|
|
180
|
+
if (range.type !== 'palette') throw new Error(`${fn} requires a named palette`);
|
|
181
|
+
range = {...range, count};
|
|
182
|
+
}
|
|
183
|
+
const colors = ['sequential', 'diverging', 'ordinal', 'quantile', 'quantize'].includes(type);
|
|
184
|
+
if (colors && range.type === 'numeric') throw new Error(`${fn} requires a color range`);
|
|
185
|
+
if (!colors && range.type !== 'numeric') throw new Error(`${fn} requires a numeric RANGE`);
|
|
186
|
+
if (
|
|
187
|
+
domain.type === 'fixed' &&
|
|
188
|
+
range.type === 'colors' &&
|
|
189
|
+
domain.values.length !== range.values.length
|
|
190
|
+
) {
|
|
191
|
+
throw new Error('Explicit domain and color stops must have equal lengths');
|
|
192
|
+
}
|
|
193
|
+
const scale = validateScaleSpec({type, domain, range});
|
|
194
|
+
return {value, scale};
|
|
195
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
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
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Portable, JSON-safe vocabulary for data-driven visualization scales.
|
|
7
|
+
* A specification describes an unresolved scale. It never contains an evaluator,
|
|
8
|
+
* so it can be serialized, transferred to workers, or used by other renderers.
|
|
9
|
+
*/
|
|
10
|
+
export type ScaleColor = readonly [number, number, number, number];
|
|
11
|
+
/** Numeric or categorical value accepted by a scale. */
|
|
12
|
+
export type ScaleValue = number | string;
|
|
13
|
+
|
|
14
|
+
/** Fixed values or a request to derive the domain from a column. */
|
|
15
|
+
export type ScaleDomain =
|
|
16
|
+
| {readonly type: 'fixed'; readonly values: readonly ScaleValue[]}
|
|
17
|
+
| {readonly type: 'extent'; readonly column: string; readonly includeZero?: boolean}
|
|
18
|
+
| {readonly type: 'symmetric'; readonly column: string; readonly pivot: number}
|
|
19
|
+
| {
|
|
20
|
+
readonly type: 'percentile';
|
|
21
|
+
readonly column: string;
|
|
22
|
+
readonly lower: number;
|
|
23
|
+
readonly upper: number;
|
|
24
|
+
}
|
|
25
|
+
| {
|
|
26
|
+
readonly type: 'categories';
|
|
27
|
+
readonly column: string;
|
|
28
|
+
readonly order?: 'sorted' | 'first-seen';
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Numeric output, explicit colors, or a named palette to resolve. */
|
|
32
|
+
export type ScaleRange =
|
|
33
|
+
| {readonly type: 'numeric'; readonly values: readonly number[]}
|
|
34
|
+
| {
|
|
35
|
+
readonly type: 'colors';
|
|
36
|
+
/** Color values remain in the declared encoding, without implicit clipping. */
|
|
37
|
+
readonly values: readonly ScaleColor[];
|
|
38
|
+
}
|
|
39
|
+
| {readonly type: 'palette'; readonly name: string; readonly count?: number};
|
|
40
|
+
|
|
41
|
+
/** A scale's storage type is distinct from its primaries, transfer and interpolation. */
|
|
42
|
+
export type ScaleColorEncoding = {
|
|
43
|
+
readonly primaries: 'srgb' | 'display-p3' | 'rec2020';
|
|
44
|
+
readonly transfer: 'srgb' | 'linear' | 'pq' | 'hlg';
|
|
45
|
+
readonly format: 'rgba8unorm' | 'rgba16unorm' | 'rgba16float' | 'rgba32float';
|
|
46
|
+
/** Scene-linear reference white, when known. */
|
|
47
|
+
readonly referenceWhiteNits?: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Serializable scale inputs and optional presentation metadata. */
|
|
51
|
+
export type ScaleSpec = {
|
|
52
|
+
readonly id?: string;
|
|
53
|
+
readonly type:
|
|
54
|
+
| 'linear'
|
|
55
|
+
| 'sqrt'
|
|
56
|
+
| 'log'
|
|
57
|
+
| 'symlog'
|
|
58
|
+
| 'sequential'
|
|
59
|
+
| 'diverging'
|
|
60
|
+
| 'ordinal'
|
|
61
|
+
| 'quantize'
|
|
62
|
+
| 'quantile'
|
|
63
|
+
| 'threshold'
|
|
64
|
+
| 'piecewise';
|
|
65
|
+
readonly domain: ScaleDomain;
|
|
66
|
+
readonly range: ScaleRange;
|
|
67
|
+
readonly clamp?: boolean;
|
|
68
|
+
readonly pivot?: number;
|
|
69
|
+
readonly interpolation?: 'srgb' | 'linear-srgb' | 'oklab' | 'jzazbz';
|
|
70
|
+
readonly encoding?: ScaleColorEncoding;
|
|
71
|
+
readonly nullColor?: ScaleColor;
|
|
72
|
+
readonly underColor?: ScaleColor;
|
|
73
|
+
readonly overColor?: ScaleColor;
|
|
74
|
+
readonly label?: string;
|
|
75
|
+
readonly units?: string;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The resolved descriptor records *actual* domain and class breaks, not an
|
|
80
|
+
* inferred palette. Consumers can generate faithful ticks and legend labels.
|
|
81
|
+
*/
|
|
82
|
+
export type ResolvedScaleDescriptor = Omit<ScaleSpec, 'domain' | 'range'> & {
|
|
83
|
+
readonly domain: readonly ScaleValue[];
|
|
84
|
+
readonly range: Exclude<ScaleRange, {readonly type: 'palette'}>;
|
|
85
|
+
readonly thresholds?: readonly number[];
|
|
86
|
+
readonly palette?: string;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** Runtime evaluator is intentionally separate from the JSON-safe descriptor. */
|
|
90
|
+
export type ResolvedScale = {
|
|
91
|
+
readonly descriptor: ResolvedScaleDescriptor;
|
|
92
|
+
sample(value: ScaleValue | null): number | ScaleColor | null;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
function assertFinite(value: number, description: string): void {
|
|
96
|
+
if (!Number.isFinite(value)) throw new Error(`${description} must be finite`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function checkColor(color: ScaleColor, label: string): void {
|
|
100
|
+
if (!Array.isArray(color) || color.length !== 4) {
|
|
101
|
+
throw new Error(`${label} must contain four channels`);
|
|
102
|
+
}
|
|
103
|
+
color.forEach((channel, index) => assertFinite(channel, `${label}[${index}]`));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Validates the serializable contract without resolving data-dependent domains. */
|
|
107
|
+
export function validateScaleSpec(spec: ScaleSpec): ScaleSpec {
|
|
108
|
+
if (!spec || typeof spec !== 'object') throw new Error('Scale specification is required');
|
|
109
|
+
const kinds = [
|
|
110
|
+
'linear',
|
|
111
|
+
'sqrt',
|
|
112
|
+
'log',
|
|
113
|
+
'symlog',
|
|
114
|
+
'sequential',
|
|
115
|
+
'diverging',
|
|
116
|
+
'ordinal',
|
|
117
|
+
'quantize',
|
|
118
|
+
'quantile',
|
|
119
|
+
'threshold',
|
|
120
|
+
'piecewise'
|
|
121
|
+
];
|
|
122
|
+
if (!kinds.includes(spec.type)) throw new Error(`Unsupported scale type: ${spec.type}`);
|
|
123
|
+
const minimumStops = spec.type === 'ordinal' ? 1 : 2;
|
|
124
|
+
const domain = spec.domain;
|
|
125
|
+
if (!domain || typeof domain !== 'object') throw new Error('Scale domain is required');
|
|
126
|
+
switch (domain.type) {
|
|
127
|
+
case 'fixed':
|
|
128
|
+
if (
|
|
129
|
+
!Array.isArray(domain.values) ||
|
|
130
|
+
domain.values.length < minimumStops ||
|
|
131
|
+
domain.values.some(
|
|
132
|
+
value =>
|
|
133
|
+
typeof value !== 'string' && (typeof value !== 'number' || !Number.isFinite(value))
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
throw new Error(
|
|
137
|
+
`Fixed scale domain requires at least ${minimumStops} finite or string values`
|
|
138
|
+
);
|
|
139
|
+
break;
|
|
140
|
+
case 'extent':
|
|
141
|
+
case 'categories':
|
|
142
|
+
if (typeof domain.column !== 'string' || !domain.column) {
|
|
143
|
+
throw new Error('Scale domain requires a column');
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
case 'symmetric':
|
|
147
|
+
if (!domain.column) throw new Error('Symmetric domain requires a column');
|
|
148
|
+
assertFinite(domain.pivot, 'Scale pivot');
|
|
149
|
+
break;
|
|
150
|
+
case 'percentile':
|
|
151
|
+
if (
|
|
152
|
+
!domain.column ||
|
|
153
|
+
!Number.isFinite(domain.lower) ||
|
|
154
|
+
!Number.isFinite(domain.upper) ||
|
|
155
|
+
domain.lower < 0 ||
|
|
156
|
+
domain.upper > 1 ||
|
|
157
|
+
domain.lower >= domain.upper
|
|
158
|
+
) {
|
|
159
|
+
throw new Error('Percentile domain requires 0 <= lower < upper <= 1');
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
throw new Error('Unsupported scale domain');
|
|
164
|
+
}
|
|
165
|
+
if (!spec.range) throw new Error('Scale range is required');
|
|
166
|
+
switch (spec.range.type) {
|
|
167
|
+
case 'numeric':
|
|
168
|
+
if (spec.range.values.length < minimumStops)
|
|
169
|
+
throw new Error(`Numeric range requires at least ${minimumStops} values`);
|
|
170
|
+
spec.range.values.forEach(value => assertFinite(value, 'Numeric range'));
|
|
171
|
+
break;
|
|
172
|
+
case 'colors':
|
|
173
|
+
if (spec.range.values.length < minimumStops)
|
|
174
|
+
throw new Error(`Color range requires at least ${minimumStops} colors`);
|
|
175
|
+
spec.range.values.forEach((value, index) => checkColor(value, `Color stop ${index}`));
|
|
176
|
+
break;
|
|
177
|
+
case 'palette':
|
|
178
|
+
if (
|
|
179
|
+
!spec.range.name ||
|
|
180
|
+
(spec.range.count !== undefined &&
|
|
181
|
+
(!Number.isSafeInteger(spec.range.count) || spec.range.count < 2))
|
|
182
|
+
) {
|
|
183
|
+
throw new Error('Palette requires a name and optional positive count >= 2');
|
|
184
|
+
}
|
|
185
|
+
break;
|
|
186
|
+
default:
|
|
187
|
+
throw new Error('Unsupported scale range');
|
|
188
|
+
}
|
|
189
|
+
if (spec.pivot !== undefined) assertFinite(spec.pivot, 'Scale pivot');
|
|
190
|
+
if (
|
|
191
|
+
spec.encoding?.referenceWhiteNits !== undefined &&
|
|
192
|
+
(!Number.isFinite(spec.encoding.referenceWhiteNits) || spec.encoding.referenceWhiteNits <= 0)
|
|
193
|
+
) {
|
|
194
|
+
throw new Error('referenceWhiteNits must be a positive finite number');
|
|
195
|
+
}
|
|
196
|
+
for (const key of ['nullColor', 'underColor', 'overColor'] as const) {
|
|
197
|
+
if (spec[key]) checkColor(spec[key], key);
|
|
198
|
+
}
|
|
199
|
+
return spec;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Binds an already resolved domain/range to the same evaluator that produces
|
|
204
|
+
* layer colors. No independent legend interpolation is performed here.
|
|
205
|
+
*/
|
|
206
|
+
export function createResolvedScale(
|
|
207
|
+
descriptor: ResolvedScaleDescriptor,
|
|
208
|
+
sample: ResolvedScale['sample']
|
|
209
|
+
): ResolvedScale {
|
|
210
|
+
validateScaleSpec({
|
|
211
|
+
...descriptor,
|
|
212
|
+
domain: {type: 'fixed', values: descriptor.domain}
|
|
213
|
+
});
|
|
214
|
+
if (descriptor.thresholds) {
|
|
215
|
+
if (
|
|
216
|
+
descriptor.thresholds.some(
|
|
217
|
+
(value, i) => !Number.isFinite(value) || (i > 0 && value <= descriptor.thresholds![i - 1]!)
|
|
218
|
+
)
|
|
219
|
+
) {
|
|
220
|
+
throw new Error('Thresholds must be finite and strictly ordered');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return Object.freeze({descriptor, sample});
|
|
224
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {RecordBatch} from 'apache-arrow';
|
|
6
|
+
import type {ScaleDomain, ScaleValue} from './scale-types';
|
|
7
|
+
import {arrowScaleValue} from './arrow-domains';
|
|
8
|
+
|
|
9
|
+
import {ScaleQuantileSketch} from './quantile-sketch';
|
|
10
|
+
|
|
11
|
+
type ColumnState = {
|
|
12
|
+
min: number;
|
|
13
|
+
max: number;
|
|
14
|
+
seen: number;
|
|
15
|
+
categories?: Set<ScaleValue>;
|
|
16
|
+
sketch?: ScaleQuantileSketch;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type StreamingDomainOptions = {
|
|
20
|
+
/** Maximum retained weighted samples per referenced percentile column. */
|
|
21
|
+
quantileCapacity?: number;
|
|
22
|
+
/** Also keep quantile sketches for these columns, e.g. SCALE_QUANTILE inputs. */
|
|
23
|
+
quantileColumns?: readonly string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Incremental domain accumulator. Each Arrow batch is visited once per
|
|
28
|
+
* referenced column, even when multiple visual scales depend on that column.
|
|
29
|
+
*/
|
|
30
|
+
export function createStreamingDomainAccumulator(
|
|
31
|
+
domains: readonly ScaleDomain[],
|
|
32
|
+
options: StreamingDomainOptions = {}
|
|
33
|
+
): {
|
|
34
|
+
update(batch: RecordBatch): void;
|
|
35
|
+
result(): readonly (readonly ScaleValue[])[];
|
|
36
|
+
quantileBreaks(column: string, classes: number): readonly number[];
|
|
37
|
+
} {
|
|
38
|
+
const columns = new Map<string, ColumnState>();
|
|
39
|
+
const quantileColumns = new Set(options.quantileColumns ?? []);
|
|
40
|
+
for (const domain of domains) {
|
|
41
|
+
if (domain.type === 'fixed') continue;
|
|
42
|
+
const previous = columns.get(domain.column);
|
|
43
|
+
if (previous) {
|
|
44
|
+
if (
|
|
45
|
+
(domain.type === 'percentile' || quantileColumns.has(domain.column)) &&
|
|
46
|
+
!previous.sketch
|
|
47
|
+
) {
|
|
48
|
+
previous.sketch = new ScaleQuantileSketch(options.quantileCapacity);
|
|
49
|
+
}
|
|
50
|
+
if (domain.type === 'categories') previous.categories ??= new Set();
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
columns.set(domain.column, {
|
|
54
|
+
min: Infinity,
|
|
55
|
+
max: -Infinity,
|
|
56
|
+
seen: 0,
|
|
57
|
+
...(domain.type === 'categories' ? {categories: new Set<ScaleValue>()} : {}),
|
|
58
|
+
...(domain.type === 'percentile' || quantileColumns.has(domain.column)
|
|
59
|
+
? {sketch: new ScaleQuantileSketch(options.quantileCapacity)}
|
|
60
|
+
: {})
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
for (const name of quantileColumns) {
|
|
65
|
+
if (!columns.has(name))
|
|
66
|
+
columns.set(name, {
|
|
67
|
+
min: Infinity,
|
|
68
|
+
max: -Infinity,
|
|
69
|
+
seen: 0,
|
|
70
|
+
sketch: new ScaleQuantileSketch(options.quantileCapacity)
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
update(batch) {
|
|
76
|
+
for (const [name, state] of columns) {
|
|
77
|
+
const vector = batch.getChild(name);
|
|
78
|
+
if (!vector) throw new Error(`Missing scale column: ${name}`);
|
|
79
|
+
for (let row = 0; row < vector.length; row++) {
|
|
80
|
+
const value = arrowScaleValue(vector.get(row));
|
|
81
|
+
if (value === null || value === undefined) continue;
|
|
82
|
+
if (typeof value === 'string') {
|
|
83
|
+
state.categories?.add(value);
|
|
84
|
+
} else if (typeof value === 'number' && Number.isFinite(value)) {
|
|
85
|
+
state.categories?.add(value);
|
|
86
|
+
state.seen++;
|
|
87
|
+
state.min = Math.min(state.min, value);
|
|
88
|
+
state.max = Math.max(state.max, value);
|
|
89
|
+
state.sketch?.add(value);
|
|
90
|
+
} else {
|
|
91
|
+
throw new Error(`Scale column ${name} has unsupported value at row ${row}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
result() {
|
|
97
|
+
return domains.map(domain => {
|
|
98
|
+
if (domain.type === 'fixed') return [...domain.values];
|
|
99
|
+
const state = columns.get(domain.column)!;
|
|
100
|
+
if (domain.type === 'categories') {
|
|
101
|
+
const labels = [...state.categories!];
|
|
102
|
+
return domain.order === 'first-seen'
|
|
103
|
+
? labels
|
|
104
|
+
: labels.sort((a, b) =>
|
|
105
|
+
typeof a === 'number' && typeof b === 'number' ? a - b : a < b ? -1 : a > b ? 1 : 0
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
if (!state.seen) return [];
|
|
109
|
+
if (domain.type === 'percentile') {
|
|
110
|
+
return [state.sketch!.quantile(domain.lower)!, state.sketch!.quantile(domain.upper)!];
|
|
111
|
+
}
|
|
112
|
+
if (domain.type === 'symmetric') {
|
|
113
|
+
const radius = Math.max(
|
|
114
|
+
Math.abs(state.min - domain.pivot),
|
|
115
|
+
Math.abs(state.max - domain.pivot)
|
|
116
|
+
);
|
|
117
|
+
return [domain.pivot - radius, domain.pivot + radius];
|
|
118
|
+
}
|
|
119
|
+
if (domain.includeZero) return [Math.min(state.min, 0), Math.max(state.max, 0)];
|
|
120
|
+
return [state.min, state.max];
|
|
121
|
+
});
|
|
122
|
+
},
|
|
123
|
+
quantileBreaks(column, classes) {
|
|
124
|
+
if (!Number.isSafeInteger(classes) || classes < 2) {
|
|
125
|
+
throw new Error('Scale class count must be at least two');
|
|
126
|
+
}
|
|
127
|
+
const sketch = columns.get(column)?.sketch;
|
|
128
|
+
if (!sketch) throw new Error(`Column ${column} has no quantile sketch`);
|
|
129
|
+
if (!sketch.length) return [];
|
|
130
|
+
return Array.from({length: classes - 1}, (_, i) => sketch.quantile((i + 1) / classes)!);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {bindColumnarPredicateParameters} from '../expressions/columnar-predicate';
|
|
6
|
+
|
|
7
|
+
import {validateSQLPredicate} from './sql-predicate-schema';
|
|
8
|
+
import {
|
|
9
|
+
type BoundSQLPredicate,
|
|
10
|
+
type SQLPredicate,
|
|
11
|
+
type SQLPredicateParameterValues
|
|
12
|
+
} from './sql-predicate-types';
|
|
13
|
+
|
|
14
|
+
/** Binds named parameter references in a portable SQL predicate without mutating the input AST. */
|
|
15
|
+
export function bindSQLPredicate(
|
|
16
|
+
predicate: SQLPredicate,
|
|
17
|
+
parameters: SQLPredicateParameterValues
|
|
18
|
+
): BoundSQLPredicate {
|
|
19
|
+
validateSQLPredicate(predicate);
|
|
20
|
+
return bindColumnarPredicateParameters(predicate, parameters);
|
|
21
|
+
}
|