Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(data): keep null coordinates out of numeric chart bins
Afloat0a8e9666572a
diff --git a/server/data/chart-compiler.test.ts b/server/data/chart-compiler.test.tsindex d1e15e2ae4..e6e662d288 100644--- a/server/data/chart-compiler.test.ts+++ b/server/data/chart-compiler.test.ts@@ -101,7 +101,7 @@ describe("compileChart: reductions per chart type and column class", () => {method: "bins",sql: ['bins_x AS ( SELECT min(CAST(src."amount" AS DOUBLE)) AS lo, CASE WHEN max(CAST(src."amount" AS DOUBLE)) = min(CAST(src."amount" AS DOUBLE)) THEN 1 ELSE (max(CAST(src."amount" AS DOUBLE)) - min(CAST(src."amount" AS DOUBLE))) / 30 END AS width FROM src )',- 'bins_x.lo + least(floor((CAST(src."amount" AS DOUBLE) - bins_x.lo) / bins_x.width), 29) * bins_x.width AS x',+ 'bins_x.lo + CASE WHEN src."amount" IS NULL THEN NULL ELSE least(floor((CAST(src."amount" AS DOUBLE) - bins_x.lo) / bins_x.width), 29) END * bins_x.width AS x',"+ 1) * bins_x.width AS x2",'sum(src."qty") AS y FROM src, bins_x GROUP BY ALL ORDER BY x',],@@ -213,7 +213,7 @@ describe("compileChart: reductions per chart type and column class", () => {name: "histogram counts into 30 equal-width bins",spec: { type: "histogram", x: "amount" },method: "bins",- sql: ["/ 30 END AS width", ", 29) * bins_x.width AS x", "count(*) AS y"],+ sql: ["/ 30 END AS width", ", 29) END * bins_x.width AS x", "count(*) AS y"],check: (vl) => {expect(mark(vl)).toBe("bar");expect(enc(vl).x).toMatchObject({ bin: "binned" });@@ -224,7 +224,7 @@ describe("compileChart: reductions per chart type and column class", () => {name: "histogram honours bins and an aggregate",spec: { type: "histogram", x: "amount", y: "qty", agg: "avg", bins: 10 },method: "bins",- sql: ["/ 10 END AS width", ", 9) * bins_x.width AS x", 'avg(src."qty") AS y'],+ sql: ["/ 10 END AS width", ", 9) END * bins_x.width AS x", 'avg(src."qty") AS y'],check: (vl) => expect(enc(vl).y.title).toBe("avg(qty)"),},{diff --git a/server/data/chart-compiler.ts b/server/data/chart-compiler.tsindex 578f5bd4f1..d1d63aa2de 100644--- a/server/data/chart-compiler.ts+++ b/server/data/chart-compiler.ts@@ -125,7 +125,7 @@ function binned(x: Resolved, channel: string, bins: number, bars: boolean): DimeFROM src`,};- const bin = `least(floor((${v} - ${cte.name}.lo) / ${cte.name}.width), ${bins - 1})`;+ const bin = `CASE WHEN ${x.id} IS NULL THEN NULL ELSE least(floor((${v} - ${cte.name}.lo) / ${cte.name}.width), ${bins - 1}) END`;const start = `${cte.name}.lo + ${bin} * ${cte.name}.width`;const end = `${channel}2`;return barsdiff --git a/server/data/chart-null-bins.test.ts b/server/data/chart-null-bins.test.tsnew file mode 100644index 0000000000..5eb8d6b042--- /dev/null+++ b/server/data/chart-null-bins.test.ts@@ -0,0 +1,61 @@+import { DuckDBInstance, type DuckDBConnection } from "@duckdb/node-api";+import { afterAll, beforeAll, describe, expect, it } from "vitest";++import { compileChart } from "./chart-compiler.ts";+import type { OmbChartSpec } from "./types.ts";++let instance: DuckDBInstance;+let connection: DuckDBConnection;++beforeAll(async () => {+ instance = await DuckDBInstance.create(":memory:");+ connection = await instance.connect();+});+afterAll(() => {+ connection?.closeSync();+ instance?.closeSync();+});++async function rows(sql: string, spec: OmbChartSpec) {+ const source = `(${sql})`;+ const described = await connection.run(`SELECT * FROM ${source} LIMIT 0`);+ const columns = Array.from({ length: described.columnCount }, (_, i) => ({+ name: described.columnName(i), type: described.columnType(i).toString(),+ }));+ const result = await connection.runAndReadAll(compileChart(spec, columns, source).sql);+ return result.getRowsJson();+}++describe("missing numeric chart coordinates", () => {+ it.each(["bar", "histogram", "line", "area"] as const)("keeps null values separate from the final %s bin", async (type) => {+ const result = await rows("SELECT * FROM (VALUES (1), (2), (NULL)) t(value)", { type, x: "value", bins: 2 });+ const visible = result.filter(([x]) => x !== null);+ expect(visible.map((row) => Number(row.at(-1)))).toEqual([1, 1]);+ const missing = result.filter(([x]) => x === null);+ expect(missing).toHaveLength(1);+ expect(Number(missing[0]!.at(-1))).toBe(1);+ if (type === "bar" || type === "histogram") expect(missing[0]![1]).toBeNull();+ });++ it("does not add a missing coordinate's measure to a real numeric bin", async () => {+ const result = await rows("SELECT * FROM (VALUES (1.0, 3), (2.0, 5), (NULL, 100)) t(value, amount)", {+ type: "bar", x: "value", y: "amount", bins: 2,+ });+ expect(result.filter(([x]) => x !== null).map((row) => Number(row.at(-1)))).toEqual([3, 5]);+ expect(Number(result.find(([x]) => x === null)?.at(-1))).toBe(100);+ });++ it("keeps missing coordinates out of both numeric heatmap axes", async () => {+ const result = await rows("SELECT * FROM (VALUES (1, 10), (2, 20), (NULL, 20), (2, NULL), (NULL, NULL)) t(horizontal, vertical)", {+ type: "heatmap", x: "horizontal", color: "vertical", bins: 2,+ });+ expect(result.filter(([x, , y]) => x !== null && y !== null).map((row) => Number(row.at(-1)))).toEqual([1, 1]);+ expect(result.filter(([x, , y]) => x === null || y === null)).toHaveLength(3);+ });++ it("preserves all-null and empty numeric input", async () => {+ const missing = await rows("SELECT NULL::INTEGER AS value FROM range(3)", { type: "histogram", x: "value" });+ expect(missing).toEqual([[null, null, "3"]]);+ expect(await rows("SELECT 1 AS value WHERE false", { type: "histogram", x: "value" })).toEqual([]);+ });+});