Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
172 commits
Select commit Hold shift + click to select a range
53c6d37
Sync main into feature/lookup-join-ux (#224190)
darnautov Jun 17, 2025
5256427
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jun 17, 2025
b73cda6
[ES|QL] Support editing and viewing LOOKUP JOIN index content in the …
darnautov Jun 17, 2025
dcdd99b
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jun 19, 2025
c6a6008
[ES|QL] CSV Upload for the LOOKUP JOIN UX (#224474)
darnautov Jun 19, 2025
8959bb8
[LOOKUP_JOIN] Add new row to index (PART 1) (#224567)
sddonne Jun 20, 2025
94dfe1b
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jun 23, 2025
fcffa51
[ES|QL] CSV File Preview in Data Grid format (#224722)
darnautov Jun 24, 2025
d9e5e25
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jun 24, 2025
8daf744
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jun 25, 2025
2102805
[ES|QL] Pagination and sorting for the lookup index documents (#225051)
darnautov Jun 25, 2025
531d80a
[ES|QL] Index editor cell validations (#224728)
sddonne Jun 26, 2025
0d0ef7e
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 2, 2025
afdaf0e
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 3, 2025
7b4ee59
[ES|QL] Lookup join - Add new column (#225410)
sddonne Jul 7, 2025
301a545
[ES|QL] Lookup join: show validations using a tooltip (#225748)
sddonne Jul 9, 2025
3877851
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 14, 2025
075008f
[ES|QL] Lookup index - Ad-hoc creation (#227660)
sddonne Jul 17, 2025
6fb498a
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 22, 2025
eb6f0be
Merge remote-tracking branch 'upstream/feature/lookup-join-ux' into f…
darnautov Jul 22, 2025
0766dea
[ES|QL] Update ES|QL editor state after index creation (#226441)
darnautov Jul 22, 2025
cd6dcb6
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 23, 2025
fc23ef4
[ES|QL] - Lookup join - Allow editing unsaved columns (#228421)
sddonne Jul 24, 2025
b182314
[ES|QL] LOOKUP JOIN: Hover Action, Query Insertion Fix and decorator …
darnautov Jul 28, 2025
d239dba
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Jul 28, 2025
d7dc48f
[ES|QL] - Lookup join - Fixes on columns handling, undo functionality…
sddonne Jul 29, 2025
4cde6bc
[ES|QL] Lookup index - Add warning message when dropping a file upon …
sddonne Jul 29, 2025
f9014f1
[ES|QL] LOOKUP JOIN - Keyboard accessibility improvements (#229907)
sddonne Jul 31, 2025
1a714aa
[ES|QL] LOOKUP JOIN - Unify add row and add column behaviours (#229954)
sddonne Aug 4, 2025
aa5995e
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 5, 2025
d1a323e
[ES|QL] LOOKUP JOIN: Query lookup index with ES|QL (#230140)
darnautov Aug 5, 2025
831a6aa
[ES|QL] LOOKUP JOIN - Index name input improvements (#230186)
sddonne Aug 6, 2025
5c4b143
[ES|QL] LOOKUP JOIN - Allow to edit cells and columns on blur (#230074)
sddonne Aug 6, 2025
63db544
[ES|QL] LOOKUP JOIN - Empty state iteration (#230811)
sddonne Aug 7, 2025
bced9e4
[ES|QL] LOOKUP JOIN - fix mapping persistence between editions and di…
sddonne Aug 7, 2025
134e75a
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 12, 2025
efb16b2
[ES|QL] LOOKUP JOIN - Fix: column movement when saving value (#230173)
sddonne Aug 13, 2025
6585ab1
[ES|QL] LOOKUP-JOIN - Use data table to preview file (#231073)
sddonne Aug 13, 2025
0e0dc1a
[ES|QL] Support doc deletion, Save changes on click (#230738)
darnautov Aug 13, 2025
f605cbc
[ESQL] LOOKUP JOIN - Refresh join indexes get after creating a lookup…
sddonne Aug 13, 2025
661de39
[ES|QL] LOOKUP JOIN - Add error callout (#231617)
sddonne Aug 14, 2025
7700d24
[ES|QL] LOOKUP JOIN - query bar minor changes (#231001)
sddonne Aug 14, 2025
c674755
[ES|QL] LOOKUP JOIN - Always display a placeholder row (#231777)
sddonne Aug 14, 2025
aaddc32
[ES|QL] LOOKUP JOIN - Unsaved changes warning (#231745)
sddonne Aug 14, 2025
e1bcf3d
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 18, 2025
0bf56b6
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 19, 2025
6e8acad
[ES|QL] LOOKUP JOIN: Merge ES docs with pending changes (#232117)
darnautov Aug 19, 2025
afe8b6a
[ES|QL] LOOKUP JOIN - Import file loading improvement (#231146)
sddonne Aug 19, 2025
c14fad7
[ES|QL] LOOKUP JOIN - Fix autosaving after error (#232272)
sddonne Aug 20, 2025
baa66ee
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 20, 2025
ea1cde3
[ES|QL] LOOKUP JOIN - feedback loop (#232493)
sddonne Aug 21, 2025
b5b604b
[ES|QL] Check index privileges (#232494)
darnautov Aug 22, 2025
6462678
[ES|QL] LOOKUP JOIN - Recalculate placeholders after importing file (…
sddonne Aug 22, 2025
7684fc3
[ES|QL] FIx jest mocks (#232659)
darnautov Aug 22, 2025
06e5b62
[ESQL] LOOKUP JOIN - Functional tests (#232447)
sddonne Aug 22, 2025
671e668
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 22, 2025
49ca0c6
Merge branch 'main' into feature/lookup-join-ux
stratoula Aug 25, 2025
1d2af14
fix: row-column creator handlers names
sddonne Aug 26, 2025
719a30f
rename FILE_PREVIEW_LIMIT
sddonne Aug 26, 2025
444060e
rename closePopover
sddonne Aug 26, 2025
7db8727
improve error handling
sddonne Aug 26, 2025
cb69f2c
disable discover link using discoverLink var
sddonne Aug 26, 2025
efa8f73
display unsaved changes modal on unsaved columns
sddonne Aug 26, 2025
9a96d4a
fix: column out of bounds
sddonne Aug 26, 2025
f1424aa
display analysis errors
sddonne Aug 27, 2025
d664270
use composer api for building esql query
sddonne Aug 27, 2025
ba6b796
fix: duplicated rows added after filtering
sddonne Aug 27, 2025
e28fb6c
avoid double scape when editing cells
sddonne Aug 27, 2025
640257d
fix: duplicated kbn reference
sddonne Aug 28, 2025
1300b66
fix tests
sddonne Aug 28, 2025
88f416c
[ES|QL] LOOKUP JOIN - Allow saving without closing flyout (#233143)
sddonne Aug 29, 2025
ba0158d
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Aug 29, 2025
8b6125a
replace isESQLSourceItem
darnautov Aug 29, 2025
42a7a3a
use type from es client
darnautov Aug 29, 2025
21e3a10
move getLookupIndicesFromQuery to utils package
darnautov Aug 29, 2025
40ccf3b
hook for index privileges
darnautov Aug 29, 2025
f3557e7
register command once on mount and dispose on unmount
darnautov Aug 29, 2025
b82f528
[ES|QL] LOOKUP JOIN - Dont show save button if nothing to save (#233436)
sddonne Aug 29, 2025
7aa7c63
append_index_to_join_command refactor
darnautov Aug 29, 2025
3382974
[CI] Auto-commit changed files from 'node scripts/eslint_all_files --…
kibanamachine Aug 29, 2025
fa2002a
add more tests to index editor service
sddonne Aug 29, 2025
149feb3
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Aug 29, 2025
10f41e9
optimize decoration rendering
darnautov Aug 29, 2025
bbbd8fc
fix useLookupIndexPrivileges and add tests
darnautov Aug 29, 2025
5c84bc2
fix empty array for privileges check
darnautov Aug 29, 2025
f408dc4
remove app id check for autocomplete
darnautov Aug 29, 2025
7551a60
check for supported app in decorators
darnautov Aug 29, 2025
3e0c3c8
getMonacoCommandString
darnautov Aug 29, 2025
1c30e61
rename hook
darnautov Aug 29, 2025
0ce3baf
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 1, 2025
c687adf
use_lookup_index_editor tests
darnautov Sep 1, 2025
ea373e3
update mocks
darnautov Sep 1, 2025
07cd220
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Sep 1, 2025
ee6b62b
clean un needed async function
sddonne Sep 1, 2025
ed4c808
consolidate validations and save actions in indexName, added tests
sddonne Sep 1, 2025
8835c27
use getESQLAdHocDataview
sddonne Sep 1, 2025
4e77ce3
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Sep 1, 2025
87767c9
explaining comment
sddonne Sep 1, 2025
56a8f4c
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 1, 2025
91d246d
don't use active columns on discover link
sddonne Sep 1, 2025
55c4a97
clean unused code
sddonne Sep 1, 2025
280b16e
extract empty array into a const
sddonne Sep 1, 2025
7ace6d6
Update README.md
sddonne Sep 1, 2025
e7990ab
fix: upload icon background on dark theme
sddonne Sep 1, 2025
c00a9d3
fix: data being erased when changing index name
sddonne Sep 2, 2025
8367413
clean monaco wordPattern
sddonne Sep 2, 2025
6794f42
refactor: placeholders recalculation
sddonne Sep 2, 2025
4c5ad55
get back word pattern
sddonne Sep 2, 2025
db8191b
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
sddonne Sep 3, 2025
74ee230
use onAllDocsSearchable callback instead of timeout
sddonne Sep 3, 2025
cbf20af
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 3, 2025
77426dc
add unit tests to marginal flows
sddonne Sep 3, 2025
443a585
add new ftr tests
sddonne Sep 3, 2025
033b2b9
clean
sddonne Sep 3, 2025
a716c96
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Sep 3, 2025
8278533
fix circular dependency
sddonne Sep 4, 2025
cd49a45
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 4, 2025
0ef4e78
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 4, 2025
5b3e5d7
fix: race condition when recalculating placeholders
sddonne Sep 4, 2025
9474172
set default rows to 10
sddonne Sep 4, 2025
b5a5b5c
dont show file preview of large files
sddonne Sep 5, 2025
58533a1
typos
sddonne Sep 5, 2025
833da68
add test for file_drop_zone
sddonne Sep 5, 2025
83403bd
add tech preview badge
sddonne Sep 5, 2025
d3f035f
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 5, 2025
04f14d4
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 5, 2025
aba099a
always keep the pagination in display
sddonne Sep 5, 2025
746cf3c
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 5, 2025
b3aaa9c
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Sep 8, 2025
c02828d
fix table flickering after import
sddonne Sep 8, 2025
2a8d3e8
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 8, 2025
e5f3c42
update monaco hover popup background
sddonne Sep 8, 2025
ab4c36f
change sort text for lookup join suggestions
sddonne Sep 8, 2025
bb18ac5
Revert "update monaco hover popup background"
sddonne Sep 8, 2025
0257163
only expose allowNoIndex option
sddonne Sep 8, 2025
b7c27e8
don't suggest creating an already existing index
sddonne Sep 8, 2025
5db5a96
display toast after appending file data to the index
sddonne Sep 9, 2025
21984ff
fix: types
sddonne Sep 9, 2025
0e73bf0
normalize number
sddonne Sep 9, 2025
69a6707
iterate not column header defined message
sddonne Sep 9, 2025
5d3450c
fix scroll in input popover
sddonne Sep 9, 2025
be814af
update UI action Ids
sddonne Sep 9, 2025
fe4ba57
extract onSort
sddonne Sep 9, 2025
4ab364a
update totalHits when deleting documents
sddonne Sep 9, 2025
0f2ba5b
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 9, 2025
8f477ef
trim boolean input value
sddonne Sep 9, 2025
0772391
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 9, 2025
d0217b6
fix old mappings being displayed from previous dataview
sddonne Sep 9, 2025
246f969
allow substring search
sddonne Sep 9, 2025
3823b92
hide the remove column action from saved fields
sddonne Sep 9, 2025
2373a60
fix tests
sddonne Sep 9, 2025
da92ca0
add skeleton while dataView is not created yet
sddonne Sep 10, 2025
96b8e61
Merge with main, update caching function
darnautov Sep 10, 2025
db449fb
Merge remote-tracking branch 'upstream/feature/lookup-join-ux' into f…
darnautov Sep 10, 2025
a55d3e2
don't save empty docs
sddonne Sep 10, 2025
868a89d
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 10, 2025
2a6a530
update copies
sddonne Sep 10, 2025
9cb8223
fix: blank screen while saving
sddonne Sep 10, 2025
b518a7d
set saving to false if import fails
sddonne Sep 10, 2025
324abef
display an error if the index to edit does not exists anymore
sddonne Sep 10, 2025
9705d64
[CI] Auto-commit changed files from 'node scripts/yarn_deduplicate'
kibanamachine Sep 10, 2025
754c7e5
clear sources cache after creating a new index
sddonne Sep 10, 2025
ef47ffd
Merge branch 'feature/lookup-join-ux' of https://github.com/elastic/k…
sddonne Sep 10, 2025
673425b
file clash tab
darnautov Sep 10, 2025
b4a8144
Merge remote-tracking branch 'upstream/main' into feature/lookup-join-ux
darnautov Sep 12, 2025
24ecbb3
debounce options to const
darnautov Sep 12, 2025
1d9e100
use optional chain
sddonne Sep 15, 2025
9275e4d
fix tests
sddonne Sep 15, 2025
b676f28
Merge branch 'main' into feature/lookup-join-ux
sddonne Sep 15, 2025
43f506e
fallback to file content if preview fails
darnautov Sep 15, 2025
198142c
disable sort
sddonne Sep 15, 2025
2a133c6
revert charts changes
darnautov Sep 15, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ src/platform/packages/private/kbn-handlebars @elastic/kibana-security
src/platform/packages/private/kbn-hapi-mocks @elastic/kibana-core
src/platform/packages/private/kbn-health-gateway-server @elastic/kibana-core
src/platform/packages/private/kbn-import-resolver @elastic/kibana-operations
src/platform/packages/private/kbn-index-editor @elastic/kibana-esql
src/platform/packages/private/kbn-item-buffer @elastic/appex-sharedux
src/platform/packages/private/kbn-jest-serializers @elastic/kibana-operations
src/platform/packages/private/kbn-journeys @elastic/kibana-operations @elastic/appex-qa
Expand Down Expand Up @@ -1351,6 +1352,7 @@ src/platform/plugins/shared/discover/public/context_awareness/profile_providers/
# ES|QL
/src/platform/test/api_integration/apis/esql/*.ts @elastic/kibana-esql
/src/platform/test/functional/services/esql.ts @elastic/kibana-esql
/src/platform/test/functional/page_objects/index_editor.ts @elastic/kibana-esql
/src/platform/packages/shared/kbn-monaco/src/languages/esql @elastic/kibana-esql
x-pack/solutions/observability/plugins/observability/server/lib/esql_extensions @elastic/obs-ux-infra_services-team @elastic/obs-ux-logs-team

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@
"@kbn/iframe-embedded-plugin": "link:x-pack/platform/test/functional_embedded/plugins/iframe_embedded",
"@kbn/image-embeddable-plugin": "link:src/platform/plugins/private/image_embeddable",
"@kbn/index-adapter": "link:x-pack/solutions/security/packages/index-adapter",
"@kbn/index-editor": "link:src/platform/packages/private/kbn-index-editor",
"@kbn/index-lifecycle-management-common-shared": "link:x-pack/platform/packages/shared/index-lifecycle-management/index_lifecycle_management_common_shared",
"@kbn/index-lifecycle-management-plugin": "link:x-pack/platform/plugins/private/index_lifecycle_management",
"@kbn/index-management-plugin": "link:x-pack/platform/plugins/shared/index_management",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-optimizer/limits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pageLoadAssetSize:
embeddableAlertsTable: 6596
embeddableEnhanced: 10332
enterpriseSearch: 38494
esql: 10370
esql: 12323
esqlDataGrid: 10209
esUiShared: 100338
eventAnnotation: 22361
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { monaco } from '@kbn/monaco';
import {
appendIndexToJoinCommandByName,
appendIndexToJoinCommandByPosition,
} from './append_index_to_join_command';

describe('appendIndexToJoinCommandByName', () => {
it('replaces existing index argument', () => {
const result = appendIndexToJoinCommandByName(
`FROM kibana_sample_data_ecommerce
| EVAL customer_id = TO_LONG(customer_id)
| LOOKUP JOIN customer_data ON customer_id | LOOKUP JOIN test1123`,
'test1123',
'new_index'
);
expect(result).toBe(`FROM kibana_sample_data_ecommerce
| EVAL customer_id = TO_LONG(customer_id)
| LOOKUP JOIN customer_data ON customer_id
| LOOKUP JOIN new_index`);
});
});

describe('appendIndexToJoinCommandByPosition', () => {
it('should append an index name to the join command', () => {
const result = appendIndexToJoinCommandByPosition(
'FROM kibana_sample_data_logs | LOOKUP JOIN | LIMIT 10',
{ lineNumber: 1, column: 44 } as monaco.Position,
'new_index'
);
expect(result).toBe(`FROM kibana_sample_data_logs
| LOOKUP JOIN new_index
| LIMIT 10`);
});

it('should append an index name to the join command in a multi-line query', () => {
const result = appendIndexToJoinCommandByPosition(
`FROM kibana_sample_data_logs
| LOOKUP JOIN\u0020
| LIMIT 10`,
{ lineNumber: 2, column: 17 } as monaco.Position,
'new_index'
);
expect(result).toBe(`FROM kibana_sample_data_logs
| LOOKUP JOIN new_index
| LIMIT 10`);
});

it('should append an index name to the correct join command', () => {
const result = appendIndexToJoinCommandByPosition(
'FROM kibana_sample_data_logs | LOOKUP JOIN new_index ON some_field | LOOKUP JOIN | LIMIT 10',
{ lineNumber: 1, column: 82 } as monaco.Position,
'another_index'
);
expect(result).toBe(`FROM kibana_sample_data_logs
| LOOKUP JOIN new_index ON some_field
| LOOKUP JOIN another_index
| LIMIT 10`);
});

it('should not append an index name if an index argument with the same name is already present', () => {
const result = appendIndexToJoinCommandByPosition(
'FROM kibana_sample_data_logs | LOOKUP JOIN new_index | LIMIT 10',
{ lineNumber: 1, column: 53 } as monaco.Position,
'new_index'
);
expect(result).toBe('FROM kibana_sample_data_logs | LOOKUP JOIN new_index | LIMIT 10');
});

it('should replace the existing index argument', () => {
const result = appendIndexToJoinCommandByPosition(
'FROM kibana_sample_data_logs | LOOKUP JOIN new_index | LIMIT 10',
{ lineNumber: 1, column: 53 } as monaco.Position,
'new_index_2'
);
expect(result).toBe(`FROM kibana_sample_data_logs
| LOOKUP JOIN new_index_2
| LIMIT 10`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import {
BasicPrettyPrinter,
Parser,
isSource,
mutate,
type ESQLAstItem,
type ESQLAstJoinCommand,
type ESQLAstQueryExpression,
type ESQLSingleAstItem,
type ESQLSource,
} from '@kbn/esql-ast';
import type { monaco } from '@kbn/monaco';

interface SelectedJoin {
joinCmd: ESQLAstJoinCommand;
src: ESQLSource | undefined;
firstArg: ESQLAstItem;
}

type JoinCommandSelector = (root: ESQLAstQueryExpression) => SelectedJoin | undefined;

function modifyJoinCommand(
root: ESQLAstQueryExpression,
selectedJoin: SelectedJoin,
createdIndexName: string
): string {
const { joinCmd, src, firstArg } = selectedJoin;

const newSource: ESQLSource = {
type: 'source',
sourceType: 'index',
incomplete: false,
location: src?.location ?? {
min: joinCmd.location?.min ?? 0,
max: (joinCmd.location?.min ?? 0) + createdIndexName.length,
},
text: createdIndexName,
name: createdIndexName,
};

if (src) {
const idx = joinCmd.args.indexOf(firstArg);
// remove the original argument (source or AS option) from the JOIN command
mutate.generic.commands.args.remove(root, firstArg as unknown as ESQLSingleAstItem);
mutate.generic.commands.args.insert(joinCmd, newSource, idx);
} else {
mutate.generic.commands.args.insert(joinCmd, newSource, 0);
}

return BasicPrettyPrinter.multiline(root);
}

function getJoinSourceInfo(joinCmd: ESQLAstJoinCommand) {
const firstArg = joinCmd.args[0];
let src: ESQLSource | undefined;
if (isSource(firstArg)) {
src = firstArg;
} else if (
firstArg &&
!Array.isArray(firstArg) &&
firstArg.type === 'option' &&
firstArg.name === 'as'
) {
src = firstArg.args[0] as unknown as ESQLSource;
}
return { src, firstArg };
}

function byNameSelector(targetName: string): JoinCommandSelector {
const trimmedTargetName = targetName.trim();
return (root: ESQLAstQueryExpression) => {
for (const joinCmd of mutate.commands.join.list(root)) {
const { src, firstArg } = getJoinSourceInfo(joinCmd);
if (src?.name === trimmedTargetName) {
return { joinCmd, src, firstArg };
}
}
return undefined;
};
}

function byPositionSelector(position: monaco.Position, query: string): JoinCommandSelector {
const cursorOffset = (() => {
const { lineNumber, column } = position;
const lines = query.split('\n');
let off = 0;
for (let i = 0; i < lineNumber - 1; i++) {
off += lines[i].length + 1;
}
return off + column - 1;
})();

return (root: ESQLAstQueryExpression) => {
let selectedJoin: SelectedJoin | undefined;
let smallestDistance = Number.MAX_SAFE_INTEGER;

for (const joinCmd of mutate.commands.join.list(root)) {
if (joinCmd.location) {
const { min, max } = joinCmd.location;
let distance = 0;
if (cursorOffset < min) distance = min - cursorOffset;
else if (cursorOffset > max) distance = cursorOffset - max;

if (distance < smallestDistance) {
smallestDistance = distance;
const { src, firstArg } = getJoinSourceInfo(joinCmd);
selectedJoin = { joinCmd, src, firstArg };
}
}
}
return selectedJoin;
};
}

function appendIndexToJoinCommand(
query: string,
createdIndexName: string,
selector: JoinCommandSelector
): string {
if (!createdIndexName) return query;

const { root } = Parser.parse(query);
const selectedJoin = selector(root);

if (!selectedJoin) return query;

if (selectedJoin.src && selectedJoin.src.name === createdIndexName) return query;

return modifyJoinCommand(root, selectedJoin, createdIndexName);
}

export function appendIndexToJoinCommandByName(
query: string,
targetName: string,
createdIndexName: string
): string {
if (targetName.trim() === createdIndexName) return query;
return appendIndexToJoinCommand(query, createdIndexName, byNameSelector(targetName));
}

export function appendIndexToJoinCommandByPosition(
query: string,
position: monaco.Position,
createdIndexName: string
): string {
return appendIndexToJoinCommand(query, createdIndexName, byPositionSelector(position, query));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { useLookupIndexCommand, useCanCreateLookupIndex } from './use_lookup_index_editor';
Loading