-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query): add resolve value query - #1107
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/queries/catalog/resolve-value-query/resolve-value-query-executor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { ResolveValueQuery, ResolveValueQueryResult } from './resolve-value-query-format'; | ||
import { staticSlicing } from '../../../slicing/static/static-slicer'; | ||
import { reconstructToCode } from '../../../reconstruct/reconstruct'; | ||
import { doNotAutoSelect } from '../../../reconstruct/auto-select/auto-select-defaults'; | ||
import { makeMagicCommentHandler } from '../../../reconstruct/auto-select/magic-comments'; | ||
import { log } from '../../../util/log'; | ||
import type { BasicQueryData } from '../../base-query-format'; | ||
import { slicingCriterionToId } from '../../../slicing/criterion/parse'; | ||
import { resolveToValues } from '../../../dataflow/environments/resolve-by-name'; | ||
import { recoverName } from '../../../r-bridge/lang-4.x/ast/model/processing/node-id'; | ||
|
||
export function fingerPrintOfQuery(query: ResolveValueQuery): string { | ||
return JSON.stringify(query); | ||
} | ||
|
||
export function executeResolveValueQuery({ dataflow: { graph, environment }, ast }: BasicQueryData, queries: readonly ResolveValueQuery[]): ResolveValueQueryResult { | ||
const start = Date.now(); | ||
const results: ResolveValueQueryResult['results'] = []; | ||
for(const query of queries) { | ||
const key = fingerPrintOfQuery(query); | ||
|
||
if(results[key]) { | ||
log.warn(`Duplicate Key for slicing-query: ${key}, skipping...`); | ||
} | ||
|
||
const ids = query.criteria.map(criteria => slicingCriterionToId(criteria, graph.idMap!)); | ||
const values = new Set<unknown>(); | ||
|
||
const resolveStart = Date.now(); | ||
for(const id of ids) { | ||
resolveToValues(recoverName(id, graph.idMap), environment, graph) | ||
?.forEach(values.add); | ||
} | ||
const resolveEnd = Date.now(); | ||
|
||
results[key] = { | ||
values: values.values(), | ||
'.meta': { timing: resolveEnd - resolveStart } | ||
} | ||
} | ||
return { | ||
'.meta': { | ||
timing: Date.now() - start | ||
}, | ||
results | ||
}; | ||
} |
43 changes: 43 additions & 0 deletions
43
src/queries/catalog/resolve-value-query/resolve-value-query-format.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { BaseQueryFormat, BaseQueryResult } from '../../base-query-format'; | ||
import type { PipelineOutput } from '../../../core/steps/pipeline/pipeline'; | ||
import type { | ||
DEFAULT_DATAFLOW_PIPELINE, DEFAULT_SLICE_WITHOUT_RECONSTRUCT_PIPELINE, | ||
DEFAULT_SLICING_PIPELINE | ||
} from '../../../core/steps/pipeline/default-pipelines'; | ||
import type { SlicingCriteria } from '../../../slicing/criterion/parse'; | ||
import type { QueryResults, SupportedQuery } from '../../query'; | ||
import { bold } from '../../../util/ansi'; | ||
import { printAsMs } from '../../../util/time'; | ||
import Joi from 'joi'; | ||
|
||
import { summarizeIdsIfTooLong } from '../../query-print'; | ||
import { executeResolveValueQuery } from './resolve-value-query-executor'; | ||
|
||
|
||
export interface ResolveValueQuery extends BaseQueryFormat { | ||
readonly type: 'resolve-value'; | ||
/** The slicing criteria to use */ | ||
readonly criteria: SlicingCriteria, | ||
} | ||
|
||
export interface ResolveValueQueryResult extends BaseQueryResult { | ||
results: Record<string, any> | ||
} | ||
|
||
export const ResolveValueQueryDefinition = { | ||
executor: executeResolveValueQuery, | ||
asciiSummarizer: (formatter, _processed, queryResults, result) => { | ||
const out = queryResults as QueryResults<'resolve-value'>['resolve-value']; | ||
result.push(`Query: ${bold('resolve-value', formatter)} (${printAsMs(out['.meta'].timing, 0)})`); | ||
for(const [fingerprint, obj] of Object.entries(out.results)) { | ||
const { criteria } = JSON.parse(fingerprint) as ResolveValueQuery; | ||
result.push(` ╰ Values for {${criteria.join(', ')}}`); | ||
result.push(` ╰ ${obj.values.join(', ')}`); | ||
} | ||
return true; | ||
}, | ||
schema: Joi.object({ | ||
type: Joi.string().valid('resolve-value').required().description('The type of the query.'), | ||
criteria: Joi.array().items(Joi.string()).min(0).required().description('The slicing criteria to use.'), | ||
}).description('Resolve Value query used to get definitions of an identifier') | ||
} as const satisfies SupportedQuery<'resolve-value'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters