2026-05-10 23:51:24 +02:00
|
|
|
import { createDefaultLocalQueryExecutor, type KtxSqlQueryExecutorPort } from '@ktx/context/connections';
|
2026-05-11 15:50:34 +02:00
|
|
|
import type { KtxSemanticLayerComputePort } from '@ktx/context/daemon';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { loadKtxProject, type KtxLocalProject } from '@ktx/context/project';
|
2026-05-10 23:12:26 +02:00
|
|
|
import {
|
|
|
|
|
compileLocalSlQuery,
|
|
|
|
|
listLocalSlSources,
|
|
|
|
|
readLocalSlSource,
|
|
|
|
|
validateLocalSlSource,
|
|
|
|
|
writeLocalSlSource,
|
|
|
|
|
type SemanticLayerQueryInput,
|
2026-05-10 23:51:24 +02:00
|
|
|
} from '@ktx/context/sl';
|
2026-05-11 15:50:34 +02:00
|
|
|
import {
|
|
|
|
|
createManagedPythonSemanticLayerComputePort,
|
|
|
|
|
type KtxManagedPythonInstallPolicy,
|
|
|
|
|
} from './managed-python-command.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { profileMark } from './startup-profile.js';
|
|
|
|
|
|
|
|
|
|
profileMark('module:sl');
|
|
|
|
|
|
|
|
|
|
type SlQueryFormat = 'json' | 'sql';
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export type KtxSlArgs =
|
2026-05-10 23:12:26 +02:00
|
|
|
| { command: 'list'; projectDir: string; connectionId?: string; output?: string; json?: boolean }
|
|
|
|
|
| { command: 'read'; projectDir: string; connectionId: string; sourceName: string }
|
|
|
|
|
| { command: 'validate'; projectDir: string; connectionId: string; sourceName: string }
|
|
|
|
|
| { command: 'write'; projectDir: string; connectionId: string; sourceName: string; yaml: string }
|
|
|
|
|
| {
|
|
|
|
|
command: 'query';
|
|
|
|
|
projectDir: string;
|
|
|
|
|
connectionId?: string;
|
|
|
|
|
query: SemanticLayerQueryInput;
|
|
|
|
|
format: SlQueryFormat;
|
|
|
|
|
execute: boolean;
|
|
|
|
|
maxRows?: number;
|
2026-05-11 15:50:34 +02:00
|
|
|
cliVersion: string;
|
|
|
|
|
runtimeInstallPolicy: KtxManagedPythonInstallPolicy;
|
2026-05-10 23:12:26 +02:00
|
|
|
};
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
interface KtxSlIo {
|
2026-05-10 23:12:26 +02:00
|
|
|
stdout: { write(chunk: string): void };
|
|
|
|
|
stderr: { write(chunk: string): void };
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
interface KtxSlDeps {
|
|
|
|
|
loadProject?: typeof loadKtxProject;
|
|
|
|
|
createSemanticLayerCompute?: () => KtxSemanticLayerComputePort;
|
2026-05-11 15:50:34 +02:00
|
|
|
createManagedSemanticLayerCompute?: (options: {
|
|
|
|
|
cliVersion: string;
|
|
|
|
|
installPolicy: KtxManagedPythonInstallPolicy;
|
|
|
|
|
io: KtxSlIo;
|
|
|
|
|
}) => Promise<KtxSemanticLayerComputePort>;
|
2026-05-10 23:51:24 +02:00
|
|
|
createQueryExecutor?: () => KtxSqlQueryExecutorPort;
|
2026-05-10 23:12:26 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
export async function runKtxSl(args: KtxSlArgs, io: KtxSlIo = process, deps: KtxSlDeps = {}): Promise<number> {
|
2026-05-10 23:12:26 +02:00
|
|
|
try {
|
2026-05-10 23:51:24 +02:00
|
|
|
const project = await (deps.loadProject ?? loadKtxProject)({ projectDir: args.projectDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
if (args.command === 'list') {
|
|
|
|
|
const sources = await listLocalSlSources(project, { connectionId: args.connectionId });
|
|
|
|
|
const { resolveOutputMode } = await import('./io/mode.js');
|
|
|
|
|
const { printList } = await import('./io/print-list.js');
|
|
|
|
|
const mode = resolveOutputMode({ explicit: args.output, json: args.json, io });
|
|
|
|
|
printList({
|
|
|
|
|
rows: sources,
|
|
|
|
|
columns: [
|
|
|
|
|
{ key: 'connectionId', label: 'CONNECTION', plain: '' },
|
|
|
|
|
{ key: 'name', label: 'NAME', plain: '' },
|
|
|
|
|
{ key: 'columnCount', label: 'COLS', plain: 'columns=', dim: true },
|
|
|
|
|
{ key: 'measureCount', label: 'MEASURES', plain: 'measures=', dim: true },
|
|
|
|
|
{ key: 'joinCount', label: 'JOINS', plain: 'joins=', dim: true },
|
|
|
|
|
{ key: 'description', label: 'DESCRIPTION', plain: false, optional: true, dim: true },
|
|
|
|
|
],
|
|
|
|
|
groupBy: 'connectionId',
|
|
|
|
|
emptyMessage: `No semantic-layer sources found in ${project.projectDir}`,
|
|
|
|
|
command: 'sl list',
|
|
|
|
|
mode,
|
|
|
|
|
io,
|
|
|
|
|
});
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (args.command === 'read') {
|
|
|
|
|
const source = await readLocalSlSource(project, {
|
|
|
|
|
connectionId: args.connectionId,
|
|
|
|
|
sourceName: args.sourceName,
|
|
|
|
|
});
|
|
|
|
|
if (!source) {
|
|
|
|
|
throw new Error(`Semantic-layer source "${args.connectionId}/${args.sourceName}" was not found`);
|
|
|
|
|
}
|
|
|
|
|
io.stdout.write(source.yaml);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (args.command === 'validate') {
|
|
|
|
|
const source = await readLocalSlSource(project, {
|
|
|
|
|
connectionId: args.connectionId,
|
|
|
|
|
sourceName: args.sourceName,
|
|
|
|
|
});
|
|
|
|
|
if (!source) {
|
|
|
|
|
throw new Error(`Semantic-layer source "${args.connectionId}/${args.sourceName}" was not found`);
|
|
|
|
|
}
|
2026-05-12 16:56:58 -04:00
|
|
|
const result = await validateLocalSlSource(source.yaml, { project, connectionId: args.connectionId });
|
2026-05-10 23:12:26 +02:00
|
|
|
if (!result.valid) {
|
|
|
|
|
for (const error of result.errors) {
|
|
|
|
|
io.stderr.write(`${error}\n`);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
io.stdout.write(`Valid semantic-layer source: ${args.connectionId}/${args.sourceName}\n`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (args.command === 'query') {
|
2026-05-11 15:50:34 +02:00
|
|
|
const compute = deps.createSemanticLayerCompute
|
|
|
|
|
? deps.createSemanticLayerCompute()
|
|
|
|
|
: await (deps.createManagedSemanticLayerCompute ?? createManagedPythonSemanticLayerComputePort)({
|
|
|
|
|
cliVersion: args.cliVersion,
|
|
|
|
|
installPolicy: args.runtimeInstallPolicy,
|
|
|
|
|
io,
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
const queryExecutor = args.execute ? (deps.createQueryExecutor ?? createDefaultLocalQueryExecutor)() : undefined;
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await compileLocalSlQuery(project as KtxLocalProject, {
|
2026-05-10 23:12:26 +02:00
|
|
|
connectionId: args.connectionId,
|
|
|
|
|
query: args.query,
|
|
|
|
|
compute,
|
|
|
|
|
execute: args.execute,
|
|
|
|
|
maxRows: args.maxRows,
|
|
|
|
|
queryExecutor,
|
|
|
|
|
});
|
|
|
|
|
if (args.format === 'sql') {
|
|
|
|
|
io.stdout.write(`${result.sql}\n`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
io.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const write = await writeLocalSlSource(project, {
|
|
|
|
|
connectionId: args.connectionId,
|
|
|
|
|
sourceName: args.sourceName,
|
|
|
|
|
yaml: args.yaml,
|
|
|
|
|
});
|
|
|
|
|
io.stdout.write(`Wrote ${write.path}\n`);
|
|
|
|
|
return 0;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
io.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|