Skip to content

Commit 1a8366c

Browse files
chore: remove deprecated types from query, refactor
1 parent cf5bd6c commit 1a8366c

File tree

4 files changed

+94
-140
lines changed

4 files changed

+94
-140
lines changed

src/services/api.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ interface Window {
3838
},
3939
axiosOptions?: AxiosOptions,
4040
) => Promise<import('../types/api/query').QueryAPIResponse<Action, Schema>>;
41-
getExplainQuery: (
41+
getExplainQuery: <Action extends import('../types/api/query').ExplainActions = 'explain'>(
4242
query: string,
4343
database: string,
44-
) => Promise<import('../types/api/query').QueryAPIExplainResponse<'explain'>>;
44+
) => Promise<import('../types/api/query').ExplainResponse<Action>>;
4545
getExplainQueryAst: (
4646
query: string,
4747
database: string,
48-
) => Promise<import('../types/api/query').QueryAPIExplainResponse<'explain-ast'>>;
48+
) => Promise<import('../types/api/query').ExplainResponse<'explain-ast'>>;
4949
getHealthcheckInfo: (
5050
database: string,
5151
) => Promise<import('../types/api/healthcheck').HealthCheckAPIResponse>;

src/types/api/query.ts

Lines changed: 76 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -159,38 +159,7 @@ export interface ScanPlan {
159159
meta: PlanMeta;
160160
}
161161

162-
// ====================
163-
// common
164-
165-
type Plan = Record<string, any>;
166-
type AST = string;
167-
type Stats = Record<string, any>;
168-
169-
export interface CommonFields {
170-
ast?: AST;
171-
plan?: Plan;
172-
stats?: Stats;
173-
}
174-
175-
interface DeprecatedCommonFields {
176-
stats?: Stats;
177-
}
178-
179-
export interface ErrorResponse {
180-
error?: IssueMessage;
181-
issues?: IssueMessage[];
182-
}
183-
184-
export type ExecuteActions = 'execute-script' | 'execute' | 'execute-scan' | undefined;
185-
export type ExplainActions = 'explain' | 'explain-ast';
186-
export type Actions = ExecuteActions | ExplainActions;
187-
188-
// undefined == 'classic'
189-
export type Schemas = 'classic' | 'modern' | 'ydb' | undefined;
190-
191-
// ==== EXECUTE ====
192-
193-
// common types
162+
// ==== Common types ====
194163

195164
export type CellValue = string | number | null | undefined;
196165

@@ -205,113 +174,91 @@ export interface ColumnType {
205174
type: string;
206175
}
207176

208-
// modern response
209-
210-
export type ExecuteModernResponse = {
211-
result: ArrayRow[];
212-
columns: ColumnType[];
213-
} & CommonFields;
214-
215-
export type ExecuteClassicResponseDeep = {
216-
result: KeyValueRow[];
217-
} & CommonFields;
218-
219-
// can be undefined for queries like `insert into`
220-
export type ExecuteClassicResponsePlain = KeyValueRow[] | undefined;
221-
222-
export type ExecuteClassicResponse = ExecuteClassicResponseDeep | ExecuteClassicResponsePlain;
223-
224-
export type ExecuteYdbResponse = {
225-
result: KeyValueRow[];
226-
} & CommonFields;
227-
228-
// prettier-ignore
229-
type ExecuteResponse<Schema extends Schemas> =
230-
| CommonFields // result can be undefined for queries like `insert into`
231-
| (Schema extends 'modern'
232-
? ExecuteModernResponse
233-
: Schema extends 'ydb'
234-
? ExecuteYdbResponse
235-
: Schema extends 'classic' | undefined
236-
? ExecuteClassicResponse
237-
: unknown);
238-
239-
// deprecated response from older versions, backward compatibility
240-
241-
type DeprecatedExecuteResponseValue =
242-
| KeyValueRow[]
243-
| string
244-
// can be here because of a bug in the previous backend version
245-
// should be ignored in parsing
246-
| Plan;
247-
248-
export type DeprecatedExecuteResponseDeep = {
249-
// can be undefined for queries like `insert into`
250-
result?: DeprecatedExecuteResponseValue;
251-
} & DeprecatedCommonFields;
252-
253-
// can be undefined for queries like `insert into`
254-
export type DeprecatedExecuteResponsePlain = DeprecatedExecuteResponseValue | undefined;
255-
256-
export type DeprecatedExecuteResponse =
257-
| DeprecatedExecuteResponseDeep
258-
| DeprecatedExecuteResponsePlain;
177+
/** undefined = 'classic' */
178+
export type Schemas = 'classic' | 'modern' | 'ydb' | undefined;
259179

260-
// ==== EXPLAIN ====
180+
/**
181+
* undefined = 'execute'
182+
*
183+
* execute and execute-script have similar responses
184+
*/
185+
export type ExecuteActions = 'execute' | 'execute-scan' | 'execute-script' | undefined;
261186

262-
// modern response
187+
/** explain, explain-scan and explain-ast have similar responses */
188+
export type ExplainActions = 'explain' | 'explain-scan' | 'explain-script' | 'explain-ast';
263189

264-
type ExplainResponse = CommonFields;
190+
export type Actions = ExecuteActions | ExplainActions;
265191

266-
// deprecated response from older versions, backward compatibility
192+
// ==== Error response ====
267193

268-
// prettier-ignore
269-
type DeprecatedExplainResponse<Action extends ExplainActions> =
270-
Action extends 'explain-ast'
271-
? ({result: {ast: AST}} & Required<DeprecatedCommonFields>) | {ast: AST}
272-
: Action extends 'explain'
273-
? ({result: Plan} & Required<DeprecatedCommonFields>) | Plan
274-
: unknown;
194+
export interface ErrorResponse {
195+
error?: IssueMessage;
196+
issues?: IssueMessage[];
197+
}
275198

276-
// ==== COMBINED API RESPONSE ====
199+
// ==== Explain Responses ====
277200

278-
export type QueryAPIExecuteResponse<Schema extends Schemas = undefined> =
279-
| ExecuteResponse<Schema>
280-
| DeprecatedExecuteResponse
281-
| null;
201+
export interface ExplainScriptResponse {
202+
plan?: ScriptPlan;
203+
}
282204

283-
export type QueryAPIExplainResponse<Action extends ExplainActions> =
284-
| ExplainResponse
285-
| DeprecatedExplainResponse<Action>
286-
| null;
205+
export interface ExplainScanResponse {
206+
ast?: string;
207+
plan?: ScanPlan;
208+
}
287209

288-
// prettier-ignore
289-
export type QueryAPIResponse<Action extends Actions, Schema extends Schemas = undefined> =
290-
Action extends ExecuteActions
291-
? QueryAPIExecuteResponse<Schema>
292-
: Action extends ExplainActions
293-
? QueryAPIExplainResponse<Action>
294-
: unknown;
210+
export type ExplainResponse<Action extends ExplainActions> = Action extends 'explain-script'
211+
? ExplainScriptResponse
212+
: ExplainScanResponse;
213+
214+
// ==== Execute Responses ====
215+
216+
type ResultFields<Schema extends Schemas> = Schema extends 'modern'
217+
? {
218+
result?: ArrayRow[];
219+
columns?: ColumnType[];
220+
}
221+
: {
222+
result?: KeyValueRow[];
223+
};
224+
225+
export type ExecuteScanResponse<Schema extends Schemas> = {
226+
plan?: ScanPlan;
227+
ast?: string;
228+
stats?: TKqpStatsQuery;
229+
} & ResultFields<Schema>;
230+
231+
export type ExecuteScriptResponse<Schema extends Schemas> = {
232+
plan?: ScriptPlan;
233+
ast?: string;
234+
stats?: TKqpStatsQuery;
235+
} & ResultFields<Schema>;
236+
237+
export type ExecuteResponse<
238+
Action extends ExecuteActions,
239+
Schema extends Schemas,
240+
> = Action extends 'execute-scan' ? ExecuteScanResponse<Schema> : ExecuteScriptResponse<Schema>;
241+
242+
// ==== Combined API response ====
243+
export type QueryAPIResponse<
244+
Action extends Actions,
245+
Schema extends Schemas,
246+
> = Action extends ExplainActions
247+
? ExplainResponse<Action>
248+
: Action extends ExecuteActions
249+
? ExecuteResponse<Action, Schema>
250+
: unknown;
251+
252+
// ==== types to use in query result preparation ====
253+
export type AnyExplainResponse = ExplainScanResponse | ExplainScriptResponse;
254+
255+
export type ExecuteModernResponse = ExecuteScanResponse<'modern'> | ExecuteScriptResponse<'modern'>;
256+
export type ExecuteClassicResponse =
257+
| ExecuteScanResponse<'classic'>
258+
| ExecuteScriptResponse<'classic'>;
259+
export type ExecuteYdbResponse = ExecuteScanResponse<'ydb'> | ExecuteScriptResponse<'ydb'>;
295260

296261
export type AnyExecuteResponse =
297262
| ExecuteModernResponse
298263
| ExecuteClassicResponse
299-
| ExecuteYdbResponse
300-
| CommonFields
301-
| DeprecatedExecuteResponse
302-
| null;
303-
304-
export type DeepExecuteResponse =
305-
| ExecuteModernResponse
306-
| ExecuteClassicResponseDeep
307-
| ExecuteYdbResponse
308-
| DeprecatedExecuteResponseDeep;
309-
310-
export type AnyExplainResponse =
311-
| ExplainResponse
312-
| CommonFields
313-
| DeprecatedExplainResponse<'explain'>
314-
| DeprecatedExplainResponse<'explain-ast'>
315-
| null;
316-
317-
export type AnyResponse = AnyExecuteResponse | AnyExplainResponse;
264+
| ExecuteYdbResponse;

src/types/store/explainQuery.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ import type {ExplainPlanNodeData, GraphNode, Link} from '@gravity-ui/paranoid';
22

33
import {GET_EXPLAIN_QUERY, GET_EXPLAIN_QUERY_AST} from '../../store/reducers/explainQuery';
44
import type {ApiRequestAction} from '../../store/utils';
5-
import type {ErrorResponse} from '../api/query';
5+
import type {PlanTable, ErrorResponse, ScanPlan, ScriptPlan} from '../api/query';
66
import type {IQueryResult, QueryError} from './query';
77

88
export interface PreparedExplainResponse {
99
plan?: {
1010
links?: Link[];
1111
nodes?: GraphNode<ExplainPlanNodeData>[];
12-
tables?: unknown;
12+
tables?: PlanTable[];
1313
version?: string;
14-
pristine?: unknown;
14+
pristine?: ScanPlan | ScriptPlan;
1515
};
16-
ast?: IQueryResult['ast'];
16+
ast?: string;
1717
}
1818

1919
export interface ExplainQueryState {
2020
loading: boolean;
2121
data?: PreparedExplainResponse['plan'];
22-
dataAst?: IQueryResult['ast'];
22+
dataAst?: PreparedExplainResponse['ast'];
2323
error?: string | ErrorResponse;
2424
errorAst?: string | ErrorResponse;
2525
}

src/types/store/query.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import type {NetworkError} from '../api/error';
2-
import type {KeyValueRow, ColumnType, ErrorResponse} from '../api/query';
2+
import type {
3+
KeyValueRow,
4+
ColumnType,
5+
ErrorResponse,
6+
ScriptPlan,
7+
ScanPlan,
8+
TKqpStatsQuery,
9+
} from '../api/query';
310

411
export interface IQueryResult {
512
result?: KeyValueRow[];
613
columns?: ColumnType[];
7-
stats?: any;
8-
plan?: any;
14+
stats?: TKqpStatsQuery;
15+
plan?: ScriptPlan | ScanPlan;
916
ast?: string;
1017
}
1118

0 commit comments

Comments
 (0)