Skip to content

Commit 41695f2

Browse files
authored
Converter method reuse (#235360)
## Summary Summarize your PR. If it involves visual changes include a screenshot or gif. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [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 - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent fad9abd commit 41695f2

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,14 @@ export class CstToAstConverter {
442442
// --------------------------------------------------------------------- FROM
443443

444444
private fromFromCommand(ctx: cst.FromCommandContext): ast.ESQLCommand<'from'> {
445-
const command = this.createCommand('from', ctx);
445+
return this.fromFromCompatibleCommand('from', ctx);
446+
}
447+
448+
private fromFromCompatibleCommand<Name extends string>(
449+
commandName: Name,
450+
ctx: antlr.ParserRuleContext & Pick<cst.FromCommandContext, 'indexPatternAndMetadataFields'>
451+
): ast.ESQLCommand<Name> {
452+
const command = this.createCommand(commandName, ctx);
446453
const indexPatternCtx = ctx.indexPatternAndMetadataFields();
447454
const metadataCtx = indexPatternCtx.metadata();
448455
const sources = indexPatternCtx
@@ -477,25 +484,7 @@ export class CstToAstConverter {
477484
// ----------------------------------------------------------------------- TS
478485

479486
private fromTimeseriesCommand(ctx: cst.TimeSeriesCommandContext): ast.ESQLCommand<'ts'> {
480-
const command = this.createCommand('ts', ctx);
481-
const indexPatternCtx = ctx.indexPatternAndMetadataFields();
482-
const metadataCtx = indexPatternCtx.metadata();
483-
const sources = indexPatternCtx
484-
.getTypedRuleContexts(cst.IndexPatternContext as any)
485-
.map((sourceCtx) => this.toSource(sourceCtx));
486-
487-
command.args.push(...sources);
488-
489-
if (metadataCtx && metadataCtx.METADATA()) {
490-
const name = metadataCtx.METADATA().getText().toLowerCase();
491-
const option = this.toOption(name, metadataCtx);
492-
const optionArgs = this.toColumnsFromCommand(metadataCtx);
493-
494-
option.args.push(...optionArgs);
495-
command.args.push(option);
496-
}
497-
498-
return command;
487+
return this.fromFromCompatibleCommand('ts', ctx);
499488
}
500489

501490
// --------------------------------------------------------------------- SHOW
@@ -1526,7 +1515,7 @@ export class CstToAstConverter {
15261515
): ast.ESQLColumn[] {
15271516
const identifiers = this.extractIdentifiers(ctx);
15281517

1529-
return this.makeColumnsOutOfIdentifiers(identifiers);
1518+
return this.toColumns(identifiers);
15301519
}
15311520

15321521
private extractIdentifiers(
@@ -1568,7 +1557,7 @@ export class CstToAstConverter {
15681557
return context;
15691558
}
15701559

1571-
private makeColumnsOutOfIdentifiers(identifiers: antlr.ParserRuleContext[]): ast.ESQLColumn[] {
1560+
private toColumns(identifiers: antlr.ParserRuleContext[]): ast.ESQLColumn[] {
15721561
const args: ast.ESQLColumn[] =
15731562
identifiers
15741563
.filter((child) => textExistsAndIsValid(child.getText()))

0 commit comments

Comments
 (0)