Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuzelin committed Aug 3, 2024
1 parent 7ee3b7d commit 8799497
Showing 1 changed file with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.apache.paimon.utils.BooleanArrayList;
import org.apache.paimon.utils.LongArrayList;

import java.util.Arrays;

import static java.lang.String.format;

/** Utils to calculate nested type position. */
Expand Down Expand Up @@ -108,42 +106,29 @@ public static CollectionPosition calculateCollectionOffsets(
i < definitionLevels.length;
i = getNextCollectionStartIndex(repetitionLevels, collectionRepetitionLevel, i)) {
if (definitionLevels[i] >= collectionDefinitionLevel - 1) {
valueCount++;

boolean isNull =
isOptionalFieldValueNull(definitionLevels[i], collectionDefinitionLevel);
if (isNull) {
nullCollectionFlags.add(true);
nullValuesCount++;
// 1. don't increase offset for null values
// 2. offsets and emptyCollectionFlags are meaningless for null values, but they
// must be set at each index for calculating lengths later
offsets.add(offset);
emptyCollectionFlags.add(false);
continue;
}

nullCollectionFlags.add(false);
// definitionLevels[i] > collectionDefinitionLevel => Collection is defined and not
// empty
// definitionLevels[i] == collectionDefinitionLevel => Collection is defined but
// empty
// definitionLevels[i] == maxDefinitionLevel - 1 => Collection is defined but null
if (definitionLevels[i] > collectionDefinitionLevel) {
nullCollectionFlags.add(false);
emptyCollectionFlags.add(false);
offset += getCollectionSize(repetitionLevels, collectionRepetitionLevel, i + 1);
} else if (definitionLevels[i] == collectionDefinitionLevel) {
// don't increase offset for empty values
nullCollectionFlags.add(false);
emptyCollectionFlags.add(true);
// don't increase offset for empty values
} else {
throw new IllegalStateException(
String.format(
"This case should be handled as null value. "
+ "index: %d, definitionLevels: %s, collectionDefinitionLevel: %s.",
i,
Arrays.toString(definitionLevels),
collectionDefinitionLevel));
nullCollectionFlags.add(true);
nullValuesCount++;
// 1. don't increase offset for null values
// 2. offsets and emptyCollectionFlags are meaningless for null values, but they
// must be set at each index for calculating lengths later
emptyCollectionFlags.add(false);
}
offsets.add(offset);
valueCount++;
}
// else when definitionLevels[i] < collectionDefinitionLevel - 1, it means the
// collection is not defined, just ignore it
Expand All @@ -157,10 +142,6 @@ public static CollectionPosition calculateCollectionOffsets(
nullCollectionFlags.toArray(), offsetsArray, length, valueCount);
}

public static boolean isOptionalFieldValueNull(int definitionLevel, int maxDefinitionLevel) {
return definitionLevel == maxDefinitionLevel - 1;
}

public static long[] calculateLengthByOffsets(
boolean[] collectionIsEmpty, long[] arrayOffsets) {
LongArrayList lengthList = new LongArrayList(arrayOffsets.length);
Expand Down

0 comments on commit 8799497

Please sign in to comment.