Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(data): preserve finite chart bins around nonfinite values

Afloatcaaaee8e14a1
diff --git a/server/data/chart-compiler.test.ts b/server/data/chart-compiler.test.ts
index f739671929..720b397bab 100644
--- a/server/data/chart-compiler.test.ts
+++ b/server/data/chart-compiler.test.ts
@@ -100,8 +100,8 @@ describe("compileChart: reductions per chart type and column class", () => {
spec: { type: "bar", x: "amount", y: "qty" },
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 + 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',
+ '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 WHERE isfinite(CAST(src."amount" AS DOUBLE)) )',
+ 'bins_x.lo + CASE WHEN isfinite(CAST(src."amount" AS DOUBLE)) THEN least(floor((CAST(src."amount" AS DOUBLE) - bins_x.lo) / bins_x.width), 29) ELSE NULL 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) END * bins_x.width AS x", "count(*) AS y"],
+ sql: ["/ 30 END AS width", ", 29) ELSE NULL 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) END * bins_x.width AS x", 'avg(src."qty") AS y'],
+ sql: ["/ 10 END AS width", ", 9) ELSE NULL 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.ts
index cf1b14d8a1..8c95886d62 100644
--- a/server/data/chart-compiler.ts
+++ b/server/data/chart-compiler.ts
@@ -123,9 +123,10 @@ function binned(x: Resolved, channel: string, bins: number, bars: boolean): Dime
SELECT min(${v}) AS lo,
${width} AS width
FROM src
+ WHERE isfinite(${v})
`,
};
- const bin = `CASE WHEN ${x.id} IS NULL THEN NULL ELSE least(floor((${v} - ${cte.name}.lo) / ${cte.name}.width), ${bins - 1}) END`;
+ const bin = `CASE WHEN isfinite(${v}) THEN least(floor((${v} - ${cte.name}.lo) / ${cte.name}.width), ${bins - 1}) ELSE NULL END`;
const start = `${cte.name}.lo + ${bin} * ${cte.name}.width`;
const end = `${channel}2`;
return bars
diff --git a/server/data/chart-finite-bins.test.ts b/server/data/chart-finite-bins.test.ts
new file mode 100644
index 0000000000..9215ffa045
--- /dev/null
+++ b/server/data/chart-finite-bins.test.ts
@@ -0,0 +1,103 @@
+import { mkdtempSync, readFileSync } from "node:fs";
+import { rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { afterAll, describe, expect, it } from "vitest";
+
+import { compileChart } from "./chart-compiler.ts";
+import { createDataEngine } from "./engine.ts";
+import { chartRenderer } from "./render.ts";
+import { dataRpc, type DataRpcDeps } from "./rpc.ts";
+import { DataSheetStore } from "./sheet.ts";
+import { type DataToolResult, renderCard } from "./tools.ts";
+import type { OmbChartSpec } from "./types.ts";
+import { validateVegaLite } from "./vega-lite-validator.ts";
+
+const root = mkdtempSync(join(tmpdir(), "omb-chart-finite-"));
+const engine = createDataEngine({ botFolder: (id) => join(root, id), dataDir: join(root, "data") });
+const connection = "bot" as const;
+afterAll(async () => {
+ await engine.closeAll();
+ await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
+});
+
+async function chart(sql: string, spec: OmbChartSpec) {
+ const database = await engine.forBot("bins");
+ const columns = (await database.run(`SELECT * FROM (${sql}) LIMIT 0`, { connection })).columns;
+ const compiled = compileChart(spec, columns, `(${sql})`);
+ const result = await database.run(compiled.sql, { connection });
+ return { compiled, ...result };
+}
+
+describe("numeric chart bins with nonfinite query results", () => {
+ it.each(["bar", "histogram", "line", "area"] as const)("keeps finite %s bins when ratios include division by zero", async (type) => {
+ const result = await chart("SELECT * FROM (VALUES (1.0::DOUBLE), (2.0), (1.0/0.0), (-1.0/0.0), (0.0/0.0), (NULL)) t(value)", { type, x: "value", bins: 2 });
+ const visible = result.rows.filter(([x]) => x !== null);
+ expect(visible.map((row) => Number(row.at(-1)))).toEqual([1, 1]);
+ expect(visible.map(([x]) => x)).toEqual(type === "bar" || type === "histogram" ? [1, 1.5] : [1.25, 1.75]);
+ expect(result.rows.filter(([x]) => x === null).map((row) => Number(row.at(-1)))).toEqual([4]);
+ expect(result.rows.flat()).not.toEqual(expect.arrayContaining(["NaN", "Infinity", "-Infinity"]));
+ });
+
+ it("does not add a nonfinite coordinate's measure to a finite bar", async () => {
+ const result = await chart("SELECT * FROM (VALUES (1.0::DOUBLE, 3), (2.0, 5), (1.0/0.0, 100)) t(value, amount)", {
+ type: "bar", x: "value", y: "amount", bins: 2,
+ });
+ expect(result.rows).toEqual([[1, 1.5, "3"], [1.5, 2, "5"], [null, null, "100"]]);
+ });
+
+ it("handles both numeric heatmap axes without losing finite cells", async () => {
+ const result = await chart("SELECT * FROM (VALUES (1.0::DOUBLE, 10.0::DOUBLE), (2.0, 20.0), (1.0/0.0, 20.0), (2.0, 0.0/0.0), (-1.0/0.0, 1.0/0.0)) t(horizontal, vertical)", {
+ type: "heatmap", x: "horizontal", color: "vertical", bins: 2,
+ });
+ expect(result.rows.filter(([x, , y]) => x !== null && y !== null)).toEqual([[1, 1.5, 10, 15, "1"], [1.5, 2, 15, 20, "1"]]);
+ expect(result.rows.filter(([x, , y]) => x === null || y === null)).toHaveLength(3);
+ expect(result.rows.flat()).not.toEqual(expect.arrayContaining(["NaN", "Infinity", "-Infinity"]));
+ });
+
+ it("preserves a constant finite distribution beside NaN", async () => {
+ const result = await chart("SELECT * FROM (VALUES (7.0::DOUBLE), (7.0), (0.0/0.0)) t(value)", { type: "histogram", x: "value" });
+ expect(result.rows).toEqual([[7, 8, "2"], [null, null, "1"]]);
+ });
+
+ it("keeps all-invalid coordinates missing and empty input empty", async () => {
+ const missing = await chart("SELECT * FROM (VALUES (1.0/0.0), (-1.0/0.0), (0.0/0.0), (NULL)) t(value)", { type: "histogram", x: "value" });
+ expect(missing.rows).toEqual([[null, null, "4"]]);
+ expect((await chart("SELECT 1.0::DOUBLE AS value WHERE false", { type: "histogram", x: "value" })).rows).toEqual([]);
+ });
+
+ it("keeps ordinary whole-number bins and nonfinite-looking text categories", async () => {
+ expect((await chart("SELECT * FROM (VALUES (1), (2)) t(value)", { type: "histogram", x: "value", bins: 2 })).rows).toEqual([[1, 2, "1"], [2, 3, "1"]]);
+ expect((await chart("SELECT * FROM (VALUES ('NaN'), ('Infinity'), ('Infinity')) t(value)", { type: "bar", x: "value", sort: "x" })).rows).toEqual([["Infinity", "2"], ["NaN", "1"]]);
+ });
+
+ it("saves, reloads and exports the finite bars from a real Data MCP chart", async () => {
+ const botId = "rpc";
+ const dir = join(root, botId);
+ const database = await engine.forBot(botId);
+ const published: string[] = [];
+ const deps: DataRpcDeps = {
+ database, sheet: new DataSheetStore({ botId, dir }), compileChart, validateVegaLite, renderer: chartRenderer,
+ exportRoots: () => [root], signal: new AbortController().signal,
+ assertActive() {}, onShow: (card) => published.push(card.id),
+ };
+ const call = (name: string, args: Record<string, unknown>) => dataRpc({ method: "tools/call", params: { name, arguments: args } }, deps) as Promise<DataToolResult>;
+ const shown = await call("data_show", {
+ kind: "chart", title: "Ratios", chart: { type: "histogram", x: "ratio", bins: 2 },
+ sql: "SELECT numerator / denominator AS ratio FROM (VALUES (1.0, 1.0), (2.0, 1.0), (1.0, 0.0), (0.0, 0.0)) t(numerator, denominator)",
+ });
+ expect(shown.isError).not.toBe(true);
+ const id = String(shown.structuredContent?.id);
+ expect(published).toEqual([id]);
+ deps.sheet = new DataSheetStore({ botId, dir });
+ const card = deps.sheet.card(id)!;
+ expect(card).toMatchObject({ status: "ready", reduction: { inputRows: 4 }, chart: { type: "histogram" } });
+ const saved = await database.page(`omb_results."${card.result}"`, { offset: 0, limit: 10 });
+ expect(saved.rows).toEqual([[1, 1.5, "1"], [1.5, 2, "1"], [null, null, "2"]]);
+ const svg = String(await renderCard({ ...deps, by: "bot", connection }, card, "svg"));
+ expect(svg.match(/aria-roledescription="bar"/g)).toHaveLength(2);
+ const exported = await call("data_export", { id, format: "svg", path: join(root, "ratios.svg") });
+ expect(exported.isError).not.toBe(true);
+ expect(readFileSync(String(exported.structuredContent?.path), "utf8")).toBe(svg);
+ });
+});