You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/06/22 15:15:00 UTC

[GitHub] [iotdb] mzp0514 commented on a change in pull request #3162: [IOTDB-1143] Continuous query

mzp0514 commented on a change in pull request #3162:
URL: https://github.com/apache/iotdb/pull/3162#discussion_r656322435



##########
File path: server/src/main/java/org/apache/iotdb/db/cq/ContinuousQuery.java
##########
@@ -0,0 +1,276 @@
+/*
+ * 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.db.cq;
+
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.exception.metadata.MetadataException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.Planner;
+import org.apache.iotdb.db.qp.executor.PlanExecutor;
+import org.apache.iotdb.db.qp.logical.crud.GroupByClauseComponent;
+import org.apache.iotdb.db.qp.logical.crud.QueryOperator;
+import org.apache.iotdb.db.qp.logical.crud.SelectComponent;
+import org.apache.iotdb.db.qp.physical.crud.GroupByTimePlan;
+import org.apache.iotdb.db.qp.physical.crud.InsertTabletPlan;
+import org.apache.iotdb.db.qp.physical.sys.CreateContinuousQueryPlan;
+import org.apache.iotdb.db.query.context.QueryContext;
+import org.apache.iotdb.db.query.control.QueryResourceManager;
+import org.apache.iotdb.db.utils.TypeInferenceUtils;
+import org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.common.Field;
+import org.apache.iotdb.tsfile.read.common.Path;
+import org.apache.iotdb.tsfile.read.common.RowRecord;
+import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class ContinuousQuery implements Runnable {
+
+  private static final Logger logger = LoggerFactory.getLogger(ContinuousQuery.class);
+
+  private final PlanExecutor planExecutor;
+  private final CreateContinuousQueryPlan plan;
+  private final Planner planner;
+
+  public ContinuousQuery(CreateContinuousQueryPlan plan) throws QueryProcessException {
+    this.plan = plan;
+    this.planExecutor = new PlanExecutor();
+    this.planner = new Planner();
+  }
+
+  @Override
+  public void run() {
+
+    try {
+
+      GroupByTimePlan queryPlan = getQueryPlan();
+
+      if (queryPlan.getDeduplicatedPaths().isEmpty()) {
+        logger.error(plan.getContinuousQueryName() + ": deduplicated paths empty");
+        return;
+      }
+
+      QueryDataSet result = doQuery(queryPlan);
+
+      if (result == null) {
+        logger.error(plan.getContinuousQueryName() + ": query result empty");
+        return;
+      }
+
+      doInsert(result, queryPlan);
+
+    } catch (Exception e) {
+      logger.error(plan.getContinuousQueryName() + ": run error", e);
+    }
+  }
+
+  private GroupByTimePlan getQueryPlan() throws QueryProcessException {
+
+    QueryOperator queryOperator = plan.getQueryOperator();
+
+    SelectComponent selectComponentCopy = new SelectComponent(queryOperator.getSelectComponent());
+
+    GroupByTimePlan queryPlan = planner.operatorToPhysicalPlan(queryOperator, 1024);
+
+    queryOperator.setSelectComponent(selectComponentCopy);
+
+    long timestamp = System.currentTimeMillis();
+    queryPlan.setStartTime(timestamp - plan.getForInterval());
+    queryPlan.setEndTime(timestamp);
+
+    return queryPlan;
+  }
+
+  private QueryDataSet doQuery(GroupByTimePlan queryPlan)
+      throws StorageEngineException, QueryFilterOptimizationException, MetadataException,
+          IOException, InterruptedException, QueryProcessException {
+    long queryId =
+        QueryResourceManager.getInstance()
+            .assignQueryId(true, 1024, queryPlan.getDeduplicatedPaths().size());
+
+    QueryDataSet result;
+    result = planExecutor.processQuery(queryPlan, new QueryContext(queryId));
+    QueryResourceManager.getInstance().endQuery(queryId);
+    return result;
+  }
+
+  private void doInsert(QueryDataSet result, GroupByTimePlan queryPlan)
+      throws QueryProcessException, IOException, IllegalPathException {
+
+    int columnSize = result.getDataTypes().size();
+    TSDataType dataType =
+        TypeInferenceUtils.getAggrDataType(
+            queryPlan.getAggregations().get(0), queryPlan.getDataTypes().get(0));
+
+    InsertTabletPlan[] insertTabletPlans = getInsertTabletPlans(columnSize, result, dataType);
+
+    int fetchSize =
+        (int)
+            Math.min(
+                10,

Review comment:
       if empty query, rowNum will be 0 < batchSize




-- 
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:
users@infra.apache.org