Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge from main #216

Merged
merged 20 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add unsigned data to jdbc
  • Loading branch information
sheyanjie-qq committed Jan 18, 2025
commit 585686f189f7cd9cc6e5e7d46edcf267f740366c
13 changes: 9 additions & 4 deletions src/main/java/com/taosdata/jdbc/TSDBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ public abstract class TSDBConstants {

// precision for data types, this is used for metadata
public static final int BOOLEAN_PRECISION = 1;
public static final int TINYINT_PRECISION = 4;
public static final int SMALLINT_PRECISION = 6;
public static final int INT_PRECISION = 11;
public static final int BIGINT_PRECISION = 20;
public static final int TINYINT_PRECISION = 3;
public static final int UNSIGNED_TINYINT_PRECISION = 3;
public static final int SMALLINT_PRECISION = 5;
public static final int UNSIGNED_SMALLINT_PRECISION = 5;

public static final int INT_PRECISION = 10;
public static final int UNSIGNED_INT_PRECISION = 10;
public static final int BIGINT_PRECISION = 19;
public static final int UNSIGNED_BIGINT_PRECISION = 20;
public static final int FLOAT_PRECISION = 12;
public static final int DOUBLE_PRECISION = 22;
public static final int TIMESTAMP_MS_PRECISION = 23;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -464,6 +465,11 @@ public void setTagLong(int index, long value) {
this.tagValueLength += Long.BYTES;
}

@Override
public void setTagBigInteger(int index, BigInteger value) throws SQLException {
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}

@Override
public void setTagTimestamp(int index, long value) {
ensureTagCapacity(index);
Expand Down Expand Up @@ -564,6 +570,11 @@ public void setLong(int columnIndex, List<Long> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BIGINT, Long.BYTES);
}

@Override
public void setBigInteger(int columnIndex, List<BigInteger> list) throws SQLException{
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
}

@Override
public void setDouble(int columnIndex, List<Double> list) throws SQLException {
setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_DOUBLE, Double.BYTES);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/taosdata/jdbc/TaosPrepareStatement.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.taosdata.jdbc;

import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand All @@ -21,6 +22,8 @@ public interface TaosPrepareStatement extends PreparedStatement {

void setTagLong(int index, long value);

void setTagBigInteger(int index, BigInteger value) throws SQLException;

void setTagFloat(int index, float value);

void setTagDouble(int index, double value);
Expand All @@ -43,6 +46,7 @@ public interface TaosPrepareStatement extends PreparedStatement {
void setTimestamp(int columnIndex, List<Long> list) throws SQLException;

void setLong(int columnIndex, List<Long> list) throws SQLException;
void setBigInteger(int columnIndex, List<BigInteger> list) throws SQLException;

void setDouble(int columnIndex, List<Double> list) throws SQLException;

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/com/taosdata/jdbc/common/DataLengthCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ public class DataLengthCfg {
4,//TSDB_DATA_TYPE_FLOAT
8,//TSDB_DATA_TYPE_DOUBLE
null,
8//TSDB_DATA_TYPE_TIMESTAMP
};
8, //TSDB_DATA_TYPE_TIMESTAMP
null,
1, //TSDB_DATA_TYPE_UTINYINT
2, //TSDB_DATA_TYPE_USMALLINT
4, //TSDB_DATA_TYPE_UINT
8 //TSDB_DATA_TYPE_UBIGINT
};

public static Integer getDataLength(int dataType){
if (dataType < dataLenArr.length){
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/taosdata/jdbc/common/SerializeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.SQLException;
import java.time.Instant;
import java.time.OffsetDateTime;
Expand Down Expand Up @@ -175,6 +176,23 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UTINYINT: {
SerializeInt(buf, offset, objectList.size());
offset += Integer.BYTES;

for (Object o: objectList){
if (o == null) {
buf[offset++] = 0;
} else {
short v = (Short) o;
if (v < 0 || v > 255){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "utinyint value is out of range");
}
buf[offset++] = (byte) (v & 0xFF);
}
}
break;
}
case TSDB_DATA_TYPE_SMALLINT: {
SerializeInt(buf, offset, objectList.size() * Short.BYTES);
offset += Integer.BYTES;
Expand All @@ -190,6 +208,25 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_USMALLINT: {
SerializeInt(buf, offset, objectList.size() * Short.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
int v = (Integer) o;
if (v < 0 || v > 65535){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "usmallint value is out of range");
}
SerializeShort(buf, offset, (short) (v & 0xFFFF));
} else {
SerializeShort(buf, offset, (short)0);
}

offset += Short.BYTES;
}
break;
}
case TSDB_DATA_TYPE_INT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;
Expand All @@ -204,6 +241,24 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UINT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
long v = (Long) o;
if (v < 0 || v > 4294967295L){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "uint value is out of range");
}
SerializeInt(buf, offset, (int) (v & 0xFFFFFFFFL));
} else {
SerializeInt(buf, offset, 0);
}
offset += Integer.BYTES;
}
break;
}
case TSDB_DATA_TYPE_BIGINT: {
SerializeInt(buf, offset, objectList.size() * Long.BYTES);
offset += Integer.BYTES;
Expand All @@ -218,6 +273,24 @@ private static void SerializeNormalDataType(int dataType , byte[] buf, int offse
}
break;
}
case TSDB_DATA_TYPE_UBIGINT: {
SerializeInt(buf, offset, objectList.size() * Long.BYTES);
offset += Integer.BYTES;

for (Object o: objectList){
if (o != null) {
BigInteger v = (BigInteger) o;
if (v.compareTo(BigInteger.ZERO) < 0 || v.compareTo(new BigInteger("18446744073709551615")) > 0){
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE, "ubigint value is out of range");
}
SerializeLong(buf, offset, v.longValue());
} else {
SerializeLong(buf, offset, 0L);
}
offset += Long.BYTES;
}
break;
}
case TSDB_DATA_TYPE_FLOAT: {
SerializeInt(buf, offset, objectList.size() * Integer.BYTES);
offset += Integer.BYTES;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/taosdata/jdbc/enums/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
public enum DataType {
NULL("NULL", Types.NULL, TSDB_DATA_TYPE_NULL, 0),
BOOL("BOOL", Types.BOOLEAN, TSDB_DATA_TYPE_BOOL, BOOLEAN_PRECISION),

TINYINT("TINYINT", Types.TINYINT, TSDB_DATA_TYPE_TINYINT, TINYINT_PRECISION),
UTINYINT("TINYINT UNSIGNED", Types.TINYINT, TSDB_DATA_TYPE_UTINYINT, TINYINT_PRECISION),
USMALLINT("SMALLINT UNSIGNED", Types.SMALLINT, TSDB_DATA_TYPE_USMALLINT, SMALLINT_PRECISION),
UTINYINT("TINYINT UNSIGNED", Types.SMALLINT, TSDB_DATA_TYPE_UTINYINT, UNSIGNED_TINYINT_PRECISION),
USMALLINT("SMALLINT UNSIGNED", Types.INTEGER, TSDB_DATA_TYPE_USMALLINT, UNSIGNED_SMALLINT_PRECISION),
SMALLINT("SMALLINT", Types.SMALLINT, TSDB_DATA_TYPE_SMALLINT, SMALLINT_PRECISION),
UINT("INT UNSIGNED", Types.INTEGER, TSDB_DATA_TYPE_UINT, INT_PRECISION),
UINT("INT UNSIGNED", Types.BIGINT, TSDB_DATA_TYPE_UINT, UNSIGNED_INT_PRECISION),
INT("INT", Types.INTEGER, TSDB_DATA_TYPE_INT, INT_PRECISION),
UBIGINT("BIGINT UNSIGNED", Types.BIGINT, TSDB_DATA_TYPE_UBIGINT, BIGINT_PRECISION),
UBIGINT("BIGINT UNSIGNED", Types.OTHER, TSDB_DATA_TYPE_UBIGINT, UNSIGNED_BIGINT_PRECISION),
BIGINT("BIGINT", Types.BIGINT, TSDB_DATA_TYPE_BIGINT, BIGINT_PRECISION),
FLOAT("FLOAT", Types.FLOAT, TSDB_DATA_TYPE_FLOAT, FLOAT_PRECISION),
DOUBLE("DOUBLE", Types.DOUBLE, TSDB_DATA_TYPE_DOUBLE, DOUBLE_PRECISION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
Expand Down Expand Up @@ -136,7 +137,11 @@ public V deserialize(ResultSet data, String topic, String dbName) throws Deseria
} else if (param.clazz.isAssignableFrom(BigDecimal.class)){
BigDecimal bigDecimal = data.getBigDecimal(param.name);
param.method.invoke(t, data.wasNull() ? null : bigDecimal);
} else if (param.clazz.isAssignableFrom(BigInteger.class)){
BigInteger bigInteger = (BigInteger) data.getObject(param.name);
param.method.invoke(t, data.wasNull() ? null : bigInteger);
}

} catch (IllegalAccessException | InvocationTargetException e) {
throw new SQLException(this.getClass().getSimpleName() + ": " + param.name
+ " through method:" + param.method.getName() +" get Data error: ", e);
Expand Down
40 changes: 24 additions & 16 deletions src/main/java/com/taosdata/jdbc/utils/DataTypeConverUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static boolean getBoolean(int taosType, Object value) throws SQLDataExcep
case TSDB_DATA_TYPE_TIMESTAMP:
return ((Instant) value).toEpochMilli() == 0L ? Boolean.FALSE : Boolean.TRUE;
case TSDB_DATA_TYPE_UBIGINT:
return value.equals(new BigDecimal(0)) ? Boolean.FALSE : Boolean.TRUE;
return value.equals(BigInteger.ZERO) ? Boolean.FALSE : Boolean.TRUE;

case TSDB_DATA_TYPE_FLOAT:
return (((float) value) == 0) ? Boolean.FALSE : Boolean.TRUE;
Expand Down Expand Up @@ -108,8 +109,8 @@ public static byte getByte(int taosType, Object value, int columnIndex) throws S
return (byte) tmp;
}
case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Byte.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Byte.MAX_VALUE)) > 0)
BigInteger tmp = (BigInteger) value;
if (tmp.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) < 0 || tmp.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.TINYINT);

return tmp.byteValue();
Expand Down Expand Up @@ -170,8 +171,8 @@ public static short getShort(int taosType, Object value, int columnIndex) throws
return (short) tmp;
}
case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Short.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Short.MAX_VALUE)) > 0)
BigInteger tmp = (BigInteger) value;
if (tmp.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) < 0 || tmp.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.SMALLINT);
return tmp.shortValue();
}
Expand Down Expand Up @@ -227,8 +228,8 @@ public static int getInt(int taosType, Object value, int columnIndex) throws SQL
return (int) tmp;
}
case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Integer.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Integer.MAX_VALUE)) > 0)
BigInteger tmp = (BigInteger) value;
if (tmp.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 || tmp.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.INTEGER);
return tmp.intValue();
}
Expand Down Expand Up @@ -293,8 +294,8 @@ public static long getLong(int taosType, Object value, int columnIndex, int time
return (long) value;

case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Long.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Long.MAX_VALUE)) > 0)
BigInteger tmp = (BigInteger) value;
if (tmp.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0 || tmp.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.BIGINT);
return tmp.longValue();
}
Expand Down Expand Up @@ -350,9 +351,7 @@ public static float getFloat(int taosType, Object value, int columnIndex) throws
return (long) value;

case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Float.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Float.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.FLOAT);
BigInteger tmp = (BigInteger) value;
return tmp.floatValue();
}
case TSDB_DATA_TYPE_DOUBLE: {
Expand Down Expand Up @@ -400,9 +399,7 @@ public static double getDouble(int taosType, Object value, int columnIndex, int
return (long) value;

case TSDB_DATA_TYPE_UBIGINT: {
BigDecimal tmp = (BigDecimal) value;
if (tmp.compareTo(new BigDecimal(Double.MIN_VALUE)) < 0 || tmp.compareTo(new BigDecimal(Double.MAX_VALUE)) > 0)
throwRangeException(value.toString(), columnIndex, Types.DOUBLE);
BigInteger tmp = (BigInteger) value;
return tmp.doubleValue();
}

Expand Down Expand Up @@ -519,6 +516,8 @@ public static BigDecimal getBigDecimal(int taosType, Object value) {
case TSDB_DATA_TYPE_UINT:
case TSDB_DATA_TYPE_BIGINT:
return new BigDecimal((long) value);
case TSDB_DATA_TYPE_UBIGINT:
return new BigDecimal((BigInteger) value);

case TSDB_DATA_TYPE_FLOAT:
return BigDecimal.valueOf((float) value);
Expand Down Expand Up @@ -579,7 +578,16 @@ static public Object parseValue(int type, Object source){
}
case TSDB_DATA_TYPE_UBIGINT: {
long val = (long) source;
return parseUBigInt(val);
return new BigInteger(1, new byte[]{
(byte) (val >>> 56),
(byte) (val >>> 48),
(byte) (val >>> 40),
(byte) (val >>> 32),
(byte) (val >>> 24),
(byte) (val >>> 16),
(byte) (val >>> 8),
(byte) val
});
}
case TSDB_DATA_TYPE_NCHAR: {
int[] tmp = (int[]) source;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/taosdata/jdbc/ws/BlockResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.*;
import java.time.*;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -318,6 +319,8 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
return type.cast(((Number) value).floatValue());
} else if (type == BigDecimal.class && value instanceof Number) {
return type.cast(new BigDecimal(value.toString()));
} else if (type == BigInteger.class && value instanceof Number) {
return type.cast(new BigInteger(value.toString()));
} else if (type == Byte.class && value instanceof Number) {
return type.cast(((Number) value).byteValue());
} else if (type == LocalDateTime.class && value instanceof Instant) {
Expand Down
Loading
Loading