@@ -2,13 +2,10 @@ import {YQLType} from '../types';
2
2
import type {
3
3
AnyExecuteResponse ,
4
4
AnyExplainResponse ,
5
- AnyResponse ,
6
- CommonFields ,
7
- DeepExecuteResponse ,
8
- DeprecatedExecuteResponsePlain ,
9
- ExecuteClassicResponsePlain ,
10
5
ExecuteModernResponse ,
11
6
KeyValueRow ,
7
+ ScanPlan ,
8
+ ScriptPlan ,
12
9
} from '../types/api/query' ;
13
10
import type { IQueryResult } from '../types/store/query' ;
14
11
@@ -49,6 +46,7 @@ export const getColumnType = (type: string) => {
49
46
}
50
47
} ;
51
48
49
+ /** parse response result field from ArrayRow to KeyValueRow */
52
50
const parseExecuteModernResponse = ( data : ExecuteModernResponse ) : IQueryResult => {
53
51
const { result, columns, ...restData } = data ;
54
52
@@ -57,43 +55,17 @@ const parseExecuteModernResponse = (data: ExecuteModernResponse): IQueryResult =
57
55
result &&
58
56
columns &&
59
57
result . map ( ( row ) => {
60
- return row . reduce ( ( newRow : KeyValueRow , cellData , columnIndex ) => {
58
+ return row . reduce ( ( newRow , cellData , columnIndex ) => {
61
59
const { name} = columns [ columnIndex ] ;
62
60
newRow [ name ] = cellData ;
63
61
return newRow ;
64
- } , { } ) ;
62
+ } , { } as KeyValueRow ) ;
65
63
} ) ,
66
64
columns,
67
65
...restData ,
68
66
} ;
69
67
} ;
70
68
71
- const parseDeprecatedExecuteResponseValue = (
72
- data ?: DeprecatedExecuteResponsePlain | ExecuteClassicResponsePlain ,
73
- ) : KeyValueRow [ ] | undefined => {
74
- if ( ! data ) {
75
- return undefined ;
76
- }
77
-
78
- if ( typeof data === 'string' ) {
79
- try {
80
- return JSON . parse ( data ) ;
81
- } catch ( e ) {
82
- return undefined ;
83
- }
84
- }
85
-
86
- if ( Array . isArray ( data ) ) {
87
- return data ;
88
- }
89
-
90
- // Plan is not a valid response in this case
91
- return undefined ;
92
- } ;
93
-
94
- const hasResult = ( data : AnyExecuteResponse ) : data is DeepExecuteResponse =>
95
- Boolean ( data && typeof data === 'object' && 'result' in data ) ;
96
-
97
69
const isModern = ( response : AnyExecuteResponse ) : response is ExecuteModernResponse =>
98
70
Boolean (
99
71
response &&
@@ -102,70 +74,64 @@ const isModern = (response: AnyExecuteResponse): response is ExecuteModernRespon
102
74
Array . isArray ( ( response as ExecuteModernResponse ) . columns ) ,
103
75
) ;
104
76
105
- const hasCommonFields = ( data : AnyResponse ) : data is CommonFields =>
106
- Boolean (
107
- data && typeof data === 'object' && ( 'ast' in data || 'plan' in data || 'stats' in data ) ,
77
+ type UnsupportedQueryResponseFormat =
78
+ | Array < unknown >
79
+ | string
80
+ | null
81
+ | undefined
82
+ | { result : string | Record < string , unknown > } ;
83
+
84
+ const isUnsupportedType = (
85
+ data : AnyExecuteResponse | AnyExplainResponse | UnsupportedQueryResponseFormat ,
86
+ ) : data is UnsupportedQueryResponseFormat => {
87
+ return Boolean (
88
+ ! data ||
89
+ typeof data !== 'object' ||
90
+ Array . isArray ( data ) ||
91
+ ( 'result' in data && ! Array . isArray ( data . result ) ) ,
108
92
) ;
93
+ } ;
109
94
110
- // complex logic because of the variety of possible responses
111
- // after all backends are updated to the latest version, it can be simplified
112
- export const parseQueryAPIExecuteResponse = ( data : AnyExecuteResponse ) : IQueryResult => {
113
- if ( ! data ) {
95
+ export const parseQueryAPIExecuteResponse = (
96
+ data : AnyExecuteResponse | UnsupportedQueryResponseFormat ,
97
+ ) : IQueryResult => {
98
+ if ( isUnsupportedType ( data ) ) {
114
99
return { } ;
115
100
}
116
-
117
- if ( hasResult ( data ) ) {
118
- if ( isModern ( data ) ) {
119
- return parseExecuteModernResponse ( data ) ;
120
- }
121
-
122
- return {
123
- ...data ,
124
- result : parseDeprecatedExecuteResponseValue ( data . result ) ,
125
- } ;
126
- }
127
-
128
- if ( hasCommonFields ( data ) ) {
129
- return data ;
101
+ if ( isModern ( data ) ) {
102
+ return parseExecuteModernResponse ( data ) ;
130
103
}
131
104
132
- return {
133
- result : parseDeprecatedExecuteResponseValue ( data ) ,
134
- } ;
105
+ return data ;
135
106
} ;
136
107
137
- // complex logic because of the variety of possible responses
138
- // after all backends are updated to the latest version, it can be simplified
139
- export const parseQueryAPIExplainResponse = ( data : AnyExplainResponse ) : IQueryResult => {
140
- if ( ! data ) {
108
+ export const parseQueryAPIExplainResponse = (
109
+ data : AnyExplainResponse | UnsupportedQueryResponseFormat ,
110
+ ) : IQueryResult => {
111
+ if ( isUnsupportedType ( data ) ) {
141
112
return { } ;
142
113
}
143
114
144
- if ( 'ast' in data ) {
145
- return data ;
146
- }
115
+ return data ;
116
+ } ;
147
117
148
- if ( 'result' in data ) {
149
- const { result , ... restData } = data ;
118
+ const isExplainScriptPlan = ( plan : ScriptPlan | ScanPlan ) : plan is ScriptPlan =>
119
+ Boolean ( plan && 'queries' in plan ) ;
150
120
151
- if ( 'ast' in data . result ) {
152
- return {
153
- ast : result . ast ,
154
- ...restData ,
155
- } ;
121
+ export const parseQueryExplainPlan = ( plan : ScriptPlan | ScanPlan ) : ScanPlan => {
122
+ if ( isExplainScriptPlan ( plan ) ) {
123
+ if ( ! plan . queries || ! plan . queries . length ) {
124
+ return { meta : plan . meta } ;
156
125
}
157
126
158
127
return {
159
- plan : data . result ,
160
- ...restData ,
128
+ Plan : plan . queries [ 0 ] . Plan ,
129
+ tables : plan . queries [ 0 ] . tables ,
130
+ meta : plan . meta ,
161
131
} ;
162
132
}
163
133
164
- if ( hasCommonFields ( data ) ) {
165
- return data ;
166
- }
167
-
168
- return { plan : data } ;
134
+ return plan ;
169
135
} ;
170
136
171
137
export const prepareQueryResponse = ( data ?: KeyValueRow [ ] ) => {
0 commit comments