Skip to content

Commit

Permalink
ARROW-9377: [Java] Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
liyafan82 committed Aug 4, 2020
1 parent d2cb8e2 commit a5911cb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
14 changes: 12 additions & 2 deletions java/vector/src/main/java/org/apache/arrow/vector/UInt1Vector.java
Expand Up @@ -36,6 +36,16 @@
* maintained to track which elements in the vector are null.
*/
public final class UInt1Vector extends BaseFixedWidthVector implements BaseIntVector {
/**
* The mask to use when promoting the unsigned byte value to an integer.
*/
public static final int PROMOTION_MASK = 0xFF;

/**
* The maximum 8-bit unsigned integer.
*/
public static final byte MAX_UINT1 = (byte) 0XFF;

private static final byte TYPE_WIDTH = 1;
private final FieldReader reader;

Expand Down Expand Up @@ -83,7 +93,7 @@ public MinorType getMinorType() {
*/
public static short getNoOverflow(final ArrowBuf buffer, final int index) {
byte b = buffer.getByte(index * TYPE_WIDTH);
return (short) (0xFF & b);
return (short) (PROMOTION_MASK & b);
}


Expand Down Expand Up @@ -315,7 +325,7 @@ public void setUnsafeWithPossibleTruncate(int index, long value) {

@Override
public long getValueAsLong(int index) {
return this.get(index) & 0xffL;
return this.get(index) & PROMOTION_MASK;
}


Expand Down
Expand Up @@ -36,6 +36,12 @@
* maintained to track which elements in the vector are null.
*/
public final class UInt2Vector extends BaseFixedWidthVector implements BaseIntVector {

/**
* The maximum 16-bit unsigned integer.
*/
public static final char MAX_UINT2 = (char) 0XFFFF;

private static final byte TYPE_WIDTH = 2;
private final FieldReader reader;

Expand Down
15 changes: 13 additions & 2 deletions java/vector/src/main/java/org/apache/arrow/vector/UInt4Vector.java
Expand Up @@ -36,6 +36,17 @@
* maintained to track which elements in the vector are null.
*/
public final class UInt4Vector extends BaseFixedWidthVector implements BaseIntVector {

/**
* The mask to use when promoting the unsigned int value to a long int.
*/
public static final long PROMOTION_MASK = 0x00000000FFFFFFFFL;

/**
* The maximum 32-bit unsigned integer.
*/
public static final int MAX_UINT4 = 0XFFFFFFFF;

private static final byte TYPE_WIDTH = 4;
private final FieldReader reader;

Expand Down Expand Up @@ -83,7 +94,7 @@ public MinorType getMinorType() {
*/
public static long getNoOverflow(final ArrowBuf buffer, final int index) {
long l = buffer.getInt((long) index * TYPE_WIDTH);
return (0x00000000FFFFFFFFL) & l;
return PROMOTION_MASK & l;
}

/**
Expand Down Expand Up @@ -286,7 +297,7 @@ public void setUnsafeWithPossibleTruncate(int index, long value) {

@Override
public long getValueAsLong(int index) {
return this.get(index) & 0xffffffffL;
return this.get(index) & PROMOTION_MASK;
}

private class TransferImpl implements TransferPair {
Expand Down
Expand Up @@ -38,6 +38,12 @@
* maintained to track which elements in the vector are null.
*/
public final class UInt8Vector extends BaseFixedWidthVector implements BaseIntVector {

/**
* The maximum 64-bit unsigned long integer.
*/
public static final long MAX_UINT8 = 0XFFFFFFFFFFFFFFFFL;

private static final byte TYPE_WIDTH = 8;
private final FieldReader reader;

Expand Down
Expand Up @@ -911,7 +911,8 @@ public void testDictionaryUInt1() {
try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) {
setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
Dictionary dictionary1 = new Dictionary(dictionaryVector,
new DictionaryEncoding(/*id=*/10L, /*ordered=*/false, /*indexType=*/ new ArrowType.Int(8, false)));
new DictionaryEncoding(/*id=*/10L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(/*bitWidth*/8, /*isSigned*/false)));
testDictionary(dictionary1, (vector, index) -> ((UInt1Vector) vector).get(index));
}
}
Expand All @@ -921,7 +922,8 @@ public void testDictionaryUInt2() {
try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) {
setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
Dictionary dictionary2 = new Dictionary(dictionaryVector,
new DictionaryEncoding(/*id=*/20L, /*ordered=*/false, /*indexType=*/ new ArrowType.Int(16, false)));
new DictionaryEncoding(/*id=*/20L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(/*indexType=*/16, /*isSigned*/false)));
testDictionary(dictionary2, (vector, index) -> ((UInt2Vector) vector).get(index));
}
}
Expand All @@ -931,7 +933,8 @@ public void testDictionaryUInt4() {
try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) {
setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
Dictionary dictionary4 = new Dictionary(dictionaryVector,
new DictionaryEncoding(/*id=*/30L, /*ordered=*/false, /*indexType=*/ new ArrowType.Int(32, false)));
new DictionaryEncoding(/*id=*/30L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(/*indexType=*/32, /*isSigned*/false)));
testDictionary(dictionary4, (vector, index) -> ((UInt4Vector) vector).get(index));
}
}
Expand All @@ -941,7 +944,8 @@ public void testDictionaryUInt8() {
try (VarCharVector dictionaryVector = new VarCharVector("dict vector", allocator)) {
setVector(dictionaryVector, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
Dictionary dictionary8 = new Dictionary(dictionaryVector,
new DictionaryEncoding(/*id=*/40L, /*ordered=*/false, /*indexType=*/ new ArrowType.Int(64, false)));
new DictionaryEncoding(/*id=*/40L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(/*indexType=*/64, /*isSigned*/false)));
testDictionary(dictionary8, (vector, index) -> (int) ((UInt8Vector) vector).get(index));
}
}
Expand All @@ -958,7 +962,8 @@ public void testDictionaryUIntOverflow() {
dictionaryVector.setValueCount(vecLength);

Dictionary dictionary = new Dictionary(dictionaryVector,
new DictionaryEncoding(/*id=*/10L, /*ordered=*/false, /*indexType=*/ new ArrowType.Int(8, false)));
new DictionaryEncoding(/*id=*/10L, /*ordered=*/false,
/*indexType=*/new ArrowType.Int(/*indexType=*/8, /*isSigned*/false)));

try (VarCharVector vector = new VarCharVector("vector", allocator)) {
setVector(vector, "255");
Expand Down
Expand Up @@ -2983,12 +2983,12 @@ public void testSetGetUInt1() {
try (UInt1Vector vector = new UInt1Vector("vector", allocator)) {
vector.allocateNew(2);

vector.setWithPossibleTruncate(0, 0xffL);
vector.setUnsafeWithPossibleTruncate(1, 0xffL);
vector.setWithPossibleTruncate(0, UInt1Vector.MAX_UINT1);
vector.setUnsafeWithPossibleTruncate(1, UInt1Vector.MAX_UINT1);
vector.setValueCount(2);

assertEquals(255, vector.getValueAsLong(0));
assertEquals(255, vector.getValueAsLong(1));
assertEquals(UInt1Vector.MAX_UINT1 & UInt1Vector.PROMOTION_MASK, vector.getValueAsLong(0));
assertEquals(UInt1Vector.MAX_UINT1 & UInt1Vector.PROMOTION_MASK, vector.getValueAsLong(1));
}
}

Expand All @@ -2997,12 +2997,12 @@ public void testSetGetUInt2() {
try (UInt2Vector vector = new UInt2Vector("vector", allocator)) {
vector.allocateNew(2);

vector.setWithPossibleTruncate(0, 0xffffL);
vector.setUnsafeWithPossibleTruncate(1, 0xffffL);
vector.setWithPossibleTruncate(0, UInt2Vector.MAX_UINT2);
vector.setUnsafeWithPossibleTruncate(1, UInt2Vector.MAX_UINT2);
vector.setValueCount(2);

assertEquals(65535, vector.getValueAsLong(0));
assertEquals(65535, vector.getValueAsLong(1));
assertEquals(UInt2Vector.MAX_UINT2, vector.getValueAsLong(0));
assertEquals(UInt2Vector.MAX_UINT2, vector.getValueAsLong(1));
}
}

Expand All @@ -3011,11 +3011,11 @@ public void testSetGetUInt4() {
try (UInt4Vector vector = new UInt4Vector("vector", allocator)) {
vector.allocateNew(2);

vector.setWithPossibleTruncate(0, 0xffffffffL);
vector.setUnsafeWithPossibleTruncate(1, 0xffffffffL);
vector.setWithPossibleTruncate(0, UInt4Vector.MAX_UINT4);
vector.setUnsafeWithPossibleTruncate(1, UInt4Vector.MAX_UINT4);
vector.setValueCount(2);

long expected = (1L << 32) - 1L;
long expected = UInt4Vector.MAX_UINT4 & UInt4Vector.PROMOTION_MASK;
assertEquals(expected, vector.getValueAsLong(0));
assertEquals(expected, vector.getValueAsLong(1));
}
Expand Down

0 comments on commit a5911cb

Please sign in to comment.