@tabularis/explain-db2 0.0.0-stage → 0.1.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/LICENSE +203 -0
- package/README.md +62 -2
- package/dist/index.d.ts +110 -0
- package/dist/index.iife.js +261 -0
- package/dist/index.js +260 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Db2 Visual EXPLAIN parser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turns the payload the Db2 plugin's `explain_query` returns
|
|
4
|
+
(`engine: db2`, `format: db2-explain-tables-json`) into the plan tree
|
|
5
|
+
Tabularis's Visual EXPLAIN draws.
|
|
6
|
+
|
|
7
|
+
The payload carries the rows Db2's `EXPLAIN PLAN` wrote into its explain
|
|
8
|
+
tables for one statement (`EXPLAIN_OPERATOR`, `EXPLAIN_STREAM`,
|
|
9
|
+
`EXPLAIN_PREDICATE`, `EXPLAIN_ARGUMENT`, plus the tables of the indexes the
|
|
10
|
+
plan reads) and, with Analyze, the row count and elapsed time of the run.
|
|
11
|
+
`src/payload.ts` describes it; the plugin's `src/models.rs` produces it.
|
|
12
|
+
|
|
13
|
+
## npm usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @tabularis/explain @tabularis/explain-db2
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Import `@tabularis/explain-db2` to register the parser with
|
|
20
|
+
`@tabularis/explain` (version 0.2.0 or newer), or import `parseDb2Explain`
|
|
21
|
+
directly to parse a payload string.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import "@tabularis/explain-db2";
|
|
25
|
+
import { parseRawExplain } from "@tabularis/explain";
|
|
26
|
+
|
|
27
|
+
const plan = parseRawExplain({
|
|
28
|
+
engine: "db2",
|
|
29
|
+
format: "db2-explain-tables-json",
|
|
30
|
+
payload,
|
|
31
|
+
original_query: "SELECT * FROM orders",
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The release workflow publishes the package using npm Trusted Publishing.
|
|
36
|
+
Configure the trusted publisher on npm for `TabularisDB`, repository
|
|
37
|
+
`tabularis-db2-plugin`, and workflow `release.yml` before the first release.
|
|
38
|
+
|
|
39
|
+
The parser:
|
|
40
|
+
|
|
41
|
+
- makes tables and indexes the `relation` of the operator that reads them
|
|
42
|
+
- places predicates where Db2 applies them: index ranges in
|
|
43
|
+
`index_condition`, filters in `filter`, the condition of a hash join in
|
|
44
|
+
`hash_condition`
|
|
45
|
+
- reports join types from `OUTERJN`, and lists a join's outer input first
|
|
46
|
+
- keeps every operator argument in `extra`
|
|
47
|
+
- reads the compile time from `PREPTIME`
|
|
48
|
+
|
|
49
|
+
## Build and test
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pnpm install --frozen-lockfile
|
|
53
|
+
pnpm typecheck
|
|
54
|
+
pnpm test # builds dist/index.iife.js, then runs vitest
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`dist/index.iife.js` is the bundle `.tabularium` declares under
|
|
58
|
+
`explain_parsers`; the release workflow ships it in every plugin archive.
|
|
59
|
+
|
|
60
|
+
The fixtures in `tests/fixtures/` are payloads captured from Db2 12.1; the
|
|
61
|
+
plans they produce are committed in `tests/fixtures/expected/`. After an
|
|
62
|
+
intended change, regenerate them with `UPDATE_GOLDENS=1 pnpm test` and review
|
|
63
|
+
the diff.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { RegisteredExplainParser, ExplainNode, ExplainPlan } from '@tabularis/explain';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Db2 parser descriptor consumed by both package and plugin loaders. The
|
|
5
|
+
* engine and format match the plugin's `explain_query` answer and the
|
|
6
|
+
* `explain_parsers` entry of its `.tabularium`.
|
|
7
|
+
*/
|
|
8
|
+
declare const db2ExplainParser: RegisteredExplainParser;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The payload the Db2 plugin's `explain_query` sends: the rows Db2's
|
|
12
|
+
* `EXPLAIN` wrote into its explain tables for one statement, one list per
|
|
13
|
+
* table, with values as Db2 gives them.
|
|
14
|
+
*
|
|
15
|
+
* Mirrors `Db2ExplainPayload` in the plugin's `src/models.rs`: change both
|
|
16
|
+
* together.
|
|
17
|
+
*/
|
|
18
|
+
interface Db2ExplainPayload {
|
|
19
|
+
/** Version of this shape. Always the first key. */
|
|
20
|
+
db2_explain: 1;
|
|
21
|
+
/** The schema the statement was explained in. */
|
|
22
|
+
schema: string;
|
|
23
|
+
operators: Db2Operator[];
|
|
24
|
+
streams: Db2Stream[];
|
|
25
|
+
predicates: Db2Predicate[];
|
|
26
|
+
arguments: Db2Argument[];
|
|
27
|
+
indexes: Db2Index[];
|
|
28
|
+
/** What running the statement showed, when Analyze was asked for. */
|
|
29
|
+
analyze: Db2Analyze | null;
|
|
30
|
+
}
|
|
31
|
+
/** One `EXPLAIN_OPERATOR` row. */
|
|
32
|
+
interface Db2Operator {
|
|
33
|
+
id: number;
|
|
34
|
+
type: string;
|
|
35
|
+
total_cost: number | null;
|
|
36
|
+
io_cost: number | null;
|
|
37
|
+
cpu_cost: number | null;
|
|
38
|
+
first_row_cost: number | null;
|
|
39
|
+
re_total_cost: number | null;
|
|
40
|
+
buffers: number | null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* One `EXPLAIN_STREAM` row ending in an operator. `source_type` is `O` for
|
|
44
|
+
* an operator and `D` for a data object (a table or an index, source id -1).
|
|
45
|
+
*/
|
|
46
|
+
interface Db2Stream {
|
|
47
|
+
source_type: string;
|
|
48
|
+
source_id: number;
|
|
49
|
+
target_id: number;
|
|
50
|
+
object_schema: string | null;
|
|
51
|
+
object_name: string | null;
|
|
52
|
+
/** Estimated number of rows. */
|
|
53
|
+
count: number | null;
|
|
54
|
+
}
|
|
55
|
+
/** One `EXPLAIN_PREDICATE` row. */
|
|
56
|
+
interface Db2Predicate {
|
|
57
|
+
operator_id: number;
|
|
58
|
+
how_applied: string;
|
|
59
|
+
text: string;
|
|
60
|
+
}
|
|
61
|
+
/** One `EXPLAIN_ARGUMENT` row. */
|
|
62
|
+
interface Db2Argument {
|
|
63
|
+
operator_id: number;
|
|
64
|
+
type: string;
|
|
65
|
+
value: string;
|
|
66
|
+
}
|
|
67
|
+
/** An index the plan reads, with its table. */
|
|
68
|
+
interface Db2Index {
|
|
69
|
+
schema: string;
|
|
70
|
+
name: string;
|
|
71
|
+
table_schema: string;
|
|
72
|
+
table_name: string;
|
|
73
|
+
}
|
|
74
|
+
interface Db2Analyze {
|
|
75
|
+
/** Rows the statement returned, or changed when it returns none. */
|
|
76
|
+
rows: number;
|
|
77
|
+
elapsed_ms: number;
|
|
78
|
+
}
|
|
79
|
+
/** Whether `payload` starts like a Db2 payload. */
|
|
80
|
+
declare function looksLikeDb2Payload(payload: string): boolean;
|
|
81
|
+
/** Reads a payload, refusing anything that is not one this parser knows. */
|
|
82
|
+
declare function readPayload(payload: string): Db2ExplainPayload;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Builds the plan tree Tabularis draws from the explain-table rows.
|
|
86
|
+
*
|
|
87
|
+
* `EXPLAIN_OPERATOR` lists the operators (`RETURN`, `TBSCAN`, `HSJOIN`...),
|
|
88
|
+
* `EXPLAIN_STREAM` the rows flowing between them and from tables and
|
|
89
|
+
* indexes into them, `EXPLAIN_PREDICATE` where each predicate is applied
|
|
90
|
+
* and `EXPLAIN_ARGUMENT` the settings of each operator.
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
/** Reads `PREPTIME` ("46 milliseconds"), the time Db2 took to compile. */
|
|
94
|
+
declare function planningTimeMs(payload: Db2ExplainPayload): number | null;
|
|
95
|
+
/**
|
|
96
|
+
* The plan tree, rooted at the operator that passes its rows to no other
|
|
97
|
+
* one (`RETURN` for a query). `null` when there is no such operator.
|
|
98
|
+
*/
|
|
99
|
+
declare function buildPlanTree(payload: Db2ExplainPayload): ExplainNode | null;
|
|
100
|
+
/** The plan as indented text, one operator per line, for the raw view. */
|
|
101
|
+
declare function planText(root: ExplainNode): string;
|
|
102
|
+
/**
|
|
103
|
+
* Parses the Db2 plugin's payload into a plan. With Analyze, the statement
|
|
104
|
+
* ran: the top operator carries its real row count and elapsed time, and
|
|
105
|
+
* the operators below keep their estimates.
|
|
106
|
+
*/
|
|
107
|
+
declare function parseDb2Explain(rawPayload: string): ExplainPlan;
|
|
108
|
+
|
|
109
|
+
export { buildPlanTree, db2ExplainParser, looksLikeDb2Payload, parseDb2Explain, planText, planningTimeMs, readPayload };
|
|
110
|
+
export type { Db2Analyze, Db2Argument, Db2ExplainPayload, Db2Index, Db2Operator, Db2Predicate, Db2Stream };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
var __tabularis_explain_parser__ = (function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// src/payload.ts
|
|
5
|
+
function looksLikeDb2Payload(payload) {
|
|
6
|
+
return /^\s*\{\s*"db2_explain"\s*:/.test(payload.slice(0, 64));
|
|
7
|
+
}
|
|
8
|
+
function readPayload(payload) {
|
|
9
|
+
let value;
|
|
10
|
+
try {
|
|
11
|
+
value = JSON.parse(payload);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
throw new Error(`Db2 EXPLAIN payload is not JSON: ${error.message}`);
|
|
14
|
+
}
|
|
15
|
+
if (typeof value !== "object" || value === null) {
|
|
16
|
+
throw new Error("Db2 EXPLAIN payload is not an object");
|
|
17
|
+
}
|
|
18
|
+
const version = value.db2_explain;
|
|
19
|
+
if (version !== 1) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Db2 EXPLAIN payload version ${String(version)} is not supported by this parser`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const parsed = value;
|
|
25
|
+
for (const key of ["operators", "streams", "predicates", "arguments", "indexes"]) {
|
|
26
|
+
if (!Array.isArray(parsed[key])) {
|
|
27
|
+
throw new Error(`Db2 EXPLAIN payload has no '${key}' list`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return parsed;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/plan.ts
|
|
34
|
+
function objectLabel(schema, name, currentSchema) {
|
|
35
|
+
return schema === currentSchema ? name : `${schema}.${name}`;
|
|
36
|
+
}
|
|
37
|
+
function joinPredicates(texts) {
|
|
38
|
+
const unique = [...new Set(texts)];
|
|
39
|
+
return unique.length > 0 ? unique.join(" AND ") : null;
|
|
40
|
+
}
|
|
41
|
+
function joinType(operatorType, args) {
|
|
42
|
+
if (!operatorType.endsWith("JOIN")) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const outerJoin = args.get("OUTERJN")?.[0]?.trim().toUpperCase() ?? "";
|
|
46
|
+
switch (outerJoin) {
|
|
47
|
+
case "":
|
|
48
|
+
return "Inner";
|
|
49
|
+
case "LEFT":
|
|
50
|
+
return "Left";
|
|
51
|
+
case "RIGHT":
|
|
52
|
+
return "Right";
|
|
53
|
+
case "FULL":
|
|
54
|
+
return "Full";
|
|
55
|
+
default:
|
|
56
|
+
return outerJoin;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function planningTimeMs(payload) {
|
|
60
|
+
const prepTime = payload.arguments.find((argument) => argument.type === "PREPTIME");
|
|
61
|
+
if (!prepTime) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const [amount, unit = ""] = prepTime.value.trim().split(/\s+/);
|
|
65
|
+
const value = Number(amount);
|
|
66
|
+
if (!Number.isFinite(value)) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
if (unit.startsWith("milli")) return value;
|
|
70
|
+
if (unit.startsWith("micro")) return value / 1e3;
|
|
71
|
+
if (unit.startsWith("second")) return value * 1e3;
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
var PLACED_PREDICATES = /* @__PURE__ */ new Set(["START", "STOP", "SARG", "RESID", "JOIN"]);
|
|
75
|
+
function operatorNode(operator, payload) {
|
|
76
|
+
const id = operator.id;
|
|
77
|
+
const node = {
|
|
78
|
+
id: String(id),
|
|
79
|
+
node_type: operator.type.trim(),
|
|
80
|
+
relation: null,
|
|
81
|
+
startup_cost: operator.first_row_cost,
|
|
82
|
+
total_cost: operator.total_cost,
|
|
83
|
+
plan_rows: null,
|
|
84
|
+
actual_rows: null,
|
|
85
|
+
actual_time_ms: null,
|
|
86
|
+
actual_loops: null,
|
|
87
|
+
buffers_hit: null,
|
|
88
|
+
buffers_read: null,
|
|
89
|
+
filter: null,
|
|
90
|
+
index_condition: null,
|
|
91
|
+
join_type: null,
|
|
92
|
+
hash_condition: null,
|
|
93
|
+
extra: {},
|
|
94
|
+
children: []
|
|
95
|
+
};
|
|
96
|
+
const args = /* @__PURE__ */ new Map();
|
|
97
|
+
for (const argument of payload.arguments.filter((a) => a.operator_id === id)) {
|
|
98
|
+
const key = argument.type.trim();
|
|
99
|
+
args.set(key, [...args.get(key) ?? [], argument.value.trim()]);
|
|
100
|
+
}
|
|
101
|
+
const current = payload.schema;
|
|
102
|
+
for (const stream of payload.streams) {
|
|
103
|
+
if (stream.target_id !== id || stream.source_type !== "D") {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const schema = stream.object_schema ?? "";
|
|
107
|
+
const name = stream.object_name ?? "";
|
|
108
|
+
const index = payload.indexes.find(
|
|
109
|
+
(candidate) => candidate.schema === schema && candidate.name === name
|
|
110
|
+
);
|
|
111
|
+
if (index) {
|
|
112
|
+
node.relation = objectLabel(index.table_schema, index.table_name, current);
|
|
113
|
+
node.extra.index = objectLabel(schema, name, current);
|
|
114
|
+
} else {
|
|
115
|
+
node.relation = objectLabel(schema, name, current);
|
|
116
|
+
if (node.node_type === "TBSCAN") {
|
|
117
|
+
node.extra.access_type = "ALL";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const output = payload.streams.find(
|
|
122
|
+
(stream) => stream.source_type === "O" && stream.source_id === id
|
|
123
|
+
);
|
|
124
|
+
if (output && output.count !== null) {
|
|
125
|
+
node.plan_rows = output.count;
|
|
126
|
+
} else {
|
|
127
|
+
const inputs = payload.streams.filter((stream) => stream.target_id === id && stream.source_type === "O").map((stream) => stream.count).filter((count) => count !== null);
|
|
128
|
+
node.plan_rows = inputs.length > 0 ? Math.max(...inputs) : null;
|
|
129
|
+
}
|
|
130
|
+
const predicates = payload.predicates.filter((predicate) => predicate.operator_id === id);
|
|
131
|
+
const applied = (...kinds) => joinPredicates(
|
|
132
|
+
predicates.filter((predicate) => kinds.includes(predicate.how_applied.trim())).map((predicate) => predicate.text.trim())
|
|
133
|
+
);
|
|
134
|
+
node.index_condition = applied("START", "STOP");
|
|
135
|
+
node.filter = applied("SARG", "RESID");
|
|
136
|
+
const joinCondition = applied("JOIN");
|
|
137
|
+
if (node.node_type === "HSJOIN") {
|
|
138
|
+
node.hash_condition = joinCondition;
|
|
139
|
+
} else if (joinCondition !== null) {
|
|
140
|
+
node.extra.join_condition = joinCondition;
|
|
141
|
+
}
|
|
142
|
+
for (const predicate of predicates) {
|
|
143
|
+
const how = predicate.how_applied.trim();
|
|
144
|
+
if (!PLACED_PREDICATES.has(how)) {
|
|
145
|
+
node.extra[`${how.toLowerCase()} predicate`] = predicate.text.trim();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
node.join_type = joinType(node.node_type, args);
|
|
149
|
+
const costs = [
|
|
150
|
+
["io_cost", operator.io_cost],
|
|
151
|
+
["cpu_cost", operator.cpu_cost],
|
|
152
|
+
["re_total_cost", operator.re_total_cost],
|
|
153
|
+
["buffers", operator.buffers]
|
|
154
|
+
];
|
|
155
|
+
for (const [key, value] of costs) {
|
|
156
|
+
if (value !== null) {
|
|
157
|
+
node.extra[key] = value;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const [key, values] of [...args.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
161
|
+
node.extra[key] = values.join(", ");
|
|
162
|
+
}
|
|
163
|
+
return node;
|
|
164
|
+
}
|
|
165
|
+
function inputOrder(args, source) {
|
|
166
|
+
const side = args.find(
|
|
167
|
+
(argument) => argument.operator_id === source && argument.type.trim() === "JN INPUT"
|
|
168
|
+
)?.value.trim();
|
|
169
|
+
return [side === "OUTER" ? 0 : side === "INNER" ? 1 : 2, source];
|
|
170
|
+
}
|
|
171
|
+
function buildNode(operator, payload, byId, path) {
|
|
172
|
+
const node = operatorNode(operator, payload);
|
|
173
|
+
const inputs = [
|
|
174
|
+
...new Set(
|
|
175
|
+
payload.streams.filter((stream) => stream.target_id === operator.id && stream.source_type === "O").map((stream) => stream.source_id)
|
|
176
|
+
)
|
|
177
|
+
].sort((a, b) => {
|
|
178
|
+
const [sideA, idA] = inputOrder(payload.arguments, a);
|
|
179
|
+
const [sideB, idB] = inputOrder(payload.arguments, b);
|
|
180
|
+
return sideA - sideB || idA - idB;
|
|
181
|
+
});
|
|
182
|
+
for (const source of inputs) {
|
|
183
|
+
const input = byId.get(source);
|
|
184
|
+
if (input && !path.includes(source)) {
|
|
185
|
+
node.children.push(buildNode(input, payload, byId, [...path, operator.id]));
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return node;
|
|
189
|
+
}
|
|
190
|
+
function buildPlanTree(payload) {
|
|
191
|
+
const byId = new Map(payload.operators.map((operator) => [operator.id, operator]));
|
|
192
|
+
const hasConsumer = (id) => payload.streams.some(
|
|
193
|
+
(stream) => stream.source_type === "O" && stream.source_id === id && byId.has(stream.target_id)
|
|
194
|
+
);
|
|
195
|
+
const root = payload.operators.filter((operator) => !hasConsumer(operator.id)).sort((a, b) => a.id - b.id)[0];
|
|
196
|
+
return root ? buildNode(root, payload, byId, []) : null;
|
|
197
|
+
}
|
|
198
|
+
function planText(root) {
|
|
199
|
+
const lines = [];
|
|
200
|
+
const write = (node, depth) => {
|
|
201
|
+
const indent = " ".repeat(depth);
|
|
202
|
+
let line = `${indent}${node.id} ${node.node_type}`;
|
|
203
|
+
if (node.relation !== null) line += ` on ${node.relation}`;
|
|
204
|
+
if (typeof node.extra.index === "string") line += ` using ${node.extra.index}`;
|
|
205
|
+
if (node.total_cost !== null) {
|
|
206
|
+
line += ` (cost=${node.total_cost.toFixed(2)}`;
|
|
207
|
+
if (node.plan_rows !== null) line += ` rows=${Math.round(node.plan_rows)}`;
|
|
208
|
+
line += ")";
|
|
209
|
+
}
|
|
210
|
+
lines.push(line);
|
|
211
|
+
const details = [
|
|
212
|
+
["Join", node.hash_condition],
|
|
213
|
+
["Index", node.index_condition],
|
|
214
|
+
["Filter", node.filter]
|
|
215
|
+
];
|
|
216
|
+
for (const [label, value] of details) {
|
|
217
|
+
if (value !== null) lines.push(`${indent} ${label}: ${value}`);
|
|
218
|
+
}
|
|
219
|
+
for (const child of node.children) write(child, depth + 1);
|
|
220
|
+
};
|
|
221
|
+
write(root, 0);
|
|
222
|
+
return `${lines.join("\n")}
|
|
223
|
+
`;
|
|
224
|
+
}
|
|
225
|
+
function parseDb2Explain(rawPayload) {
|
|
226
|
+
const payload = readPayload(rawPayload);
|
|
227
|
+
const root = buildPlanTree(payload);
|
|
228
|
+
if (root === null) {
|
|
229
|
+
throw new Error("Db2 EXPLAIN payload has no operator to start the plan from");
|
|
230
|
+
}
|
|
231
|
+
if (payload.analyze) {
|
|
232
|
+
root.actual_rows = payload.analyze.rows;
|
|
233
|
+
root.actual_time_ms = payload.analyze.elapsed_ms;
|
|
234
|
+
root.actual_loops = 1;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
root,
|
|
238
|
+
planning_time_ms: planningTimeMs(payload),
|
|
239
|
+
execution_time_ms: payload.analyze?.elapsed_ms ?? null,
|
|
240
|
+
original_query: "",
|
|
241
|
+
driver: "db2",
|
|
242
|
+
has_analyze_data: payload.analyze !== null,
|
|
243
|
+
raw_output: planText(root)
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/parser.ts
|
|
248
|
+
var db2ExplainParser = {
|
|
249
|
+
engine: "db2",
|
|
250
|
+
format: "db2-explain-tables-json",
|
|
251
|
+
label: "Db2 explain tables",
|
|
252
|
+
parse: parseDb2Explain,
|
|
253
|
+
sniff: looksLikeDb2Payload
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// src/iife.ts
|
|
257
|
+
var iife_default = db2ExplainParser;
|
|
258
|
+
|
|
259
|
+
return iife_default;
|
|
260
|
+
|
|
261
|
+
})();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { registerExplainParser } from '@tabularis/explain';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
|
|
5
|
+
// src/payload.ts
|
|
6
|
+
function looksLikeDb2Payload(payload) {
|
|
7
|
+
return /^\s*\{\s*"db2_explain"\s*:/.test(payload.slice(0, 64));
|
|
8
|
+
}
|
|
9
|
+
function readPayload(payload) {
|
|
10
|
+
let value;
|
|
11
|
+
try {
|
|
12
|
+
value = JSON.parse(payload);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
throw new Error(`Db2 EXPLAIN payload is not JSON: ${error.message}`);
|
|
15
|
+
}
|
|
16
|
+
if (typeof value !== "object" || value === null) {
|
|
17
|
+
throw new Error("Db2 EXPLAIN payload is not an object");
|
|
18
|
+
}
|
|
19
|
+
const version = value.db2_explain;
|
|
20
|
+
if (version !== 1) {
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Db2 EXPLAIN payload version ${String(version)} is not supported by this parser`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
const parsed = value;
|
|
26
|
+
for (const key of ["operators", "streams", "predicates", "arguments", "indexes"]) {
|
|
27
|
+
if (!Array.isArray(parsed[key])) {
|
|
28
|
+
throw new Error(`Db2 EXPLAIN payload has no '${key}' list`);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return parsed;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/plan.ts
|
|
35
|
+
function objectLabel(schema, name, currentSchema) {
|
|
36
|
+
return schema === currentSchema ? name : `${schema}.${name}`;
|
|
37
|
+
}
|
|
38
|
+
function joinPredicates(texts) {
|
|
39
|
+
const unique = [...new Set(texts)];
|
|
40
|
+
return unique.length > 0 ? unique.join(" AND ") : null;
|
|
41
|
+
}
|
|
42
|
+
function joinType(operatorType, args) {
|
|
43
|
+
if (!operatorType.endsWith("JOIN")) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const outerJoin = args.get("OUTERJN")?.[0]?.trim().toUpperCase() ?? "";
|
|
47
|
+
switch (outerJoin) {
|
|
48
|
+
case "":
|
|
49
|
+
return "Inner";
|
|
50
|
+
case "LEFT":
|
|
51
|
+
return "Left";
|
|
52
|
+
case "RIGHT":
|
|
53
|
+
return "Right";
|
|
54
|
+
case "FULL":
|
|
55
|
+
return "Full";
|
|
56
|
+
default:
|
|
57
|
+
return outerJoin;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function planningTimeMs(payload) {
|
|
61
|
+
const prepTime = payload.arguments.find((argument) => argument.type === "PREPTIME");
|
|
62
|
+
if (!prepTime) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const [amount, unit = ""] = prepTime.value.trim().split(/\s+/);
|
|
66
|
+
const value = Number(amount);
|
|
67
|
+
if (!Number.isFinite(value)) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (unit.startsWith("milli")) return value;
|
|
71
|
+
if (unit.startsWith("micro")) return value / 1e3;
|
|
72
|
+
if (unit.startsWith("second")) return value * 1e3;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
var PLACED_PREDICATES = /* @__PURE__ */ new Set(["START", "STOP", "SARG", "RESID", "JOIN"]);
|
|
76
|
+
function operatorNode(operator, payload) {
|
|
77
|
+
const id = operator.id;
|
|
78
|
+
const node = {
|
|
79
|
+
id: String(id),
|
|
80
|
+
node_type: operator.type.trim(),
|
|
81
|
+
relation: null,
|
|
82
|
+
startup_cost: operator.first_row_cost,
|
|
83
|
+
total_cost: operator.total_cost,
|
|
84
|
+
plan_rows: null,
|
|
85
|
+
actual_rows: null,
|
|
86
|
+
actual_time_ms: null,
|
|
87
|
+
actual_loops: null,
|
|
88
|
+
buffers_hit: null,
|
|
89
|
+
buffers_read: null,
|
|
90
|
+
filter: null,
|
|
91
|
+
index_condition: null,
|
|
92
|
+
join_type: null,
|
|
93
|
+
hash_condition: null,
|
|
94
|
+
extra: {},
|
|
95
|
+
children: []
|
|
96
|
+
};
|
|
97
|
+
const args = /* @__PURE__ */ new Map();
|
|
98
|
+
for (const argument of payload.arguments.filter((a) => a.operator_id === id)) {
|
|
99
|
+
const key = argument.type.trim();
|
|
100
|
+
args.set(key, [...args.get(key) ?? [], argument.value.trim()]);
|
|
101
|
+
}
|
|
102
|
+
const current = payload.schema;
|
|
103
|
+
for (const stream of payload.streams) {
|
|
104
|
+
if (stream.target_id !== id || stream.source_type !== "D") {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const schema = stream.object_schema ?? "";
|
|
108
|
+
const name = stream.object_name ?? "";
|
|
109
|
+
const index = payload.indexes.find(
|
|
110
|
+
(candidate) => candidate.schema === schema && candidate.name === name
|
|
111
|
+
);
|
|
112
|
+
if (index) {
|
|
113
|
+
node.relation = objectLabel(index.table_schema, index.table_name, current);
|
|
114
|
+
node.extra.index = objectLabel(schema, name, current);
|
|
115
|
+
} else {
|
|
116
|
+
node.relation = objectLabel(schema, name, current);
|
|
117
|
+
if (node.node_type === "TBSCAN") {
|
|
118
|
+
node.extra.access_type = "ALL";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const output = payload.streams.find(
|
|
123
|
+
(stream) => stream.source_type === "O" && stream.source_id === id
|
|
124
|
+
);
|
|
125
|
+
if (output && output.count !== null) {
|
|
126
|
+
node.plan_rows = output.count;
|
|
127
|
+
} else {
|
|
128
|
+
const inputs = payload.streams.filter((stream) => stream.target_id === id && stream.source_type === "O").map((stream) => stream.count).filter((count) => count !== null);
|
|
129
|
+
node.plan_rows = inputs.length > 0 ? Math.max(...inputs) : null;
|
|
130
|
+
}
|
|
131
|
+
const predicates = payload.predicates.filter((predicate) => predicate.operator_id === id);
|
|
132
|
+
const applied = (...kinds) => joinPredicates(
|
|
133
|
+
predicates.filter((predicate) => kinds.includes(predicate.how_applied.trim())).map((predicate) => predicate.text.trim())
|
|
134
|
+
);
|
|
135
|
+
node.index_condition = applied("START", "STOP");
|
|
136
|
+
node.filter = applied("SARG", "RESID");
|
|
137
|
+
const joinCondition = applied("JOIN");
|
|
138
|
+
if (node.node_type === "HSJOIN") {
|
|
139
|
+
node.hash_condition = joinCondition;
|
|
140
|
+
} else if (joinCondition !== null) {
|
|
141
|
+
node.extra.join_condition = joinCondition;
|
|
142
|
+
}
|
|
143
|
+
for (const predicate of predicates) {
|
|
144
|
+
const how = predicate.how_applied.trim();
|
|
145
|
+
if (!PLACED_PREDICATES.has(how)) {
|
|
146
|
+
node.extra[`${how.toLowerCase()} predicate`] = predicate.text.trim();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
node.join_type = joinType(node.node_type, args);
|
|
150
|
+
const costs = [
|
|
151
|
+
["io_cost", operator.io_cost],
|
|
152
|
+
["cpu_cost", operator.cpu_cost],
|
|
153
|
+
["re_total_cost", operator.re_total_cost],
|
|
154
|
+
["buffers", operator.buffers]
|
|
155
|
+
];
|
|
156
|
+
for (const [key, value] of costs) {
|
|
157
|
+
if (value !== null) {
|
|
158
|
+
node.extra[key] = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
for (const [key, values] of [...args.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
162
|
+
node.extra[key] = values.join(", ");
|
|
163
|
+
}
|
|
164
|
+
return node;
|
|
165
|
+
}
|
|
166
|
+
function inputOrder(args, source) {
|
|
167
|
+
const side = args.find(
|
|
168
|
+
(argument) => argument.operator_id === source && argument.type.trim() === "JN INPUT"
|
|
169
|
+
)?.value.trim();
|
|
170
|
+
return [side === "OUTER" ? 0 : side === "INNER" ? 1 : 2, source];
|
|
171
|
+
}
|
|
172
|
+
function buildNode(operator, payload, byId, path) {
|
|
173
|
+
const node = operatorNode(operator, payload);
|
|
174
|
+
const inputs = [
|
|
175
|
+
...new Set(
|
|
176
|
+
payload.streams.filter((stream) => stream.target_id === operator.id && stream.source_type === "O").map((stream) => stream.source_id)
|
|
177
|
+
)
|
|
178
|
+
].sort((a, b) => {
|
|
179
|
+
const [sideA, idA] = inputOrder(payload.arguments, a);
|
|
180
|
+
const [sideB, idB] = inputOrder(payload.arguments, b);
|
|
181
|
+
return sideA - sideB || idA - idB;
|
|
182
|
+
});
|
|
183
|
+
for (const source of inputs) {
|
|
184
|
+
const input = byId.get(source);
|
|
185
|
+
if (input && !path.includes(source)) {
|
|
186
|
+
node.children.push(buildNode(input, payload, byId, [...path, operator.id]));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return node;
|
|
190
|
+
}
|
|
191
|
+
function buildPlanTree(payload) {
|
|
192
|
+
const byId = new Map(payload.operators.map((operator) => [operator.id, operator]));
|
|
193
|
+
const hasConsumer = (id) => payload.streams.some(
|
|
194
|
+
(stream) => stream.source_type === "O" && stream.source_id === id && byId.has(stream.target_id)
|
|
195
|
+
);
|
|
196
|
+
const root = payload.operators.filter((operator) => !hasConsumer(operator.id)).sort((a, b) => a.id - b.id)[0];
|
|
197
|
+
return root ? buildNode(root, payload, byId, []) : null;
|
|
198
|
+
}
|
|
199
|
+
function planText(root) {
|
|
200
|
+
const lines = [];
|
|
201
|
+
const write = (node, depth) => {
|
|
202
|
+
const indent = " ".repeat(depth);
|
|
203
|
+
let line = `${indent}${node.id} ${node.node_type}`;
|
|
204
|
+
if (node.relation !== null) line += ` on ${node.relation}`;
|
|
205
|
+
if (typeof node.extra.index === "string") line += ` using ${node.extra.index}`;
|
|
206
|
+
if (node.total_cost !== null) {
|
|
207
|
+
line += ` (cost=${node.total_cost.toFixed(2)}`;
|
|
208
|
+
if (node.plan_rows !== null) line += ` rows=${Math.round(node.plan_rows)}`;
|
|
209
|
+
line += ")";
|
|
210
|
+
}
|
|
211
|
+
lines.push(line);
|
|
212
|
+
const details = [
|
|
213
|
+
["Join", node.hash_condition],
|
|
214
|
+
["Index", node.index_condition],
|
|
215
|
+
["Filter", node.filter]
|
|
216
|
+
];
|
|
217
|
+
for (const [label, value] of details) {
|
|
218
|
+
if (value !== null) lines.push(`${indent} ${label}: ${value}`);
|
|
219
|
+
}
|
|
220
|
+
for (const child of node.children) write(child, depth + 1);
|
|
221
|
+
};
|
|
222
|
+
write(root, 0);
|
|
223
|
+
return `${lines.join("\n")}
|
|
224
|
+
`;
|
|
225
|
+
}
|
|
226
|
+
function parseDb2Explain(rawPayload) {
|
|
227
|
+
const payload = readPayload(rawPayload);
|
|
228
|
+
const root = buildPlanTree(payload);
|
|
229
|
+
if (root === null) {
|
|
230
|
+
throw new Error("Db2 EXPLAIN payload has no operator to start the plan from");
|
|
231
|
+
}
|
|
232
|
+
if (payload.analyze) {
|
|
233
|
+
root.actual_rows = payload.analyze.rows;
|
|
234
|
+
root.actual_time_ms = payload.analyze.elapsed_ms;
|
|
235
|
+
root.actual_loops = 1;
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
root,
|
|
239
|
+
planning_time_ms: planningTimeMs(payload),
|
|
240
|
+
execution_time_ms: payload.analyze?.elapsed_ms ?? null,
|
|
241
|
+
original_query: "",
|
|
242
|
+
driver: "db2",
|
|
243
|
+
has_analyze_data: payload.analyze !== null,
|
|
244
|
+
raw_output: planText(root)
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/parser.ts
|
|
249
|
+
var db2ExplainParser = {
|
|
250
|
+
engine: "db2",
|
|
251
|
+
format: "db2-explain-tables-json",
|
|
252
|
+
label: "Db2 explain tables",
|
|
253
|
+
parse: parseDb2Explain,
|
|
254
|
+
sniff: looksLikeDb2Payload
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
// src/index.ts
|
|
258
|
+
registerExplainParser(db2ExplainParser);
|
|
259
|
+
|
|
260
|
+
export { buildPlanTree, db2ExplainParser, parseDb2Explain, planText, planningTimeMs };
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tabularis/explain-db2",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "IBM Db2 explain-table parser for Tabularis Visual EXPLAIN",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"homepage": "https://github.com/TabularisDB/tabularis-db2-plugin/tree/master/explain",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/TabularisDB/tabularis-db2-plugin.git",
|
|
13
|
+
"directory": "explain"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./dist/index.js"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup",
|
|
32
|
+
"clean": "rm -rf dist",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "pnpm run build && vitest run"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@tabularis/explain": "^0.2.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@tabularis/explain": "file:tests/host",
|
|
41
|
+
"@types/node": "^26.4.0",
|
|
42
|
+
"tsup": "^9.0.0",
|
|
43
|
+
"typescript": "~5.9.3",
|
|
44
|
+
"vitest": "^5.0.5"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=20"
|
|
48
|
+
},
|
|
49
|
+
"packageManager": "pnpm@11.23.0"
|
|
50
|
+
}
|