Line data Source code
1 : import * as core from '@ankoh/dashql-core';
2 :
3 : import { ConnectorType } from '../../connections/connector_info.js';
4 : import type { LoggerLike } from '../../platform/logger/logger.js';
5 : import type { ResolvedVisualizeQuery } from './script_types.js';
6 :
7 1 : const LOG_CTX = 'script_compilation';
8 :
9 : /// The formatting of compiled queries in the dialect of the connector
10 1 : function executionFormattingConfig(connectorType: ConnectorType): core.buffers.formatting.FormattingConfigT {
11 1 : return new core.buffers.formatting.FormattingConfigT(
12 1 : connectorType === ConnectorType.TRINO
13 1 : ? core.buffers.formatting.FormattingDialect.TRINO
14 1 : : core.buffers.formatting.FormattingDialect.HYPER,
15 1 : core.buffers.formatting.FormattingMode.INLINE,
16 1 : 120,
17 1 : 2,
18 1 : false,
19 1 : );
20 1 : }
21 :
22 : /// The statement that a chart of the script visualizes, the last statement. Null when the script has no statement.
23 : /// Compilation errors, e.g. of other statements, do not matter here, the rewrite of a tag refuses unparsed scripts.
24 1 : export function chartStatementId(scriptSession: core.DashQLScriptSession): number | null {
25 1 : const reader = scriptSession.compileQuery(executionFormattingConfig(ConnectorType.HYPER)).read();
26 1 : const statementId = reader.terminalStatementId();
27 1 : return statementId === 0xFFFFFFFF ? null : statementId;
28 1 : }
29 :
30 1 : export function compileVisualizeQuery(scriptSession: core.DashQLScriptSession, connectorType: ConnectorType,
31 1 : logger?: LoggerLike): ResolvedVisualizeQuery | null {
32 1 : const compiled = scriptSession.compileQuery(executionFormattingConfig(connectorType));
33 1 : const reader = compiled.read();
34 1 : if (reader.errorsLength() > 0 ||
35 1 : reader.kind() !== core.buffers.execution.ScriptCompilationStatementKind.VISUALIZE) {
36 1 : return null;
37 : }
38 1 : const sql = reader.sql();
39 1 : const visualization = reader.visualization();
40 1 : if (!sql || !visualization) return null;
41 1 : logger?.debug('Compiled visualization for execution', { sql }, LOG_CTX);
42 1 : return resolveCompiledVisualization(sql, visualization.unpack(), reader.terminalStatementId());
43 1 : }
44 :
45 1 : export function resolveCompiledVisualization(
46 1 : sql: string,
47 1 : visualization: core.buffers.execution.CompiledVisualizationT,
48 1 : statementId: number,
49 1 : ): ResolvedVisualizeQuery | null {
50 1 : const dashboardSpec = visualization.dashboard ?? null;
51 1 : switch (visualization.renderer) {
52 0 : case 'markdown': {
53 0 : const markdownSpec = visualization.markdownSpec;
54 0 : return markdownSpec?.document ? { renderer: 'markdown', sql, statementId, markdownSpec, dashboardSpec } : null;
55 0 : }
56 1 : case 'vegalite': {
57 1 : const raw = visualization.vegaliteSpec;
58 1 : if (!raw) return null;
59 1 : try {
60 1 : return { renderer: 'vegalite', sql, statementId, vegaLiteSpec: JSON.parse(String(raw)), dashboardSpec };
61 1 : } catch {
62 1 : return null;
63 1 : }
64 1 : }
65 1 : case 'table':
66 1 : return { renderer: 'table', sql, statementId, dashboardSpec };
67 0 : default:
68 0 : return null;
69 1 : }
70 1 : }
|