Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datacube: fixing caching for csv #3891

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tidy-ads-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@finos/legend-application-data-cube': patch
'@finos/legend-shared': patch
---

Fixing Caching for CSV
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from '@finos/legend-graph';
import {
assertNonNullable,
csvStringify,
guaranteeNonNullable,
UnsupportedOperationError,
} from '@finos/legend-shared';
Expand Down Expand Up @@ -92,32 +93,22 @@ export class LegendDataCubeDataCubeCacheManager {

const connection = await this.database.connect();

const columnString = result.builder.columns
.map((col) => col.name)
.join(',');
const columnNames: string[] = [];
result.builder.columns.forEach((col) => columnNames.push(col.name));

const dataString: string[] = [columnString];
const data = result.result.rows.map((row) => row.values);

result.result.rows.forEach((row) => {
const updatedRows = row.values.map((val) => {
if (val !== null && typeof val === 'string') {
return `'${val.replaceAll(`'`, `''`)}'`;
} else if (val === null) {
return `NULL`;
}
return val;
});
dataString.push(`${updatedRows.join(',')}`);
const csv = csvStringify([columnNames, ...data], {
escapeChar: `'`,
quoteChar: `'`,
});

const csvString = dataString.join('\n');

await this._database?.registerFileText(csvFileName, csvString);
await this._database?.registerFileText(csvFileName, csv);

await connection.insertCSVFromPath(csvFileName, {
schema: schema,
name: table,
create: false,
create: true,
header: true,
detect: true,
escape: `'`,
Expand Down
8 changes: 5 additions & 3 deletions packages/legend-shared/src/format/FormatterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
parse as losslessParse,
isSafeNumber as lossIsSafeNumber,
} from 'lossless-json';
import CSVParser from 'papaparse';
import CSVParser, { type UnparseConfig } from 'papaparse';
import { assertNonNullable } from '../error/AssertionUtils.js';

export const capitalize = (value: string): string =>
Expand Down Expand Up @@ -152,8 +152,10 @@ export const parseCSVString = (value: string): string[] | undefined => {
}
};

export const csvStringify = (value: unknown[]): string =>
CSVParser.unparse(value);
export const csvStringify = (
value: unknown[],
config?: UnparseConfig,
): string => CSVParser.unparse(value, config);

/**
* One very common use case is that we get the JSON as response from the server than we will convert this to a string and persist
Expand Down
Loading