Alima777 commented on a change in pull request #902:
URL: https://github.com/apache/incubator-iotdb/pull/902#discussion_r487762031



##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBFilter.java
##########
@@ -0,0 +1,321 @@
+/*
+ * 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.iotdb.calcite;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.util.Util;
+import org.apache.iotdb.db.exception.query.LogicalOptimizeException;
+import org.apache.iotdb.db.qp.constant.SQLConstant;
+import org.apache.iotdb.db.qp.logical.crud.BasicFunctionOperator;
+import org.apache.iotdb.db.qp.logical.crud.FilterOperator;
+import org.apache.iotdb.db.qp.strategy.optimizer.DnfFilterOptimizer;
+import org.apache.iotdb.tsfile.read.common.Path;
+
+public class IoTDBFilter extends Filter implements IoTDBRel {
+
+  private final FilterOperator filterOperator;
+  private final List<String> fieldNames;
+  private List<String> predicates;  // for global predicate
+  Map<String, String> deviceToFilterMap = new LinkedHashMap<>();  // for 
device predicate
+
+  protected IoTDBFilter(RelOptCluster cluster, RelTraitSet traits, RelNode 
child, RexNode condition)
+      throws LogicalOptimizeException {
+    super(cluster, traits, child, condition);
+    this.fieldNames = IoTDBRules.IoTDBFieldNames(getRowType());
+    this.filterOperator = getIoTDBOperator(condition);
+    this.predicates = translateWhere(filterOperator);
+
+    // add global predicate to each device if both global and device predicate 
exist
+    if (!this.predicates.isEmpty() && !this.deviceToFilterMap.isEmpty()) {
+      for (String device : deviceToFilterMap.keySet()) {
+        StringBuilder builder = new 
StringBuilder(this.deviceToFilterMap.get(device));
+        builder.append(Util.toString(predicates, " OR ", " OR ", ""));
+        this.deviceToFilterMap.put(device, builder.toString());
+      }
+    }
+    assert getConvention() == IoTDBRel.CONVENTION;
+    assert getConvention() == child.getConvention();
+  }
+
+  @Override
+  public RelOptCost computeSelfCost(RelOptPlanner planner,
+      RelMetadataQuery mq) {
+    return super.computeSelfCost(planner, mq).multiplyBy(0.1);
+  }
+
+  @Override
+  public Filter copy(RelTraitSet relTraitSet, RelNode input, RexNode 
condition) {
+    try {
+      return new IoTDBFilter(getCluster(), traitSet, input, condition);
+    } catch (LogicalOptimizeException e) {
+      throw new AssertionError(e.getMessage());
+    }
+  }
+
+  @Override
+  public void implement(Implementor implementor) {
+    implementor.visitChild(0, getInput());
+    implementor.add(deviceToFilterMap, predicates);
+  }
+
+  /**
+   * Translates {@link RexNode} expressions into IoTDB filter operator.
+   */
+  private FilterOperator getIoTDBOperator(RexNode filter) {
+    switch (filter.getKind()) {
+      case EQUALS:
+        return getBasicOperator(SQLConstant.EQUAL, (RexCall) filter);
+      case NOT_EQUALS:
+        return getBasicOperator(SQLConstant.NOTEQUAL, (RexCall) filter);
+      case GREATER_THAN:
+        return getBasicOperator(SQLConstant.GREATERTHAN, (RexCall) filter);
+      case GREATER_THAN_OR_EQUAL:
+        return getBasicOperator(SQLConstant.GREATERTHANOREQUALTO, (RexCall) 
filter);
+      case LESS_THAN:
+        return getBasicOperator(SQLConstant.LESSTHAN, (RexCall) filter);
+      case LESS_THAN_OR_EQUAL:
+        return getBasicOperator(SQLConstant.LESSTHANOREQUALTO, (RexCall) 
filter);
+      case AND:
+        return getBinaryOperator(SQLConstant.KW_AND, ((RexCall) 
filter).getOperands());
+      case OR:
+        return getBinaryOperator(SQLConstant.KW_OR, ((RexCall) 
filter).getOperands());
+      default:
+        throw new AssertionError("cannot get IoTDBOperator from " + filter);

Review comment:
       I just used it for convenience before...Sorry, I've added a new 
exception for it.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to