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



##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBSchema.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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.ImmutableMap;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+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.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.jdbc.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IoTDBSchema extends AbstractSchema {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(IoTDBSchema.class);
+  final Connection connection;
+  private Map<String, Table> tableMap;
+  private final SchemaPlus parentSchema;
+  final String name;
+  public static Map<String, List<String>> sgToDeviceMap = new HashMap<>();
+
+  /**
+   * Creates a IoTDB schema.
+   *
+   * @param host     IoTDB host, e.g. "localhost"
+   * @param port     IoTDB port, e.g. 6667
+   * @param username IoTDB username
+   * @param password IoTDB password
+   */
+  public IoTDBSchema(String host, int port, String username, String password,
+      SchemaPlus parentSchema, String name) {
+    super();
+    try {
+      Class.forName(Config.JDBC_DRIVER_NAME);
+      this.connection = DriverManager
+          .getConnection(Config.IOTDB_URL_PREFIX + host + ":" + port + "/", 
username, password);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+
+    this.parentSchema = parentSchema;
+    this.name = name;
+  }
+
+  /**
+   * Generate the columns' names and data types in the given table.
+   * @param storageGroup the table name
+   * @return the columns' names and data types
+   */
+  RelProtoDataType getRelDataType(String storageGroup) throws SQLException, 
QueryProcessException {

Review comment:
       I would pass the RelDataTypeFactory here via Parameter (from calling 
IoTDBTable and not create a new one). WDYT?

##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBSchema.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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.ImmutableMap;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+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.Table;
+import org.apache.calcite.schema.impl.AbstractSchema;
+import org.apache.calcite.sql.type.SqlTypeFactoryImpl;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.jdbc.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class IoTDBSchema extends AbstractSchema {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(IoTDBSchema.class);
+  final Connection connection;
+  private Map<String, Table> tableMap;
+  private final SchemaPlus parentSchema;
+  final String name;
+  public static Map<String, List<String>> sgToDeviceMap = new HashMap<>();
+
+  /**
+   * Creates a IoTDB schema.
+   *
+   * @param host     IoTDB host, e.g. "localhost"
+   * @param port     IoTDB port, e.g. 6667
+   * @param username IoTDB username
+   * @param password IoTDB password
+   */
+  public IoTDBSchema(String host, int port, String username, String password,
+      SchemaPlus parentSchema, String name) {
+    super();
+    try {
+      Class.forName(Config.JDBC_DRIVER_NAME);
+      this.connection = DriverManager
+          .getConnection(Config.IOTDB_URL_PREFIX + host + ":" + port + "/", 
username, password);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+
+    this.parentSchema = parentSchema;
+    this.name = name;
+  }
+
+  /**
+   * Generate the columns' names and data types in the given table.
+   * @param storageGroup the table name
+   * @return the columns' names and data types
+   */
+  RelProtoDataType getRelDataType(String storageGroup) throws SQLException, 
QueryProcessException {

Review comment:
       A rather general question about the data model itself. Generally, if i 
understand it correctly we map the whole storage group to a single table where 
we have one column for the device and then unroll all possible sensors of each 
device as a column, is that right?
   And if so, this could lead to issues in cases where I have two devices which 
have the same sensors but with different types (see Line 119 here). This could 
be avoided in situations where the query is something like
   `SELECT * FROM 'root.vehicles' where device = 'mycar'` as we only have this 
single device there. What do you think, should we, perhaps in a later PR try to 
push the query further down to to evaluate the `RelDataType` with further 
information to avoid the situation descibed above?

##########
File path: calcite/src/main/java/org/apache/iotdb/calcite/IoTDBRules.java
##########
@@ -0,0 +1,192 @@
+/*
+ * 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.List;
+import org.apache.calcite.adapter.enumerable.EnumerableLimit;
+import org.apache.calcite.adapter.java.JavaTypeFactory;
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelTraitSet;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.convert.ConverterRule;
+import org.apache.calcite.rel.logical.LogicalFilter;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
+import org.apache.iotdb.db.exception.query.LogicalOptimizeException;
+
+/**
+ * Rules and relational operators for {@link IoTDBRel#CONVENTION} calling 
convention.
+ */
+public class IoTDBRules {
+
+  private IoTDBRules() {
+  }
+
+  public static final RelOptRule[] RULES = {
+      IoTDBFilterRule.INSTANCE,
+      IoTDBProjectRule.INSTANCE,

Review comment:
       I am very unshure about this Rule or, the use of a custom Project Rule 
in general. By default Calcite will transform a LogicalProject into 
EnumerableCalc Rule which especially implements all Rex-Operations (like 
addition, substraction, ...).
   
   In the current Implementation the `IoTDBProject` implements the default 
`RexVisitorImpl` which translates any relational expression to its first 
operand. So, e.g. `a + b` which is the REX `+(a, b)` will simply be transformed 
to `a`.
   
   If we remove this rule by commenting out L51 Calcite will use the default 
`EnumerableCalc` and Rex Expressions will work as expected. You can easily see 
that by adding a test case like the following:
   
   ```
   @Test
     public void testProject() {
       CalciteAssert.that()
           .with(MODEL)
           .with("UnQuotedCasing", IoTDBConstant.UNQUOTED_CASING)
           .query("select *, s0 + s1 as test from \"root.vehicle\" " +
               "where s0 <= 10")
           .limit(1)
           .returns("time=1000; device=root.vehicle.d1; s0=10; s1=5; s2=null; 
s3=thousand; s4=null; test=15\n")
           .explainContains("PLAN=EnumerableCalc(expr#0..6=[{inputs}], 
expr#7=[+($t2, $t3)], proj#0..7=[{exprs}])\n" +
               "  IoTDBToEnumerableConverter\n" +
               "    IoTDBFilter(condition=[<=($2, 10)])\n" +
               "      IoTDBTableScan(table=[[IoTDBSchema, root.vehicle]])");
     }
   ```
   
   If the IotDBProjectRule is active, then this will give the wrong result 5 
(only s1), without this RULE (commenting out L51) this will give the correct 
result 15 and the test will pass.
   
   Is there a specific reason why you decided to implement the Project Rel 
explicitly? We could do this partial at least to push down aggregate queries of 
course but I would do this in a second step as I personally consider rex 
expressions "more important" so to say.
   
   WDYT?




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


Reply via email to