A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: bin step was too small for large bin values (#5924)
Shahmir Varqhaefdb6443c467
diff --git a/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap b/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snapindex e77110bc3c..6d2f56d945 100644--- a/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap+++ b/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap@@ -164,7 +164,7 @@ exports[`ColumnChartSpecModel > should expect bin values to be used for number a"x": {"bin": {"binned": true,- "step": 2,+ "step": 10,},"field": "bin_start","type": "quantitative",@@ -224,7 +224,7 @@ exports[`ColumnChartSpecModel > should expect bin values to be used for number a},"bin": {"binned": true,- "step": 2,+ "step": 10,},"field": "bin_start","type": "quantitative",@@ -485,7 +485,7 @@ exports[`ColumnChartSpecModel > should handle datetime bin values when feat flag"axis": null,"bin": {"binned": true,- "step": 2,+ "step": 86400000,},"field": "bin_start","type": "temporal",@@ -544,7 +544,7 @@ exports[`ColumnChartSpecModel > should handle datetime bin values when feat flag"axis": null,"bin": {"binned": true,- "step": 2,+ "step": 86400000,},"field": "bin_start","type": "temporal",@@ -553,7 +553,7 @@ exports[`ColumnChartSpecModel > should handle datetime bin values when feat flag"axis": null,"bin": {"binned": true,- "step": 2,+ "step": 86400000,},"field": "bin_end","type": "temporal",diff --git a/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts b/frontend/src/components/data-table/__tests__/chart-spec-model.test.tsindex 695f24f8e3..f7c920650f 100644--- a/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts+++ b/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts@@ -1,6 +1,7 @@/* Copyright 2024 Marimo. All rights reserved. */import { describe, expect, it, vi } from "vitest";import { ColumnChartSpecModel } from "../column-summary/chart-spec-model";+import { calculateBinStep } from "../column-summary/utils";import type {BinValues,ColumnHeaderStats,@@ -439,3 +440,268 @@ describe("ColumnChartSpecModel", () => {});});});++describe("calculateBinStep", () => {+ describe("numeric data", () => {+ it("should calculate proper step for numeric data", () => {+ const values: BinValues = [+ { bin_start: 0, bin_end: 10, count: 5 },+ { bin_start: 10, bin_end: 20, count: 8 },+ { bin_start: 20, bin_end: 30, count: 12 },+ { bin_start: 30, bin_end: 40, count: 6 },+ { bin_start: 40, bin_end: 50, count: 3 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 50, 5 bins, so 50/5 = 10+ });++ it("should handle zero range", () => {+ const values: BinValues = [+ { bin_start: 5, bin_end: 5, count: 10 },+ { bin_start: 5, bin_end: 5, count: 15 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1); // Minimum step size when range is 0+ });++ it("should handle very small ranges", () => {+ const values: BinValues = [+ { bin_start: 0.001, bin_end: 0.002, count: 5 },+ { bin_start: 0.002, bin_end: 0.003, count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1); // Minimum step size when calculated step is too small+ });++ it("should handle very large ranges", () => {+ const values: BinValues = [+ { bin_start: 0, bin_end: 1_000_000, count: 5 },+ { bin_start: 1_000_000, bin_end: 2_000_000, count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1_000_000); // Range is 2_000_000, 2 bins, so 2_000_000/2 = 1_000_000+ });++ it("should use actual number of bins", () => {+ const values: BinValues = [+ { bin_start: 0, bin_end: 10, count: 5 },+ { bin_start: 10, bin_end: 20, count: 8 },+ { bin_start: 20, bin_end: 30, count: 12 },+ { bin_start: 30, bin_end: 40, count: 6 },+ { bin_start: 40, bin_end: 50, count: 3 },+ { bin_start: 50, bin_end: 60, count: 7 },+ { bin_start: 60, bin_end: 70, count: 9 },+ { bin_start: 70, bin_end: 80, count: 4 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 80, 8 bins, so 80/8 = 10+ });+ });++ describe("string date data", () => {+ it("should calculate proper step for string dates", () => {+ const values: BinValues = [+ { bin_start: "2023-01-01", bin_end: "2023-01-02", count: 5 },+ { bin_start: "2023-01-02", bin_end: "2023-01-03", count: 8 },+ { bin_start: "2023-01-03", bin_end: "2023-01-04", count: 12 },+ { bin_start: "2023-01-04", bin_end: "2023-01-05", count: 6 },+ ];++ const step = calculateBinStep(values);+ const oneDayMs = 24 * 60 * 60 * 1000;+ expect(step).toBe(oneDayMs); // 4 days range, 4 bins, so 4 days / 4 = 1 day in ms+ });++ it("should handle datetime strings", () => {+ const values: BinValues = [+ {+ bin_start: "2023-01-01 00:00:00",+ bin_end: "2023-01-01 01:00:00",+ count: 5,+ },+ {+ bin_start: "2023-01-01 01:00:00",+ bin_end: "2023-01-01 02:00:00",+ count: 8,+ },+ {+ bin_start: "2023-01-01 02:00:00",+ bin_end: "2023-01-01 03:00:00",+ count: 12,+ },+ ];++ const step = calculateBinStep(values);+ const oneHourMs = 60 * 60 * 1000;+ expect(step).toBe(oneHourMs); // 3 hours range, 3 bins, so 3 hours / 3 = 1 hour in ms+ });++ it("should handle ISO datetime strings", () => {+ const values: BinValues = [+ {+ bin_start: "2023-01-01T00:00:00Z",+ bin_end: "2023-01-01T01:00:00Z",+ count: 5,+ },+ {+ bin_start: "2023-01-01T01:00:00Z",+ bin_end: "2023-01-01T02:00:00Z",+ count: 8,+ },+ ];++ const step = calculateBinStep(values);+ const oneHourMs = 60 * 60 * 1000;+ expect(step).toBe(oneHourMs); // 2 hours range, 2 bins, so 2 hours / 2 = 1 hour in ms+ });+ });++ describe("Date object data", () => {+ it("should calculate proper step for Date objects", () => {+ const values: BinValues = [+ {+ bin_start: new Date("2023-01-01"),+ bin_end: new Date("2023-01-02"),+ count: 5,+ },+ {+ bin_start: new Date("2023-01-02"),+ bin_end: new Date("2023-01-03"),+ count: 8,+ },+ {+ bin_start: new Date("2023-01-03"),+ bin_end: new Date("2023-01-04"),+ count: 12,+ },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(24 * 60 * 60 * 1000); // 3 days range, 3 bins, so 3 days / 3 = 1 day in ms+ });+ });++ describe("edge cases", () => {+ it("should handle empty array", () => {+ const values: BinValues = [];+ const step = calculateBinStep(values);+ expect(step).toBe(1);+ });++ it("should handle array with only null values", () => {+ const values: BinValues = [+ { bin_start: null, bin_end: null, count: 5 },+ { bin_start: null, bin_end: null, count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1);+ });++ it("should handle mixed null and valid values", () => {+ const values: BinValues = [+ { bin_start: null, bin_end: null, count: 5 },+ { bin_start: 0, bin_end: 10, count: 8 },+ { bin_start: 10, bin_end: 20, count: 12 },+ { bin_start: null, bin_end: null, count: 6 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 20, 2 valid bins, so 20/2 = 10+ });++ it("should handle single valid value", () => {+ const values: BinValues = [{ bin_start: 5, bin_end: 15, count: 10 }];++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 10, 1 bin, so 10/1 = 10+ });++ it("should handle very small step sizes", () => {+ const values: BinValues = [+ { bin_start: 0.0001, bin_end: 0.0002, count: 5 },+ { bin_start: 0.0002, bin_end: 0.0003, count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1); // Minimum step size when calculated step is too small+ });++ it("should handle very large step sizes", () => {+ const values: BinValues = [+ { bin_start: 0, bin_end: 1_000_000, count: 5 },+ { bin_start: 1_000_000, bin_end: 2_000_000, count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(step).toBe(1_000_000); // Range is 2_000_000, 2 bins, so 2_000_000/2 = 1_000_000+ });++ it("should handle single bin", () => {+ const values: BinValues = [{ bin_start: 0, bin_end: 10, count: 5 }];++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 10, 1 bin, so 10/1 = 10+ });++ it("should handle many bins", () => {+ const values: BinValues = Array.from({ length: 100 }, (_, i) => ({+ bin_start: i * 10,+ bin_end: (i + 1) * 10,+ count: Math.floor(Math.random() * 20) + 1,+ }));++ const step = calculateBinStep(values);+ expect(step).toBe(10); // Range is 1000, 100 bins, so 1000/100 = 10+ });+ });++ describe("data type consistency", () => {+ it("should handle consistent numeric types", () => {+ const values: BinValues = [+ { bin_start: 0, bin_end: 10, count: 5 },+ { bin_start: 10, bin_end: 20, count: 8 },+ { bin_start: 20, bin_end: 30, count: 12 },+ ];++ const step = calculateBinStep(values);+ expect(typeof step).toBe("number");+ expect(step).toBe(10); // Range is 30, 3 bins, so 30/3 = 10+ });++ it("should handle consistent string date types", () => {+ const values: BinValues = [+ { bin_start: "2023-01-01", bin_end: "2023-01-02", count: 5 },+ { bin_start: "2023-01-02", bin_end: "2023-01-03", count: 8 },+ ];++ const step = calculateBinStep(values);+ expect(typeof step).toBe("number");+ expect(step).toBe(24 * 60 * 60 * 1000); // 2 days range, 2 bins, so 2 days / 2 = 1 day in ms+ });++ it("should handle consistent Date object types", () => {+ const values: BinValues = [+ {+ bin_start: new Date("2023-01-01"),+ bin_end: new Date("2023-01-02"),+ count: 5,+ },+ {+ bin_start: new Date("2023-01-02"),+ bin_end: new Date("2023-01-03"),+ count: 8,+ },+ ];++ const step = calculateBinStep(values);+ expect(typeof step).toBe("number");+ expect(step).toBe(24 * 60 * 60 * 1000); // 2 days range, 2 bins, so 2 days / 2 = 1 day in ms+ });+ });+});diff --git a/frontend/src/components/data-table/column-summary/chart-spec-model.tsx b/frontend/src/components/data-table/column-summary/chart-spec-model.tsxindex ed3b5338c0..bae4ee2578 100644--- a/frontend/src/components/data-table/column-summary/chart-spec-model.tsx+++ b/frontend/src/components/data-table/column-summary/chart-spec-model.tsx@@ -28,7 +28,7 @@ import {getLegacyNumericSpec,getLegacyTemporalSpec,} from "./legacy-chart-spec";-import { getPartialTimeTooltip } from "./utils";+import { calculateBinStep, getPartialTimeTooltip } from "./utils";// We rely on vega's built-in binning to determine bar widths.const MAX_BAR_HEIGHT = 20; // px@@ -198,6 +198,8 @@ export class ColumnChartSpecModel<T> {return getLegacyTemporalSpec(column, type, base, scale);}+ const binStep = calculateBinStep(binValues || []);+const tooltip = getPartialTimeTooltip(binValues || []);const singleValue = binValues?.length === 1;@@ -261,7 +263,7 @@ export class ColumnChartSpecModel<T> {x: {field: "bin_start",type: "temporal",- bin: { binned: true, step: 2 },+ bin: { binned: true, step: binStep },axis: null,},x2: {@@ -295,13 +297,13 @@ export class ColumnChartSpecModel<T> {x: {field: "bin_start",type: "temporal",- bin: { binned: true, step: 2 },+ bin: { binned: true, step: binStep },axis: null,},x2: {field: "bin_end",type: "temporal",- bin: { binned: true, step: 2 },+ bin: { binned: true, step: binStep },axis: null,},y: {@@ -428,6 +430,7 @@ export class ColumnChartSpecModel<T> {case "number": {// Create a histogram spec that properly handles null valuesconst format = type === "integer" ? ",d" : ".2f";+ const binStep = calculateBinStep(binValues || []);if (!usePreComputedValues) {return getLegacyNumericSpec(column, format, base);@@ -461,7 +464,7 @@ export class ColumnChartSpecModel<T> {x: {field: "bin_start",type: "quantitative",- bin: { binned: true, step: 2 },+ bin: { binned: true, step: binStep },},x2: {field: "bin_end",@@ -493,7 +496,7 @@ export class ColumnChartSpecModel<T> {x: {field: "bin_start",type: "quantitative",- bin: { binned: true, step: 2 },+ bin: { binned: true, step: binStep },axis: {title: null,labelFontSize: 8.5,diff --git a/frontend/src/components/data-table/column-summary/utils.ts b/frontend/src/components/data-table/column-summary/utils.tsindex d0170eba0b..9801305e12 100644--- a/frontend/src/components/data-table/column-summary/utils.ts+++ b/frontend/src/components/data-table/column-summary/utils.ts@@ -76,3 +76,67 @@ export function getPartialTimeTooltip(timeUnit: "yearmonthdate",};}++/**+ * Calculate the bin step for a given set of values.+ * If the bin step is too small for a large range, it can crash the browser.+ *+ * @param values - The values to calculate the bin step for.+ * @returns The bin step.+ */+export function calculateBinStep(values: BinValues) {+ if (values.length === 0) {+ return 1;+ }++ const validValues = values.filter(+ (v) => v.bin_start !== null && v.bin_end !== null,+ );++ if (validValues.length === 0) {+ return 1;+ }++ // Check the data types+ const firstStart = validValues[0].bin_start;+ const firstEnd = validValues[0].bin_end;++ // If values are strings or dates, we need to convert them to numbers+ let min: number;+ let max: number;++ // Use first and last values since binValues are sorted+ if (typeof firstStart === "number" && typeof firstEnd === "number") {+ const firstValue = validValues[0];+ const lastValue = validValues[validValues.length - 1];+ min = firstValue.bin_start as number;+ max = lastValue.bin_end as number;+ } else if (typeof firstStart === "string" || typeof firstEnd === "string") {+ // String data (likely dates) - convert to timestamps+ const firstValue = validValues[0];+ const lastValue = validValues[validValues.length - 1];+ const firstStart = new Date(firstValue.bin_start as string).getTime();+ const lastEnd = new Date(lastValue.bin_end as string).getTime();+ min = firstStart;+ max = lastEnd;+ } else {+ // Date objects - use first and last values since they're sorted+ const firstValue = validValues[0];+ const lastValue = validValues[validValues.length - 1];+ min = (firstValue.bin_start as Date).getTime();+ max = (lastValue.bin_end as Date).getTime();+ }++ const range = max - min;++ // Handle edge case where range is 0+ if (range === 0) {+ return 1;+ }++ const numBins = validValues.length;+ const step = range / numBins;++ // Ensure minimum step size+ return Math.max(step, 1);+}