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

suggest models in the code editor #3705

Merged
merged 3 commits into from
Dec 14, 2023
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
26 changes: 26 additions & 0 deletions web-common/src/features/models/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {
} from "@rilldata/web-common/runtime-client";
import type { QueryClient } from "@tanstack/query-core";
import { TIMESTAMPS } from "../../lib/duckdb-data-types";
import type { Readable } from "svelte/motion";
import { derived } from "svelte/store";
import {
createTableColumnsWithName,
type TableColumnsWithName,
} from "../sources/selectors";

export function useModels(instanceId: string) {
return useFilteredResources(instanceId, ResourceKind.Model);
Expand All @@ -31,6 +37,26 @@ export function useModel(instanceId: string, name: string) {
return useResource(instanceId, name, ResourceKind.Model);
}

export function useAllModelColumns(
queryClient: QueryClient,
instanceId: string
): Readable<Array<TableColumnsWithName>> {
return derived([useModels(instanceId)], ([allModels], set) => {
if (!allModels.data?.length) {
set([]);
return;
}

derived(
allModels.data.map((r) =>
createTableColumnsWithName(queryClient, instanceId, r.meta.name.name)
),
(modelColumnResponses) =>
modelColumnResponses.filter((res) => !!res.data).map((res) => res.data)
).subscribe(set);
});
}

export async function getModelNames(
queryClient: QueryClient,
instanceId: string
Expand Down
14 changes: 12 additions & 2 deletions web-common/src/features/models/workspace/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import { editorTheme } from "../../../components/editor/theme";
import { runtime } from "../../../runtime-client/runtime-store";
import { useAllSourceColumns } from "../../sources/selectors";
import { useAllModelColumns } from "../selectors";
export let content: string;
export let editorHeight = 0;
Expand Down Expand Up @@ -89,18 +90,27 @@
"select from where group by all having order limit sample unnest with window qualify values filter exclude replace like ilike glob as case when then else end in cast left join on not desc asc sum union",
});
const schema: { [table: string]: string[] } = {};
// Autocomplete: source tables
let schema: { [table: string]: string[] };
$: allSourceColumns = useAllSourceColumns(queryClient, $runtime?.instanceId);
$: if ($allSourceColumns?.length) {
schema = {};
for (const sourceTable of $allSourceColumns) {
const sourceIdentifier = sourceTable?.tableName;
schema[sourceIdentifier] =
sourceTable.profileColumns?.map((c) => c.name) ?? [];
}
}
//Auto complete: model tables
$: allModelColumns = useAllModelColumns(queryClient, $runtime?.instanceId);
$: if ($allModelColumns?.length) {
for (const modelTable of $allModelColumns) {
const modelIdentifier = modelTable?.tableName;
schema[modelIdentifier] = modelTable.profileColumns?.map((c) => c.name);
}
}
function getTableNameFromFromClause(
sql: string,
schema: { [table: string]: string[] }
Expand Down
4 changes: 2 additions & 2 deletions web-common/src/features/sources/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function useIsLocalFileConnector(
);
}

type TableColumnsWithName = {
export type TableColumnsWithName = {
tableName: string;
profileColumns: Array<V1ProfileColumn>;
};
Expand Down Expand Up @@ -116,7 +116,7 @@ export function useAllSourceColumns(
/**
* Fetches columns and adds the table name. By using the selector the results will be cached.
*/
function createTableColumnsWithName(
export function createTableColumnsWithName(
queryClient: QueryClient,
instanceId: string,
tableName: string
Expand Down