Line data Source code
1 : import { detectPlanEngine } from '../query_profile/plan_engine.js';
2 :
3 : export type FormatHint = 'array' | 'struct' | 'json' | 'sql' | 'plan' | null;
4 :
5 : /// Values can be megabytes large, only look at the first and last characters
6 1 : const PEEK_WINDOW = 4096;
7 1 : const SQL_RE = /^\s*(SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|WITH|EXPLAIN|COPY|SET|BEGIN|COMMIT|ROLLBACK)\b/i;
8 :
9 1 : export function peekFormat(value: string | null): FormatHint {
10 1 : if (value == null || value.length < 2) return null;
11 1 : const head = value.slice(0, PEEK_WINDOW).trimStart();
12 1 : const tail = value.slice(-PEEK_WINDOW).trimEnd();
13 1 : const first = head.length > 0 ? head[0] : null;
14 1 : const last = tail.length > 0 ? tail[tail.length - 1] : null;
15 1 : if ((first === '{' || first === '[') && (last === '}' || last === ']')) {
16 : // A plan of an engine (Hyper, Trino)
17 1 : if (detectPlanEngine(value) != null) return 'plan';
18 1 : return 'json';
19 1 : }
20 1 : if (SQL_RE.test(head)) return 'sql';
21 1 : return null;
22 1 : }
|