From 168dbc49f5d666a6820b253fabbc19b1f517cfa9 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Sat, 3 Apr 2021 18:47:59 +0300 Subject: [PATCH] QL: Improve removal of items during iteration (#71193) Clean-up the range optimization rule by avoid the use of indices during iteration and removal (since it skips an element each time) Remove redundant ternary calls --- .../xpack/ql/optimizer/OptimizerRules.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java index 96f782e7c8959..2b245ea8055bd 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java @@ -344,8 +344,8 @@ private Expression propagate(And and) { for (BinaryComparison eq : equals) { Object eqValue = eq.right().fold(); - for (int i = 0; i < ranges.size(); i++) { - Range range = ranges.get(i); + for (Iterator iterator = ranges.iterator(); iterator.hasNext(); ) { + Range range = iterator.next(); if (range.value().semanticEquals(eq.left())) { // if equals is outside the interval, evaluate the whole expression to FALSE @@ -373,7 +373,7 @@ private Expression propagate(And and) { } // it's in the range and thus, remove it - ranges.remove(i); + iterator.remove(); changed = true; } } @@ -789,8 +789,7 @@ private static boolean findExistingRange(Range main, List ranges, boolean if (conjunctive) { // can tighten range if (lower || upper) { - ranges.remove(i); - ranges.add(i, + ranges.set(i, new Range(main.source(), main.value(), lower ? main.lower() : other.lower(), lower ? main.includeLower() : other.includeLower(), @@ -806,13 +805,12 @@ private static boolean findExistingRange(Range main, List ranges, boolean else { // can loosen range if (lower && upper) { - ranges.remove(i); - ranges.add(i, + ranges.set(i, new Range(main.source(), main.value(), - lower ? main.lower() : other.lower(), - lower ? main.includeLower() : other.includeLower(), - upper ? main.upper() : other.upper(), - upper ? main.includeUpper() : other.includeUpper(), + main.lower(), + main.includeLower(), + main.upper(), + main.includeUpper(), main.zoneId())); return true; } @@ -844,8 +842,7 @@ private boolean findConjunctiveComparisonInRange(BinaryComparison main, List 0 || lowerEq; if (lower) { - ranges.remove(i); - ranges.add(i, + ranges.set(i, new Range(other.source(), other.value(), main.right(), lowerEq ? false : main instanceof GreaterThanOrEqual, other.upper(), other.includeUpper(), other.zoneId())); @@ -865,8 +862,7 @@ private boolean findConjunctiveComparisonInRange(BinaryComparison main, List