-
-
Notifications
You must be signed in to change notification settings - Fork 925
feat(converters): add parquet to csv conversion support #553
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
Open
pedro3pv
wants to merge
1
commit into
C4illin:main
Choose a base branch
from
pedro3pv:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+221
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import * as fs from "node:fs/promises"; | ||
| import { createWriteStream } from "node:fs"; | ||
| import { stringify } from "csv-stringify"; | ||
| import { parquetMetadataAsync, parquetRead } from "hyparquet"; | ||
| import { compressors } from "hyparquet-compressors"; | ||
|
|
||
| export const properties = { | ||
| from: { | ||
| data: ["parquet"], | ||
| }, | ||
| to: { | ||
| data: ["csv"], | ||
| }, | ||
| }; | ||
|
|
||
| export async function convert( | ||
| filePath: string, | ||
| fileType: string, | ||
| convertTo: string, | ||
| targetPath: string, | ||
| options?: unknown, // eslint-disable-line @typescript-eslint/no-unused-vars | ||
| ): Promise<string> { | ||
| const fileHandle = await fs.open(filePath, "r"); | ||
| try { | ||
| const stat = await fileHandle.stat(); | ||
| const file = { | ||
| byteLength: stat.size, | ||
| async slice(start: number, end?: number): Promise<ArrayBuffer> { | ||
| const length = (end ?? stat.size) - start; | ||
| const buf = Buffer.allocUnsafe(length); | ||
| const { bytesRead } = await fileHandle.read(buf, 0, length, start); | ||
| return buf.buffer.slice(buf.byteOffset, buf.byteOffset + bytesRead); | ||
| }, | ||
| }; | ||
|
|
||
| const metadata = await parquetMetadataAsync(file, { compressors } as never); | ||
| const stringifier = stringify({ | ||
| header: true, | ||
| cast: { | ||
| object: (value: unknown) => | ||
| JSON.stringify(value, (_key: string, val: unknown) => | ||
| typeof val === "bigint" ? Number(val) : val, | ||
| ), | ||
| }, | ||
| }); | ||
| const writeStream = createWriteStream(targetPath); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| stringifier.pipe(writeStream); | ||
|
|
||
| let settled = false; | ||
| const settle = (err?: unknown) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| fileHandle.close().catch(() => {}); | ||
| if (err) reject(err); | ||
| else resolve("Done"); | ||
| }; | ||
|
|
||
| writeStream.on("finish", settle); | ||
| writeStream.on("error", settle); | ||
| stringifier.on("error", settle); | ||
|
|
||
| const writeRow = async (rawRow: Record<string, unknown>) => { | ||
| const row: Record<string, unknown> = {}; | ||
| for (const [key, value] of Object.entries(rawRow)) { | ||
| row[key] = typeof value === "bigint" ? Number(value) : value; | ||
| } | ||
| if (stringifier.write(row)) return; | ||
| await new Promise<void>((resolve) => { | ||
| stringifier.once("drain", resolve); | ||
| }); | ||
| }; | ||
|
|
||
| const writePromises: Promise<void>[] = []; | ||
|
|
||
| (async () => { | ||
| try { | ||
| let rowStart = 0; | ||
| for (const rowGroup of metadata.row_groups) { | ||
| const numRows = Number(rowGroup.num_rows); | ||
| const rowEnd = rowStart + numRows; | ||
|
|
||
| await parquetRead({ | ||
| file, | ||
| rowStart, | ||
| rowEnd, | ||
| rowFormat: "object", | ||
| compressors, | ||
| onComplete: (rows) => { | ||
| if (Array.isArray(rows)) { | ||
| writePromises.push( | ||
| (async () => { | ||
| for (const row of rows) { | ||
| await writeRow(row); | ||
| } | ||
| })(), | ||
| ); | ||
| } | ||
| }, | ||
| }); | ||
| rowStart = rowEnd; | ||
| } | ||
|
|
||
| await Promise.all(writePromises); | ||
| stringifier.end(); | ||
| } catch (err) { | ||
| settle(err); | ||
| writeStream.destroy(err as Error); | ||
| stringifier.destroy(err as Error); | ||
| } | ||
| })(); | ||
| }); | ||
| } catch (err) { | ||
| await fileHandle.close(); | ||
| throw err; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { expect, test, describe, mock } from "bun:test"; | ||
| import { Writable } from "node:stream"; | ||
|
|
||
| const mockParquetMetadataAsync = mock(async () => { | ||
| return { | ||
| row_groups: [{ num_rows: 1 }], | ||
| }; | ||
| }); | ||
|
|
||
| const mockParquetRead = mock( | ||
| async (options: { | ||
| file: unknown; | ||
| rowStart: number; | ||
| rowEnd: number; | ||
| rowFormat: string; | ||
| onComplete: (rows: { col1: string }[]) => void; | ||
| }) => { | ||
| if (options.onComplete) { | ||
| options.onComplete([{ col1: "value" }]); | ||
| } | ||
| return []; | ||
| }, | ||
| ); | ||
|
|
||
| const mockCreateWriteStream = mock(() => { | ||
| return new Writable({ | ||
| write(_chunk, _encoding, callback) { | ||
| callback(); | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| const mockFileHandle = { | ||
| stat: mock(async () => ({ size: 100 })), | ||
| read: mock(async (buf: Buffer) => { | ||
| return { bytesRead: buf.length, buffer: buf }; | ||
| }), | ||
| close: mock(async () => {}), | ||
| }; | ||
|
|
||
| mock.module("node:fs/promises", () => ({ | ||
| open: mock(async () => mockFileHandle), | ||
| })); | ||
|
|
||
| mock.module("hyparquet", () => { | ||
| return { | ||
| parquetMetadataAsync: mockParquetMetadataAsync, | ||
| parquetRead: mockParquetRead, | ||
| }; | ||
| }); | ||
|
|
||
| mock.module("node:fs", () => { | ||
| return { | ||
| createWriteStream: mockCreateWriteStream, | ||
| }; | ||
| }); | ||
|
|
||
| import { convert } from "../../src/converters/parquet"; | ||
|
|
||
| describe("parquet converter", () => { | ||
| test("convert resolves when process succeeds", async () => { | ||
| const result = await convert("input.parquet", "parquet", "csv", "output.csv"); | ||
| expect(result).toBe("Done"); | ||
| }); | ||
|
pedro3pv marked this conversation as resolved.
|
||
|
|
||
| test("convert rejects when parquetRead fails", async () => { | ||
| mockParquetRead.mockRejectedValueOnce(new Error("Read error")); | ||
|
|
||
| await expect(convert("invalid.parquet", "parquet", "csv", "output.csv")).rejects.toThrow( | ||
| "Read error", | ||
| ); | ||
| }); | ||
|
|
||
| test("convert rejects when metadata fails", async () => { | ||
| mockParquetMetadataAsync.mockRejectedValueOnce(new Error("Metadata error")); | ||
|
|
||
| await expect(convert("invalid.parquet", "parquet", "csv", "output.csv")).rejects.toThrow( | ||
| "Metadata error", | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.