|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.qa.rest.generative.command.pipe; |
| 9 | + |
| 10 | +import org.elasticsearch.xpack.esql.qa.rest.generative.EsqlQueryGenerator; |
| 11 | +import org.elasticsearch.xpack.esql.qa.rest.generative.command.CommandGenerator; |
| 12 | + |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import static org.elasticsearch.test.ESTestCase.randomIntBetween; |
| 17 | + |
| 18 | +public class ForkGenerator implements CommandGenerator { |
| 19 | + |
| 20 | + public static final String FORK = "fork"; |
| 21 | + public static final CommandGenerator INSTANCE = new ForkGenerator(); |
| 22 | + |
| 23 | + @Override |
| 24 | + public CommandDescription generate( |
| 25 | + List<CommandDescription> previousCommands, |
| 26 | + List<EsqlQueryGenerator.Column> previousOutput, |
| 27 | + QuerySchema schema |
| 28 | + ) { |
| 29 | + // FORK can only be allowed once - so we skip adding another FORK if we already have one |
| 30 | + // otherwise, most generated queries would only result in a validation error |
| 31 | + for (CommandDescription command : previousCommands) { |
| 32 | + if (command.commandName().equals(FORK)) { |
| 33 | + return new CommandDescription(FORK, this, " ", Map.of()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + int n = randomIntBetween(2, 10); |
| 38 | + |
| 39 | + String cmd = " | FORK " + "( WHERE true ) ".repeat(n) + " | WHERE _fork == \"fork" + randomIntBetween(1, n) + "\" | DROP _fork"; |
| 40 | + |
| 41 | + return new CommandDescription(FORK, this, cmd, Map.of()); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public ValidationResult validateOutput( |
| 46 | + List<CommandDescription> previousCommands, |
| 47 | + CommandDescription command, |
| 48 | + List<EsqlQueryGenerator.Column> previousColumns, |
| 49 | + List<List<Object>> previousOutput, |
| 50 | + List<EsqlQueryGenerator.Column> columns, |
| 51 | + List<List<Object>> output |
| 52 | + ) { |
| 53 | + return CommandGenerator.expectSameRowCount(previousCommands, previousOutput, output); |
| 54 | + } |
| 55 | +} |
0 commit comments