Skip to content

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.

fix: support big-ints in datatable (#853)

Myles Scolnickedb8d79fd34e
diff --git a/frontend/src/components/data-table/columns.tsx b/frontend/src/components/data-table/columns.tsx
index b33162b5a8..d103a2f865 100644
--- a/frontend/src/components/data-table/columns.tsx
+++ b/frontend/src/components/data-table/columns.tsx
@@ -75,7 +75,11 @@ export function generateColumns<T>(
cell: ({ renderValue, getValue }) => {
const value = getValue();
if (isPrimitiveOrNullish(value)) {
- return renderValue();
+ const rendered = renderValue();
+ if (rendered == null) {
+ return "";
+ }
+ return String(rendered);
}
return <MimeCell value={value} />;
},
@@ -144,5 +148,6 @@ function isPrimitiveOrNullish(value: unknown): boolean {
if (value == null) {
return true;
}
- return typeof value !== "object";
+ const isObject = typeof value === "object";
+ return !isObject;
}
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 01a7530d98..844c71f669 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -90,7 +90,7 @@ export const LoadingDataTableComponent = (
if (!props.data) {
return Promise.resolve([]);
}
- return vegaLoadData(props.data, { type: "csv", parse: "auto" });
+ return vegaLoadData(props.data, { type: "csv", parse: "auto" }, true);
}, [props.data]);
if (loading && !data) {
diff --git a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
index 75c0b31731..cf83fb14e1 100644
--- a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
@@ -97,6 +97,33 @@ yield_error,yield_center
]
`);
});
+
+ it("should parse csv data with out of bound integers", async () => {
+ const csvData = `id\n912312851340981241284`;
+
+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));
+ const data = await vegaLoadData(
+ csvData,
+ { type: "csv", parse: "auto" },
+ true,
+ );
+ expect(data).toMatchInlineSnapshot(`
+ [
+ {
+ "id": 912312851340981241284n,
+ },
+ ]
+ `);
+
+ const dataWithoutFlag = await vegaLoadData(csvData, { type: "csv" }, false);
+ expect(dataWithoutFlag).toMatchInlineSnapshot(`
+ [
+ {
+ "id": 912312851340981300000,
+ },
+ ]
+ `);
+ });
});
describe("uniquifyColumnNames", () => {
diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.ts
index 53ebdfa114..ffddf6e0b2 100644
--- a/frontend/src/plugins/impl/vega/loader.ts
+++ b/frontend/src/plugins/impl/vega/loader.ts
@@ -2,9 +2,35 @@
// @ts-expect-error - no types
import { loader as createLoader, read, typeParsers } from "vega-loader";
import { DataFormat } from "./types";
+import { isNumber } from "lodash-es";
// Augment the typeParsers to support Date
typeParsers.date = (value: string) => new Date(value).toISOString();
+const previousIntegerParser = typeParsers.integer;
+const previousNumberParser = typeParsers.number;
+// We need to use BigInt for numbers to support large numbers
+const bigIntIntegerParser = (v: string) => {
+ if (v === "") {
+ return "";
+ }
+ return isNumber(Number.parseInt(v)) ? BigInt(v) : "";
+};
+const bigIntNumberParser = (v: string) => {
+ if (v === "") {
+ return "";
+ }
+ return isNumber(Number.parseFloat(v)) ? BigInt(v) : "";
+};
+
+function enableBigInt() {
+ typeParsers.integer = bigIntIntegerParser;
+ typeParsers.number = bigIntNumberParser;
+}
+
+function disableBigInt() {
+ typeParsers.integer = previousIntegerParser;
+ typeParsers.number = previousNumberParser;
+}
export const vegaLoader = createLoader();
@@ -16,6 +42,7 @@ export const vegaLoader = createLoader();
export function vegaLoadData(
url: string,
format: DataFormat | undefined | { type: "csv"; parse: "auto" },
+ handleBigInt = false,
): Promise<object[]> {
return vegaLoader.load(url).then((csvData: string) => {
// CSV data comes columnar and may have duplicate column names.
@@ -27,12 +54,24 @@ export function vegaLoadData(
// we would need to store the data in columnar format.
csvData = uniquifyColumnNames(csvData);
+ // We support enabling/disabling since the Table enables it
+ // but Vega does not support BigInts
+ if (handleBigInt) {
+ enableBigInt();
+ }
+
// Always set parse to auto for csv data, to be able to parse dates and floats
- if (format && format.type === "csv") {
- // csv -> json
- return read(csvData, { ...format, parse: "auto" });
+ const results =
+ format && format.type === "csv"
+ ? // csv -> json
+ read(csvData, { ...format, parse: "auto" })
+ : read(csvData, format);
+
+ if (handleBigInt) {
+ disableBigInt();
}
- return read(csvData, format);
+
+ return results;
});
}
diff --git a/marimo/_smoke_tests/bugs/852.py b/marimo/_smoke_tests/bugs/852.py
new file mode 100644
index 0000000000..cc9b86df39
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/852.py
@@ -0,0 +1,41 @@
+import marimo
+
+__generated_with = "0.2.9"
+app = marimo.App()
+
+
+def __(mo):
+ mo.md(
+ """
+ # Bug 852
+
+ Explanation: The table was rendering incorrectly due to javascript number precision.
+ """
+ )
+ return
+
+
+def __():
+ import marimo as mo
+ import pandas as pd
+ return mo, pd
+
+
+def __(pd):
+ df = pd.DataFrame({"data": [912312851340981241284, None, "abc"]})
+ df
+ return df,
+
+
+def __(df, mo):
+ table = mo.ui.table(df)
+ table
+ return table,
+
+
+if __name__ == "__main__":
+ app.run()