Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,21 @@ export namespace Builder {
name: '',
};
};

export const bare = (
template: Omit<AstNodeTemplate<ESQLList>, 'name' | 'values'> &
Partial<Pick<ESQLList, 'values'>> = {},
fromParser?: Partial<AstNodeParserFields>
): ESQLList => {
return {
values: [],
...template,
...Builder.parserFields(fromParser),
type: 'list',
subtype: 'bare',
name: '',
};
};
}

export namespace literal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { promqlFunctionDefinitions } from '../generated/promql_functions';
import { buildFunctionDocumentation } from './documentation';
import { withAutoSuggest } from './autocomplete/helpers';
import { isIdentifier, isSource } from '../../../ast/is';
import { isIdentifier, isList, isSource } from '../../../ast/is';
import { SuggestionCategory } from '../../../shared/sorting';
import { techPreviewLabel } from './shared';

Expand Down Expand Up @@ -153,6 +153,21 @@ export function getIndexFromPromQLParams({

const { value } = indexEntry ?? {};

if (isList(value) && value.values.length > 0) {
const listText = value.text?.trim();
if (listText) {
return listText;
}

const names = value.values
.map((item) => (isIdentifier(item) || isSource(item) ? item.name : ''))
.filter(Boolean);

if (names.length > 0) {
return names.join(',');
}
}

if ((isIdentifier(value) || isSource(value)) && !value.name.includes(EDITOR_MARKER)) {
return value.name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,25 @@ describe('PROMQL columnsAfter', () => {

expect(result).toEqual([]);
});

it('passes multiple indices to fromFrom', async () => {
const fromFrom = jest.fn().mockResolvedValue([]);

await columnsAfter(
synth.cmd`PROMQL index=metrics,logs-tsdb rate(http_requests_total[5m])`,
[],
'',
{
fromFrom,
fromJoin: () => Promise.resolve([]),
fromEnrich: () => Promise.resolve([]),
}
);

expect(fromFrom).toHaveBeenCalledTimes(1);

const [cmd] = fromFrom.mock.calls[0];
expect(String(cmd)).toContain('metrics');
expect(String(cmd)).toContain('logs-tsdb');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import type {
ESQLAst,
ESQLAstAllCommands,
ESQLAstExpression,
ESQLAstPromqlCommand,
ESQLLocation,
ESQLMessage,
} from '../../../types';
import { isIdentifier } from '../../../ast/is';
import { isIdentifier, isList, isSource } from '../../../ast/is';
import type { ICommandContext } from '../types';
import { getMessageFromId } from '../../definitions/utils';
import { sourceExists } from '../../definitions/utils/sources';
Expand Down Expand Up @@ -219,7 +220,7 @@ function collectPromqlParamValues(
continue;
}

const rawValue = entry.value.text?.trim() ?? '';
const rawValue = getPromqlParamValueText(entry.value);
const value = looksLikePromqlParamAssignment(rawValue) ? '' : rawValue;

values.set(key, {
Expand All @@ -245,6 +246,24 @@ function collectPromqlParamValues(
return values;
}

function getPromqlParamValueText(value: ESQLAstExpression | undefined): string {
if (!value) return '';

const text = value.text?.trim();
if (text) return text;

if (isList(value)) {
const parts = value.values
.map((item) => item.text?.trim() || (isIdentifier(item) || isSource(item) ? item.name : ''))
.filter(Boolean);

return parts.join(',');
}

if (isIdentifier(value) || isSource(value)) return value.name;

return '';
}
/*
* Extracts the PromQL query text from the AST.
*
Expand Down Expand Up @@ -314,7 +333,7 @@ function getPromqlQueryTail(command: ESQLAstPromqlCommand): string {
continue;
}

const rawValue = entry.value.text ?? '';
const rawValue = getPromqlParamValueText(entry.value);
const keyPrefix = `${key}=`;
const valuePrefix = `${keyPrefix}${rawValue}`;
const lowerRest = rest.toLowerCase();
Expand Down
Loading
Loading