@nongt/visql-core 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -281,6 +281,21 @@ export declare const DEFAULT_MESSAGES: {
281
281
  problemsWarnings: string;
282
282
  /** Issues about the query as a whole (no one section). */
283
283
  problemsQuery: string;
284
+ checkPerformance: string;
285
+ checkingPerformance: string;
286
+ /** The button's tooltip. */
287
+ checkPerformanceHint: string;
288
+ /** What the host's onExplain threw. */
289
+ checkPerformanceFailed: (message: string) => string;
290
+ performanceHints: (count: number) => string;
291
+ problemsPerformance: string;
292
+ /** The hints are from before the latest edits. */
293
+ problemsPerformanceOutdated: string;
294
+ /** `rows`: the plan's estimate, when it gives one. */
295
+ planFullScan: (table: string, rows?: number) => string;
296
+ planMissingIndex: (table: string, columns: string[]) => string;
297
+ planSort: (rows?: number) => string;
298
+ planTemporaryTable: (rows?: number) => string;
284
299
  locationJoinCondition: string;
285
300
  locationColumn: (n: number) => string;
286
301
  locationGroupKey: (n: number) => string;
@@ -0,0 +1,27 @@
1
+ import type { QueryPipeline } from './queryAst.js';
2
+ /**
3
+ * - `fullScan`: a table read whole (`pathSegments` at its FROM or join).
4
+ * - `missingIndex`: no index for `columns` of a table the query filters on.
5
+ * - `sort`: a sort the database does itself, for ORDER BY or grouping.
6
+ * - `temporaryTable`: a temporary table the database builds.
7
+ * - `other`: anything else; the editor shows its `message`.
8
+ *
9
+ * Like issue kinds, they're public API: a kind is only ever added.
10
+ */
11
+ export type PlanFindingKind = 'fullScan' | 'missingIndex' | 'sort' | 'temporaryTable' | 'other';
12
+ export interface PlanFinding {
13
+ kind: PlanFindingKind;
14
+ /** Where in the query, as `validateQuery`'s issues say it. */
15
+ pathSegments: (string | number)[];
16
+ /** For people, in English. The editor words the known kinds itself. */
17
+ message: string;
18
+ /** The table it's about, by its schema name, when it's about one. */
19
+ table?: string;
20
+ /** The plan's estimate of the rows involved. */
21
+ rows?: number;
22
+ /** A missing index's columns. */
23
+ columns?: string[];
24
+ }
25
+ /** Gets the plan's findings for a query: the host checks it, writes its SQL
26
+ * and runs EXPLAIN on its server, the same way as `onRun`. */
27
+ export type OnExplain = (query: QueryPipeline) => PlanFinding[] | Promise<PlanFinding[]>;
@@ -79,7 +79,7 @@ export declare function removeJoin(schema: DatabaseSchema, query: QueryPipeline,
79
79
  *
80
80
  * On a query reading a union of tables (addUnionTable, issue 121), a
81
81
  * condition naming tables goes into **every** branch, each joining what it
82
- * needs — so a backend scoping every query (a tenant, a facility) scopes
82
+ * needs — so a backend scoping every query (a tenant, a branch) scopes
83
83
  * every table's rows. It throws, changing nothing, when a branch can't
84
84
  * reach a table it names: a branch it skipped would leak rows. A condition
85
85
  * naming the union itself (its alias) is the query's own. */
@@ -16,6 +16,12 @@ export interface Table {
16
16
  description?: string;
17
17
  /** Anything the host's own code needs about the table. Never read here. */
18
18
  meta?: unknown;
19
+ /** false: joined only as the table asked for, never as a step on the way
20
+ * to another, and never the table a new join is tied to while the query
21
+ * has another to tie it to. For a table every other one references only
22
+ * to be scoped by (a tenant, a store): `a → tenant → b` pairs every `a`
23
+ * with every `b` of the same tenant. Default true. */
24
+ passThrough?: boolean;
19
25
  }
20
26
  export interface TableColumn {
21
27
  name: string;
@@ -103,33 +109,42 @@ export declare function relationsForTable(schema: DatabaseSchema, tableName: str
103
109
  * (`fromTable`), so callers never need to know which side is "many". When
104
110
  * several keys connect the same two tables, the first declared wins. */
105
111
  export declare function findRelation(schema: DatabaseSchema, tableA: string, tableB: string): Relation | undefined;
106
- /** Shortest chain of tables connecting `target` to any of `existingTables`
107
- * through the relation graph (walked in both directions), as the tables that
108
- * would need to be added — in join order, intermediates first, ending with
109
- * `target` itself. e.g. from [sale_transaction] to product_category that's
110
- * [product, product_category]: product_category has no relation to
111
- * sale_transaction directly, only through product. Breadth-first from every
112
- * existing table at once (seeded in `existingTables` order, neighbours in
113
- * `relations` order), so the result is deterministic and never longer than
114
- * needed. Returns [] when `target` is already present, undefined when
115
- * nothing connects it. */
112
+ /** The chain of tables connecting `target` to any of `existingTables`
113
+ * through the relation graph (walked in both directions), as the tables
114
+ * that would need to be added, in join order, intermediates first, ending
115
+ * with `target` itself. e.g. from [sale_transaction] to product_category
116
+ * that's [product, product_category]: product_category has no relation to
117
+ * sale_transaction directly, only through product.
118
+ *
119
+ * Of all the chains, the one with the fewest fan-out steps, then the
120
+ * fewest steps (issue 6). A fan-out step walks a relation from its `toTable`
121
+ * to its `fromTable`, one row to many, which repeats the rows before it.
122
+ * Ties go the way a breadth-first walk would: seeded in `existingTables`
123
+ * order, neighbours in `relations` order, so the result is deterministic.
124
+ * A table with `passThrough: false` is never a step (see startsOf for
125
+ * where it may start). Returns [] when `target` is already present,
126
+ * undefined when nothing connects it. */
116
127
  export declare function findJoinPath(schema: DatabaseSchema, existingTables: string[], target: string): string[] | undefined;
117
128
  /** Every table the relation graph (walked in both directions) reaches from
118
- * `existingTables`, them included — what findJoinPath finds a path to,
119
- * for all tables in one walk. */
129
+ * `existingTables`, them included: what findJoinPath finds a path to, for
130
+ * all tables in one walk. A table with `passThrough: false` is reached,
131
+ * but nothing past it. */
120
132
  export declare function reachableTables(schema: DatabaseSchema, existingTables: string[]): Set<string>;
121
133
  export interface JoinSuggestion {
122
134
  joinType: JoinType;
123
135
  on: ExpressionNode;
124
136
  }
125
- /** Suggests a join type + ON condition for adding `newTable` alongside the tables
126
- * already in the pipeline, using the relation graph. Tries each existing table in
127
- * order (base table first, then join-insertion order) — first relation found wins,
128
- * so the result is deterministic. `joinType` defaults to 'left' when the relation's
129
- * FK column (`fromColumn`) is nullable (e.g. sale_transaction.promotion_id — an INNER
130
- * join there would silently drop unpromoted sales) and 'inner' otherwise, read
131
- * directly off the column's own `nullable` flag rather than hardcoded per-relation.
132
- * Returns undefined if no relation connects `newTable` to anything already present. */
137
+ /** Suggests a join type + ON condition for adding `newTable` alongside the
138
+ * tables already in the pipeline, using the relation graph. Tries each
139
+ * existing table in order (base table first, then join-insertion order);
140
+ * the first relation found wins, so the result is deterministic. A table
141
+ * with `passThrough: false` is tried only for itself, or when the query has
142
+ * no other table (startsOf). `joinType` defaults to 'left' when the
143
+ * relation's FK column (`fromColumn`) is nullable (e.g.
144
+ * sale_transaction.promotion_id: an INNER join there would silently drop
145
+ * unpromoted sales) and 'inner' otherwise, read off the column's own
146
+ * `nullable` flag. Returns undefined if no relation connects `newTable` to
147
+ * anything already present. */
133
148
  export declare function suggestJoin(schema: DatabaseSchema, existingTables: string[], newTable: string): JoinSuggestion | undefined;
134
149
  export type SchemaIssueKind = 'duplicateTable' | 'duplicateColumn' | 'unknownTable' | 'unknownColumn' | 'duplicateRelation' | 'invalidFormat';
135
150
  export interface SchemaIssue {
@@ -4,10 +4,10 @@ import { type DatabaseSchema } from './schema.js';
4
4
  /** What must be scoped, and how. */
5
5
  export interface ScopeRule {
6
6
  /** The schema tables that must be scoped (e.g. every fact carrying a
7
- * facility), by name. */
7
+ * branch), by name. */
8
8
  requires: (table: string) => boolean;
9
9
  /** The condition, naming tables every scoped table reaches through the
10
- * relations (the facility dimension, say) — params rather than values. */
10
+ * relations (the branch dimension, say) — params rather than values. */
11
11
  condition: ExpressionNode;
12
12
  }
13
13
  /** A query body that reads a table needing scope but can't reach the
@@ -16,5 +16,6 @@ export type { ResolveName } from './core/resolveNames.js';
16
16
  export { astToParameterizedSql, astToSql, DIALECTS, DIALECT_LABELS, type NullSafeEqual, type ParameterizedSql, type ParameterizedSqlOptions, type SqlDialect, } from './core/sqlGen.js';
17
17
  export { EMPTY_SCHEMA, filterSchema, validateSchema, type DatabaseColumnType, type DatabaseSchema, type ColumnOption, type Relation, type SchemaIssue, type SchemaIssueKind, type Table, type TableColumn, type TableGroup, } from './core/schema.js';
18
18
  export { QUERY_ISSUE_KINDS, QUERY_WARNING_KINDS, validateQuery, type QueryIssue, type QueryIssueKind, type QueryWarningKind, type QueryIssueSeverity, type ValidateQueryOptions, } from './core/queryValidation.js';
19
+ export type { OnExplain, PlanFinding, PlanFindingKind } from './core/planFindings.js';
19
20
  export { default as issueLocation, type IssueLocationStep } from './core/issueLocation.js';
20
21
  export { DEFAULT_FUNCTIONS, functionsForDialect, type ArgDef, type ArgKind, type FunctionRegistry, type FunctionSignature, } from './core/functionRegistry.js';
@@ -0,0 +1,53 @@
1
+ import type { PlanFinding } from '../core/planFindings.js';
2
+ import type { QueryPipeline } from '../core/queryAst.js';
3
+ import { type ScopeRule } from '../core/scopeQuery.js';
4
+ import { type ParameterizedSqlOptions } from '../core/sqlGen.js';
5
+ /** The databases whose plans can be read. */
6
+ export type ExplainDialect = 'postgres';
7
+ export interface ExplainOptions extends ParameterizedSqlOptions {
8
+ /** The rule the query runs under (see scopeQuery), so the plan is the
9
+ * one the query really gets. Needs `schema`. */
10
+ scope?: ScopeRule;
11
+ }
12
+ export interface PlanReadOptions {
13
+ /** How many rows each table has, by its name in the database, e.g. from
14
+ * `SELECT relname, reltuples FROM pg_class`. A plan only gives the rows a
15
+ * scan returns, after its filter: with the sizes, a big table read
16
+ * whole for a few rows is reported as a missing index. */
17
+ tableRows?: Record<string, number> | ((table: string) => number | undefined);
18
+ /** Below this many rows, nothing is reported. Default 10,000. */
19
+ minRows?: number;
20
+ }
21
+ export interface ExplainedQuery {
22
+ /** The statement to run, EXPLAIN included, with `params` bound. */
23
+ sql: string;
24
+ params: unknown[];
25
+ /** What the plan EXPLAIN gave says, on the parts of the query given to
26
+ * explainQuery. Takes the plan as the driver returns it (rows with a
27
+ * `QUERY PLAN` column) or as its JSON text. */
28
+ findings: (plan: unknown, options?: PlanReadOptions) => PlanFinding[];
29
+ }
30
+ /** A table, sub query or shared query's place in the query, under the name
31
+ * the explained SQL gives it. */
32
+ export interface Place {
33
+ /** Where it is: its FROM or join. */
34
+ path: (string | number)[];
35
+ /** Where the query body it's in is. */
36
+ body: (string | number)[];
37
+ /** Its schema table, for a table. */
38
+ table?: string;
39
+ }
40
+ /** `query` with every table, sub query and shared query's place named
41
+ * apart, and where each new name leads. Innermost bodies first: an
42
+ * EXISTS sees the names around it, so a name left in it after its own are
43
+ * renamed is the outer one, and renaming the outer body reaches it. */
44
+ export declare function namePlaces(query: QueryPipeline): {
45
+ named: QueryPipeline;
46
+ places: Map<string, Place>;
47
+ };
48
+ /** The statement to explain `query` with, and a reader for the plan it
49
+ * gives. Call it as you'd write the query's SQL to run it: on the checked
50
+ * query, with the same options (and `scope`, the rule it runs under), so
51
+ * the plan is the one it really gets. The findings are on `query` itself,
52
+ * the parts a user sees in the editor. */
53
+ export declare function explainQuery(query: QueryPipeline, dialect: ExplainDialect, options?: ExplainOptions): ExplainedQuery;
@@ -0,0 +1,2 @@
1
+ export { explainQuery, type ExplainDialect, type ExplainedQuery, type ExplainOptions, type PlanReadOptions, } from './explain/explainQuery.js';
2
+ export type { PlanFinding, PlanFindingKind } from './core/planFindings.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nongt/visql-core",
3
- "version": "0.8.0",
4
- "description": "The query logic of @nongt/visql, for a backend: load, check, scope, write SQL, import SQL. No React, no dependencies.",
3
+ "version": "0.9.0",
4
+ "description": "The query logic of @nongt/visql, for a backend: load, check, scope, write SQL, import SQL, read query plans. No React, no dependencies.",
5
5
  "keywords": [
6
6
  "sql",
7
7
  "query",
@@ -36,6 +36,10 @@
36
36
  "types": "./dist/types/sql-import.d.ts",
37
37
  "import": "./dist/sql-import.js"
38
38
  },
39
+ "./explain": {
40
+ "types": "./dist/types/explain.d.ts",
41
+ "import": "./dist/explain.js"
42
+ },
39
43
  "./fixtures": {
40
44
  "types": "./dist/types/fixtures.d.ts",
41
45
  "import": "./dist/fixtures.js"
@@ -49,9 +53,13 @@
49
53
  "sql-import": [
50
54
  "./dist/types/sql-import.d.ts"
51
55
  ],
56
+ "explain": [
57
+ "./dist/types/explain.d.ts"
58
+ ],
52
59
  "fixtures": [
53
60
  "./dist/types/fixtures.d.ts"
54
61
  ]
55
62
  }
56
- }
63
+ },
64
+ "homepage": "https://nongt.github.io/visql/"
57
65
  }