Skip to content

Commit ebaed66

Browse files
committed
introduce the joou library to support UByte, UShort, UInteger and ULong
1 parent 314724f commit ebaed66

File tree

10 files changed

+152
-17
lines changed

10 files changed

+152
-17
lines changed

bom/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies {
5454
apiv("com.google.protobuf:protobuf-java", "protobuf")
5555
apiv("com.h2database:h2")
5656
apiv("javax.servlet:javax.servlet-api", "servlet")
57+
apiv("org.jooq:joou-java-6", "joou-java-6")
5758
apiv("junit:junit")
5859
apiv("net.bytebuddy:byte-buddy", "bytebuddy")
5960
apiv("net.bytebuddy:byte-buddy-agent", "bytebuddy")

core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies {
3434
api("com.fasterxml.jackson.core:jackson-annotations")
3535
api("com.fasterxml.jackson.core:jackson-databind")
3636
api("com.google.protobuf:protobuf-java")
37+
api("org.jooq:joou-java-6")
3738
implementation("com.fasterxml.jackson.core:jackson-core")
3839
implementation("org.apache.httpcomponents.client5:httpclient5")
3940
implementation("org.apache.httpcomponents.core5:httpcore5")

core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
305305
return accessor.getArray();
306306
case Types.BIGINT:
307307
if (!signed) {
308-
return accessor.wasNull() ? null : accessor.getBigDecimal().toBigInteger();
308+
return accessor.wasNull() ? null : accessor.getULong();
309309
}
310310
final long aLong = accessor.getLong();
311311
if (aLong == 0 && accessor.wasNull()) {
@@ -341,7 +341,7 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
341341
return aDouble;
342342
case Types.INTEGER:
343343
if (!signed) {
344-
return accessor.wasNull() ? null : accessor.getLong();
344+
return accessor.wasNull() ? null : accessor.getUInt();
345345
}
346346
final int anInt = accessor.getInt();
347347
if (anInt == 0 && accessor.wasNull()) {
@@ -369,7 +369,7 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
369369
throw notImplemented();
370370
case Types.SMALLINT:
371371
if (!signed) {
372-
return accessor.wasNull() ? null : accessor.getInt();
372+
return accessor.wasNull() ? null : accessor.getUShort();
373373
}
374374
final short aShort = accessor.getShort();
375375
if (aShort == 0 && accessor.wasNull()) {
@@ -382,7 +382,7 @@ public static Object get(Cursor.Accessor accessor, int targetSqlType,
382382
return accessor.getTimestamp(localCalendar);
383383
case Types.TINYINT:
384384
if (!signed) {
385-
return accessor.wasNull() ? null : accessor.getShort();
385+
return accessor.wasNull() ? null : accessor.getUByte();
386386
}
387387
final byte aByte = accessor.getByte();
388388
if (aByte == 0 && accessor.wasNull()) {

core/src/main/java/org/apache/calcite/avatica/ColumnMetaData.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import com.fasterxml.jackson.annotation.JsonTypeInfo;
2626
import com.google.protobuf.Descriptors.FieldDescriptor;
2727

28+
import org.joou.UByte;
29+
import org.joou.UInteger;
30+
import org.joou.ULong;
31+
import org.joou.UShort;
32+
2833
import java.lang.reflect.Type;
2934
import java.math.BigDecimal;
3035
import java.sql.Array;
@@ -327,10 +332,14 @@ public enum Rep {
327332
PRIMITIVE_DOUBLE(double.class, Types.DOUBLE),
328333
BOOLEAN(Boolean.class, Types.BOOLEAN),
329334
BYTE(Byte.class, Types.TINYINT),
335+
UBYTE(UByte.class, Types.TINYINT),
330336
CHARACTER(Character.class, Types.CHAR),
331337
SHORT(Short.class, Types.SMALLINT),
338+
USHORT(UShort.class, Types.SMALLINT),
332339
INTEGER(Integer.class, Types.INTEGER),
340+
UINTEGER(UInteger.class, Types.INTEGER),
333341
LONG(Long.class, Types.BIGINT),
342+
ULONG(ULong.class, Types.BIGINT),
334343
FLOAT(Float.class, Types.FLOAT),
335344
DOUBLE(Double.class, Types.DOUBLE),
336345
JAVA_SQL_TIME(Time.class, Types.TIME),

core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
import org.apache.calcite.avatica.AvaticaUtils;
2121
import org.apache.calcite.avatica.ColumnMetaData;
2222

23+
import org.joou.UByte;
24+
import org.joou.UInteger;
25+
import org.joou.ULong;
26+
import org.joou.UShort;
27+
2328
import java.io.InputStream;
2429
import java.io.Reader;
2530
import java.lang.reflect.Field;
@@ -102,15 +107,13 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,
102107
}
103108
switch (columnMetaData.type.id) {
104109
case Types.TINYINT:
105-
return columnMetaData.signed ? new ByteAccessor(getter) : new ShortAccessor(getter);
110+
return columnMetaData.signed ? new ByteAccessor(getter) : new UByteAccessor(getter);
106111
case Types.SMALLINT:
107-
return columnMetaData.signed ? new ShortAccessor(getter) : new IntAccessor(getter);
112+
return columnMetaData.signed ? new ShortAccessor(getter) : new UShortAccessor(getter);
108113
case Types.INTEGER:
109-
return columnMetaData.signed ? new IntAccessor(getter) : new LongAccessor(getter);
114+
return columnMetaData.signed ? new IntAccessor(getter) : new UIntAccessor(getter);
110115
case Types.BIGINT:
111-
return columnMetaData.signed
112-
? new LongAccessor(getter)
113-
: new NumberAccessor(getter, columnMetaData.scale);
116+
return columnMetaData.signed ? new LongAccessor(getter) : new ULongAccessor(getter);
114117
case Types.BOOLEAN:
115118
case Types.BIT:
116119
return new BooleanAccessor(getter);
@@ -290,18 +293,38 @@ public byte getByte() throws SQLException {
290293
return (byte) getLong();
291294
}
292295

296+
@Override
297+
public UByte getUByte() throws SQLException {
298+
return UByte.valueOf(getLong());
299+
}
300+
293301
public short getShort() throws SQLException {
294302
return (short) getLong();
295303
}
296304

305+
@Override
306+
public UShort getUShort() throws SQLException {
307+
return UShort.valueOf(String.valueOf(getLong()));
308+
}
309+
297310
public int getInt() throws SQLException {
298311
return (int) getLong();
299312
}
300313

314+
@Override
315+
public UInteger getUInt() throws SQLException {
316+
return UInteger.valueOf(getLong());
317+
}
318+
301319
public long getLong() throws SQLException {
302320
throw cannotConvert("long");
303321
}
304322

323+
@Override
324+
public ULong getULong() throws SQLException {
325+
return ULong.valueOf(getBigDecimal().toBigInteger());
326+
}
327+
305328
public float getFloat() throws SQLException {
306329
return (float) getDouble();
307330
}
@@ -487,6 +510,30 @@ public long getLong() throws SQLException {
487510
}
488511
}
489512

513+
/**
514+
* Accessor that assumes that the underlying value is a {@link UByte};
515+
* corresponds to {@link java.sql.Types#TINYINT} with unsigned flag.
516+
*/
517+
private static class UByteAccessor extends ExactNumericAccessor {
518+
private UByteAccessor(Getter getter) {
519+
super(getter);
520+
}
521+
522+
public UByte getUByte() throws SQLException {
523+
Object obj = getObject();
524+
if (null == obj) {
525+
return UByte.valueOf(0);
526+
} else if (obj instanceof Integer) {
527+
return UByte.valueOf(((Integer) obj).byteValue());
528+
}
529+
return UByte.valueOf(String.valueOf(obj));
530+
}
531+
532+
public long getLong() throws SQLException {
533+
return getUByte().longValue();
534+
}
535+
}
536+
490537
/**
491538
* Accessor that assumes that the underlying value is a {@link Short};
492539
* corresponds to {@link java.sql.Types#SMALLINT}.
@@ -511,6 +558,30 @@ public long getLong() throws SQLException {
511558
}
512559
}
513560

561+
/**
562+
* Accessor that assumes that the underlying value is a {@link UShort};
563+
* corresponds to {@link java.sql.Types#SMALLINT} with unsigned flag.
564+
*/
565+
private static class UShortAccessor extends ExactNumericAccessor {
566+
private UShortAccessor(Getter getter) {
567+
super(getter);
568+
}
569+
570+
public UShort getUShort() throws SQLException {
571+
Object obj = getObject();
572+
if (null == obj) {
573+
return UShort.valueOf(0);
574+
} else if (obj instanceof Integer) {
575+
return UShort.valueOf(((Integer) obj).shortValue());
576+
}
577+
return UShort.valueOf(String.valueOf(obj));
578+
}
579+
580+
public long getLong() throws SQLException {
581+
return getUShort().longValue();
582+
}
583+
}
584+
514585
/**
515586
* Accessor that assumes that the underlying value is an {@link Integer};
516587
* corresponds to {@link java.sql.Types#INTEGER}.
@@ -530,6 +601,25 @@ public long getLong() throws SQLException {
530601
}
531602
}
532603

604+
/**
605+
* Accessor that assumes that the underlying value is an {@link UInteger};
606+
* corresponds to {@link java.sql.Types#INTEGER} with unsigned flag.
607+
*/
608+
private static class UIntAccessor extends ExactNumericAccessor {
609+
private UIntAccessor(Getter getter) {
610+
super(getter);
611+
}
612+
613+
public UInteger getUInt() throws SQLException {
614+
Object o = getObject();
615+
return UInteger.valueOf(o == null ? "0" : String.valueOf(o));
616+
}
617+
618+
public long getLong() throws SQLException {
619+
return getUInt().longValue();
620+
}
621+
}
622+
533623
/**
534624
* Accessor that assumes that the underlying value is a {@link Long};
535625
* corresponds to {@link java.sql.Types#BIGINT}.
@@ -545,6 +635,26 @@ public long getLong() throws SQLException {
545635
}
546636
}
547637

638+
/**
639+
* Accessor that assumes that the underlying value is a {@link ULong};
640+
* corresponds to {@link java.sql.Types#BIGINT} with unsigned flag.
641+
*/
642+
private static class ULongAccessor extends ExactNumericAccessor {
643+
private ULongAccessor(Getter getter) {
644+
super(getter);
645+
}
646+
647+
public ULong getULong() throws SQLException {
648+
Object o = getObject();
649+
return ULong.valueOf(o == null ? "0" : String.valueOf(o));
650+
}
651+
652+
@Override
653+
public long getLong() throws SQLException {
654+
return getULong().longValue();
655+
}
656+
}
657+
548658
/**
549659
* Accessor of values that are {@link Double} or null.
550660
*/

core/src/main/java/org/apache/calcite/avatica/util/Cursor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
import org.apache.calcite.avatica.ColumnMetaData;
2020

21+
import org.joou.UByte;
22+
import org.joou.UInteger;
23+
import org.joou.ULong;
24+
import org.joou.UShort;
25+
2126
import java.io.InputStream;
2227
import java.io.Reader;
2328
import java.math.BigDecimal;
@@ -87,12 +92,20 @@ interface Accessor {
8792

8893
byte getByte() throws SQLException;
8994

95+
UByte getUByte() throws SQLException;
96+
9097
short getShort() throws SQLException;
9198

99+
UShort getUShort() throws SQLException;
100+
92101
int getInt() throws SQLException;
93102

103+
UInteger getUInt() throws SQLException;
104+
94105
long getLong() throws SQLException;
95106

107+
ULong getULong() throws SQLException;
108+
96109
float getFloat() throws SQLException;
97110

98111
double getDouble() throws SQLException;

core/src/test/java/org/apache/calcite/avatica/CursorFactoryDeduceTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,15 @@ protected static class SimplePOJO {
305305
@Test public void deduceRecordCursorFactoryProjectedUnsignedField() throws Exception {
306306
List<ColumnMetaData> columnsMetaDataList = Arrays.asList(
307307
MetaImpl.columnMetaData("shortField", 1, ColumnMetaData.scalar(
308-
Types.TINYINT, "TINYINT_UNSIGNED", ColumnMetaData.Rep.SHORT), true, false),
308+
Types.TINYINT, "TINYINT_UNSIGNED", ColumnMetaData.Rep.UBYTE), true, false),
309309
MetaImpl.columnMetaData("intField", 2, ColumnMetaData.scalar(
310-
Types.SMALLINT, "SMALLINT_UNSIGNED", ColumnMetaData.Rep.INTEGER), true, false),
310+
Types.SMALLINT, "SMALLINT_UNSIGNED", ColumnMetaData.Rep.USHORT), true, false),
311311
MetaImpl.columnMetaData("longField", 3, ColumnMetaData.scalar(
312-
Types.INTEGER, "MEDIUMINT_UNSIGNED", ColumnMetaData.Rep.LONG), true, false),
312+
Types.INTEGER, "MEDIUMINT_UNSIGNED", ColumnMetaData.Rep.UINTEGER), true, false),
313313
MetaImpl.columnMetaData("longField", 4, ColumnMetaData.scalar(
314-
Types.INTEGER, "INT_UNSIGNED", ColumnMetaData.Rep.LONG), true, false),
314+
Types.INTEGER, "INT_UNSIGNED", ColumnMetaData.Rep.UINTEGER), true, false),
315315
MetaImpl.columnMetaData("bigIntField", 5, ColumnMetaData.scalar(
316-
Types.BIGINT, "BIGINT_UNSIGNED", ColumnMetaData.Rep.NUMBER), true, false)
316+
Types.BIGINT, "BIGINT_UNSIGNED", ColumnMetaData.Rep.ULONG), true, false)
317317
);
318318
Meta.CursorFactory cursorFactory =
319319
Meta.CursorFactory.deduce(columnsMetaDataList, SimplePOJO.class);

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ jackson.version=2.15.4
7171
jcip-annotations.version=1.0-1
7272
jcommander.version=1.72
7373
jetty.version=9.4.56.v20240826
74+
joou-java-6.version=0.9.4
7475
junit.version=4.12
7576
kerby.version=1.1.1
7677
log4j2.version=2.17.1

shaded/core/src/test/java/org/apache/calcite/avatica/shadetest/ShadingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class ShadingTest {
3535
// Note that many of these files could be excluded.
3636
// This is for regression testing, and not the minimum file set.
3737
String[] allowedPathPatterns = { "^META-INF", "^org/apache/calcite/", ".*\\.proto",
38-
"^org/slf4j/", "^org/publicsuffix/" };
38+
"^org/slf4j/", "^org/publicsuffix/", "^org/joou/" };
3939

4040
@Test
4141
public void validateShadedJar() throws Exception {

standalone-server/src/test/java/org/apache/calcite/avatica/shadetest/ShadingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ShadingTest {
3939
"^google/protobuf/", "^about.html$", "^org/eclipse/jetty/", "^jetty-dir.css",
4040
"^com/google/thirdparty/", "^org/checkerframework/", "^javax/annotation/",
4141
"^com/google/errorprone/", "^Log4j-.*\\.xsd$", "^Log4j-.*\\.dtd$", "^Log4j-.*\\.properties$",
42-
"^org/apache/logging/log4j/", "^org/codehaus/mojo/animal_sniffer/" };
42+
"^org/apache/logging/log4j/", "^org/codehaus/mojo/animal_sniffer/", "^org/joou/" };
4343

4444
@Test
4545
public void validateShadedJar() throws Exception {

0 commit comments

Comments
 (0)