Skip to content

Commit f1bcb07

Browse files
ES|QL: add generator for SAMPLE command (#135334)
1 parent 9081aba commit f1bcb07

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/generator/EsqlQueryGenerator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.xpack.esql.generator.command.pipe.LookupJoinGenerator;
2222
import org.elasticsearch.xpack.esql.generator.command.pipe.MvExpandGenerator;
2323
import org.elasticsearch.xpack.esql.generator.command.pipe.RenameGenerator;
24+
import org.elasticsearch.xpack.esql.generator.command.pipe.SampleGenerator;
2425
import org.elasticsearch.xpack.esql.generator.command.pipe.SortGenerator;
2526
import org.elasticsearch.xpack.esql.generator.command.pipe.StatsGenerator;
2627
import org.elasticsearch.xpack.esql.generator.command.pipe.TimeSeriesStatsGenerator;
@@ -71,6 +72,7 @@ public class EsqlQueryGenerator {
7172
LookupJoinGenerator.INSTANCE,
7273
MvExpandGenerator.INSTANCE,
7374
RenameGenerator.INSTANCE,
75+
SampleGenerator.INSTANCE,
7476
SortGenerator.INSTANCE,
7577
StatsGenerator.INSTANCE,
7678
WhereGenerator.INSTANCE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.generator.command.pipe;
9+
10+
import org.elasticsearch.xpack.esql.generator.Column;
11+
import org.elasticsearch.xpack.esql.generator.QueryExecutor;
12+
import org.elasticsearch.xpack.esql.generator.command.CommandGenerator;
13+
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
import static org.elasticsearch.test.ESTestCase.randomDoubleBetween;
18+
19+
public class SampleGenerator implements CommandGenerator {
20+
21+
public static final String SAMPLE = "sample";
22+
public static final CommandGenerator INSTANCE = new SampleGenerator();
23+
24+
@Override
25+
public CommandDescription generate(
26+
List<CommandDescription> previousCommands,
27+
List<Column> previousOutput,
28+
QuerySchema schema,
29+
QueryExecutor executor
30+
) {
31+
double n = randomDoubleBetween(0.0, 1.0, false);
32+
String cmd = " | SAMPLE " + n;
33+
return new CommandDescription(SAMPLE, this, cmd, Map.of());
34+
}
35+
36+
@Override
37+
public ValidationResult validateOutput(
38+
List<CommandDescription> previousCommands,
39+
CommandDescription commandDescription,
40+
List<Column> previousColumns,
41+
List<List<Object>> previousOutput,
42+
List<Column> columns,
43+
List<List<Object>> output
44+
) {
45+
return CommandGenerator.expectSameColumns(previousColumns, columns);
46+
}
47+
}

0 commit comments

Comments
 (0)