Skip to content

Commit 63087e3

Browse files
bartovalstratoula
andauthored
[ES|QL ] EsqlCallbacks are recreated per type (elastic#243745)
## Summary There are two elements in the Monaco editor that depend on the query text and are therefore rebuilt on every keystroke: `getJoinIndices `and `fixedQuery`. This causes them to rebuilt `esqlcallbacks`. --------- Co-authored-by: Stratou <efstratia.kalafateli@elastic.co>
1 parent 4ec90ae commit 63087e3

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

src/platform/packages/private/kbn-esql-editor/src/esql_editor.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { i18n } from '@kbn/i18n';
2323
import moment from 'moment';
2424
import { isEqual, memoize } from 'lodash';
2525
import { Global, css } from '@emotion/react';
26-
import { getESQLQueryColumns } from '@kbn/esql-utils';
26+
import { getESQLQueryColumns, getIndexPatternFromESQLQuery } from '@kbn/esql-utils';
2727
import type { CodeEditorProps } from '@kbn/code-editor';
2828
import { CodeEditor } from '@kbn/code-editor';
2929
import type { CoreStart } from '@kbn/core/public';
@@ -547,9 +547,20 @@ const ESQLEditorInternal = function ESQLEditor({
547547

548548
const canCreateLookupIndex = useCanCreateLookupIndex();
549549

550+
// Extract source command and build minimal query with cluster prefixes
551+
const minimalQuery = useMemo(() => {
552+
const prefix = code.match(/\b(FROM|TS)\b/i)?.[1]?.toUpperCase();
553+
const indexPattern = getIndexPatternFromESQLQuery(code);
554+
555+
return prefix && indexPattern ? `${prefix} ${indexPattern}` : '';
556+
}, [code]);
557+
558+
const minimalQueryRef = useRef(minimalQuery);
559+
minimalQueryRef.current = minimalQuery;
560+
550561
const getJoinIndices = useCallback<Required<ESQLCallbacks>['getJoinIndices']>(
551562
async (cacheOptions) => {
552-
const remoteClusters = getRemoteClustersFromESQLQuery(code);
563+
const remoteClusters = getRemoteClustersFromESQLQuery(minimalQueryRef.current);
553564
let result: IndicesAutocompleteResult = { indices: [] };
554565
if (kibana.services?.esql?.getJoinIndicesAutocomplete) {
555566
result = await kibana.services.esql.getJoinIndicesAutocomplete.call(
@@ -559,7 +570,7 @@ const ESQLEditorInternal = function ESQLEditor({
559570
}
560571
return result;
561572
},
562-
[code, kibana?.services?.esql?.getJoinIndicesAutocomplete]
573+
[kibana?.services?.esql?.getJoinIndicesAutocomplete]
563574
);
564575

565576
const telemetryCallbacks = useMemo<ESQLTelemetryCallbacks>(
@@ -583,7 +594,7 @@ const ESQLEditorInternal = function ESQLEditor({
583594
const esqlCallbacks = useMemo<ESQLCallbacks>(() => {
584595
const callbacks: ESQLCallbacks = {
585596
getSources: async () => {
586-
clearCacheWhenOld(dataSourcesCache, fixedQuery);
597+
clearCacheWhenOld(dataSourcesCache, minimalQueryRef.current);
587598
const getLicense = kibana.services?.esql?.getLicense;
588599
const sources = await memoizedSources(core, getLicense).result;
589600
return sources;
@@ -688,7 +699,6 @@ const ESQLEditorInternal = function ESQLEditor({
688699
favoritesClient,
689700
kibana.services?.esql,
690701
dataSourcesCache,
691-
fixedQuery,
692702
memoizedSources,
693703
core,
694704
esqlFieldsCache,

0 commit comments

Comments
 (0)