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



##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBRel.java
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.RelNode;
+
+public interface IoTDBRel extends RelNode {
+
+  void implement(Implementor implementor);
+
+  /**
+   * Calling convention for relational operations that occur in IoTDB.
+   */
+  Convention CONVENTION = new Convention.Impl("IOTDB", IoTDBRel.class);
+
+  /**
+   * Callback for the implementation process that converts a tree of {@link 
IoTDBRel} nodes into a
+   * IoTDB SQL query.
+   */
+  class Implementor {
+
+    final List<String> selectFields = new ArrayList<>();
+    final Map<String, String> deviceToFilterMap = new LinkedHashMap<>();
+    final List<String> globalPredicate = new ArrayList<>();
+    int limit = 0;
+    int offset = 0;
+
+    RelOptTable table;
+    IoTDBTable ioTDBTable;
+
+    /**
+     * Adds newly projected fields.
+     *
+     * @param fields New fields to be projected from a query
+     */
+    public void addFields(List<String> fields) {
+      if (selectFields != null) {
+        selectFields.addAll(fields);
+      }
+    }
+
+    /**
+     * Adds newly restricted devices and predicates.
+     *
+     * @param deviceToFilterMap predicate of given device
+     * @param predicates        global predicates to be applied to the query
+     */
+    public void add(Map<String, String> deviceToFilterMap, List<String> 
predicates) {
+      if (this.deviceToFilterMap != null) {

Review comment:
       You are right! I've removed it.

##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBTable.java
##########
@@ -0,0 +1,306 @@
+/*
+ * 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 com.google.common.collect.ImmutableList;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import org.apache.calcite.adapter.java.AbstractQueryableTable;
+import org.apache.calcite.linq4j.AbstractEnumerable;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.Enumerator;
+import org.apache.calcite.linq4j.QueryProvider;
+import org.apache.calcite.linq4j.Queryable;
+import org.apache.calcite.linq4j.function.Function1;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeImpl;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.rel.type.RelProtoDataType;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.TranslatableTable;
+import org.apache.calcite.schema.impl.AbstractTableQueryable;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.util.Util;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+
+public class IoTDBTable extends AbstractQueryableTable
+    implements TranslatableTable {
+
+  RelProtoDataType protoRowType;
+  private final IoTDBSchema schema;
+  private final String storageGroup;
+
+  public IoTDBTable(IoTDBSchema schema, String storageGroup) {
+    super(Object[].class);
+    this.schema = schema;
+    this.storageGroup = storageGroup;
+  }
+
+  public String toString() {
+    return "IoTDBTable {" + storageGroup + "}";
+  }
+
+  @Override
+  public RelDataType getRowType(RelDataTypeFactory typeFactory) {
+    try {
+      if (protoRowType == null) {
+        protoRowType = schema.getRelDataType(storageGroup);
+      }
+    } catch (SQLException | QueryProcessException e) {
+      // print exception error here
+      e.printStackTrace();
+    }
+    return protoRowType.apply(typeFactory);
+  }
+
+  @Override
+  public <T> Queryable<T> asQueryable(QueryProvider queryProvider, SchemaPlus 
schema,
+      String tableName) {
+    return new IoTDBQueryable<>(queryProvider, schema, this, storageGroup);
+  }
+
+  @Override
+  public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable 
relOptTable) {
+    final RelOptCluster cluster = context.getCluster();
+    return new IoTDBTableScan(cluster, cluster.traitSetOf(IoTDBRel.CONVENTION),
+        relOptTable, this, null);
+  }
+
+  public Enumerable<Object> query(final Connection connection) {
+    return query(connection, ImmutableList.of(), ImmutableList.of(), 
ImmutableList.of(),
+        ImmutableList.of(), 0, 0);
+  }
+
+  /**
+   * Executes a IoTDB SQL query.
+   *
+   * @param connection IoTDB connection
+   * @param fields     List of fields to project
+   * @return Enumerator of results
+   */
+  public Enumerable<Object> query(final Connection connection,
+      List<Map.Entry<String, Class>> fields,
+      final List<String> selectFields, final List<Map.Entry<String, String>> 
deviceToFilterList,
+      List<String> globalPredicates, final Integer limit, final Integer 
offset) {
+    // Build the type of the resulting row based on the provided fields
+    final RelDataTypeFactory typeFactory =
+        new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+    final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
+    final RelDataType rowType = getRowType(typeFactory);
+
+    Function1<String, Void> addField = fieldName -> {
+      SqlTypeName typeName =
+          rowType.getField(fieldName, true, false).getType().getSqlTypeName();
+      fieldInfo.add(fieldName, typeFactory.createSqlType(typeName))
+          .nullable(true);
+      return null;
+    };
+
+    if (selectFields.isEmpty()) {
+      for (Map.Entry<String, Class> field : fields) {
+        addField.apply(field.getKey());
+      }
+    } else {
+      for (String field : selectFields) {
+        if (field.startsWith("'")) {
+          fieldInfo.add(field, SqlTypeName.VARCHAR);
+        } else {
+          addField.apply(field);
+        }
+      }
+    }
+
+    final RelProtoDataType resultRowType = 
RelDataTypeImpl.proto(fieldInfo.build());
+
+    // Construct the list of fields to project
+    String selectString = "";
+    if (selectFields.isEmpty()) {
+      selectString = "*";
+    } else {
+      // delete the 'time', 'device' string in select list
+      // this has to be here rather than init "selectFields" otherwise the 
resultRowType will be wrong
+      StringBuilder selectBuilder = new StringBuilder();
+      for (int i = 0; i < selectFields.size(); i++) {
+        String selectField = selectFields.get(i);
+        if (selectField.equals(IoTDBConstant.DeviceColumn) || selectField
+            .equals(IoTDBConstant.TimeColumn)) {
+          continue;
+        } else {
+          selectBuilder.append(selectField);
+          if (i < selectFields.size() - 1) {
+            selectBuilder.append(", ");
+          }
+        }
+      }
+      selectString = selectBuilder.toString();
+      if (selectString.equals("")) {
+        selectString = "*";
+      }
+    }
+
+    List<String> queryList = new ArrayList<>();
+    Set<String> tmpDevices = new HashSet<>(); // to deduplicate in global query
+    // construct query by device
+    if (!deviceToFilterList.isEmpty()) {
+      for (Entry<String, String> deviceToFilter : deviceToFilterList) {
+        String fromClause = " FROM ";
+        fromClause += deviceToFilter.getKey();
+        tmpDevices.add(deviceToFilter.getKey());
+
+        String whereClause = "";
+        if (deviceToFilter.getValue() != null) {
+          whereClause = " WHERE ";
+          whereClause += deviceToFilter.getValue();
+        }
+
+        // Build and issue the query and return an Enumerator over the results
+        StringBuilder queryBuilder = new StringBuilder("SELECT ");
+        queryBuilder.append(selectString);
+        queryBuilder.append(fromClause);
+        queryBuilder.append(whereClause);
+
+        if (limit > 0) {
+          queryBuilder.append(" LIMIT " + limit);
+        }
+        if (offset > 0) {
+          queryBuilder.append(" OFFSET " + offset);
+        }
+
+        // append align by device
+        queryBuilder.append(" " + IoTDBConstant.AlignByDevice);
+        queryList.add(queryBuilder.toString());
+      }
+    }
+
+    // construct global query
+    if (deviceToFilterList.isEmpty() || !globalPredicates.isEmpty()) {
+      String fromClause = " FROM ";
+      // deduplicate redundant device
+      if (!deviceToFilterList.isEmpty() && !globalPredicates.isEmpty()) {

Review comment:
       Removed.




----------------------------------------------------------------
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