@@ -2,19 +2,15 @@ import { ViewColumn } from "vscode";
22
33import {
44 FromCompareViewMessage ,
5- ToCompareViewMessage ,
65 QueryCompareResult ,
6+ ToCompareViewMessage ,
77} from "../common/interface-types" ;
88import { Logger , showAndLogExceptionWithTelemetry } from "../common/logging" ;
99import { extLogger } from "../common/logging/vscode" ;
1010import { CodeQLCliServer } from "../codeql-cli/cli" ;
1111import { DatabaseManager } from "../databases/local-databases" ;
1212import { jumpToLocation } from "../databases/local-databases/locations" ;
13- import {
14- transformBqrsResultSet ,
15- RawResultSet ,
16- BQRSInfo ,
17- } from "../common/bqrs-cli-types" ;
13+ import { BQRSInfo , DecodedBqrsChunk } from "../common/bqrs-cli-types" ;
1814import resultsDiff from "./resultsDiff" ;
1915import { CompletedLocalQueryInfo } from "../query-results" ;
2016import { assertNever , getErrorMessage } from "../common/helpers-pure" ;
@@ -26,10 +22,13 @@ import {
2622import { telemetryListener } from "../common/vscode/telemetry" ;
2723import { redactableError } from "../common/errors" ;
2824import { App } from "../common/app" ;
25+ import { findResultSetNames } from "./result-set-names" ;
2926
3027interface ComparePair {
3128 from : CompletedLocalQueryInfo ;
29+ fromSchemas : BQRSInfo ;
3230 to : CompletedLocalQueryInfo ;
31+ toSchemas : BQRSInfo ;
3332}
3433
3534export class CompareView extends AbstractWebview <
@@ -56,18 +55,44 @@ export class CompareView extends AbstractWebview<
5655 to : CompletedLocalQueryInfo ,
5756 selectedResultSetName ?: string ,
5857 ) {
59- this . comparePair = { from, to } ;
58+ const fromSchemas = await this . cliServer . bqrsInfo (
59+ from . completedQuery . query . resultsPaths . resultsPath ,
60+ ) ;
61+ const toSchemas = await this . cliServer . bqrsInfo (
62+ to . completedQuery . query . resultsPaths . resultsPath ,
63+ ) ;
64+
65+ this . comparePair = {
66+ from,
67+ fromSchemas,
68+ to,
69+ toSchemas,
70+ } ;
71+
72+ await this . showResultsInternal ( selectedResultSetName ) ;
73+ }
74+
75+ private async showResultsInternal ( selectedResultSetName ?: string ) {
76+ if ( ! this . comparePair ) {
77+ return ;
78+ }
79+
80+ const { from, to } = this . comparePair ;
81+
6082 const panel = await this . getPanel ( ) ;
6183 panel . reveal ( undefined , true ) ;
6284
6385 await this . waitForPanelLoaded ( ) ;
64- const [
86+ const {
6587 commonResultSetNames,
66- currentResultSetName ,
88+ currentResultSetDisplayName ,
6789 fromResultSet,
6890 toResultSet,
69- ] = await this . findCommonResultSetNames ( from , to , selectedResultSetName ) ;
70- if ( currentResultSetName ) {
91+ } = await this . findResultSetsToCompare (
92+ this . comparePair ,
93+ selectedResultSetName ,
94+ ) ;
95+ if ( currentResultSetDisplayName ) {
7196 let rows : QueryCompareResult | undefined ;
7297 let message : string | undefined ;
7398 try {
@@ -93,9 +118,9 @@ export class CompareView extends AbstractWebview<
93118 time : to . startTime ,
94119 } ,
95120 } ,
96- columns : fromResultSet . schema . columns ,
121+ columns : fromResultSet . columns ,
97122 commonResultSetNames,
98- currentResultSetName,
123+ currentResultSetName : currentResultSetDisplayName ,
99124 rows,
100125 message,
101126 databaseUri : to . initialInfo . databaseInfo . databaseUri ,
@@ -165,92 +190,56 @@ export class CompareView extends AbstractWebview<
165190 }
166191 }
167192
168- private async findCommonResultSetNames (
169- from : CompletedLocalQueryInfo ,
170- to : CompletedLocalQueryInfo ,
193+ private async findResultSetsToCompare (
194+ { from, fromSchemas, to, toSchemas } : ComparePair ,
171195 selectedResultSetName : string | undefined ,
172- ) : Promise < [ string [ ] , string , RawResultSet , RawResultSet ] > {
173- const fromSchemas = await this . cliServer . bqrsInfo (
174- from . completedQuery . query . resultsPaths . resultsPath ,
175- ) ;
176- const toSchemas = await this . cliServer . bqrsInfo (
177- to . completedQuery . query . resultsPaths . resultsPath ,
178- ) ;
179- const fromSchemaNames = fromSchemas [ "result-sets" ] . map (
180- ( schema ) => schema . name ,
181- ) ;
182- const toSchemaNames = toSchemas [ "result-sets" ] . map ( ( schema ) => schema . name ) ;
183- const commonResultSetNames = fromSchemaNames . filter ( ( name ) =>
184- toSchemaNames . includes ( name ) ,
185- ) ;
186-
187- // Fall back on the default result set names if there are no common ones.
188- const defaultFromResultSetName = fromSchemaNames . find ( ( name ) =>
189- name . startsWith ( "#" ) ,
190- ) ;
191- const defaultToResultSetName = toSchemaNames . find ( ( name ) =>
192- name . startsWith ( "#" ) ,
193- ) ;
194-
195- if (
196- commonResultSetNames . length === 0 &&
197- ! ( defaultFromResultSetName || defaultToResultSetName )
198- ) {
199- throw new Error (
200- "No common result sets found between the two queries. Please check that the queries are compatible." ,
201- ) ;
202- }
196+ ) {
197+ const {
198+ commonResultSetNames,
199+ currentResultSetDisplayName,
200+ fromResultSetName,
201+ toResultSetName,
202+ } = await findResultSetNames ( fromSchemas , toSchemas , selectedResultSetName ) ;
203203
204- const currentResultSetName =
205- selectedResultSetName || commonResultSetNames [ 0 ] ;
206204 const fromResultSet = await this . getResultSet (
207205 fromSchemas ,
208- currentResultSetName || defaultFromResultSetName ! ,
206+ fromResultSetName ,
209207 from . completedQuery . query . resultsPaths . resultsPath ,
210208 ) ;
211209 const toResultSet = await this . getResultSet (
212210 toSchemas ,
213- currentResultSetName || defaultToResultSetName ! ,
211+ toResultSetName ,
214212 to . completedQuery . query . resultsPaths . resultsPath ,
215213 ) ;
216- return [
214+ return {
217215 commonResultSetNames,
218- currentResultSetName ||
219- `${ defaultFromResultSetName } <-> ${ defaultToResultSetName } ` ,
216+ currentResultSetDisplayName,
220217 fromResultSet,
221218 toResultSet,
222- ] ;
219+ } ;
223220 }
224221
225222 private async changeTable ( newResultSetName : string ) {
226- if ( ! this . comparePair ?. from || ! this . comparePair . to ) {
227- return ;
228- }
229- await this . showResults (
230- this . comparePair . from ,
231- this . comparePair . to ,
232- newResultSetName ,
233- ) ;
223+ await this . showResultsInternal ( newResultSetName ) ;
234224 }
235225
236226 private async getResultSet (
237227 bqrsInfo : BQRSInfo ,
238228 resultSetName : string ,
239229 resultsPath : string ,
240- ) : Promise < RawResultSet > {
230+ ) : Promise < DecodedBqrsChunk > {
241231 const schema = bqrsInfo [ "result-sets" ] . find (
242232 ( schema ) => schema . name === resultSetName ,
243233 ) ;
244234 if ( ! schema ) {
245235 throw new Error ( `Schema ${ resultSetName } not found.` ) ;
246236 }
247- const chunk = await this . cliServer . bqrsDecode ( resultsPath , resultSetName ) ;
248- return transformBqrsResultSet ( schema , chunk ) ;
237+ return await this . cliServer . bqrsDecode ( resultsPath , resultSetName ) ;
249238 }
250239
251240 private compareResults (
252- fromResults : RawResultSet ,
253- toResults : RawResultSet ,
241+ fromResults : DecodedBqrsChunk ,
242+ toResults : DecodedBqrsChunk ,
254243 ) : QueryCompareResult {
255244 // Only compare columns that have the same name
256245 return resultsDiff ( fromResults , toResults ) ;
0 commit comments