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: fallback to oringinal string when cannot parse formatted number (#3529)
Myles Scolnick9a96a9d676a9
diff --git a/frontend/src/plugins/impl/DataEditorPlugin.tsx b/frontend/src/plugins/impl/DataEditorPlugin.tsxindex 7a4db2d0b0..11c626eec4 100644--- a/frontend/src/plugins/impl/DataEditorPlugin.tsx+++ b/frontend/src/plugins/impl/DataEditorPlugin.tsx@@ -93,7 +93,7 @@ const LoadingDataEditor = (props: Props) => {return await vegaLoadData(props.data,{ type: "csv", parse: getVegaFieldTypes(withoutExternalTypes) },- { handleBigInt: true },+ { handleBigIntAndNumberLike: true },);}, [props.fieldTypes, props.data]);diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsxindex 9e791a40f6..6c956a94da 100644--- a/frontend/src/plugins/impl/DataTablePlugin.tsx+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx@@ -322,7 +322,7 @@ export const LoadingDataTableComponent = memo(tableData = await vegaLoadData(tableData,{ type: "csv", parse: getVegaFieldTypes(withoutExternalTypes) },- { handleBigInt: true },+ { handleBigIntAndNumberLike: true },);return {diff --git a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts b/frontend/src/plugins/impl/vega/__tests__/vega.test.tsindex 80eed64ee5..c58b0fba17 100644--- a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts+++ b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts@@ -120,7 +120,7 @@ yield_error,yield_centerconst data = await vegaLoadData(csvData,{ type: "csv", parse: "auto" },- { handleBigInt: true },+ { handleBigIntAndNumberLike: true },);expect(data).toMatchInlineSnapshot(`[@@ -133,7 +133,7 @@ yield_error,yield_centerconst dataWithoutFlag = await vegaLoadData(csvData,{ type: "csv" },- { handleBigInt: false },+ { handleBigIntAndNumberLike: false },);expect(dataWithoutFlag).toMatchInlineSnapshot(`[@@ -148,7 +148,9 @@ yield_error,yield_centerconst csvData = "id\n912312851340981241284";vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));const format = undefined;- const data = await vegaLoadData(csvData, format, { handleBigInt: true });+ const data = await vegaLoadData(csvData, format, {+ handleBigIntAndNumberLike: true,+ });expect(data).toMatchInlineSnapshot(`[{@@ -246,7 +248,7 @@ id,nameconst dataWithBigInt = await vegaLoadData(csvData,{ type: "csv", parse: "auto" },- { handleBigInt: true },+ { handleBigIntAndNumberLike: true },);expect(dataWithBigInt).toMatchInlineSnapshot(`@@ -265,7 +267,7 @@ id,nameconst dataWithoutBigInt = await vegaLoadData(csvData,{ type: "csv", parse: "auto" },- { handleBigInt: false },+ { handleBigIntAndNumberLike: false },);expect(dataWithoutBigInt).toMatchInlineSnapshot(`@@ -395,6 +397,60 @@ Bob.Jones,25]`);});++ it("should handle NaN numbers, when handleBigInt is true", async () => {+ const csvData = 'name,amount,id\nAlice,,"#1"\nBob,"$2,000","#2"';+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));+ const data = await vegaLoadData(+ csvData,+ {+ type: "csv",+ parse: { amount: "number", name: "string", id: "integer" },+ },+ { handleBigIntAndNumberLike: true },+ );+ expect(data).toMatchInlineSnapshot(`+ [+ {+ "amount": null,+ "id": "#1",+ "name": "Alice",+ },+ {+ "amount": "$2,000",+ "id": "#2",+ "name": "Bob",+ },+ ]+ `);+ });++ it("should handle NaN numbers, when handleBigInt is false", async () => {+ const csvData = 'name,amount,id\nAlice,,"#1"\nBob,"$2,000","#2"';+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));+ const data = await vegaLoadData(+ csvData,+ {+ type: "csv",+ parse: { amount: "number", name: "string", id: "integer" },+ },+ { handleBigIntAndNumberLike: false },+ );+ expect(data).toMatchInlineSnapshot(`+ [+ {+ "amount": null,+ "id": NaN,+ "name": "Alice",+ },+ {+ "amount": NaN,+ "id": NaN,+ "name": "Bob",+ },+ ]+ `);+ });});describe("uniquifyColumnNames", () => {diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.tsindex 15bfa5e2b6..d33dddbad5 100644--- a/frontend/src/plugins/impl/vega/loader.ts+++ b/frontend/src/plugins/impl/vega/loader.ts@@ -29,18 +29,27 @@ const BIG_INT_MIDDLEWARE: Middleware = () => {return v;}+ function previousIntegerParserWithoutNaN(v: string) {+ const result = previousIntegerParser(v);+ // If the result is NaN, return the original value+ if (Number.isNaN(result)) {+ return v;+ }+ return result;+ }+const parsedInt = Number.parseInt(v);if (isNumber(parsedInt)) {const needsBigInt = Math.abs(parsedInt) > Number.MAX_SAFE_INTEGER;if (!needsBigInt) {- return previousIntegerParser(v);+ return previousIntegerParserWithoutNaN(v);}try {return BigInt(v);} catch {// Floats like 2.0 are parseable as ints but not// as BigInt- return previousIntegerParser(v);+ return previousIntegerParserWithoutNaN(v);}} else {return "";@@ -53,7 +62,11 @@ const BIG_INT_MIDDLEWARE: Middleware = () => {if (v === "inf") {return v;}- return previousNumberParser(v);+ const result = previousNumberParser(v);+ if (Number.isNaN(result)) {+ return v;+ }+ return result;};return () => {@@ -121,14 +134,14 @@ export async function vegaLoadData<T = object>(opts: {// We support enabling/disabling since the Table enables it// but Vega does not support BigInts- handleBigInt?: boolean;+ handleBigIntAndNumberLike?: boolean;replacePeriod?: boolean;} = {},): Promise<T[]> {- const { handleBigInt = false, replacePeriod = false } = opts;+ const { handleBigIntAndNumberLike = false, replacePeriod = false } = opts;const middleware: Middleware[] = [DATE_MIDDLEWARE];- if (handleBigInt) {+ if (handleBigIntAndNumberLike) {middleware.push(BIG_INT_MIDDLEWARE);}@@ -203,9 +216,12 @@ export async function vegaLoadData<T = object>(}}-export function parseCsvData(csvData: string, handleBigInt = true): object[] {+export function parseCsvData(+ csvData: string,+ handleBigIntAndNumberLike = true,+): object[] {const middleware: Middleware[] = [DATE_MIDDLEWARE];- if (handleBigInt) {+ if (handleBigIntAndNumberLike) {middleware.push(BIG_INT_MIDDLEWARE);}