Skip to content

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

fix(data): sample drawable scatter coordinates before applying the mark budget

Afloate15a66c1d280
diff --git a/server/data/chart-compiler.test.ts b/server/data/chart-compiler.test.ts
index d1e15e2ae4..e3fa8b8d0d 100644
--- a/server/data/chart-compiler.test.ts
+++ b/server/data/chart-compiler.test.ts
@@ -231,7 +231,7 @@ describe("compileChart: reductions per chart type and column class", () => {
name: "scatter samples a repeatable reservoir",
spec: { type: "scatter", x: "qty", y: "amount", color: "channel", size: 'we"ird' },
method: "sample",
- sql: [`SELECT src."qty" AS x, src."amount" AS y, src."channel" AS color, src."we""ird" AS size FROM src USING SAMPLE reservoir(${DATA_LIMITS.chartMaxMarks} ROWS) REPEATABLE (42)`],
+ sql: [`SELECT src."qty" AS x, src."amount" AS y, src."channel" AS color, src."we""ird" AS size FROM (SELECT * FROM src WHERE src."qty" IS NOT NULL AND src."amount" IS NOT NULL) AS src USING SAMPLE reservoir(${DATA_LIMITS.chartMaxMarks} ROWS) REPEATABLE (42)`],
notSql: ["GROUP BY"],
check: (vl) => {
expect(mark(vl)).toBe("point");
diff --git a/server/data/chart-compiler.ts b/server/data/chart-compiler.ts
index 578f5bd4f1..2080af7e4c 100644
--- a/server/data/chart-compiler.ts
+++ b/server/data/chart-compiler.ts
@@ -428,7 +428,9 @@ function planScatter(x: Resolved, y: Resolved | undefined, color: Resolved | und
const selects = [`${x.id} AS x`, `${y.id} AS y`];
if (color) selects.push(`${color.id} AS color`);
if (size) selects.push(`${size.id} AS size`);
- const body = `SELECT ${selects.join(",\n ")}\nFROM src USING SAMPLE reservoir(${DATA_LIMITS.chartMaxMarks} ROWS) REPEATABLE (${SAMPLE_SEED})`;
+ const required = [x, y].filter((axis) => axis.cls === "numeric" || axis.cls === "temporal");
+ const source = required.length ? `(SELECT * FROM src WHERE ${required.map((axis) => `${axis.id} IS NOT NULL`).join(" AND ")}) AS src` : "src";
+ const body = `SELECT ${selects.join(",\n ")}\nFROM ${source} USING SAMPLE reservoir(${DATA_LIMITS.chartMaxMarks} ROWS) REPEATABLE (${SAMPLE_SEED})`;
const encoding: Encoding = {
x: { field: "x", type: vegaType(x), title: x.column.name },
y: { field: "y", type: vegaType(y), title: y.column.name },
diff --git a/server/data/scatter-missing-coordinates.test.ts b/server/data/scatter-missing-coordinates.test.ts
new file mode 100644
index 0000000000..e7c2d57534
--- /dev/null
+++ b/server/data/scatter-missing-coordinates.test.ts
@@ -0,0 +1,46 @@
+import { DuckDBInstance, type DuckDBConnection } from "@duckdb/node-api";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { compileChart } from "./chart-compiler.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 scatter(sql: string) {
+ 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 compiled = compileChart({ type: "scatter", x: "horizontal", y: "vertical" }, columns, source);
+ return (await connection.runAndReadAll(compiled.sql)).getRowsJson();
+}
+
+describe("scatter sampling with missing coordinates", () => {
+ it("retains every drawable numeric point when missing rows exceed the sample budget", async () => {
+ const rows = await scatter("SELECT NULL::INTEGER AS horizontal, i AS vertical FROM range(100000) t(i) UNION ALL SELECT i, i FROM range(40) t(i)");
+ expect(rows).toHaveLength(40);
+ expect(rows.map(([x]) => Number(x)).sort((a, b) => a - b)).toEqual(Array.from({ length: 40 }, (_, i) => i));
+ });
+
+ it("filters missing temporal coordinates before sampling", async () => {
+ const rows = await scatter("SELECT NULL::DATE AS horizontal, i AS vertical FROM range(100000) t(i) UNION ALL SELECT DATE '2024-01-01' + INTERVAL (i) DAY, i FROM range(40) t(i)");
+ expect(rows).toHaveLength(40);
+ expect(rows.every(([x]) => x !== null)).toBe(true);
+ });
+
+ it("does not discard a missing nominal category", async () => {
+ const rows = await scatter("SELECT * FROM (VALUES (NULL::VARCHAR, 1), ('named', 2)) t(horizontal, vertical)");
+ expect(rows).toHaveLength(2);
+ expect(rows.some(([x]) => x === null)).toBe(true);
+ });
+
+ it("excludes missing y coordinates, including an entirely empty drawable set", async () => {
+ expect(await scatter("SELECT * FROM (VALUES (1, NULL::INTEGER), (2, 2)) t(horizontal, vertical)")).toEqual([[2, 2]]);
+ expect(await scatter("SELECT 1 AS horizontal, NULL::INTEGER AS vertical FROM range(100)")).toEqual([]);
+ });
+
+ it("keeps the sample bounded after filtering", async () => {
+ const rows = await scatter("SELECT i AS horizontal, i AS vertical FROM range(20000) t(i)");
+ expect(rows).toHaveLength(10000);
+ });
+});