Skip to content

Commit 2da81d6

Browse files
stratoulakibanamachinedej611
authored
[ES|QL] Removes the expressions dependency (#237403)
## Summary This PR is fixing a tech debt. - The expressions dependency on the ES|QL editor doesnt make any sense and is a bit of reduntant, we can retrieve the columns without it - The same for the alerts, they are just getting the table (fun fact they dont even need our extra formatiing that we do in the expressions so this will make it a bit faster) - For Lens it makes sense to get the columns with expressions, we are also including there some formatting and config that it is important for the Lens ES|QL charts. I moved the util to them - The editor package is private. Importing this helper from there is wrong considering the recent changes we did on the packages visibility. ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Marco Liberati <[email protected]>
1 parent 7f3bd14 commit 2da81d6

File tree

19 files changed

+159
-141
lines changed

19 files changed

+159
-141
lines changed

src/platform/packages/private/kbn-esql-editor/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
export type { DataErrorsControl } from './src/types';
11-
export { fetchFieldsFromESQL } from './src/fetch_fields_from_esql';
1211
export type { ESQLEditorProps } from './src/esql_editor';
1312
import { ESQLEditor } from './src/esql_editor';
1413
export type { ESQLEditorRestorableState } from './src/restorable_state';

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
type EuiButtonColor,
2121
} from '@elastic/eui';
2222
import { Global, css } from '@emotion/react';
23+
import { getESQLQueryColumns } from '@kbn/esql-utils';
2324
import type { CodeEditorProps } from '@kbn/code-editor';
2425
import { CodeEditor } from '@kbn/code-editor';
2526
import type { CoreStart } from '@kbn/core/public';
@@ -34,7 +35,6 @@ import {
3435
type IndicesAutocompleteResult,
3536
} from '@kbn/esql-types';
3637
import { fixESQLQueryWithVariables, getRemoteClustersFromESQLQuery } from '@kbn/esql-utils';
37-
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
3838
import { KBN_FIELD_TYPES } from '@kbn/field-types';
3939
import { i18n } from '@kbn/i18n';
4040
import type { SerializedEnrichPolicy } from '@kbn/index-management-shared-types';
@@ -58,7 +58,6 @@ import {
5858
esqlEditorStyles,
5959
} from './esql_editor.styles';
6060
import { ESQLEditorTelemetryService } from './telemetry/telemetry_service';
61-
import { fetchFieldsFromESQL } from './fetch_fields_from_esql';
6261
import {
6362
clearCacheWhenOld,
6463
filterDataErrors,
@@ -138,7 +137,7 @@ const ESQLEditorInternal = function ESQLEditor({
138137
const datePickerOpenStatusRef = useRef<boolean>(false);
139138
const theme = useEuiTheme();
140139
const kibana = useKibana<ESQLEditorDeps>();
141-
const { dataViews, expressions, application, core, fieldsMetadata, uiSettings, uiActions, data } =
140+
const { dataViews, application, core, fieldsMetadata, uiSettings, uiActions, data } =
142141
kibana.services;
143142

144143
const activeSolutionId = useObservable(core.chrome.getActiveSolutionNavId$());
@@ -440,18 +439,20 @@ const ESQLEditorInternal = function ESQLEditor({
440439
const fn = memoize(
441440
(
442441
...args: [
443-
{ esql: string },
444-
ExpressionsStart,
445-
TimeRange,
446-
AbortController?,
447-
string?,
448-
ESQLControlVariable[]?
442+
{
443+
esqlQuery: string;
444+
search: any;
445+
timeRange: TimeRange;
446+
signal?: AbortSignal;
447+
dropNullColumns?: boolean;
448+
variables?: ESQLControlVariable[];
449+
}
449450
]
450451
) => ({
451452
timestamp: Date.now(),
452-
result: fetchFieldsFromESQL(...args),
453+
result: getESQLQueryColumns(...args),
453454
}),
454-
({ esql }) => esql
455+
({ esqlQuery }) => esqlQuery
455456
);
456457

457458
return { cache: fn.cache, memoizedFieldsFromESQL: fn };
@@ -511,24 +512,20 @@ const ESQLEditorInternal = function ESQLEditor({
511512
},
512513
getColumnsFor: async ({ query: queryToExecute }: { query?: string } | undefined = {}) => {
513514
if (queryToExecute) {
514-
// ES|QL with limit 0 returns only the columns and is more performant
515-
const esqlQuery = {
516-
esql: `${queryToExecute} | limit 0`,
517-
};
518515
// Check if there's a stale entry and clear it
519-
clearCacheWhenOld(esqlFieldsCache, esqlQuery.esql);
516+
clearCacheWhenOld(esqlFieldsCache, `${queryToExecute} | limit 0`);
520517
const timeRange = data.query.timefilter.timefilter.getTime();
521518
try {
522-
const table = await memoizedFieldsFromESQL(
523-
esqlQuery,
524-
expressions,
519+
const columns = await memoizedFieldsFromESQL({
520+
esqlQuery: queryToExecute,
521+
search: data.search.search,
525522
timeRange,
526-
abortController,
527-
undefined,
528-
variablesService?.esqlVariables
529-
).result;
530-
const columns: ESQLFieldWithMetadata[] =
531-
table?.columns.map((c) => {
523+
signal: abortController.signal,
524+
variables: variablesService?.esqlVariables,
525+
dropNullColumns: true,
526+
}).result;
527+
const columnsWithMetadata: ESQLFieldWithMetadata[] =
528+
columns.map((c) => {
532529
return {
533530
name: c.name,
534531
type: c.meta.esType as FieldType,
@@ -537,7 +534,7 @@ const ESQLEditorInternal = function ESQLEditor({
537534
};
538535
}) || [];
539536

540-
return columns;
537+
return columnsWithMetadata;
541538
} catch (e) {
542539
// no action yet
543540
}
@@ -613,8 +610,8 @@ const ESQLEditorInternal = function ESQLEditor({
613610
core,
614611
esqlFieldsCache,
615612
data.query.timefilter.timefilter,
613+
data.search.search,
616614
memoizedFieldsFromESQL,
617-
expressions,
618615
abortController,
619616
variablesService?.esqlVariables,
620617
variablesService?.areSuggestionsEnabled,

src/platform/packages/private/kbn-esql-editor/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import type { CoreStart } from '@kbn/core/public';
1111
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
1212
import type { AggregateQuery } from '@kbn/es-query';
13-
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
1413
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
1514
import type { ILicense } from '@kbn/licensing-types';
1615
import type { UsageCollectionStart } from '@kbn/usage-collection-plugin/public';
@@ -129,7 +128,6 @@ export interface ESQLEditorDeps {
129128
core: CoreStart;
130129
dataViews: DataViewsPublicPluginStart;
131130
data: DataPublicPluginStart;
132-
expressions: ExpressionsStart;
133131
storage: Storage;
134132
uiActions: UiActionsStart;
135133
fieldsMetadata?: FieldsMetadataPublicStart;

src/platform/packages/private/kbn-esql-editor/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"@kbn/language-documentation",
2222
"@kbn/test-jest-helpers",
2323
"@kbn/data-plugin",
24-
"@kbn/expressions-plugin",
2524
"@kbn/data-views-plugin",
2625
"@kbn/index-management-shared-types",
2726
"@kbn/code-editor",

src/platform/packages/shared/kbn-esql-utils/src/utils/run_query.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ export async function getESQLQueryColumnsRaw({
7070
esqlQuery,
7171
search,
7272
signal,
73+
filter,
74+
dropNullColumns,
7375
timeRange,
7476
variables,
7577
}: {
7678
esqlQuery: string;
7779
search: ISearchGeneric;
7880
signal?: AbortSignal;
81+
dropNullColumns?: boolean;
82+
filter?: unknown;
7983
timeRange?: TimeRange;
8084
variables?: ESQLControlVariable[];
8185
}): Promise<ESQLColumn[]> {
@@ -85,7 +89,9 @@ export async function getESQLQueryColumnsRaw({
8589
search(
8690
{
8791
params: {
92+
...(filter ? { filter } : {}),
8893
query: `${esqlQuery} | limit 0`,
94+
...(dropNullColumns ? { dropNullColumns: true } : {}),
8995
...(namedParams.length ? { params: namedParams } : {}),
9096
},
9197
},
@@ -96,7 +102,20 @@ export async function getESQLQueryColumnsRaw({
96102
)
97103
);
98104

99-
return (response.rawResponse as unknown as ESQLSearchResponse).columns ?? [];
105+
const table = response.rawResponse as unknown as ESQLSearchResponse;
106+
const hasEmptyColumns = table.all_columns && table.all_columns?.length > table.columns.length;
107+
const lookup = new Set(hasEmptyColumns ? table.columns?.map(({ name }) => name) || [] : []);
108+
109+
const allColumns =
110+
(table.all_columns ?? table.columns)?.map(({ name, type }) => {
111+
return {
112+
name,
113+
type,
114+
isNull: hasEmptyColumns ? !lookup.has(name) : false,
115+
};
116+
}) ?? [];
117+
118+
return allColumns ?? [];
100119
} catch (error) {
101120
throw new Error(
102121
i18n.translate('esqlUtils.columnsErrorMsg', {
@@ -113,19 +132,25 @@ export async function getESQLQueryColumns({
113132
esqlQuery,
114133
search,
115134
signal,
135+
filter,
136+
dropNullColumns,
116137
timeRange,
117138
variables,
118139
}: {
119140
esqlQuery: string;
120141
search: ISearchGeneric;
121142
signal?: AbortSignal;
143+
filter?: unknown;
144+
dropNullColumns?: boolean;
122145
timeRange?: TimeRange;
123146
variables?: ESQLControlVariable[];
124147
}): Promise<DatatableColumn[]> {
125148
try {
126149
const rawColumns = await getESQLQueryColumnsRaw({
127150
esqlQuery,
128151
search,
152+
filter,
153+
dropNullColumns,
129154
signal,
130155
timeRange,
131156
variables,

src/platform/plugins/shared/esql/public/kibana_services.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import { BehaviorSubject } from 'rxjs';
1111
import type { CoreStart } from '@kbn/core/public';
1212
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
13-
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
1413
import type { Storage } from '@kbn/kibana-utils-plugin/public';
1514
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
1615
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
@@ -24,7 +23,6 @@ interface ServiceDeps {
2423
core: CoreStart;
2524
dataViews: DataViewsPublicPluginStart;
2625
data: DataPublicPluginStart;
27-
expressions: ExpressionsStart;
2826
storage: Storage;
2927
uiActions: UiActionsStart;
3028
fieldsMetadata?: FieldsMetadataPublicStart;
@@ -50,7 +48,6 @@ export const setKibanaServices = (
5048
kibanaCore: CoreStart,
5149
dataViews: DataViewsPublicPluginStart,
5250
data: DataPublicPluginStart,
53-
expressions: ExpressionsStart,
5451
storage: Storage,
5552
uiActions: UiActionsStart,
5653
fieldsMetadata?: FieldsMetadataPublicStart,
@@ -60,7 +57,6 @@ export const setKibanaServices = (
6057
servicesReady$.next({
6158
core,
6259
dataViews,
63-
expressions,
6460
data,
6561
storage,
6662
uiActions,

src/platform/plugins/shared/esql/public/plugin.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import type { CoreSetup, CoreStart, Plugin } from '@kbn/core/public';
1111
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
12-
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
1312
import type { LicensingPluginStart } from '@kbn/licensing-plugin/public';
1413
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
1514
import type { UiActionsSetup, UiActionsStart } from '@kbn/ui-actions-plugin/public';
@@ -45,7 +44,6 @@ interface EsqlPluginSetupDependencies {
4544

4645
interface EsqlPluginStartDependencies {
4746
dataViews: DataViewsPublicPluginStart;
48-
expressions: ExpressionsStart;
4947
uiActions: UiActionsStart;
5048
fieldsMetadata: FieldsMetadataPublicStart;
5149
licensing?: LicensingPluginStart;
@@ -81,7 +79,6 @@ export class EsqlPlugin implements Plugin<{}, EsqlPluginStart> {
8179
core: CoreStart,
8280
{
8381
dataViews,
84-
expressions,
8582
data,
8683
uiActions,
8784
fieldsMetadata,
@@ -202,7 +199,6 @@ export class EsqlPlugin implements Plugin<{}, EsqlPluginStart> {
202199
core,
203200
dataViews,
204201
data,
205-
expressions,
206202
storage,
207203
uiActions,
208204
fieldsMetadata,

src/platform/plugins/shared/esql/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@kbn/esql-editor",
1313
"@kbn/kibana-react-plugin",
1414
"@kbn/core",
15-
"@kbn/expressions-plugin",
1615
"@kbn/data-views-plugin",
1716
"@kbn/i18n",
1817
"@kbn/config-schema",

x-pack/platform/plugins/shared/lens/public/datasources/form_based/esql_layer/components/dimension_editor.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { render, screen, waitFor } from '@testing-library/react';
99
import type { TextBasedDimensionEditorProps } from './dimension_editor';
1010
import { TextBasedDimensionEditor } from './dimension_editor';
1111

12-
// Mock fetchFieldsFromESQL
13-
jest.mock('@kbn/esql-editor', () => ({
14-
fetchFieldsFromESQL: jest.fn(),
12+
// Mock fetchFieldsFromESQLExpression
13+
jest.mock('./fetch_fields_from_esql_expression', () => ({
14+
fetchFieldsFromESQLExpression: jest.fn(),
1515
}));
1616

17-
const { fetchFieldsFromESQL } = jest.requireMock('@kbn/esql-editor');
17+
const { fetchFieldsFromESQLExpression } = jest.requireMock('./fetch_fields_from_esql_expression');
1818

1919
describe('TextBasedDimensionEditor', () => {
2020
const defaultProps = {
@@ -51,7 +51,7 @@ describe('TextBasedDimensionEditor', () => {
5151
beforeEach(() => {
5252
jest.clearAllMocks();
5353

54-
fetchFieldsFromESQL.mockResolvedValue({
54+
fetchFieldsFromESQLExpression.mockResolvedValue({
5555
columns: [
5656
{ id: 'field1', name: 'Field One', meta: { type: 'string' } },
5757
{ id: 'field2', name: 'Field Two', meta: { type: 'number' } },
@@ -64,8 +64,8 @@ describe('TextBasedDimensionEditor', () => {
6464

6565
// Check if fetchFieldsFromESQL was called
6666
await waitFor(() => {
67-
expect(fetchFieldsFromESQL).toHaveBeenCalledTimes(1);
68-
expect(fetchFieldsFromESQL).toHaveBeenCalledWith(
67+
expect(fetchFieldsFromESQLExpression).toHaveBeenCalledTimes(1);
68+
expect(fetchFieldsFromESQLExpression).toHaveBeenCalledWith(
6969
{ esql: 'FROM my_data | limit 0' },
7070
{},
7171
{ from: defaultProps.dateRange.fromDate, to: defaultProps.dateRange.toDate },

x-pack/platform/plugins/shared/lens/public/datasources/form_based/esql_layer/components/dimension_editor.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import React, { useEffect, useState, useMemo, useCallback } from 'react';
99
import { i18n } from '@kbn/i18n';
1010
import { EuiFormRow, useEuiTheme, EuiText } from '@elastic/eui';
1111
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
12-
import { fetchFieldsFromESQL } from '@kbn/esql-editor';
1312
import { NameInput } from '@kbn/visualization-ui-components';
1413
import { css } from '@emotion/react';
1514
import { mergeLayer, updateColumnFormat, updateColumnLabel } from '../utils';
@@ -20,6 +19,7 @@ import { FieldSelect, type FieldOptionCompatible } from './field_select';
2019
import type { TextBasedPrivateState } from '../types';
2120
import { isNotNumeric, isNumeric } from '../utils';
2221
import type { TextBasedLayer } from '../types';
22+
import { fetchFieldsFromESQLExpression } from './fetch_fields_from_esql_expression';
2323

2424
export type TextBasedDimensionEditorProps =
2525
DatasourceDimensionEditorProps<TextBasedPrivateState> & {
@@ -46,7 +46,7 @@ export function TextBasedDimensionEditor(props: TextBasedDimensionEditorProps) {
4646
// in case the columns are not in the cache, I refetch them
4747
async function fetchColumns() {
4848
if (query) {
49-
const table = await fetchFieldsFromESQL(
49+
const table = await fetchFieldsFromESQLExpression(
5050
{ esql: `${query.esql} | limit 0` },
5151
expressions,
5252
{ from: dateRange.fromDate, to: dateRange.toDate },
@@ -56,6 +56,7 @@ export function TextBasedDimensionEditor(props: TextBasedDimensionEditorProps) {
5656
: undefined,
5757
esqlVariables
5858
);
59+
5960
if (table) {
6061
const hasNumberTypeColumns = table.columns?.some(isNumeric);
6162
const columns = table.columns.map((col) => {
@@ -82,11 +83,10 @@ export function TextBasedDimensionEditor(props: TextBasedDimensionEditorProps) {
8283
}, [
8384
dateRange.fromDate,
8485
dateRange.toDate,
86+
esqlVariables,
8587
expressions,
8688
indexPatterns,
8789
props,
88-
props.expressions,
89-
esqlVariables,
9090
query,
9191
]);
9292

0 commit comments

Comments
 (0)