jt2594838 commented on a change in pull request #738: [IOTDB-396] New query 
clause: disable align
URL: https://github.com/apache/incubator-iotdb/pull/738#discussion_r366266157
 
 

 ##########
 File path: 
jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBNonAlignQueryResultSet.java
 ##########
 @@ -0,0 +1,1289 @@
+/*
+ * 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.jdbc;
+
+import org.apache.iotdb.rpc.IoTDBRPCException;
+import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.service.rpc.thrift.*;
+import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.utils.BytesUtils;
+import org.apache.iotdb.tsfile.utils.ReadWriteIOUtils;
+import org.apache.thrift.TException;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.net.URL;
+import java.nio.ByteBuffer;
+import java.sql.Date;
+import java.sql.*;
+import java.util.*;
+
+public class IoTDBNonAlignQueryResultSet implements ResultSet {
+
+  private static final String TIMESTAMP_STR = "Time";
+  private static final int TIMESTAMP_STR_LENGTH = 4;
+  private static final int START_INDEX = 2;
+  private static final String VALUE_IS_NULL = "The value got by %s (column 
name) is NULL.";
+  private static final String EMPTY_STR = "";
+  private Statement statement = null;
+  private String sql;
+  private SQLWarning warningChain = null;
+  private boolean isClosed = false;
+  private TSIService.Iface client = null;
+  private List<String> columnInfoList; // no deduplication
+  private List<String> columnTypeList; // no deduplication
+  private Map<String, Integer> columnInfoMap; // used because the server 
returns deduplicated columns
+  private List<String> columnTypeDeduplicatedList; // deduplicated from 
columnTypeList
+  private int fetchSize;
+  private boolean emptyResultSet = false;
+
+  private TSQueryNonAlignDataSet tsQueryNonAlignDataSet = null;
+  private byte[][] times; // used for disable align
+  private byte[][] values; // used to cache the current row record value
+
+  private long sessionId;
+  private long queryId;
+  private boolean ignoreTimeStamp = false;
+
+  public IoTDBNonAlignQueryResultSet() {
+    // do nothing
+  }
+
+  // for disable align clause
+  public IoTDBNonAlignQueryResultSet(Statement statement, List<String> 
columnNameList,
+                                     List<String> columnTypeList, boolean 
ignoreTimeStamp, TSIService.Iface client,
+                                     String sql, long queryId, long sessionId, 
TSQueryNonAlignDataSet dataset)
+          throws SQLException {
+    this.statement = statement;
+    this.fetchSize = statement.getFetchSize();
+    this.columnTypeList = columnTypeList;
+
+    times = new byte[columnNameList.size()][Long.BYTES];
+    values = new byte[columnNameList.size()][];
+
+    this.columnInfoList = new ArrayList<>();
+    // deduplicate and map
+    this.columnInfoMap = new HashMap<>();
+    this.columnInfoMap.put(TIMESTAMP_STR, 1);
+    this.columnTypeDeduplicatedList = new ArrayList<>();
+    int index = START_INDEX;
+    for (int i = 0; i < columnNameList.size(); i++) {
+      String name = columnNameList.get(i);
+      columnInfoList.add(TIMESTAMP_STR + name);
+      columnInfoList.add(name);
+      if (!columnInfoMap.containsKey(name)) {
+        columnInfoMap.put(name, index++);
+        columnTypeDeduplicatedList.add(columnTypeList.get(i));
+      }
+    }
+
+    this.ignoreTimeStamp = ignoreTimeStamp;
+    this.client = client;
+    this.sql = sql;
+    this.queryId = queryId;
+    this.tsQueryNonAlignDataSet = dataset;
+    this.sessionId = sessionId;
+  }
+
+  @Override
+  public boolean isWrapperFor(Class<?> iface) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public <T> T unwrap(Class<T> iface) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public boolean absolute(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void afterLast() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void beforeFirst() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void cancelRowUpdates() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void clearWarnings() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void close() throws SQLException {
+    if (isClosed) {
+      return;
+    }
+    if (client != null) {
+      try {
+        TSCloseOperationReq closeReq = new TSCloseOperationReq(sessionId);
+        closeReq.setQueryId(queryId);
+        TSStatus closeResp = client.closeOperation(closeReq);
+        RpcUtils.verifySuccess(closeResp);
+      } catch (IoTDBRPCException e) {
+        throw new SQLException("Error occurs for close opeation in server side 
becasuse " + e);
+      } catch (TException e) {
+        throw new SQLException(
+                "Error occurs when connecting to server for close operation, 
becasue: " + e);
+      }
+    }
+    client = null;
+    isClosed = true;
+  }
+
+
+  @Override
+  public void deleteRow() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public int findColumn(String columnName) {
+    return columnInfoMap.get(columnName);
+  }
+
+  @Override
+  public boolean first() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Array getArray(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Array getArray(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public InputStream getAsciiStream(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public InputStream getAsciiStream(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
+    return getBigDecimal(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public BigDecimal getBigDecimal(String columnName) throws SQLException {
+    return new BigDecimal(Objects.requireNonNull(getValueByName(columnName)));
+  }
+
+  @Override
+  public BigDecimal getBigDecimal(int columnIndex, int scale) throws 
SQLException {
+    MathContext mc = new MathContext(scale);
+    return getBigDecimal(columnIndex).round(mc);
+  }
+
+  @Override
+  public BigDecimal getBigDecimal(String columnName, int scale) throws 
SQLException {
+    return getBigDecimal(findColumn(columnName), scale);
+  }
+
+  @Override
+  public InputStream getBinaryStream(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public InputStream getBinaryStream(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Blob getBlob(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Blob getBlob(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public boolean getBoolean(int columnIndex) throws SQLException {
+    return getBoolean(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public boolean getBoolean(String columnName) throws SQLException {
+    checkRecord();
+    int index = columnInfoMap.get(columnName) - START_INDEX;
+    if (values[index] != null) {
+      return BytesUtils.bytesToBool(values[index]);
+    }
+    else {
+      throw new SQLException(String.format(VALUE_IS_NULL, columnName));
+    }
+  }
+
+  @Override
+  public byte getByte(int columnIndex) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public byte getByte(String columnName) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public byte[] getBytes(int columnIndex) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public byte[] getBytes(String columnName) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Reader getCharacterStream(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Reader getCharacterStream(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Clob getClob(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Clob getClob(String arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public int getConcurrency() {
+    return ResultSet.CONCUR_READ_ONLY;
+  }
+
+  @Override
+  public String getCursorName() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Date getDate(int columnIndex) throws SQLException {
+    return new Date(getLong(columnIndex));
+  }
+
+  @Override
+  public Date getDate(String columnName) throws SQLException {
+    return getDate(findColumn(columnName));
+  }
+
+  @Override
+  public Date getDate(int arg0, Calendar arg1) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public Date getDate(String arg0, Calendar arg1) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public double getDouble(int columnIndex) throws SQLException {
+    return getDouble(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public double getDouble(String columnName) throws SQLException {
+    checkRecord();
+    int index = columnInfoMap.get(columnName) - START_INDEX;
+    if (values[index] != null) {
+      return BytesUtils.bytesToDouble(values[index]);
+    } else {
+      throw new SQLException(String.format(VALUE_IS_NULL, columnName));
+    }
+  }
+
+  @Override
+  public int getFetchDirection() throws SQLException {
+    return ResultSet.FETCH_FORWARD;
+  }
+
+  @Override
+  public void setFetchDirection(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public int getFetchSize() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public void setFetchSize(int arg0) throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public float getFloat(int columnIndex) throws SQLException {
+    return getFloat(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public float getFloat(String columnName) throws SQLException {
+    checkRecord();
+    int index = columnInfoMap.get(columnName) - START_INDEX;
+    if (values[index] != null) {
+      return BytesUtils.bytesToFloat(values[index]);
+    } else {
+      throw new SQLException(String.format(VALUE_IS_NULL, columnName));
+    }
+  }
+
+  @Override
+  public int getHoldability() throws SQLException {
+    throw new SQLException(Constant.METHOD_NOT_SUPPORTED);
+  }
+
+  @Override
+  public int getInt(int columnIndex) throws SQLException {
+    return getInt(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public int getInt(String columnName) throws SQLException {
+    checkRecord();
+    int index = columnInfoMap.get(columnName) - START_INDEX;
+    if (values[index] != null) {
+      return BytesUtils.bytesToInt(values[index]);
+    } else {
+      throw new SQLException(String.format(VALUE_IS_NULL, columnName));
+    }
+  }
+
+  @Override
+  public long getLong(int columnIndex) throws SQLException {
+    return getLong(findColumnNameByIndex(columnIndex));
+  }
+
+  @Override
+  public long getLong(String columnName) throws SQLException {
+    checkRecord();
+    if (columnName.startsWith(TIMESTAMP_STR)) {
+      String column = columnName.substring(TIMESTAMP_STR_LENGTH);
+      int index = columnInfoMap.get(column) - START_INDEX;
+      return BytesUtils.bytesToLong(times[index]);
+    }
 
 Review comment:
   I have to say, your current way of dealing with null values is not 
satisfactory, maybe a bitmap will help.
   
![image](https://user-images.githubusercontent.com/23610645/72337333-5b068080-36fd-11ea-99d7-533864091a56.png)
   

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


With regards,
Apache Git Services

Reply via email to