Skip to content

Commit 422e2b7

Browse files
vadimkibanadevamanv
authored andcommitted
[ES|QL] PromQL pretty-printing support (elastic#247399)
## Summary Partially addresses elastic#243932 Adds ability to transforms a PromQL AST back to text: pretty-print. Supports basic pretty printing where the whole PromQL query is printed on a single line with minimal whitespace. ### Checklist - [x] [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
1 parent fcb2614 commit 422e2b7

File tree

6 files changed

+1421
-5
lines changed

6 files changed

+1421
-5
lines changed

src/platform/packages/shared/kbn-esql-language/src/promql/builder/builder.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,25 @@ export namespace PromQLBuilder {
236236
};
237237
};
238238

239+
/**
240+
* Create a string literal.
241+
*
242+
* @param valueUnquoted - The unquoted string value (e.g., 'hello' not '"hello"')
243+
* @param rawValue - Optional raw value as it appeared in source (used by parser).
244+
* If not provided, the printer will handle quoting/escaping.
245+
* @param fromParser - Optional parser fields
246+
*/
239247
export const string = (
240-
value: string,
241248
valueUnquoted: string,
249+
rawValue?: string,
242250
fromParser?: Partial<AstNodeParserFields>
243251
): PromQLStringLiteral => {
244252
return {
245253
dialect: 'promql',
246254
type: 'literal',
247255
literalType: 'string',
248-
name: value,
249-
value,
256+
name: rawValue ?? valueUnquoted,
257+
value: rawValue ?? valueUnquoted,
250258
valueUnquoted,
251259
...PromQLBuilder.parserFields(fromParser),
252260
};

src/platform/packages/shared/kbn-esql-language/src/promql/parser/cst_to_ast_converter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,16 +596,17 @@ export class PromQLCstToAstConverter {
596596
return this.fromStringToken(token.symbol);
597597
}
598598

599-
return PromQLBuilder.expression.literal.string(ctx.getText(), '', this.getParserFields(ctx));
599+
const text = ctx.getText();
600+
return PromQLBuilder.expression.literal.string('', text, this.getParserFields(ctx));
600601
}
601602

602603
private fromStringToken(token: antlr.Token): ast.PromQLStringLiteral {
603604
const text = token.text ?? '';
604605
const valueUnquoted = this.unquoteString(text);
605606

606607
return PromQLBuilder.expression.literal.string(
607-
text,
608608
valueUnquoted,
609+
text,
609610
this.createParserFieldsFromToken(token)
610611
);
611612
}

0 commit comments

Comments
 (0)