Line data Source code
1 : import * as dashql from '@ankoh/dashql-core';
2 :
3 : /// Values can be megabytes large, only look at the first characters
4 1 : const PEEK_WINDOW = 4096;
5 : /// Trino plans map fragment ids to plan nodes: {"0": {"id": "6", "name": "Output", ...}}
6 1 : const TRINO_PLAN_RE = /^\{\s*"\d+"\s*:\s*\{\s*"id"\s*:/;
7 : /// Hyper plans start with the operator of the root, e.g. {"tree":{"operator":...}}
8 1 : const HYPER_OPERATOR_KEY = '"operator"';
9 :
10 : /// The engine whose plan a text is, null if the text is no plan. A cheap look at the first characters, the parser
11 : /// of the engine decides when the plan is loaded.
12 1 : export function detectPlanEngine(text: string | null): dashql.buffers.profile.ProfileEngine | null {
13 1 : if (text == null) return null;
14 1 : const head = text.slice(0, PEEK_WINDOW).trimStart();
15 1 : if (!head.startsWith('{') || !text.slice(-PEEK_WINDOW).trimEnd().endsWith('}')) return null;
16 1 : if (TRINO_PLAN_RE.test(head)) return dashql.buffers.profile.ProfileEngine.TRINO;
17 1 : if (head.includes(HYPER_OPERATOR_KEY)) return dashql.buffers.profile.ProfileEngine.HYPER;
18 1 : return null;
19 1 : }
|