-
Notifications
You must be signed in to change notification settings - Fork 186
feat: render .parquet files as a data preview
#9666
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
nishantmonu51
wants to merge
5
commits into
main
Choose a base branch
from
parquet_render
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4dffbf6
feat: render a data preview for .parquet files
nishantmonu51 b6edf01
fix: run Parquet preview on DuckDB and invalidate it on file writes
nishantmonu51 d59c0eb
refactor: address review feedback on Parquet preview
nishantmonu51 a94c615
Merge remote-tracking branch 'origin/main' into parquet_render
nishantmonu51 7888332
i18n: localize Parquet preview strings
nishantmonu51 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
There are no files selected for viewing
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
119 changes: 119 additions & 0 deletions
119
web-common/src/features/workspaces/ParquetWorkspace.svelte
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,119 @@ | ||
| <script lang="ts"> | ||
| import PreviewTable from "@rilldata/web-common/components/preview-table/PreviewTable.svelte"; | ||
| import type { VirtualizedTableColumns } from "@rilldata/web-common/components/virtualized-table/types"; | ||
| import ReconcilingSpinner from "@rilldata/web-common/features/entity-management/ReconcilingSpinner.svelte"; | ||
| import type { FileArtifact } from "@rilldata/web-common/features/entity-management/file-artifact"; | ||
| import WorkspaceContainer from "@rilldata/web-common/layout/workspace/WorkspaceContainer.svelte"; | ||
| import WorkspaceHeader from "@rilldata/web-common/layout/workspace/WorkspaceHeader.svelte"; | ||
| import { m } from "@rilldata/web-common/lib/i18n/gen/messages"; | ||
| import { createRuntimeServiceGetInstance } from "@rilldata/web-common/runtime-client"; | ||
| import { useRuntimeClient } from "@rilldata/web-common/runtime-client/v2"; | ||
| import { createParquetPreviewQuery } from "./parquet-preview"; | ||
|
|
||
| let { fileArtifact }: { fileArtifact: FileArtifact } = $props(); | ||
|
|
||
| const runtimeClient = useRuntimeClient(); | ||
|
|
||
| let { path } = $derived(fileArtifact); | ||
| let fileName = $derived(fileArtifact.fileName); | ||
|
|
||
| let instanceQuery = createRuntimeServiceGetInstance(runtimeClient, { | ||
| sensitive: true, | ||
| }); | ||
|
|
||
| // DuckDB resolves relative paths against its own working directory, not the | ||
| // project root, so we must read the file by its absolute path. The project's | ||
| // local root is the `dsn` of the repo connector (only present in Rill | ||
| // Developer, where the project lives on the local filesystem). | ||
| let repoRoot = $derived.by(() => { | ||
| const instance = $instanceQuery.data?.instance; | ||
| const repo = instance?.connectors?.find( | ||
| (c) => c.name === instance?.repoConnector, | ||
| ); | ||
| const dsn = repo?.config?.dsn; | ||
| return typeof dsn === "string" ? dsn : ""; | ||
| }); | ||
|
|
||
| // read_parquet is DuckDB-specific, so the query must run against a DuckDB | ||
| // connector even when the project's default OLAP connector is something else | ||
| // (e.g. ClickHouse). Prefer the default OLAP connector if it is DuckDB, | ||
| // otherwise fall back to any DuckDB connector in the project. | ||
| let duckDbConnector = $derived.by(() => { | ||
| const instance = $instanceQuery.data?.instance; | ||
| const connectors = instance?.connectors ?? []; | ||
| const olap = connectors.find((c) => c.name === instance?.olapConnector); | ||
| if (olap?.type === "duckdb") return olap.name ?? ""; | ||
| return connectors.find((c) => c.type === "duckdb")?.name ?? ""; | ||
| }); | ||
|
|
||
| // Join the repo root and the file's project-relative path into an absolute | ||
| // path for read_parquet. | ||
| let absolutePath = $derived( | ||
| repoRoot | ||
| ? `${repoRoot.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}` | ||
| : "", | ||
| ); | ||
|
|
||
| let previewQuery = $derived( | ||
| createParquetPreviewQuery(runtimeClient, { | ||
| path, | ||
| absolutePath, | ||
| connector: duckDbConnector, | ||
| }), | ||
| ); | ||
|
|
||
| // While the instance is loading we don't yet know the repo root, so treat it | ||
| // as part of the preview's loading state. | ||
| let isLoading = $derived($instanceQuery.isLoading || $previewQuery.isLoading); | ||
| let error = $derived.by(() => { | ||
| if ($instanceQuery.isLoading) return null; | ||
| if (!repoRoot) return new Error(m.parquet_preview_no_project_dir()); | ||
| if (!duckDbConnector) | ||
| return new Error(m.parquet_preview_requires_duckdb()); | ||
| return $previewQuery.error; | ||
| }); | ||
| let data = $derived($previewQuery.data); | ||
|
|
||
| let rows = $derived(data?.data ?? []); | ||
| let columns = $derived( | ||
| (data?.schema?.fields?.map((field) => ({ | ||
| name: field.name, | ||
| type: field.type?.code, | ||
| })) ?? []) as VirtualizedTableColumns[], | ||
| ); | ||
| </script> | ||
|
|
||
| <svelte:head> | ||
| <title>Rill Developer | {fileName}</title> | ||
| </svelte:head> | ||
|
|
||
| <WorkspaceContainer inspector={false}> | ||
| <WorkspaceHeader | ||
| slot="header" | ||
| filePath={path} | ||
| resourceKind={undefined} | ||
| titleInput={fileName} | ||
| editable={false} | ||
| showInspectorToggle={false} | ||
| hasUnsavedChanges={false} | ||
| /> | ||
|
|
||
| <svelte:fragment slot="body"> | ||
| {#if isLoading} | ||
| <ReconcilingSpinner /> | ||
| {:else if error} | ||
| <div | ||
| class="flex flex-col size-full items-center justify-center text-fg-secondary gap-y-1" | ||
| > | ||
| <p class="font-semibold">{m.parquet_preview_read_error_title()}</p> | ||
| <p class="text-sm"> | ||
| {error.message ?? m.parquet_preview_unknown_error()} | ||
| </p> | ||
| </div> | ||
| {:else} | ||
| <div class="size-full overflow-hidden border rounded-[2px]"> | ||
| <PreviewTable {rows} columnNames={columns} name={fileName} /> | ||
| </div> | ||
| {/if} | ||
| </svelte:fragment> | ||
| </WorkspaceContainer> | ||
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,53 @@ | ||
| import type { PartialMessage, Struct } from "@bufbuild/protobuf"; | ||
| import { createRuntimeServiceQueryResolver } from "@rilldata/web-common/runtime-client"; | ||
| import type { RuntimeClient } from "@rilldata/web-common/runtime-client/v2"; | ||
|
|
||
| // Number of rows to preview. Parquet files can be large, so we cap the preview | ||
| // the same way the model results table does. | ||
| export const PARQUET_PREVIEW_LIMIT = 150; | ||
|
|
||
| /** | ||
| * Query key for the DuckDB-powered preview of a Parquet file rendered by | ||
| * ParquetWorkspace. It is keyed on the file's project-relative path (the same | ||
| * form as file watch events) so the preview can be invalidated when the | ||
| * underlying file is rewritten. | ||
| */ | ||
| export function getParquetPreviewQueryKey(instanceId: string, path: string) { | ||
| return ["parquet-preview", instanceId, path]; | ||
| } | ||
|
|
||
| /** | ||
| * Creates the query that previews a Parquet file by reading it with DuckDB. | ||
| * | ||
| * `read_parquet` is DuckDB-specific and resolves relative paths against its own | ||
| * working directory, so the caller must pass the file's absolute path and a | ||
| * DuckDB connector to run the query against. | ||
| */ | ||
| export function createParquetPreviewQuery( | ||
| client: RuntimeClient, | ||
| { | ||
| path, | ||
| absolutePath, | ||
| connector, | ||
| }: { path: string; absolutePath: string; connector: string }, | ||
| ) { | ||
| // Single quotes are escaped to keep the generated SQL valid. | ||
| const sql = `SELECT * FROM read_parquet('${absolutePath.replaceAll("'", "''")}')`; | ||
| return createRuntimeServiceQueryResolver( | ||
| client, | ||
| { | ||
| resolver: "sql", | ||
| resolverProperties: { | ||
| connector, | ||
| sql, | ||
| limit: PARQUET_PREVIEW_LIMIT, | ||
| } as unknown as PartialMessage<Struct>, | ||
| }, | ||
| { | ||
| query: { | ||
| enabled: !!absolutePath && !!connector, | ||
| queryKey: getParquetPreviewQueryKey(client.instanceId, path), | ||
| }, | ||
| }, | ||
| ); | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets move this query to parquet-preview?