kylin-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liy...@apache.org
Subject [13/33] kylin git commit: minor, refine basic tuple filter class
Date Fri, 26 Jan 2018 11:20:39 GMT
minor, refine basic tuple filter class


Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/d43abbd6
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/d43abbd6
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/d43abbd6

Branch: refs/heads/sync
Commit: d43abbd6bb722f85035d432ae05399e55e2917e8
Parents: 13f64f4
Author: Roger Shi <rogershijicheng@hotmail.com>
Authored: Mon Jan 8 17:57:57 2018 +0800
Committer: Li Yang <liyang@apache.org>
Committed: Fri Jan 26 18:46:52 2018 +0800

----------------------------------------------------------------------
 .../metadata/filter/LogicalTupleFilter.java     | 26 ++++++++
 .../kylin/metadata/filter/TupleFilter.java      | 43 +++++++++++++
 .../kylin/metadata/filter/TupleFilterTest.java  | 65 ++++++++++++++++++++
 3 files changed, 134 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/d43abbd6/core-metadata/src/main/java/org/apache/kylin/metadata/filter/LogicalTupleFilter.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/LogicalTupleFilter.java
b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/LogicalTupleFilter.java
index 7893ed8..f0c825f 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/LogicalTupleFilter.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/LogicalTupleFilter.java
@@ -170,4 +170,30 @@ public class LogicalTupleFilter extends TupleFilter implements IOptimizeableTupl
         return transformer.visit(this);
     }
 
+    @Override
+    public boolean equals(Object other) {
+        if (other == this) {
+            return true;
+        }
+        if (!(other instanceof LogicalTupleFilter)) {
+            return false;
+        }
+        final LogicalTupleFilter otherFilter = (LogicalTupleFilter) other;
+        if (otherFilter.operator != this.operator || otherFilter.children.size() != this.children.size())
{
+            return false;
+        }
+
+        for (int i = 0; i < otherFilter.children.size(); i++) {
+            if (!otherFilter.children.get(i).equals(this.children.get(i))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return (operator == null ? 0 : operator.hashCode()) + 31 * this.children.hashCode();
+    }
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/d43abbd6/core-metadata/src/main/java/org/apache/kylin/metadata/filter/TupleFilter.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/TupleFilter.java
b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/TupleFilter.java
index 5ba8726..09b41f5 100644
--- a/core-metadata/src/main/java/org/apache/kylin/metadata/filter/TupleFilter.java
+++ b/core-metadata/src/main/java/org/apache/kylin/metadata/filter/TupleFilter.java
@@ -99,6 +99,11 @@ public abstract class TupleFilter {
             addChild(c); // subclass overrides addChild()
     }
 
+    final public void addChildren(TupleFilter... children) {
+        for (TupleFilter c : children)
+            addChild(c); // subclass overrides addChild()
+    }
+
     public List<? extends TupleFilter> getChildren() {
         return children;
     }
@@ -121,6 +126,44 @@ public abstract class TupleFilter {
     }
 
     /**
+     * The storage level dislike NOT logic
+     */
+    public TupleFilter removeNot() {
+        return removeNotInternal(this);
+    }
+
+    private TupleFilter removeNotInternal(TupleFilter filter) {
+        FilterOperatorEnum op = filter.getOperator();
+
+        if (!(filter instanceof LogicalTupleFilter)) {
+            return filter;
+        }
+
+        LogicalTupleFilter logicalFilter = (LogicalTupleFilter) filter;
+
+        switch (logicalFilter.operator) {
+        case NOT:
+            assert (filter.children.size() == 1);
+            TupleFilter reverse = filter.children.get(0).reverse();
+            return removeNotInternal(reverse);
+        case AND:
+            LogicalTupleFilter andFilter = new LogicalTupleFilter(FilterOperatorEnum.AND);
+            for (TupleFilter child : logicalFilter.children) {
+                andFilter.addChild(removeNotInternal(child));
+            }
+            return andFilter;
+        case OR:
+            LogicalTupleFilter orFilter = new LogicalTupleFilter(FilterOperatorEnum.OR);
+            for (TupleFilter child : logicalFilter.children) {
+                orFilter.addChild(removeNotInternal(child));
+            }
+            return orFilter;
+        default:
+            throw new IllegalStateException("This filter is unexpected: " + filter);
+        }
+    }
+
+    /**
      * flatten to OR-AND filter, (A AND B AND ..) OR (C AND D AND ..) OR ..
      * flatten filter will ONLY contain AND and OR , no NOT will exist.
      * This will help to decide scan ranges.

http://git-wip-us.apache.org/repos/asf/kylin/blob/d43abbd6/core-metadata/src/test/java/org/apache/kylin/metadata/filter/TupleFilterTest.java
----------------------------------------------------------------------
diff --git a/core-metadata/src/test/java/org/apache/kylin/metadata/filter/TupleFilterTest.java
b/core-metadata/src/test/java/org/apache/kylin/metadata/filter/TupleFilterTest.java
new file mode 100644
index 0000000..e17f4d1
--- /dev/null
+++ b/core-metadata/src/test/java/org/apache/kylin/metadata/filter/TupleFilterTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.kylin.metadata.filter;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TupleFilterTest {
+    @Test
+    // true ==> true
+    public void removeNotTest0() {
+        TupleFilter constFilter = ConstantTupleFilter.TRUE;
+        Assert.assertEquals(constFilter, constFilter.removeNot());
+    }
+
+    @Test
+    // Not(true) ==> false
+    public void removeNotTest1() {
+        TupleFilter notFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.NOT);
+        notFilter.addChild(ConstantTupleFilter.TRUE);
+        Assert.assertEquals(ConstantTupleFilter.FALSE, notFilter.removeNot());
+    }
+
+    @Test
+    // Not(And(true, false)) ==> Or(false, true)
+    public void removeNotTest2() {
+        TupleFilter notFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.NOT);
+        TupleFilter andFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.AND);
+        andFilter.addChildren(ConstantTupleFilter.TRUE, ConstantTupleFilter.FALSE);
+        notFilter.addChild(andFilter);
+
+        TupleFilter orFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.OR);
+        orFilter.addChildren(ConstantTupleFilter.FALSE, ConstantTupleFilter.TRUE);
+        Assert.assertEquals(orFilter, notFilter.removeNot());
+    }
+
+    @Test
+    // And(Not(true), false) ==> And(false, false)
+    public void removeNotTest3() {
+        TupleFilter andFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.AND);
+        TupleFilter notFilter = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.NOT);
+        notFilter.addChild(ConstantTupleFilter.TRUE);
+        andFilter.addChildren(notFilter, ConstantTupleFilter.FALSE);
+
+        TupleFilter andFilter2 = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.AND);
+        andFilter2.addChildren(ConstantTupleFilter.FALSE, ConstantTupleFilter.FALSE);
+        Assert.assertEquals(andFilter2, andFilter.removeNot());
+    }
+}


Mime
View raw message