@nongt/visql-core 0.10.0 → 0.11.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.
@@ -70,8 +70,16 @@ export interface FunctionCallNode extends Record<string, unknown>, ColumnCopy {
70
70
  /** An aggregate over distinct values only: `COUNT(DISTINCT x)`. */
71
71
  distinct?: boolean;
72
72
  /** The order an aggregate takes its values in (issue 164): STRING_AGG's
73
- * text lists them so. Only STRING_AGG takes one. */
73
+ * text lists them so. A window call's (`over`) is its OVER's ORDER BY.
74
+ * Only STRING_AGG and window calls take one. */
74
75
  orderBy?: FunctionSortKey[];
76
+ /** A window function's call (issue 20, `window` in the registry): written
77
+ * with `OVER (PARTITION BY … ORDER BY …)`, over the rows its body
78
+ * returns rather than one value per row or group. */
79
+ over?: boolean;
80
+ /** A window call's PARTITION BY: the rows it looks at for a row are
81
+ * those with the same values. */
82
+ partitionBy?: ExpressionNode[];
75
83
  }
76
84
  /** One key of a function call's `orderBy`. */
77
85
  export interface FunctionSortKey {
@@ -1,4 +1,4 @@
1
- import type { ExpressionNode, ValueType } from './ast.js';
1
+ import type { ExpressionNode, FunctionCallNode, ValueType } from './ast.js';
2
2
  import type { DatabaseColumnType } from './schema.js';
3
3
  import type { SqlDialect } from './sqlGen.js';
4
4
  /**
@@ -13,8 +13,11 @@ import type { SqlDialect } from './sqlGen.js';
13
13
  * fixed-text — a text written into the SQL itself, never a parameter (STRING_AGG's
14
14
  * separator: MySQL takes only a literal there), stored as string literal,
15
15
  * shown as text input
16
+ * fixed-count — a whole number from 0 written into the SQL itself, never a parameter
17
+ * (LAG's offset: MySQL takes only a literal there), stored as number
18
+ * literal, shown as number input
16
19
  */
17
- export type ArgKind = 'expr' | 'date-expr' | 'num-expr' | 'str-expr' | 'unit' | 'type-name' | 'fixed-text';
20
+ export type ArgKind = 'expr' | 'date-expr' | 'num-expr' | 'str-expr' | 'unit' | 'type-name' | 'fixed-text' | 'fixed-count';
18
21
  /**
19
22
  * Maps ArgKind to the column types that should be shown when the arg value is a field.
20
23
  * Undefined means no filter (all column types are shown).
@@ -46,6 +49,10 @@ export interface FunctionSignature {
46
49
  * SQL allows one — SELECT, HAVING and ORDER BY, not WHERE, JOIN ON or
47
50
  * GROUP BY — and what makes a query grouped (see grouping.ts). */
48
51
  aggregate?: boolean;
52
+ /** A window function (issue 20: LAG, ROW_NUMBER, …): its call has `over`
53
+ * and an ORDER BY, and goes only in SELECT and ORDER BY, of a body that
54
+ * isn't grouped. */
55
+ window?: boolean;
49
56
  /** The type of its result (issue 130), so a value compared with it gets
50
57
  * the matching input: `'arg'` for its first argument's (MIN, COALESCE,
51
58
  * DATE_ADD). Omitted: not known (the user picks the value's type). */
@@ -112,6 +119,12 @@ export declare function normalizeRegistry(registry: FunctionRegistry): FunctionR
112
119
  /** `registry` without its aggregates — what's offered where SQL doesn't
113
120
  * allow one (WHERE, JOIN ON, GROUP BY). */
114
121
  export declare function withoutAggregates(registry: FunctionRegistry): FunctionRegistry;
122
+ /** A new call to `sig`, as a function picker starts one: its default
123
+ * arguments, and OVER for a window function (issue 20). `name` is what
124
+ * it's stored by. */
125
+ export declare function startCall(sig: FunctionSignature, name: string): FunctionCallNode;
126
+ /** `registry` without its window functions (issue 20). */
127
+ export declare function withoutWindows(registry: FunctionRegistry): FunctionRegistry;
115
128
  /** Only the functions available in `dialect` (those with no `dialects` list
116
129
  * are available everywhere). */
117
130
  export declare function functionsForDialect(registry: FunctionRegistry, dialect: SqlDialect): FunctionRegistry;
@@ -94,6 +94,9 @@ export declare const DEFAULT_MESSAGES: {
94
94
  * (which group the rows) apart from the rest. Kept short: the list is
95
95
  * as narrow as the name box. */
96
96
  functionGroupAggregate: string;
97
+ /** Window functions (LAG, ROW_NUMBER, …): each looks at the rows around
98
+ * a row. */
99
+ functionGroupWindow: string;
97
100
  functionGroupOther: string;
98
101
  typeMenuCondition: string;
99
102
  /** IN's value as a query of its own (issue 122), when offered: its type,
@@ -239,6 +242,12 @@ export declare const DEFAULT_MESSAGES: {
239
242
  addSort: string;
240
243
  removeSort: string;
241
244
  toggleSortDirection: string;
245
+ /** A window function's OVER (issue 20): the columns its rows are split
246
+ * by, and their order. */
247
+ windowPartitionBy: string;
248
+ windowOrderBy: string;
249
+ addPartition: string;
250
+ removePartition: string;
242
251
  /** Opens the row-count inputs. */
243
252
  limitRows: string;
244
253
  removeLimit: string;
@@ -300,6 +309,7 @@ export declare const DEFAULT_MESSAGES: {
300
309
  locationColumn: (n: number) => string;
301
310
  locationGroupKey: (n: number) => string;
302
311
  locationSortKey: (n: number) => string;
312
+ locationPartitionKey: (n: number) => string;
303
313
  locationCondition: (n: number) => string;
304
314
  locationLeft: string;
305
315
  locationRight: string;
@@ -343,6 +353,9 @@ export declare const DEFAULT_MESSAGES: {
343
353
  issueSharedOrder: string;
344
354
  issueUnusedShared: string;
345
355
  issueJoinOnlyColumn: string;
356
+ issueMisplacedWindow: string;
357
+ issueGroupedWindow: string;
358
+ issueUnorderedWindow: string;
346
359
  issueUnknownTableNamed: (table: string) => string;
347
360
  issueUnknownColumnNamed: (table: string, column: string) => string;
348
361
  issueJoinOnlyColumnNamed: (column: string) => string;
@@ -370,6 +383,9 @@ export declare const DEFAULT_MESSAGES: {
370
383
  issueNotGroupedNamed: (column: string) => string;
371
384
  issueSelectAllGrouped: string;
372
385
  issueMisplacedAggregateNamed: (fn: string) => string;
386
+ issueMisplacedWindowNamed: (fn: string) => string;
387
+ issueGroupedWindowNamed: (fn: string) => string;
388
+ issueUnorderedWindowNamed: (fn: string) => string;
373
389
  peekHint: string;
374
390
  /** `keys`: its shortcut, as the user's keyboard has it (⌘F, Ctrl+F). */
375
391
  find: (keys: string) => string;
@@ -50,6 +50,11 @@ export type IssueLocationStep =
50
50
  kind: 'sortKey';
51
51
  number: number;
52
52
  }
53
+ /** One of a window call's PARTITION BY keys (issue 20), from 1. */
54
+ | {
55
+ kind: 'partitionKey';
56
+ number: number;
57
+ }
53
58
  /** One of a group's conditions (from 1). */
54
59
  | {
55
60
  kind: 'condition';
@@ -1,4 +1,4 @@
1
- import type { BinaryNode, BinaryOperator, ExistsNode, ExpressionNode, FieldNode, FunctionCallNode, LiteralNode, ListNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, SubqueryNode } from './ast.js';
1
+ import type { BinaryNode, BinaryOperator, ExistsNode, ExpressionNode, FieldNode, FunctionCallNode, FunctionSortKey, LiteralNode, ListNode, LiteralValue, LogicalNode, LogicalOperator, NotNode, ParamNode, RelativeDate, SubqueryNode } from './ast.js';
2
2
  import { type DerivedQuery, type JoinClause, type TableFrom, type JoinType, type QueryPipeline, type QueryValue, type SortDirection } from './queryAst.js';
3
3
  import type { DatabaseSchema } from './schema.js';
4
4
  export declare const expr: {
@@ -28,6 +28,14 @@ export declare const expr: {
28
28
  not: (operand: ExpressionNode) => NotNode;
29
29
  /** A function call: `expr.fn('LOWER', expr.field('users', 'email'))`. */
30
30
  fn: (name: string, ...args: ExpressionNode[]) => FunctionCallNode;
31
+ /** A window function's call (issue 20), over the rows of its body:
32
+ * `expr.over(expr.fn('LAG', status), { partitionBy: [employee], orderBy:
33
+ * [{ expression: payDate, direction: 'asc' }] })` is `LAG(status) OVER
34
+ * (PARTITION BY employee ORDER BY pay_date ASC)`. */
35
+ over: (call: FunctionCallNode, window: {
36
+ partitionBy?: ExpressionNode[];
37
+ orderBy?: FunctionSortKey[];
38
+ }) => FunctionCallNode;
31
39
  /** Whether the outer row has related rows: `EXISTS (SELECT 1 FROM …
32
40
  * WHERE <link> AND <where>)`. `link` matches them to the outer row;
33
41
  * `where` names the subquery's own tables only. NOT EXISTS:
@@ -61,6 +61,16 @@ export type QueryIssueKind =
61
61
  | 'unknownAggregate'
62
62
  /** An aggregate where SQL doesn't allow one: WHERE, JOIN ON, GROUP BY. */
63
63
  | 'misplacedAggregate'
64
+ /** A window function (issue 20) where SQL doesn't allow one: anywhere but
65
+ * SELECT and ORDER BY (WHERE, JOIN ON, GROUP BY, HAVING, an EXISTS), or
66
+ * inside another window function or an aggregate. */
67
+ | 'misplacedWindow'
68
+ /** A window function in a grouped query (aggregates, GROUP BY or
69
+ * HAVING): work it out in a sub query, and group around it. */
70
+ | 'groupedWindow'
71
+ /** A window function with no ORDER BY: which row comes before which
72
+ * isn't said, and SQL Server refuses it. */
73
+ | 'unorderedWindow'
64
74
  /** A param compared against columns of different types (`:x` against a
65
75
  * date and a number) — no one value suits both. */
66
76
  | 'paramConflict'
@@ -135,7 +145,7 @@ export type QueryIssueKind =
135
145
  /** A join-only column (a key, issue 234) used outside a join's ON or an
136
146
  * EXISTS's link, and not in a sub query's SELECT or GROUP BY. */
137
147
  | 'joinOnlyColumn';
138
- export declare const QUERY_ISSUE_KINDS: readonly ["invalidStructure", "duplicateId", "unknownTable", "unknownColumn", "tableNotInQuery", "unqualifiedField", "incomplete", "argCount", "ambiguousTable", "duplicateTableName", "derivedColumnName", "unknownFunction", "joinOrder", "distinctSort", "notGrouped", "unknownAggregate", "misplacedAggregate", "paramConflict", "fanOut", "crossJoin", "unlinkedJoin", "drillAcrossOutOfStep", "drillAcrossFilterMixed", "tooLong", "dialectUnsupported", "subqueryColumns", "notInSubquery", "noLimit", "limitTooHigh", "typeMismatch", "invalidPattern", "unionColumns", "unionTypes", "sharedOrder", "unusedShared", "joinOnlyColumn"];
148
+ export declare const QUERY_ISSUE_KINDS: readonly ["invalidStructure", "duplicateId", "unknownTable", "unknownColumn", "tableNotInQuery", "unqualifiedField", "incomplete", "argCount", "ambiguousTable", "duplicateTableName", "derivedColumnName", "unknownFunction", "joinOrder", "distinctSort", "notGrouped", "unknownAggregate", "misplacedAggregate", "paramConflict", "fanOut", "crossJoin", "misplacedWindow", "groupedWindow", "unorderedWindow", "unlinkedJoin", "drillAcrossOutOfStep", "drillAcrossFilterMixed", "tooLong", "dialectUnsupported", "subqueryColumns", "notInSubquery", "noLimit", "limitTooHigh", "typeMismatch", "invalidPattern", "unionColumns", "unionTypes", "sharedOrder", "unusedShared", "joinOnlyColumn"];
139
149
  /** The kinds that are warnings: the query can run with them. Only these
140
150
  * can be hidden (ValidateQueryOptions' hideWarnings). */
141
151
  export declare const QUERY_WARNING_KINDS: readonly ["fanOut", "crossJoin", "unlinkedJoin", "drillAcrossFilterMixed", "notInSubquery", "noLimit", "limitTooHigh", "typeMismatch", "unknownAggregate", "unusedShared"];
@@ -161,6 +161,11 @@ export interface SuggestJoinOptions {
161
161
  * join tied to one of them is LEFT too, whatever its key: an INNER join
162
162
  * there would drop the rows the LEFT join kept. */
163
163
  leftJoined?: string[];
164
+ /** Tie it to a table reached by a step up even when joining it from
165
+ * there is a step down, as scopeQuery's paths may (JoinPathOptions). By
166
+ * default such a table is no tie, so a table findJoinPath finds no path
167
+ * to gets no join (issue 17). */
168
+ anyShape?: boolean;
164
169
  }
165
170
  /** Suggests a join type + ON condition for adding `newTable` alongside the
166
171
  * tables already in the pipeline, using the relation graph. Tied to
@@ -173,7 +178,7 @@ export interface SuggestJoinOptions {
173
178
  * an INNER join there would silently drop unpromoted sales) or the table
174
179
  * it's tied to is in `options.leftJoined`, and 'inner' otherwise. Returns
175
180
  * undefined if no relation connects `newTable` to anything already
176
- * present. */
181
+ * present, or only a step down from a table reached by a step up. */
177
182
  export declare function suggestJoin(schema: DatabaseSchema, existingTables: string[], newTable: string, options?: SuggestJoinOptions): JoinSuggestion | undefined;
178
183
  export type SchemaIssueKind = 'duplicateTable' | 'duplicateColumn' | 'unknownTable' | 'unknownColumn' | 'duplicateRelation' | 'invalidFormat';
179
184
  export interface SchemaIssue {
@@ -2,7 +2,7 @@ import type { LiteralValue } from './ast.js';
2
2
  import type { QueryValue } from './queryAst.js';
3
3
  /** The format saveQuery writes. Raised when a change to the query's shape
4
4
  * needs older stored queries migrated (see MIGRATIONS). */
5
- export declare const QUERY_FORMAT_VERSION = 4;
5
+ export declare const QUERY_FORMAT_VERSION = 5;
6
6
  /** A query as stored: plain JSON, with the format it's in. */
7
7
  export interface StoredQuery {
8
8
  version: number;
@@ -111,8 +111,16 @@ export interface SqlBetween {
111
111
  high: SqlExpr;
112
112
  span: SqlSpan;
113
113
  }
114
- /** A function call, with what aggregates take besides arguments. A window
115
- * (`OVER …`) and PostgreSQL's FILTER are only marked: the AST has neither. */
114
+ /** A window's `OVER (PARTITION BY … ORDER BY …)`. A frame (`ROWS
115
+ * BETWEEN …`) is only marked: the AST has none. */
116
+ export interface SqlWindow {
117
+ partitionBy: SqlExpr[];
118
+ orderBy?: SqlOrderItem[];
119
+ frame?: SqlSpan;
120
+ }
121
+ /** A function call, with what aggregates take besides arguments. PostgreSQL's
122
+ * FILTER is only marked: the AST has none. `over` is the whole `OVER …`,
123
+ * and `window` what's in it — none for a named window (`OVER w`). */
116
124
  export interface SqlCall {
117
125
  kind: 'call';
118
126
  name: SqlIdent[];
@@ -126,6 +134,7 @@ export interface SqlCall {
126
134
  withinGroup?: SqlOrderItem[];
127
135
  filter?: SqlSpan;
128
136
  over?: SqlSpan;
137
+ window?: SqlWindow;
129
138
  span: SqlSpan;
130
139
  }
131
140
  /** `CAST(x AS type)`, or PostgreSQL's `x::type`. `type` as written, its
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nongt/visql-core",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
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",