@@ -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
195164export 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
296261export 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 ;
0 commit comments