Skip to content

An open source alternative to Tableau. Embeddable visual analytic

test: cover null aggregate workflow divergences

Elwynn Chen893db1282d6e
diff --git a/computation.md b/computation.md
index 5dc5baaa..a8503a23 100644
--- a/computation.md
+++ b/computation.md
@@ -17,7 +17,7 @@ export interface IDataQueryPayload {
A payload is evaluated against an ordered array of rows. Each row is a record keyed by field id (`fid`). Workflow steps are evaluated strictly in array order. The output of each step becomes the input to the next step.
-`offset` and `limit` are applied after all workflow steps, including `sort`. `offset` defaults to `0`. A missing `limit`, `undefined`, `0`, or a negative UI sentinel means no upper bound. When `limit > 0`, the final slice is `[offset, offset + limit)`.
+`offset` and `limit` are applied after all workflow steps, including `sort`. `offset` defaults to `0`. A missing `limit`, `undefined`, `0`, or a negative UI sentinel means no upper bound. When `limit > 0`, the final slice is `[offset, offset + limit)`. Historical client builds used JavaScript truthiness for `limit`, which made `limit: -1` drop the final row; conforming implementations must use the `limit > 0` rule instead.
Workflow steps may include multiple `view` steps. They cascade exactly like any other step: the rows produced by the first `view` step are the rows consumed by the next step.
@@ -187,14 +187,14 @@ Aggregators:
- `count`: count of rows in the group, including rows where the measured field is nullish.
- `max`: maximum by numeric comparison.
- `min`: minimum by numeric comparison.
-- `mean`: `sum(values) / rowCount`.
-- `median`: middle value after ascending numeric sort, or the average of the two middle values.
+- `mean`: `sum(values) / rowCount`. The current phase-1 semantics keeps nullish rows in the value array; `null` participates through JavaScript numeric coercion, while `undefined` can produce `NaN`.
+- `median`: middle value after ascending numeric sort, or the average of the two middle values. Nullish rows are retained under the same comparator; this means `null` sorts as a numeric zero in the current client implementation.
- `variance`: population variance, `mean((x - mean(values)) ** 2)`.
- `stdev`: square root of population variance.
- `distinctCount`: count of distinct values using SameValueZero semantics; `null` counts as one distinct value and `undefined` counts as one distinct value.
- `expr`: evaluate the SQL-like aggregate expression over the group. It must return a scalar; returning an array is an error.
-Aggregators other than `count`, `distinctCount`, and `expr` require numeric inputs. Their nullish behavior is defined by the operator formula above; implementations should converge on SQL-style null exclusion in a later spec revision, but current conformance records existing differences where they occur.
+Aggregators other than `count`, `distinctCount`, and `expr` require numeric inputs. The current normative null behavior follows the client executor for compatibility: nullish rows are not excluded before aggregation unless an operator explicitly says so. SQL-style null exclusion would be a future breaking semantic revision; current backend differences are recorded in `docs/computation-divergences.md`.
### `fold`
@@ -224,6 +224,6 @@ Nullish values sort by their string representation under the same comparator. Im
## 8. Coverage Notes
-The phase-1 conformance suite covers filter range, temporal range, one-of/not-in, regexp, transform bin/binCount/log/one/date drills/date features/expr, all aggregate operators, group-by nulls, raw, sort with limit/offset, special-character fields, numeric strings, empty data, single-row data, null-heavy data, a `toWorkflow()` generated pipeline, and multi-view cascading.
+The phase-1 conformance suite covers filter range, temporal range, one-of/not-in, regexp, transform bin/binCount/log/one/date drills/date features/expr, non-zero `offset`/`displayOffset` combinations, all aggregate operators, aggregate null handling, group-by nulls, raw, sort with limit/offset, special-character fields, numeric strings, empty data, single-row data, null-heavy data, a `toWorkflow()` generated pipeline, and multi-view cascading.
Known gaps are recorded in `docs/computation-divergences.md`. The main not-yet-covered executable areas are `paint` and full expression-language conformance; both require a backend capability contract beyond the current parser behavior.
diff --git a/docs/computation-divergences.md b/docs/computation-divergences.md
index ac428fd8..f356298d 100644
--- a/docs/computation-divergences.md
+++ b/docs/computation-divergences.md
@@ -286,7 +286,7 @@ Client result: population variance `1` and stdev `1`.
SQL result: generated SQL currently uses DuckDB aggregate behavior that does not match the client for these cases.
-Normative decision: client semantics, because `computation.md` defines population variance and stdev.
+Normative decision: client semantics, because `computation.md` defines population variance and stdev. This decision should be revisited before a broader backend-compatibility release because many SQL engines and BI tools default to sample variance/stdev.
Test handling: `test.failing`.
@@ -316,7 +316,50 @@ Client result: nullish values are compared by `String(value)`.
SQL result: DuckDB uses database null ordering unless the generated SQL makes null ordering explicit.
-Normative decision: client comparator semantics; SQL generation should make null position explicit.
+Normative decision: client comparator semantics; SQL generation should make null position explicit. This decision intentionally documents current behavior, but a future spec revision should consider explicit `NULLS FIRST` / `NULLS LAST` semantics instead of stringifying nullish values.
+
+Test handling: `test.failing`.
+
+## DVG-012: Aggregate Null Handling Differs For Common Aggregators
+
+Minimal payload:
+
+```json
+{
+ "workflow": [
+ {
+ "type": "view",
+ "query": [
+ {
+ "op": "aggregate",
+ "groupBy": ["segment"],
+ "measures": [
+ { "field": "value", "agg": "mean", "asFieldKey": "mean_value" },
+ { "field": "value", "agg": "count", "asFieldKey": "count_value" },
+ { "field": "value", "agg": "median", "asFieldKey": "median_value" }
+ ]
+ }
+ ]
+ }
+ ]
+}
+```
+
+Data:
+
+```json
+[
+ { "segment": "A", "value": 1 },
+ { "segment": "A", "value": null },
+ { "segment": "A", "value": 3 }
+]
+```
+
+Client result: `mean_value = 1.3333333333333333`, `count_value = 3`, `median_value = 1`.
+
+SQL result: `mean_value = 2`, `count_value = 2`, `median_value = 2`.
+
+Normative decision: client semantics for phase 1. `count` explicitly includes rows where the measured field is nullish; `mean` and `median` currently retain nullish rows and apply the client numeric comparator/coercion behavior. A later SQL-style null-exclusion revision would be breaking and should be made deliberately.
Test handling: `test.failing`.
diff --git a/packages/computation-conformance/src/workflow.conformance.test.ts b/packages/computation-conformance/src/workflow.conformance.test.ts
index b34ed997..42e0b201 100644
--- a/packages/computation-conformance/src/workflow.conformance.test.ts
+++ b/packages/computation-conformance/src/workflow.conformance.test.ts
@@ -207,6 +207,52 @@ describe('workflow computation conformance', () => {
});
});
+ test.each([
+ {
+ name: 'non-zero offset',
+ params: [
+ { type: 'offset' as const, value: 480 },
+ { type: 'displayOffset' as const, value: 0 },
+ ],
+ },
+ {
+ name: 'non-zero displayOffset',
+ params: [
+ { type: 'offset' as const, value: 0 },
+ { type: 'displayOffset' as const, value: 480 },
+ ],
+ },
+ {
+ name: 'different offset/displayOffset',
+ params: [
+ { type: 'offset' as const, value: 480 },
+ { type: 'displayOffset' as const, value: -300 },
+ ],
+ },
+ ])('dateTimeDrill day conforms with $name', async ({ params }) => {
+ await expectConformance(baseRows, {
+ workflow: [
+ {
+ type: 'transform',
+ transform: [
+ {
+ key: 'day',
+ expression: {
+ op: 'dateTimeDrill',
+ as: 'day',
+ params: [{ type: 'field', value: 'ts' }, { type: 'value', value: 'day' }, ...params],
+ },
+ },
+ ],
+ },
+ {
+ type: 'view',
+ query: [{ op: 'raw', fields: ['id', 'category', 'segment', 'value', 'value2', 'ts', 'nullable', 'field (quoted)', '中文', 'day'] }],
+ },
+ ],
+ });
+ });
+
test.each(['year', 'quarter', 'month', 'weekday', 'day', 'hour', 'minute', 'second'] as const)('dateTimeFeature %s conforms', async (level) => {
await expectConformance(baseRows, {
workflow: [
@@ -265,6 +311,52 @@ describe('workflow computation conformance', () => {
});
});
+ test.each([
+ {
+ name: 'non-zero offset',
+ params: [
+ { type: 'offset' as const, value: 480 },
+ { type: 'displayOffset' as const, value: 0 },
+ ],
+ },
+ {
+ name: 'non-zero displayOffset',
+ params: [
+ { type: 'offset' as const, value: 0 },
+ { type: 'displayOffset' as const, value: 480 },
+ ],
+ },
+ {
+ name: 'different offset/displayOffset',
+ params: [
+ { type: 'offset' as const, value: 480 },
+ { type: 'displayOffset' as const, value: -300 },
+ ],
+ },
+ ])('dateTimeFeature hour conforms with $name', async ({ params }) => {
+ await expectConformance(baseRows, {
+ workflow: [
+ {
+ type: 'transform',
+ transform: [
+ {
+ key: 'hour',
+ expression: {
+ op: 'dateTimeFeature',
+ as: 'hour',
+ params: [{ type: 'field', value: 'ts' }, { type: 'value', value: 'hour' }, ...params],
+ },
+ },
+ ],
+ },
+ {
+ type: 'view',
+ query: [{ op: 'raw', fields: ['id', 'category', 'segment', 'value', 'value2', 'ts', 'nullable', 'field (quoted)', '中文', 'hour'] }],
+ },
+ ],
+ });
+ });
+
test.failing.each(['iso_year', 'iso_week', 'iso_weekday'] as const)('DVG-007: dateTimeFeature %s uses ISO calendar semantics', async (level) => {
await expectConformance([{ ts: Date.UTC(2021, 0, 1) }, { ts: Date.UTC(2021, 0, 4) }], {
workflow: [
@@ -347,6 +439,34 @@ describe('workflow computation conformance', () => {
);
});
+ test.failing('DVG-012: mean/count/median retain null rows under client semantics', async () => {
+ await expectConformance(
+ [
+ { segment: 'A', value: 1 },
+ { segment: 'A', value: null },
+ { segment: 'A', value: 3 },
+ ],
+ {
+ workflow: [
+ {
+ type: 'view',
+ query: [
+ {
+ op: 'aggregate',
+ groupBy: ['segment'],
+ measures: [
+ { field: 'value', agg: 'mean', asFieldKey: 'mean_value' },
+ { field: 'value', agg: 'count', asFieldKey: 'count_value' },
+ { field: 'value', agg: 'median', asFieldKey: 'median_value' },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ );
+ });
+
test('sort supports multiple keys with limit and offset at pipeline end', async () => {
await expectConformance(
[
diff --git a/packages/graphic-walker/src/computation/clientComputation.ts b/packages/graphic-walker/src/computation/clientComputation.ts
index 716fae3d..26054e53 100644
--- a/packages/graphic-walker/src/computation/clientComputation.ts
+++ b/packages/graphic-walker/src/computation/clientComputation.ts
@@ -44,7 +44,7 @@ export const dataQueryClient = async (
}
}
}
- return res.slice(offset ?? 0, limit ? ((offset ?? 0) + limit) : undefined);
+ return res.slice(offset ?? 0, limit && limit > 0 ? (offset ?? 0) + limit : undefined);
};
export const getComputation = (rawData: IRow[]) => (payload: IDataQueryPayload) => dataQueryClient(rawData, payload.workflow, payload.offset, payload.limit)