11import React from 'react' ;
22
3+ import type { Settings } from '@gravity-ui/react-data-table' ;
34import { isEqual } from 'lodash' ;
45import { v4 as uuidv4 } from 'uuid' ;
56
67import 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' ;
813import {
914 queryApi ,
1015 saveQueryToHistory ,
@@ -23,6 +28,7 @@ import {cn} from '../../../../utils/cn';
2328import {
2429 DEFAULT_IS_QUERY_RESULT_COLLAPSED ,
2530 DEFAULT_SIZE_RESULT_PANE_KEY ,
31+ ENABLE_QUERY_STREAMING ,
2632 LAST_USED_QUERY_ACTION_KEY ,
2733} from '../../../../utils/constants' ;
2834import {
@@ -34,7 +40,7 @@ import {
3440} from '../../../../utils/hooks' ;
3541import { useChangedQuerySettings } from '../../../../utils/hooks/useChangedQuerySettings' ;
3642import { useLastQueryExecutionSettings } from '../../../../utils/hooks/useLastQueryExecutionSettings' ;
37- import { QUERY_ACTIONS } from '../../../../utils/query' ;
43+ import { DEFAULT_QUERY_SETTINGS , QUERY_ACTIONS } from '../../../../utils/query' ;
3844import type { InitialPaneState } from '../../utils/paneVisibilityToggleHelpers' ;
3945import {
4046 PaneVisibilityActionTypes ,
@@ -86,8 +92,24 @@ export default function QueryEditor(props: QueryEditorProps) {
8692 LAST_USED_QUERY_ACTION_KEY ,
8793 ) ;
8894 const [ lastExecutedQueryText , setLastExecutedQueryText ] = React . useState < string > ( '' ) ;
95+ const [ isQueryStreamingEnabled ] = useSetting ( ENABLE_QUERY_STREAMING ) ;
96+ const isStreamingEnabled = useStreamingAvailable ( ) && isQueryStreamingEnabled ;
8997
9098 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 ] ) ;
91113
92114 React . useEffect ( ( ) => {
93115 if ( savedPath !== tenantName ) {
@@ -121,14 +143,25 @@ export default function QueryEditor(props: QueryEditorProps) {
121143 }
122144 const queryId = uuidv4 ( ) ;
123145
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+ }
132165
133166 dispatch ( setShowPreview ( false ) ) ;
134167
@@ -155,7 +188,7 @@ export default function QueryEditor(props: QueryEditorProps) {
155188
156189 const queryId = uuidv4 ( ) ;
157190
158- sendQuery ( {
191+ runningQueryRef . current = sendQuery ( {
159192 actionType : 'explain' ,
160193 query : text ,
161194 database : tenantName ,
@@ -169,6 +202,14 @@ export default function QueryEditor(props: QueryEditorProps) {
169202 dispatchResultVisibilityState ( PaneVisibilityActionTypes . triggerExpand ) ;
170203 } ) ;
171204
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+
172213 const onCollapseResultHandler = ( ) => {
173214 dispatchResultVisibilityState ( PaneVisibilityActionTypes . triggerCollapse ) ;
174215 } ;
@@ -229,10 +270,13 @@ export default function QueryEditor(props: QueryEditorProps) {
229270 theme = { theme }
230271 key = { result ?. queryId }
231272 result = { result }
273+ cancelQueryResponse = { cancelQueryResponse }
232274 tenantName = { tenantName }
233275 path = { path }
234276 showPreview = { showPreview }
235277 queryText = { lastExecutedQueryText }
278+ onCancelRunningQuery = { handleCancelRunningQuery }
279+ tableSettings = { tableSettings }
236280 />
237281 </ div >
238282 </ SplitPane >
@@ -248,13 +292,17 @@ interface ResultProps {
248292 type ?: EPathType ;
249293 theme : string ;
250294 result ?: QueryResult ;
295+ cancelQueryResponse ?: Pick < QueryResult , 'isLoading' | 'error' > ;
251296 tenantName : string ;
252297 path : string ;
253298 showPreview ?: boolean ;
254299 queryText : string ;
300+ tableSettings ?: Partial < Settings > ;
301+ onCancelRunningQuery : VoidFunction ;
255302}
256303function Result ( {
257304 resultVisibilityState,
305+ cancelQueryResponse,
258306 onExpandResultHandler,
259307 onCollapseResultHandler,
260308 type,
@@ -264,6 +312,8 @@ function Result({
264312 path,
265313 showPreview,
266314 queryText,
315+ tableSettings,
316+ onCancelRunningQuery,
267317} : ResultProps ) {
268318 if ( showPreview ) {
269319 return < Preview database = { tenantName } path = { path } type = { type } /> ;
@@ -277,9 +327,13 @@ function Result({
277327 theme = { theme }
278328 tenantName = { tenantName }
279329 isResultsCollapsed = { resultVisibilityState . collapsed }
330+ isCancelError = { Boolean ( cancelQueryResponse ?. error ) }
331+ isCancelling = { Boolean ( cancelQueryResponse ?. isLoading ) }
332+ tableSettings = { tableSettings }
280333 onExpandResults = { onExpandResultHandler }
281334 onCollapseResults = { onCollapseResultHandler }
282335 queryText = { queryText }
336+ onCancelRunningQuery = { onCancelRunningQuery }
283337 />
284338 ) ;
285339 }
0 commit comments