1
1
import React from 'react' ;
2
2
3
+ import type { Settings } from '@gravity-ui/react-data-table' ;
3
4
import { isEqual } from 'lodash' ;
4
5
import { v4 as uuidv4 } from 'uuid' ;
5
6
6
7
import SplitPane from '../../../../components/SplitPane' ;
7
- import { useTracingLevelOptionAvailable } from '../../../../store/reducers/capabilities/hooks' ;
8
+ import { cancelQueryApi } from '../../../../store/reducers/cancelQuery' ;
9
+ import {
10
+ useStreamingAvailable ,
11
+ useTracingLevelOptionAvailable ,
12
+ } from '../../../../store/reducers/capabilities/hooks' ;
8
13
import {
9
14
queryApi ,
10
15
saveQueryToHistory ,
@@ -23,6 +28,7 @@ import {cn} from '../../../../utils/cn';
23
28
import {
24
29
DEFAULT_IS_QUERY_RESULT_COLLAPSED ,
25
30
DEFAULT_SIZE_RESULT_PANE_KEY ,
31
+ ENABLE_QUERY_STREAMING ,
26
32
LAST_USED_QUERY_ACTION_KEY ,
27
33
} from '../../../../utils/constants' ;
28
34
import {
@@ -34,7 +40,7 @@ import {
34
40
} from '../../../../utils/hooks' ;
35
41
import { useChangedQuerySettings } from '../../../../utils/hooks/useChangedQuerySettings' ;
36
42
import { useLastQueryExecutionSettings } from '../../../../utils/hooks/useLastQueryExecutionSettings' ;
37
- import { QUERY_ACTIONS } from '../../../../utils/query' ;
43
+ import { DEFAULT_QUERY_SETTINGS , QUERY_ACTIONS } from '../../../../utils/query' ;
38
44
import type { InitialPaneState } from '../../utils/paneVisibilityToggleHelpers' ;
39
45
import {
40
46
PaneVisibilityActionTypes ,
@@ -86,8 +92,24 @@ export default function QueryEditor(props: QueryEditorProps) {
86
92
LAST_USED_QUERY_ACTION_KEY ,
87
93
) ;
88
94
const [ lastExecutedQueryText , setLastExecutedQueryText ] = React . useState < string > ( '' ) ;
95
+ const [ isQueryStreamingEnabled ] = useSetting ( ENABLE_QUERY_STREAMING ) ;
96
+ const isStreamingEnabled = useStreamingAvailable ( ) && isQueryStreamingEnabled ;
89
97
90
98
const [ sendQuery ] = queryApi . useUseSendQueryMutation ( ) ;
99
+ const [ streamQuery ] = queryApi . useUseStreamQueryMutation ( ) ;
100
+ const [ sendCancelQuery , cancelQueryResponse ] = cancelQueryApi . useCancelQueryMutation ( ) ;
101
+
102
+ const runningQueryRef = React . useRef < { abort : VoidFunction } | null > ( null ) ;
103
+
104
+ const tableSettings = React . useMemo ( ( ) => {
105
+ return isStreamingEnabled
106
+ ? {
107
+ displayIndices : {
108
+ maxIndex : ( querySettings . limitRows || DEFAULT_QUERY_SETTINGS . limitRows ) + 1 ,
109
+ } ,
110
+ }
111
+ : undefined ;
112
+ } , [ isStreamingEnabled , querySettings . limitRows ] ) ;
91
113
92
114
React . useEffect ( ( ) => {
93
115
if ( savedPath !== tenantName ) {
@@ -121,14 +143,25 @@ export default function QueryEditor(props: QueryEditorProps) {
121
143
}
122
144
const queryId = uuidv4 ( ) ;
123
145
124
- sendQuery ( {
125
- actionType : 'execute' ,
126
- query : text ,
127
- database : tenantName ,
128
- querySettings,
129
- enableTracingLevel,
130
- queryId,
131
- } ) ;
146
+ if ( isStreamingEnabled ) {
147
+ runningQueryRef . current = streamQuery ( {
148
+ actionType : 'execute' ,
149
+ query : text ,
150
+ database : tenantName ,
151
+ querySettings,
152
+ enableTracingLevel,
153
+ queryId,
154
+ } ) ;
155
+ } else {
156
+ runningQueryRef . current = sendQuery ( {
157
+ actionType : 'execute' ,
158
+ query : text ,
159
+ database : tenantName ,
160
+ querySettings,
161
+ enableTracingLevel,
162
+ queryId,
163
+ } ) ;
164
+ }
132
165
133
166
dispatch ( setShowPreview ( false ) ) ;
134
167
@@ -155,7 +188,7 @@ export default function QueryEditor(props: QueryEditorProps) {
155
188
156
189
const queryId = uuidv4 ( ) ;
157
190
158
- sendQuery ( {
191
+ runningQueryRef . current = sendQuery ( {
159
192
actionType : 'explain' ,
160
193
query : text ,
161
194
database : tenantName ,
@@ -169,6 +202,14 @@ export default function QueryEditor(props: QueryEditorProps) {
169
202
dispatchResultVisibilityState ( PaneVisibilityActionTypes . triggerExpand ) ;
170
203
} ) ;
171
204
205
+ const handleCancelRunningQuery = React . useCallback ( ( ) => {
206
+ if ( isStreamingEnabled && runningQueryRef . current ) {
207
+ runningQueryRef . current . abort ( ) ;
208
+ } else if ( result ?. queryId ) {
209
+ sendCancelQuery ( { queryId : result ?. queryId , database : tenantName } ) ;
210
+ }
211
+ } , [ isStreamingEnabled , result ?. queryId , sendCancelQuery , tenantName ] ) ;
212
+
172
213
const onCollapseResultHandler = ( ) => {
173
214
dispatchResultVisibilityState ( PaneVisibilityActionTypes . triggerCollapse ) ;
174
215
} ;
@@ -229,10 +270,13 @@ export default function QueryEditor(props: QueryEditorProps) {
229
270
theme = { theme }
230
271
key = { result ?. queryId }
231
272
result = { result }
273
+ cancelQueryResponse = { cancelQueryResponse }
232
274
tenantName = { tenantName }
233
275
path = { path }
234
276
showPreview = { showPreview }
235
277
queryText = { lastExecutedQueryText }
278
+ onCancelRunningQuery = { handleCancelRunningQuery }
279
+ tableSettings = { tableSettings }
236
280
/>
237
281
</ div >
238
282
</ SplitPane >
@@ -248,13 +292,17 @@ interface ResultProps {
248
292
type ?: EPathType ;
249
293
theme : string ;
250
294
result ?: QueryResult ;
295
+ cancelQueryResponse ?: Pick < QueryResult , 'isLoading' | 'error' > ;
251
296
tenantName : string ;
252
297
path : string ;
253
298
showPreview ?: boolean ;
254
299
queryText : string ;
300
+ tableSettings ?: Partial < Settings > ;
301
+ onCancelRunningQuery : VoidFunction ;
255
302
}
256
303
function Result ( {
257
304
resultVisibilityState,
305
+ cancelQueryResponse,
258
306
onExpandResultHandler,
259
307
onCollapseResultHandler,
260
308
type,
@@ -264,6 +312,8 @@ function Result({
264
312
path,
265
313
showPreview,
266
314
queryText,
315
+ tableSettings,
316
+ onCancelRunningQuery,
267
317
} : ResultProps ) {
268
318
if ( showPreview ) {
269
319
return < Preview database = { tenantName } path = { path } type = { type } /> ;
@@ -277,9 +327,13 @@ function Result({
277
327
theme = { theme }
278
328
tenantName = { tenantName }
279
329
isResultsCollapsed = { resultVisibilityState . collapsed }
330
+ isCancelError = { Boolean ( cancelQueryResponse ?. error ) }
331
+ isCancelling = { Boolean ( cancelQueryResponse ?. isLoading ) }
332
+ tableSettings = { tableSettings }
280
333
onExpandResults = { onExpandResultHandler }
281
334
onCollapseResults = { onCollapseResultHandler }
282
335
queryText = { queryText }
336
+ onCancelRunningQuery = { onCancelRunningQuery }
283
337
/>
284
338
) ;
285
339
}
0 commit comments