From 71c3a60aeeba8e6132c8af81baac404e37cf2447 Mon Sep 17 00:00:00 2001 From: Zoltan Haindrich Date: Fri, 26 Jan 2024 07:26:43 +0000 Subject: [PATCH 1/2] Identify not range filters without negating subexpressions Earlier betweenish (range/bounds) filters were identified thru a process of negating the subexpressions which may have not performed that well. (it could have dominated the runtime in some cases) This patch makes that unnecessary as its able to create the negate expression directly. --- .../filtration/CombineAndSimplifyBounds.java | 71 +++++++------------ 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java b/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java index 1d29dbc92b04..2b3de1985dd2 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java @@ -63,25 +63,13 @@ public DimFilter process(DimFilter filter) return filter; } else if (filter instanceof AndDimFilter) { final List children = getAndFilterChildren((AndDimFilter) filter); - final DimFilter one = doSimplifyAnd(children); - final DimFilter two = negate(doSimplifyOr(negateAll(children))); - return computeCost(one) <= computeCost(two) ? one : two; + return doSimplifyAnd(children); } else if (filter instanceof OrDimFilter) { final List children = getOrFilterChildren((OrDimFilter) filter); - final DimFilter one = doSimplifyOr(children); - final DimFilter two = negate(doSimplifyAnd(negateAll(children))); - return computeCost(one) <= computeCost(two) ? one : two; + return doSimplifyOr(children); } else if (filter instanceof NotDimFilter) { final DimFilter field = ((NotDimFilter) filter).getField(); - final DimFilter candidate; - if (field instanceof OrDimFilter) { - candidate = doSimplifyAnd(negateAll(getOrFilterChildren((OrDimFilter) field))); - } else if (field instanceof AndDimFilter) { - candidate = doSimplifyOr(negateAll(getAndFilterChildren((AndDimFilter) field))); - } else { - candidate = negate(field); - } - return computeCost(filter) <= computeCost(candidate) ? filter : candidate; + return negate(field); } else { return filter; } @@ -210,7 +198,18 @@ private static DimFilter doSimplify(final List children, boolean disj childrenToAdd.add(Bounds.toFilter(boundRefKey, range)); } } + } else if (disjunction && rangeSet.asRanges().size() == 2) { + if (Range.all().equals(rangeSet.span())) { + // 2 ranges in disjunction - spanning ALL + // complementer must be a negated range + for (final ObjectIntPair boundAndChildIndex : filterList) { + childrenToRemove.add(boundAndChildIndex.rightInt()); + } + RangeSet newRange = RangeSets.intersectRanges(rangeSet.complement().asRanges()); + childrenToAdd.add(new NotDimFilter(Bounds.toFilter(boundRefKey, newRange.span()))); + } } + } // Consolidate groups of numeric ranges in "ranges", using the leastRestrictiveNumericTypes computed earlier. @@ -270,6 +269,18 @@ private static DimFilter doSimplify(final List children, boolean disj childrenToAdd.add(Ranges.toFilter(rangeRefKey, range)); } } + } else { + if (disjunction && rangeSet.asRanges().size() == 2) { + if (Range.all().equals(rangeSet.span())) { + // 2 ranges in disjunction - spanning ALL + // complementer must be a negated range + for (final ObjectIntPair rangeAndChildIndex : filterList) { + childrenToRemove.add(rangeAndChildIndex.rightInt()); + } + RangeSet newRange = RangeSets.intersectRanges(rangeSet.complement().asRanges()); + childrenToAdd.add(new NotDimFilter(Ranges.toFilter(rangeRefKey, newRange.span()))); + } + } } } @@ -345,34 +356,4 @@ private static DimFilter negate(final DimFilter filter) return new NotDimFilter(filter); } } - - private static List negateAll(final List children) - { - final List newChildren = Lists.newArrayListWithCapacity(children.size()); - for (final DimFilter child : children) { - newChildren.add(negate(child)); - } - return newChildren; - } - - private static int computeCost(final DimFilter filter) - { - if (filter instanceof NotDimFilter) { - return computeCost(((NotDimFilter) filter).getField()); - } else if (filter instanceof AndDimFilter) { - int cost = 0; - for (DimFilter field : ((AndDimFilter) filter).getFields()) { - cost += computeCost(field); - } - return cost; - } else if (filter instanceof OrDimFilter) { - int cost = 0; - for (DimFilter field : ((OrDimFilter) filter).getFields()) { - cost += computeCost(field); - } - return cost; - } else { - return 1; - } - } } From 0664450a7293f0e65d384ce6a5bb8b4b0ad646c9 Mon Sep 17 00:00:00 2001 From: Zoltan Haindrich Date: Wed, 31 Jan 2024 13:27:32 +0000 Subject: [PATCH 2/2] add test;fix for multiple intervals --- .../filtration/CombineAndSimplifyBounds.java | 56 +++--- .../CombineAndSimplifyBoundsTest.java | 170 ++++++++++++++++++ 2 files changed, 205 insertions(+), 21 deletions(-) create mode 100644 sql/src/test/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBoundsTest.java diff --git a/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java b/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java index 2b3de1985dd2..7c77ed73439e 100644 --- a/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java +++ b/sql/src/main/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBounds.java @@ -41,6 +41,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; public class CombineAndSimplifyBounds extends BottomUpTransform { @@ -154,6 +155,7 @@ private static DimFilter doSimplify(final List children, boolean disj (c, existingType) -> ColumnType.leastRestrictiveType(existingType, range.getMatchValueType()) ); } + final List> filterList = ranges.computeIfAbsent(rangeRefKey, k -> new ArrayList<>()); filterList.add(ObjectIntPair.of(range, childIndex)); @@ -198,18 +200,20 @@ private static DimFilter doSimplify(final List children, boolean disj childrenToAdd.add(Bounds.toFilter(boundRefKey, range)); } } - } else if (disjunction && rangeSet.asRanges().size() == 2) { - if (Range.all().equals(rangeSet.span())) { - // 2 ranges in disjunction - spanning ALL - // complementer must be a negated range - for (final ObjectIntPair boundAndChildIndex : filterList) { - childrenToRemove.add(boundAndChildIndex.rightInt()); - } - RangeSet newRange = RangeSets.intersectRanges(rangeSet.complement().asRanges()); - childrenToAdd.add(new NotDimFilter(Bounds.toFilter(boundRefKey, newRange.span()))); + } else if (disjunction && Range.all().equals(rangeSet.span())) { + // ranges in disjunction - spanning ALL + // complementer must be a negated set of ranges + for (final ObjectIntPair boundAndChildIndex : filterList) { + childrenToRemove.add(boundAndChildIndex.rightInt()); } + Set> newRanges = rangeSet.complement().asRanges(); + List newFilters = new ArrayList<>(); + for (Range range : newRanges) { + BoundDimFilter filter = Bounds.toFilter(boundRefKey, range); + newFilters.add(filter); + } + childrenToAdd.add(new NotDimFilter(disjunction(newFilters))); } - } // Consolidate groups of numeric ranges in "ranges", using the leastRestrictiveNumericTypes computed earlier. @@ -269,18 +273,19 @@ private static DimFilter doSimplify(final List children, boolean disj childrenToAdd.add(Ranges.toFilter(rangeRefKey, range)); } } - } else { - if (disjunction && rangeSet.asRanges().size() == 2) { - if (Range.all().equals(rangeSet.span())) { - // 2 ranges in disjunction - spanning ALL - // complementer must be a negated range - for (final ObjectIntPair rangeAndChildIndex : filterList) { - childrenToRemove.add(rangeAndChildIndex.rightInt()); - } - RangeSet newRange = RangeSets.intersectRanges(rangeSet.complement().asRanges()); - childrenToAdd.add(new NotDimFilter(Ranges.toFilter(rangeRefKey, newRange.span()))); - } + } else if (disjunction && Range.all().equals(rangeSet.span())) { + // ranges in disjunction - spanning ALL + // complementer must be a negated set of ranges + for (final ObjectIntPair boundAndChildIndex : filterList) { + childrenToRemove.add(boundAndChildIndex.rightInt()); } + Set> newRanges = rangeSet.complement().asRanges(); + List newFilters = new ArrayList<>(); + for (Range range : newRanges) { + RangeFilter filter = Ranges.toFilter(rangeRefKey, range); + newFilters.add(filter); + } + childrenToAdd.add(new NotDimFilter(disjunction(newFilters))); } } @@ -338,6 +343,15 @@ private static DimFilter doSimplify(final List children, boolean disj } } + private static DimFilter disjunction(List operands) + { + Preconditions.checkArgument(operands.size() > 0, "invalid number of operands"); + if (operands.size() == 1) { + return operands.get(0); + } + return new OrDimFilter(operands); + } + private static DimFilter negate(final DimFilter filter) { if (Filtration.matchEverything().equals(filter)) { diff --git a/sql/src/test/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBoundsTest.java b/sql/src/test/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBoundsTest.java new file mode 100644 index 000000000000..3192dc052df7 --- /dev/null +++ b/sql/src/test/java/org/apache/druid/sql/calcite/filtration/CombineAndSimplifyBoundsTest.java @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.sql.calcite.filtration; + +import com.google.common.collect.ImmutableList; +import org.apache.druid.query.filter.BoundDimFilter; +import org.apache.druid.query.filter.DimFilter; +import org.apache.druid.query.filter.RangeFilter; +import org.apache.druid.segment.column.ColumnType; +import org.apache.druid.sql.calcite.BaseCalciteQueryTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +@RunWith(Parameterized.class) +public class CombineAndSimplifyBoundsTest extends BaseCalciteQueryTest +{ + + enum RangeFilterType + { + BOUND { + @Override + public DimFilter range(String lit1, boolean gte, String name, boolean lte, String lit2) + { + return new BoundDimFilter( + name, + lit1, + lit2, + !gte, + !lte, + true, + null, + null + ); + } + }, + RANGE { + @Override + public DimFilter range(String lit1, boolean gte, String name, boolean lte, String lit2) + { + return new RangeFilter( + name, + ColumnType.STRING, + lit1, + lit2, + !gte, + !lte, + null + ); + } + }; + + public final DimFilter lt(String name, String literal) + { + return range(null, false, name, false, literal); + } + + public final DimFilter le(String name, String literal) + { + return range(null, false, name, true, literal); + } + + public final DimFilter gt(String name, String literal) + { + return range(literal, false, name, false, null); + } + + public final DimFilter ge(String name, String literal) + { + return range(literal, true, name, false, null); + } + + public final DimFilter eq(String name, String literal) + { + return range(literal, true, name, true, literal); + } + + public abstract DimFilter range(String lit1, boolean gte, String name, boolean lte, String lit2); + } + + @Parameters + public static List getParameters() + { + return ImmutableList.of( + new Object[] {RangeFilterType.BOUND}, + new Object[] {RangeFilterType.RANGE} + ); + } + + final RangeFilterType rangeFilter; + + public CombineAndSimplifyBoundsTest(RangeFilterType rangeFilter) + { + this.rangeFilter = rangeFilter; + } + + @Test + public void testNotAZ() + { + String dim1 = "dim1"; + DimFilter inputFilter = or( + rangeFilter.lt(dim1, "a"), + rangeFilter.gt(dim1, "z") + ); + DimFilter expected = not(rangeFilter.range("a", true, dim1, true, "z")); + + check(inputFilter, expected); + } + + @Test + public void testAZ() + { + String dim1 = "dim1"; + DimFilter inputFilter = and( + rangeFilter.gt(dim1, "a"), + rangeFilter.lt(dim1, "z") + ); + DimFilter expected = rangeFilter.range("a", false, dim1, false, "z"); + + check(inputFilter, expected); + } + + @Test + public void testNot2() + { + String dim1 = "dim1"; + DimFilter inputFilter = or( + rangeFilter.le(dim1, "a"), + rangeFilter.range("f", true, dim1, false, "j"), + rangeFilter.gt(dim1, "z") + ); + DimFilter expected = not( + or( + rangeFilter.range("a", false, dim1, false, "f"), + rangeFilter.range("j", true, dim1, true, "z") + ) + ); + + check(inputFilter, expected); + } + + private void check(DimFilter inputFilter, DimFilter expected) + { + Filtration filt = Filtration.create(inputFilter, null); + Filtration ret = CombineAndSimplifyBounds.instance().apply(filt); + assertEquals(expected, ret.getDimFilter()); + } +}