Skip to content

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

fix(data): exclude nonfinite coordinates from inspector histograms

Afloat3dfda815c73e
diff --git a/server/data/engine.ts b/server/data/engine.ts
index 857f796036..e326dcbfbb 100644
--- a/server/data/engine.ts
+++ b/server/data/engine.ts
@@ -584,9 +584,12 @@ class BotDb implements BotDatabase {
if (numeric || temporal) {
const sourceId = `omb_source.${id}`;
const value = numeric ? `CAST(${sourceId} AS DOUBLE)` : `epoch(CAST(${sourceId} AS TIMESTAMP))`;
- const sql = `WITH omb_range AS (SELECT min(${value}) AS lo, max(${value}) AS hi FROM ${from} AS omb_source WHERE ${sourceId} IS NOT NULL)
+ // NaN/infinity cannot define a finite bucket. Infinite dates produce
+ // NULL epochs; they must not be counted in the constant/first bucket.
+ const finite = `isfinite(${value})`;
+ const sql = `WITH omb_range AS (SELECT min(${value}) AS lo, max(${value}) AS hi FROM ${from} AS omb_source WHERE ${finite})
SELECT LEAST(coalesce(floor((${value} - omb_range.lo) / nullif(omb_range.hi - omb_range.lo, 0) * ${bins}), 0), ${bins - 1})::INTEGER AS bin, count(*) AS n, min(omb_range.lo), min(omb_range.hi)
-FROM ${from} AS omb_source, omb_range WHERE ${sourceId} IS NOT NULL GROUP BY bin ORDER BY bin`;
+FROM ${from} AS omb_source, omb_range WHERE ${finite} GROUP BY bin ORDER BY bin`;
const rows = (await this.statement("panel", sql, options, (o, conn) => this.read(o, conn, sql))).rows;
if (!rows.length) return result;
const lo = Number(rows[0][2]);
diff --git a/server/data/histogram-nonfinite.test.ts b/server/data/histogram-nonfinite.test.ts
new file mode 100644
index 0000000000..c5942b680e
--- /dev/null
+++ b/server/data/histogram-nonfinite.test.ts
@@ -0,0 +1,53 @@
+import { mkdtempSync } 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 { createDataEngine } from "./engine.ts";
+
+const root = mkdtempSync(join(tmpdir(), "omb-histogram-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 });
+});
+
+describe("histograms with non-finite numeric and temporal values", () => {
+ it("keeps finite buckets when floating data includes NaN and both infinities", async () => {
+ const db = await engine.forBot("mixed-numeric");
+ await db.run("CREATE TABLE samples AS SELECT * FROM (VALUES (1.0::DOUBLE), (2.0), ('NaN'::DOUBLE), ('Infinity'::DOUBLE), ('-Infinity'::DOUBLE), (NULL)) t(value)", { connection });
+ expect((await db.histogram("samples", "value", { bins: 2 })).bins).toEqual([
+ { label: "1 – 1.5", count: 1 }, { label: "1.5 – 2", count: 1 },
+ ]);
+ });
+
+ it.each(["DOUBLE", "DATE", "TIMESTAMP"])("returns no buckets for an entirely non-finite %s column", async (type) => {
+ const db = await engine.forBot(`empty-${type.toLowerCase()}`);
+ await db.run(`CREATE TABLE samples AS SELECT * FROM (VALUES ('infinity'::${type}), ('-infinity'::${type}), (NULL)) t(value)`, { connection });
+ expect((await db.histogram("samples", "value", { bins: 4 })).bins).toEqual([]);
+ });
+
+ it("preserves a constant finite distribution beside NaN", async () => {
+ const db = await engine.forBot("constant-numeric");
+ await db.run("CREATE TABLE samples AS SELECT * FROM (VALUES (7.0::DOUBLE), (7.0), ('NaN'::DOUBLE), ('Infinity'::DOUBLE)) t(value)", { connection });
+ expect((await db.histogram("samples", "value", { bins: 10 })).bins).toEqual([{ label: "7", count: 2 }]);
+ });
+
+ it.each(["DATE", "TIMESTAMP"])("does not count infinite %s values in the first finite bucket", async (type) => {
+ const db = await engine.forBot(`mixed-${type.toLowerCase()}`);
+ await db.run(`CREATE TABLE samples AS SELECT * FROM (VALUES ('2026-01-01'::${type}), ('2026-01-05'::${type}), ('infinity'::${type}), ('-infinity'::${type}), (NULL)) t(value)`, { connection });
+ const bins = (await db.histogram("samples", "value", { bins: 2 })).bins;
+ expect(bins.map((bin) => bin.count)).toEqual([1, 1]);
+ expect(bins[0].label).toMatch(/^2026-01-01/);
+ expect(bins[1].label).toMatch(/^2026-01-03/);
+ });
+
+ it("retains non-finite-looking strings as ordinary categorical labels", async () => {
+ const db = await engine.forBot("strings");
+ await db.run("CREATE TABLE samples AS SELECT * FROM (VALUES ('NaN'), ('Infinity'), ('Infinity'), ('-Infinity'), (NULL)) t(value)", { connection });
+ expect((await db.histogram("samples", "value")).bins).toEqual([
+ { label: "Infinity", count: 2 }, { label: "-Infinity", count: 1 }, { label: "NaN", count: 1 },
+ ]);
+ });
+});