@nongt/visql-core 0.8.0 → 0.10.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.
- package/README.md +13 -9
- package/dist/core.js +339 -78
- package/dist/explain.js +356 -0
- package/dist/issueLocation-Y1iaP_Cs.js +1108 -0
- package/dist/params-kc6eNrpu.js +2223 -0
- package/dist/{schema-DFC26AhZ.js → schema-KDJ1rpG5.js} +334 -271
- package/dist/sql-import.js +1 -1
- package/dist/sqlGen-Crgheqys.js +903 -0
- package/dist/types/core/i18n/defaultMessages.d.ts +15 -0
- package/dist/types/core/pipelineFlow.d.ts +1 -1
- package/dist/types/core/planFindings.d.ts +27 -0
- package/dist/types/core/queryHelpers.d.ts +1 -1
- package/dist/types/core/schema.d.ts +68 -24
- package/dist/types/core/scopeQuery.d.ts +2 -2
- package/dist/types/core-api.d.ts +2 -1
- package/dist/types/explain/explainQuery.d.ts +59 -0
- package/dist/types/explain.d.ts +2 -0
- package/package.json +11 -3
- package/dist/core-api-jaQDPXVK.js +0 -1171
- package/dist/issueLocation-DEIwB1DW.js +0 -3290
|
@@ -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;
|
|
@@ -140,7 +140,7 @@ export declare function referencedTables(ast: ExpressionNode | undefined): strin
|
|
|
140
140
|
/** Auto-adds (via addJoin's own suggestJoin logic) every table referenced by
|
|
141
141
|
* a field in `ast` that the pipeline doesn't already have. A table with no
|
|
142
142
|
* direct relation to anything already present gets the intermediate tables
|
|
143
|
-
* it needs added first (
|
|
143
|
+
* it needs added first (joinAlong) — e.g. picking a product_category
|
|
144
144
|
* column with only sale_transaction in FROM adds product, then
|
|
145
145
|
* product_category — so each added join gets a real suggested ON condition
|
|
146
146
|
* instead of an unconnected one. A table nothing connects to at all is still
|
|
@@ -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
|
|
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,34 +109,72 @@ 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
|
-
/**
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
export declare function
|
|
112
|
+
/** How findJoinPath and reachableTables may walk the relations. */
|
|
113
|
+
export interface JoinPathOptions {
|
|
114
|
+
/** Walk any path. By default a path never takes a one-to-many step
|
|
115
|
+
* (down: from a relation's `toTable` to its `fromTable`) after a
|
|
116
|
+
* many-to-one step (up), counting how the query's own tables were
|
|
117
|
+
* reached: `hours → employee → punch` pairs each hours row with every
|
|
118
|
+
* punch of its employee, on any day. A path is up only, or down and then
|
|
119
|
+
* up. scopeQuery walks any path: a rule can go up to a tenant and down to
|
|
120
|
+
* who may see it. */
|
|
121
|
+
anyShape?: boolean;
|
|
122
|
+
/** reachableTables: tables neither reached nor walked through. */
|
|
123
|
+
skip?: ReadonlySet<string>;
|
|
124
|
+
}
|
|
125
|
+
/** findJoinPath's path, with the table of the query it starts from. */
|
|
126
|
+
export declare function joinRoute(schema: DatabaseSchema, existingTables: string[], target: string, options?: JoinPathOptions): {
|
|
127
|
+
start: string;
|
|
128
|
+
tables: string[];
|
|
129
|
+
} | undefined;
|
|
130
|
+
/** The chain of tables connecting `target` to any of `existingTables`
|
|
131
|
+
* through the relation graph (walked in both directions), as the tables
|
|
132
|
+
* that would need to be added, in join order, intermediates first, ending
|
|
133
|
+
* with `target` itself. e.g. from [sale_transaction] to product_category
|
|
134
|
+
* that's [product, product_category]: product_category has no relation to
|
|
135
|
+
* sale_transaction directly, only through product.
|
|
136
|
+
*
|
|
137
|
+
* Of all the chains, the one with the fewest fan-out steps, then the
|
|
138
|
+
* fewest steps (issue 6). A fan-out step walks a relation from its `toTable`
|
|
139
|
+
* to its `fromTable`, one row to many, which repeats the rows before it.
|
|
140
|
+
* None comes after a step the other way, unless `options.anyShape`
|
|
141
|
+
* (JoinPathOptions). Ties go the way a breadth-first walk would: seeded in
|
|
142
|
+
* `existingTables` order, neighbours in `relations` order, so the result
|
|
143
|
+
* is deterministic. A table with `passThrough: false` is never a step (see
|
|
144
|
+
* startsOf for where it may start). Returns [] when `target` is already
|
|
145
|
+
* present, undefined when nothing connects it. */
|
|
146
|
+
export declare function findJoinPath(schema: DatabaseSchema, existingTables: string[], target: string, options?: JoinPathOptions): string[] | undefined;
|
|
147
|
+
/** Every table the relation graph reaches from `existingTables`, them
|
|
148
|
+
* included: what findJoinPath finds a path to, for all tables in one walk.
|
|
149
|
+
* A table with `passThrough: false` is reached, but nothing past it. */
|
|
150
|
+
export declare function reachableTables(schema: DatabaseSchema, existingTables: string[], options?: JoinPathOptions): Set<string>;
|
|
121
151
|
export interface JoinSuggestion {
|
|
122
152
|
joinType: JoinType;
|
|
123
153
|
on: ExpressionNode;
|
|
124
154
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
export interface SuggestJoinOptions {
|
|
156
|
+
/** The table before `newTable` on its join path (joinRoute): the join is
|
|
157
|
+
* tied to it, when they're related, rather than to the first table of
|
|
158
|
+
* the query that is. */
|
|
159
|
+
after?: string;
|
|
160
|
+
/** The query's tables on the optional side of a LEFT or FULL join. A
|
|
161
|
+
* join tied to one of them is LEFT too, whatever its key: an INNER join
|
|
162
|
+
* there would drop the rows the LEFT join kept. */
|
|
163
|
+
leftJoined?: string[];
|
|
164
|
+
}
|
|
165
|
+
/** Suggests a join type + ON condition for adding `newTable` alongside the
|
|
166
|
+
* tables already in the pipeline, using the relation graph. Tied to
|
|
167
|
+
* `options.after` when it's related; else tries each existing table in
|
|
168
|
+
* order (base table first, then join-insertion order) and the first
|
|
169
|
+
* relation found wins, so the result is deterministic. A table with
|
|
170
|
+
* `passThrough: false` is tried only for itself, or when the query has no
|
|
171
|
+
* other table (startsOf). `joinType` is 'left' when the relation's FK
|
|
172
|
+
* column (`fromColumn`) is nullable (e.g. sale_transaction.promotion_id:
|
|
173
|
+
* an INNER join there would silently drop unpromoted sales) or the table
|
|
174
|
+
* it's tied to is in `options.leftJoined`, and 'inner' otherwise. Returns
|
|
175
|
+
* undefined if no relation connects `newTable` to anything already
|
|
176
|
+
* present. */
|
|
177
|
+
export declare function suggestJoin(schema: DatabaseSchema, existingTables: string[], newTable: string, options?: SuggestJoinOptions): JoinSuggestion | undefined;
|
|
134
178
|
export type SchemaIssueKind = 'duplicateTable' | 'duplicateColumn' | 'unknownTable' | 'unknownColumn' | 'duplicateRelation' | 'invalidFormat';
|
|
135
179
|
export interface SchemaIssue {
|
|
136
180
|
kind: SchemaIssueKind;
|
|
@@ -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
|
-
*
|
|
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
|
|
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
|
package/dist/types/core-api.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export { relativeDateLabel } from './core/relativeDates.js';
|
|
|
14
14
|
export { QUERY_FORMAT_VERSION, QueryFormatError, loadQuery, saveQuery, type StoredQuery, } from './core/storedQuery.js';
|
|
15
15
|
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
|
-
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';
|
|
17
|
+
export { EMPTY_SCHEMA, filterSchema, validateSchema, type DatabaseColumnType, type DatabaseSchema, type ColumnOption, type Relation, findJoinPath, reachableTables, suggestJoin, type JoinPathOptions, type JoinSuggestion, type SuggestJoinOptions, 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,59 @@
|
|
|
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' | 'mssql' | 'mysql';
|
|
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 PostgreSQL plan only
|
|
15
|
+
* gives the rows a scan returns, after its filter: with the sizes, a big
|
|
16
|
+
* table read whole for a few rows is reported as a missing index. SQL
|
|
17
|
+
* Server's plan has them itself. */
|
|
18
|
+
tableRows?: Record<string, number> | ((table: string) => number | undefined);
|
|
19
|
+
/** Below this many rows, nothing is reported. Default 10,000. */
|
|
20
|
+
minRows?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface ExplainedQuery {
|
|
23
|
+
/** SQL Server: run on the same connection before `sql`, as a batch of
|
|
24
|
+
* its own, and `after` once the plan is back. */
|
|
25
|
+
before?: string;
|
|
26
|
+
/** The statement to run, EXPLAIN included, with `params` bound. */
|
|
27
|
+
sql: string;
|
|
28
|
+
params: unknown[];
|
|
29
|
+
after?: string;
|
|
30
|
+
/** What the plan EXPLAIN gave says, on the parts of the query given to
|
|
31
|
+
* explainQuery. Takes the plan as the driver returns it (PostgreSQL:
|
|
32
|
+
* rows with a `QUERY PLAN` column; SQL Server: the showplan's row) or as
|
|
33
|
+
* its JSON or XML text. */
|
|
34
|
+
findings: (plan: unknown, options?: PlanReadOptions) => PlanFinding[];
|
|
35
|
+
}
|
|
36
|
+
/** A table, sub query or shared query's place in the query, under the name
|
|
37
|
+
* the explained SQL gives it. */
|
|
38
|
+
export interface Place {
|
|
39
|
+
/** Where it is: its FROM or join. */
|
|
40
|
+
path: (string | number)[];
|
|
41
|
+
/** Where the query body it's in is. */
|
|
42
|
+
body: (string | number)[];
|
|
43
|
+
/** Its schema table, for a table. */
|
|
44
|
+
table?: string;
|
|
45
|
+
}
|
|
46
|
+
/** `query` with every table, sub query and shared query's place named
|
|
47
|
+
* apart, and where each new name leads. Innermost bodies first: an
|
|
48
|
+
* EXISTS sees the names around it, so a name left in it after its own are
|
|
49
|
+
* renamed is the outer one, and renaming the outer body reaches it. */
|
|
50
|
+
export declare function namePlaces(query: QueryPipeline): {
|
|
51
|
+
named: QueryPipeline;
|
|
52
|
+
places: Map<string, Place>;
|
|
53
|
+
};
|
|
54
|
+
/** The statement to explain `query` with, and a reader for the plan it
|
|
55
|
+
* gives. Call it as you'd write the query's SQL to run it: on the checked
|
|
56
|
+
* query, with the same options (and `scope`, the rule it runs under), so
|
|
57
|
+
* the plan is the one it really gets. The findings are on `query` itself,
|
|
58
|
+
* the parts a user sees in the editor. */
|
|
59
|
+
export declare function explainQuery(query: QueryPipeline, dialect: ExplainDialect, options?: ExplainOptions): ExplainedQuery;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nongt/visql-core",
|
|
3
|
-
"version": "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.10.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
|
}
|