Skip to content

Commit 52dba47

Browse files
Fix a bug due to static counter
1 parent b29fb22 commit 52dba47

File tree

4 files changed

+26
-39
lines changed

4 files changed

+26
-39
lines changed

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LocalLogicalPlanOptimizerTests.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.elasticsearch.xpack.esql.core.type.DataType;
3232
import org.elasticsearch.xpack.esql.core.type.EsField;
3333
import org.elasticsearch.xpack.esql.core.type.InvalidMappedField;
34+
import org.elasticsearch.xpack.esql.core.util.Holder;
3435
import org.elasticsearch.xpack.esql.expression.Order;
3536
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
3637
import org.elasticsearch.xpack.esql.expression.function.aggregate.Min;
@@ -839,20 +840,19 @@ public void testVerifierOnAdditionalAttributeAdded() throws Exception {
839840
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
840841
var salary = as(min.field(), NamedExpression.class);
841842
assertThat(salary.name(), is("salary"));
842-
843+
Holder<Integer> appliedCount = new Holder<>(0);
843844
// use a custom rule that adds another output attribute
844845
var customRuleBatch = new RuleExecutor.Batch<>(
845846
"CustomRuleBatch",
846847
RuleExecutor.Limiter.ONCE,
847848
new OptimizerRules.ParameterizedOptimizerRule<Aggregate, LocalLogicalOptimizerContext>(UP) {
848-
static Integer appliedCount = 0;
849849

850850
@Override
851851
protected LogicalPlan rule(Aggregate plan, LocalLogicalOptimizerContext context) {
852852
// This rule adds a missing attribute to the plan output
853853
// We only want to apply it once, so we use a static counter
854-
if (appliedCount == 0) {
855-
appliedCount++;
854+
if (appliedCount.get() == 0) {
855+
appliedCount.set(appliedCount.get() + 1);
856856
Literal additionalLiteral = new Literal(Source.EMPTY, "additional literal", INTEGER);
857857
return new Eval(plan.source(), plan, List.of(new Alias(Source.EMPTY, "additionalAttribute", additionalLiteral)));
858858
}
@@ -878,19 +878,17 @@ public void testVerifierOnAttributeDatatypeChanged() {
878878
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
879879
var salary = as(min.field(), NamedExpression.class);
880880
assertThat(salary.name(), is("salary"));
881-
881+
Holder<Integer> appliedCount = new Holder<>(0);
882882
// use a custom rule that changes the datatype of an output attribute
883883
var customRuleBatch = new RuleExecutor.Batch<>(
884884
"CustomRuleBatch",
885885
RuleExecutor.Limiter.ONCE,
886886
new OptimizerRules.ParameterizedOptimizerRule<LogicalPlan, LocalLogicalOptimizerContext>(DOWN) {
887-
static Integer appliedCount = 0;
888-
889887
@Override
890888
protected LogicalPlan rule(LogicalPlan plan, LocalLogicalOptimizerContext context) {
891889
// We only want to apply it once, so we use a static counter
892-
if (appliedCount == 0) {
893-
appliedCount++;
890+
if (appliedCount.get() == 0) {
891+
appliedCount.set(appliedCount.get() + 1);
894892
Limit limit = as(plan, Limit.class);
895893
Limit newLimit = new Limit(plan.source(), limit.limit(), limit.child()) {
896894
@Override

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LocalPhysicalPlanOptimizerTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,20 +2425,18 @@ public void testVerifierOnAdditionalAttributeAdded() throws Exception {
24252425
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
24262426
var salary = as(min.field(), NamedExpression.class);
24272427
assertThat(salary.name(), is("salary"));
2428-
2428+
Holder<Integer> appliedCount = new Holder<>(0);
24292429
// use a custom rule that adds another output attribute
24302430
var customRuleBatch = new RuleExecutor.Batch<>(
24312431
"CustomRuleBatch",
24322432
RuleExecutor.Limiter.ONCE,
24332433
new PhysicalOptimizerRules.ParameterizedOptimizerRule<PhysicalPlan, LocalPhysicalOptimizerContext>() {
2434-
static Integer appliedCount = 0;
2435-
24362434
@Override
24372435
public PhysicalPlan rule(PhysicalPlan plan, LocalPhysicalOptimizerContext context) {
24382436
// This rule adds a missing attribute to the plan output
24392437
// We only want to apply it once, so we use a static counter
2440-
if (appliedCount == 0) {
2441-
appliedCount++;
2438+
if (appliedCount.get() == 0) {
2439+
appliedCount.set(appliedCount.get() + 1);
24422440
Literal additionalLiteral = new Literal(Source.EMPTY, "additional literal", INTEGER);
24432441
return new EvalExec(
24442442
plan.source(),
@@ -2470,19 +2468,17 @@ public void testVerifierOnAttributeDatatypeChanged() throws Exception {
24702468
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
24712469
var salary = as(min.field(), NamedExpression.class);
24722470
assertThat(salary.name(), is("salary"));
2473-
2471+
Holder<Integer> appliedCount = new Holder<>(0);
24742472
// use a custom rule that changes the datatype of an output attribute
24752473
var customRuleBatch = new RuleExecutor.Batch<>(
24762474
"CustomRuleBatch",
24772475
RuleExecutor.Limiter.ONCE,
24782476
new PhysicalOptimizerRules.ParameterizedOptimizerRule<PhysicalPlan, LocalPhysicalOptimizerContext>() {
2479-
static Integer appliedCount = 0;
2480-
24812477
@Override
24822478
public PhysicalPlan rule(PhysicalPlan plan, LocalPhysicalOptimizerContext context) {
24832479
// We only want to apply it once, so we use a static counter
2484-
if (appliedCount == 0) {
2485-
appliedCount++;
2480+
if (appliedCount.get() == 0) {
2481+
appliedCount.set(appliedCount.get() + 1);
24862482
LimitExec limit = as(plan, LimitExec.class);
24872483
LimitExec newLimit = new LimitExec(
24882484
plan.source(),

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8052,20 +8052,18 @@ public void testVerifierOnAdditionalAttributeAdded() throws Exception {
80528052
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
80538053
var salary = as(min.field(), NamedExpression.class);
80548054
assertThat(salary.name(), is("salary"));
8055-
8055+
Holder<Integer> appliedCount = new Holder<>(0);
80568056
// use a custom rule that adds another output attribute
80578057
var customRuleBatch = new RuleExecutor.Batch<>(
80588058
"CustomRuleBatch",
80598059
RuleExecutor.Limiter.ONCE,
80608060
new OptimizerRules.ParameterizedOptimizerRule<Aggregate, LogicalOptimizerContext>(UP) {
8061-
static Integer appliedCount = 0;
8062-
80638061
@Override
80648062
protected LogicalPlan rule(Aggregate plan, LogicalOptimizerContext context) {
80658063
// This rule adds a missing attribute to the plan output
80668064
// We only want to apply it once, so we use a static counter
8067-
if (appliedCount == 0) {
8068-
appliedCount++;
8065+
if (appliedCount.get() == 0) {
8066+
appliedCount.set(appliedCount.get() + 1);
80698067
Literal additionalLiteral = new Literal(Source.EMPTY, "additional literal", INTEGER);
80708068
return new Eval(plan.source(), plan, List.of(new Alias(Source.EMPTY, "additionalAttribute", additionalLiteral)));
80718069
}
@@ -8091,19 +8089,17 @@ public void testVerifierOnAttributeDatatypeChanged() {
80918089
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
80928090
var salary = as(min.field(), NamedExpression.class);
80938091
assertThat(salary.name(), is("salary"));
8094-
8092+
Holder<Integer> appliedCount = new Holder<>(0);
80958093
// use a custom rule that changes the datatype of an output attribute
80968094
var customRuleBatch = new RuleExecutor.Batch<>(
80978095
"CustomRuleBatch",
80988096
RuleExecutor.Limiter.ONCE,
80998097
new OptimizerRules.ParameterizedOptimizerRule<LogicalPlan, LogicalOptimizerContext>(DOWN) {
8100-
static Integer appliedCount = 0;
8101-
81028098
@Override
81038099
protected LogicalPlan rule(LogicalPlan plan, LogicalOptimizerContext context) {
81048100
// We only want to apply it once, so we use a static counter
8105-
if (appliedCount == 0) {
8106-
appliedCount++;
8101+
if (appliedCount.get() == 0) {
8102+
appliedCount.set(appliedCount.get() + 1);
81078103
Limit limit = as(plan, Limit.class);
81088104
Limit newLimit = new Limit(plan.source(), limit.limit(), limit.child()) {
81098105
@Override

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/PhysicalPlanOptimizerTests.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.elasticsearch.xpack.esql.core.tree.Source;
6565
import org.elasticsearch.xpack.esql.core.type.DataType;
6666
import org.elasticsearch.xpack.esql.core.type.EsField;
67+
import org.elasticsearch.xpack.esql.core.util.Holder;
6768
import org.elasticsearch.xpack.esql.enrich.ResolvedEnrichPolicy;
6869
import org.elasticsearch.xpack.esql.expression.Order;
6970
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
@@ -8335,20 +8336,18 @@ public void testVerifierOnAdditionalAttributeAdded() throws Exception {
83358336
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
83368337
var salary = as(min.field(), NamedExpression.class);
83378338
assertThat(salary.name(), is("salary"));
8338-
8339+
Holder<Integer> appliedCount = new Holder<>(0);
83398340
// use a custom rule that adds another output attribute
83408341
var customRuleBatch = new RuleExecutor.Batch<>(
83418342
"CustomRuleBatch",
83428343
RuleExecutor.Limiter.ONCE,
83438344
new PhysicalOptimizerRules.ParameterizedOptimizerRule<PhysicalPlan, PhysicalOptimizerContext>() {
8344-
static Integer appliedCount = 0;
8345-
83468345
@Override
83478346
public PhysicalPlan rule(PhysicalPlan plan, PhysicalOptimizerContext context) {
83488347
// This rule adds a missing attribute to the plan output
83498348
// We only want to apply it once, so we use a static counter
8350-
if (appliedCount == 0) {
8351-
appliedCount++;
8349+
if (appliedCount.get() == 0) {
8350+
appliedCount.set(appliedCount.get() + 1);
83528351
Literal additionalLiteral = new Literal(Source.EMPTY, "additional literal", INTEGER);
83538352
return new EvalExec(
83548353
plan.source(),
@@ -8378,19 +8377,17 @@ public void testVerifierOnAttributeDatatypeChanged() throws Exception {
83788377
var min = as(Alias.unwrap(aggregate.aggregates().get(0)), Min.class);
83798378
var salary = as(min.field(), NamedExpression.class);
83808379
assertThat(salary.name(), is("salary"));
8381-
8380+
Holder<Integer> appliedCount = new Holder<>(0);
83828381
// use a custom rule that changes the datatype of an output attribute
83838382
var customRuleBatch = new RuleExecutor.Batch<>(
83848383
"CustomRuleBatch",
83858384
RuleExecutor.Limiter.ONCE,
83868385
new PhysicalOptimizerRules.ParameterizedOptimizerRule<PhysicalPlan, PhysicalOptimizerContext>() {
8387-
static Integer appliedCount = 0;
8388-
83898386
@Override
83908387
public PhysicalPlan rule(PhysicalPlan plan, PhysicalOptimizerContext context) {
83918388
// We only want to apply it once, so we use a static counter
8392-
if (appliedCount == 0) {
8393-
appliedCount++;
8389+
if (appliedCount.get() == 0) {
8390+
appliedCount.set(appliedCount.get() + 1);
83948391
LimitExec limit = as(plan, LimitExec.class);
83958392
LimitExec newLimit = new LimitExec(
83968393
plan.source(),

0 commit comments

Comments
 (0)