|
4 | 4 | * 2.0; you may not use this file except in compliance with the Elastic License |
5 | 5 | * 2.0. |
6 | 6 | */ |
7 | | -import { |
8 | | - getIndexPatternFromSQLQuery, |
9 | | - getIndexPatternFromESQLQuery, |
10 | | - getESQLAdHocDataview, |
11 | | - getESQLQueryColumns, |
12 | | -} from '@kbn/esql-utils'; |
| 7 | +import { getIndexPatternFromSQLQuery, getIndexPatternFromESQLQuery } from '@kbn/esql-utils'; |
13 | 8 | import type { AggregateQuery } from '@kbn/es-query'; |
| 9 | +import { getESQLAdHocDataview } from '@kbn/esql-utils'; |
14 | 10 | import { getLensAttributesFromSuggestion } from '@kbn/visualization-utils'; |
| 11 | +import { fetchFieldsFromESQL } from '@kbn/text-based-editor'; |
15 | 12 | import type { DataViewSpec } from '@kbn/data-views-plugin/public'; |
16 | 13 | import type { TypedLensByValueInput } from '../../../embeddable/embeddable_component'; |
17 | 14 | import type { LensPluginStartDependencies } from '../../../plugin'; |
18 | 15 | import type { DatasourceMap, VisualizationMap } from '../../../types'; |
19 | 16 | import { suggestionsApi } from '../../../lens_suggestions_api'; |
20 | 17 |
|
| 18 | +export const getQueryColumns = async ( |
| 19 | + query: AggregateQuery, |
| 20 | + deps: LensPluginStartDependencies, |
| 21 | + abortController?: AbortController |
| 22 | +) => { |
| 23 | + // Fetching only columns for ES|QL for performance reasons with limit 0 |
| 24 | + // Important note: ES doesnt return the warnings for 0 limit, |
| 25 | + // I am skipping them in favor of performance now |
| 26 | + // but we should think another way to get them (from Lens embeddable or store) |
| 27 | + const performantQuery = { ...query }; |
| 28 | + if ('esql' in performantQuery && performantQuery.esql) { |
| 29 | + performantQuery.esql = `${performantQuery.esql} | limit 0`; |
| 30 | + } |
| 31 | + const table = await fetchFieldsFromESQL( |
| 32 | + performantQuery, |
| 33 | + deps.expressions, |
| 34 | + undefined, |
| 35 | + abortController |
| 36 | + ); |
| 37 | + return table?.columns; |
| 38 | +}; |
| 39 | + |
21 | 40 | export const getSuggestions = async ( |
22 | 41 | query: AggregateQuery, |
23 | 42 | deps: LensPluginStartDependencies, |
@@ -46,12 +65,7 @@ export const getSuggestions = async ( |
46 | 65 | if (dataView.fields.getByName('@timestamp')?.type === 'date' && !dataViewSpec) { |
47 | 66 | dataView.timeFieldName = '@timestamp'; |
48 | 67 | } |
49 | | - |
50 | | - const columns = await getESQLQueryColumns({ |
51 | | - esqlQuery: 'esql' in query ? query.esql : '', |
52 | | - search: deps.data.search, |
53 | | - signal: abortController?.signal, |
54 | | - }); |
| 68 | + const columns = await getQueryColumns(query, deps, abortController); |
55 | 69 | const context = { |
56 | 70 | dataViewSpec: dataView?.toSpec(), |
57 | 71 | fieldName: '', |
|
0 commit comments