From b5c05972c01227456a1ad873a130581de62ace1e Mon Sep 17 00:00:00 2001 From: Stratoula Kalafateli Date: Fri, 12 Apr 2024 14:09:34 +0200 Subject: [PATCH] [ES|QL] Fixes the problem with Discover and queries without the from command --- ...get_data_view_by_text_based_query_lang.test.ts | 15 +++++++++++++++ .../get_data_view_by_text_based_query_lang.ts | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.test.ts b/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.test.ts index 0dfbd84224f4d..c30dc888c85c4 100644 --- a/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.test.ts +++ b/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.test.ts @@ -45,4 +45,19 @@ describe('getDataViewByTextBasedQueryLang', () => { expect(dataView.isPersisted()).toEqual(false); expect(dataView.timeFieldName).toBeUndefined(); }); + + it('creates an adhoc ES|QL dataview if the query doesnt have from command', async () => { + discoverServiceMock.dataViews.create = jest.fn().mockReturnValue({ + ...dataViewAdHoc, + isPersisted: () => false, + id: 'ad-hoc-id-1', + title: 'test-1', + timeFieldName: undefined, + }); + const query = { esql: 'ROW x = "ES|QL is awesome"' }; + const dataView = await getDataViewByTextBasedQueryLang(query, dataViewAdHoc, services); + expect(dataView.isPersisted()).toEqual(false); + expect(dataView.name).toEqual(dataViewAdHoc.name); + expect(dataView.timeFieldName).toBeUndefined(); + }); }); diff --git a/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.ts b/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.ts index 42e36c7ac9ab8..ea97a5d849248 100644 --- a/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.ts +++ b/src/plugins/discover/public/application/main/utils/get_data_view_by_text_based_query_lang.ts @@ -27,7 +27,12 @@ export async function getDataViewByTextBasedQueryLang( indexPatternFromQuery = getIndexPatternFromESQLQuery(query.esql); } // we should find a better way to work with ESQL queries which dont need a dataview - if (!indexPatternFromQuery && currentDataView) return currentDataView; + if (!indexPatternFromQuery && currentDataView) { + // Here the user used either the ROW or SHOW META / SHOW INFO commands + // if we use the current dataview will create this error https://github.com/elastic/kibana/issues/163417 + // so we are creating an adhoc dataview without an @timestamp timeFieldName + return await getESQLAdHocDataview(currentDataView.name, services.dataViews); + } if ( currentDataView?.isPersisted() ||