Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
apiv("com.google.protobuf:protobuf-java", "protobuf")
apiv("com.h2database:h2")
apiv("javax.servlet:javax.servlet-api", "servlet")
apiv("org.jooq:joou-java-6", "joou-java-6")
apiv("junit:junit")
apiv("net.bytebuddy:byte-buddy", "bytebuddy")
apiv("net.bytebuddy:byte-buddy-agent", "bytebuddy")
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
api("com.fasterxml.jackson.core:jackson-annotations")
api("com.fasterxml.jackson.core:jackson-databind")
api("com.google.protobuf:protobuf-java")
api("org.jooq:joou-java-6")
implementation("com.fasterxml.jackson.core:jackson-core")
implementation("org.apache.httpcomponents.client5:httpclient5")
implementation("org.apache.httpcomponents.core5:httpcore5")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public ResultSetMetaData getMetaData() throws SQLException {
public Object getObject(int columnIndex) throws SQLException {
final Cursor.Accessor accessor = getAccessor(columnIndex);
final ColumnMetaData metaData = columnMetaDataList.get(columnIndex - 1);
return AvaticaSite.get(accessor, metaData.type.id, localCalendar);
return AvaticaSite.get(accessor, metaData.type.id, metaData.signed, localCalendar);
}

public Object getObject(String columnLabel) throws SQLException {
Expand Down
14 changes: 13 additions & 1 deletion core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void setObject(Object x, int targetSqlType) {

/** Similar logic to {@link #setObject}. */
public static Object get(Cursor.Accessor accessor, int targetSqlType,
Calendar localCalendar) throws SQLException {
boolean signed, Calendar localCalendar) throws SQLException {
switch (targetSqlType) {
case Types.CLOB:
case Types.DATALINK:
Expand All @@ -304,6 +304,9 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
case Types.ARRAY:
return accessor.getArray();
case Types.BIGINT:
if (!signed) {
return accessor.wasNull() ? null : accessor.getULong();
}
final long aLong = accessor.getLong();
if (aLong == 0 && accessor.wasNull()) {
return null;
Expand Down Expand Up @@ -337,6 +340,9 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
}
return aDouble;
case Types.INTEGER:
if (!signed) {
return accessor.wasNull() ? null : accessor.getUInt();
}
final int anInt = accessor.getInt();
if (anInt == 0 && accessor.wasNull()) {
return null;
Expand All @@ -362,6 +368,9 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
case Types.ROWID:
throw notImplemented();
case Types.SMALLINT:
if (!signed) {
return accessor.wasNull() ? null : accessor.getUShort();
}
final short aShort = accessor.getShort();
if (aShort == 0 && accessor.wasNull()) {
return null;
Expand All @@ -372,6 +381,9 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
case Types.TIMESTAMP:
return accessor.getTimestamp(localCalendar);
case Types.TINYINT:
if (!signed) {
return accessor.wasNull() ? null : accessor.getUByte();
}
final byte aByte = accessor.getByte();
if (aByte == 0 && accessor.wasNull()) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.protobuf.Descriptors.FieldDescriptor;

import org.joou.UByte;
import org.joou.UInteger;
import org.joou.ULong;
import org.joou.UShort;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Array;
Expand Down Expand Up @@ -327,10 +332,14 @@ public enum Rep {
PRIMITIVE_DOUBLE(double.class, Types.DOUBLE),
BOOLEAN(Boolean.class, Types.BOOLEAN),
BYTE(Byte.class, Types.TINYINT),
UBYTE(UByte.class, Types.TINYINT),
CHARACTER(Character.class, Types.CHAR),
SHORT(Short.class, Types.SMALLINT),
USHORT(UShort.class, Types.SMALLINT),
INTEGER(Integer.class, Types.INTEGER),
UINTEGER(UInteger.class, Types.INTEGER),
LONG(Long.class, Types.BIGINT),
ULONG(ULong.class, Types.BIGINT),
FLOAT(Float.class, Types.FLOAT),
DOUBLE(Double.class, Types.DOUBLE),
JAVA_SQL_TIME(Time.class, Types.TIME),
Expand Down
16 changes: 13 additions & 3 deletions core/src/main/java/org/apache/calcite/avatica/MetaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ public static ColumnMetaData columnMetaData(String name, int index,

public static ColumnMetaData columnMetaData(String name, int index, AvaticaType type,
boolean columnNullable) {
return columnMetaData(name, index, type, intForColumnNullable(columnNullable));
return columnMetaData(name, index, type, intForColumnNullable(columnNullable), true);
}

public static ColumnMetaData columnMetaData(String name, int index, AvaticaType type,
boolean columnNullable, boolean signed) {
return columnMetaData(name, index, type, intForColumnNullable(columnNullable), signed);
}

public static ColumnMetaData columnMetaData(String name, int index,
Expand All @@ -231,15 +236,20 @@ public static ColumnMetaData columnMetaData(String name, int index,
ColumnMetaData.Rep.VALUE_MAP.get(type);
ColumnMetaData.AvaticaType scalarType =
ColumnMetaData.scalar(pair.sqlType, pair.sqlTypeName, rep);
return columnMetaData(name, index, scalarType, columnNullable);
return columnMetaData(name, index, scalarType, columnNullable, true);
}

public static ColumnMetaData columnMetaData(String name, int index, AvaticaType type,
int columnNullable) {
return columnMetaData(name, index, type, columnNullable, true);
}

public static ColumnMetaData columnMetaData(String name, int index, AvaticaType type,
int columnNullable, boolean signed) {
return new ColumnMetaData(
index, false, true, false, false,
columnNullable,
true, -1, name, name, null,
signed, -1, name, name, null,
0, 0, null, null, type, true, false, false,
type.columnClassName());
}
Expand Down
120 changes: 116 additions & 4 deletions core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ColumnMetaData;

import org.joou.UByte;
import org.joou.UInteger;
import org.joou.ULong;
import org.joou.UShort;

import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -102,13 +107,13 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,
}
switch (columnMetaData.type.id) {
case Types.TINYINT:
return new ByteAccessor(getter);
return columnMetaData.signed ? new ByteAccessor(getter) : new UByteAccessor(getter);
case Types.SMALLINT:
return new ShortAccessor(getter);
return columnMetaData.signed ? new ShortAccessor(getter) : new UShortAccessor(getter);
case Types.INTEGER:
return new IntAccessor(getter);
return columnMetaData.signed ? new IntAccessor(getter) : new UIntAccessor(getter);
case Types.BIGINT:
return new LongAccessor(getter);
return columnMetaData.signed ? new LongAccessor(getter) : new ULongAccessor(getter);
case Types.BOOLEAN:
case Types.BIT:
return new BooleanAccessor(getter);
Expand Down Expand Up @@ -288,18 +293,38 @@ public byte getByte() throws SQLException {
return (byte) getLong();
}

@Override
public UByte getUByte() throws SQLException {
return UByte.valueOf(getLong());
}

public short getShort() throws SQLException {
return (short) getLong();
}

@Override
public UShort getUShort() throws SQLException {
return UShort.valueOf(String.valueOf(getLong()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems a bit convoluted

Copy link
Member Author

@strongduanmu strongduanmu Aug 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the UShort.valueOf method does not support long type parameters.

}

public int getInt() throws SQLException {
return (int) getLong();
}

@Override
public UInteger getUInt() throws SQLException {
return UInteger.valueOf(getLong());
}

public long getLong() throws SQLException {
throw cannotConvert("long");
}

@Override
public ULong getULong() throws SQLException {
return ULong.valueOf(getBigDecimal().toBigInteger());
}

public float getFloat() throws SQLException {
return (float) getDouble();
}
Expand Down Expand Up @@ -485,6 +510,30 @@ public long getLong() throws SQLException {
}
}

/**
* Accessor that assumes that the underlying value is a {@link UByte};
* corresponds to {@link java.sql.Types#TINYINT} with unsigned flag.
*/
private static class UByteAccessor extends ExactNumericAccessor {
private UByteAccessor(Getter getter) {
super(getter);
}

public UByte getUByte() throws SQLException {
Object obj = getObject();
if (null == obj) {
return UByte.valueOf(0);
} else if (obj instanceof Integer) {
return UByte.valueOf(((Integer) obj).byteValue());
}
return UByte.valueOf(String.valueOf(obj));
}

public long getLong() throws SQLException {
return getUByte().longValue();
}
}

/**
* Accessor that assumes that the underlying value is a {@link Short};
* corresponds to {@link java.sql.Types#SMALLINT}.
Expand All @@ -509,6 +558,30 @@ public long getLong() throws SQLException {
}
}

/**
* Accessor that assumes that the underlying value is a {@link UShort};
* corresponds to {@link java.sql.Types#SMALLINT} with unsigned flag.
*/
private static class UShortAccessor extends ExactNumericAccessor {
private UShortAccessor(Getter getter) {
super(getter);
}

public UShort getUShort() throws SQLException {
Object obj = getObject();
if (null == obj) {
return UShort.valueOf(0);
} else if (obj instanceof Integer) {
return UShort.valueOf(((Integer) obj).shortValue());
}
return UShort.valueOf(String.valueOf(obj));
}

public long getLong() throws SQLException {
return getUShort().longValue();
}
}

/**
* Accessor that assumes that the underlying value is an {@link Integer};
* corresponds to {@link java.sql.Types#INTEGER}.
Expand All @@ -528,6 +601,25 @@ public long getLong() throws SQLException {
}
}

/**
* Accessor that assumes that the underlying value is an {@link UInteger};
* corresponds to {@link java.sql.Types#INTEGER} with unsigned flag.
*/
private static class UIntAccessor extends ExactNumericAccessor {
private UIntAccessor(Getter getter) {
super(getter);
}

public UInteger getUInt() throws SQLException {
Object o = getObject();
return UInteger.valueOf(o == null ? "0" : String.valueOf(o));
}

public long getLong() throws SQLException {
return getUInt().longValue();
}
}

/**
* Accessor that assumes that the underlying value is a {@link Long};
* corresponds to {@link java.sql.Types#BIGINT}.
Expand All @@ -543,6 +635,26 @@ public long getLong() throws SQLException {
}
}

/**
* Accessor that assumes that the underlying value is a {@link ULong};
* corresponds to {@link java.sql.Types#BIGINT} with unsigned flag.
*/
private static class ULongAccessor extends ExactNumericAccessor {
private ULongAccessor(Getter getter) {
super(getter);
}

public ULong getULong() throws SQLException {
Object o = getObject();
return ULong.valueOf(o == null ? "0" : String.valueOf(o));
}

@Override
public long getLong() throws SQLException {
return getULong().longValue();
}
}

/**
* Accessor of values that are {@link Double} or null.
*/
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/java/org/apache/calcite/avatica/util/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

import org.apache.calcite.avatica.ColumnMetaData;

import org.joou.UByte;
import org.joou.UInteger;
import org.joou.ULong;
import org.joou.UShort;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
Expand Down Expand Up @@ -87,12 +92,20 @@ interface Accessor {

byte getByte() throws SQLException;

UByte getUByte() throws SQLException;

short getShort() throws SQLException;

UShort getUShort() throws SQLException;

int getInt() throws SQLException;

UInteger getUInt() throws SQLException;

long getLong() throws SQLException;

ULong getULong() throws SQLException;

float getFloat() throws SQLException;

double getDouble() throws SQLException;
Expand Down
Loading
Loading