Skip to content

Remove optional seed from ES|QL SAMPLE #128887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -27,16 +28,29 @@

public class SampleOperator implements Operator {

public record Factory(double probability, int seed) implements OperatorFactory {
public static class Factory implements OperatorFactory {

private final double probability;
private final Integer seed;

public Factory(double probability) {
this(probability, null);
}

// visible for testing
Factory(double probability, Integer seed) {
this.probability = probability;
this.seed = seed;
}

@Override
public SampleOperator get(DriverContext driverContext) {
return new SampleOperator(probability, seed);
return new SampleOperator(probability, seed == null ? Randomness.get().nextInt() : seed);
}

@Override
public String describe() {
return "SampleOperator[probability = " + probability + ", seed = " + seed + "]";
return "SampleOperator[probability = " + probability + "]";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.matchesPattern;

public class SampleOperatorTests extends OperatorTestCase {

Expand All @@ -46,7 +45,7 @@ protected SampleOperator.Factory simple(SimpleOptions options) {

@Override
protected Matcher<String> expectedDescriptionOfSimple() {
return matchesPattern("SampleOperator\\[probability = 0.5, seed = -?\\d+]");
return equalTo("SampleOperator[probability = 0.5]");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugin/esql/src/main/antlr/EsqlBaseParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -312,5 +312,5 @@ completionCommand
;

sampleCommand
: DEV_SAMPLE probability=decimalValue seed=integerValue?
: DEV_SAMPLE probability=decimalValue
;
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ protected LogicalPlan rule(Sample sample, LogicalOptimizerContext context) {
var child = sample.child();
if (child instanceof Sample sampleChild) {
var probability = combinedProbability(context, sample, sampleChild);
var seed = combinedSeed(context, sample, sampleChild);
plan = new Sample(sample.source(), probability, seed, sampleChild.child());
plan = new Sample(sample.source(), probability, sampleChild.child());
} else if (child instanceof Enrich
|| child instanceof Eval
|| child instanceof Filter
Expand All @@ -82,22 +81,4 @@ private static Expression combinedProbability(LogicalOptimizerContext context, S
var childProbability = (double) Foldables.valueOf(context.foldCtx(), child.probability());
return Literal.of(parent.probability(), parentProbability * childProbability);
}

private static Expression combinedSeed(LogicalOptimizerContext context, Sample parent, Sample child) {
var parentSeed = parent.seed();
var childSeed = child.seed();
Expression seed;
if (parentSeed != null) {
if (childSeed != null) {
var seedValue = (int) Foldables.valueOf(context.foldCtx(), parentSeed);
seedValue ^= (int) Foldables.valueOf(context.foldCtx(), childSeed);
seed = Literal.of(parentSeed, seedValue);
} else {
seed = parentSeed;
}
} else {
seed = childSeed;
}
return seed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ protected PhysicalPlan rule(SampleExec sample, LocalPhysicalOptimizerContext ctx
}

var sampleQuery = new RandomSamplingQueryBuilder((double) Foldables.valueOf(ctx.foldCtx(), sample.probability()));
if (sample.seed() != null) {
sampleQuery.seed((int) Foldables.valueOf(ctx.foldCtx(), sample.seed()));
}

fullQuery.filter(sampleQuery);

Expand Down

Large diffs are not rendered by default.

Loading