use of org.apache.commons.lang3.tuple.MutablePair in project hive by apache.
the class DecimalIntervalSplitter method getIntervals.
@Override
public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions, TypeInfo typeInfo) {
List<MutablePair<String, String>> intervals = new ArrayList<>();
DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo;
int scale = decimalTypeInfo.getScale();
BigDecimal decimalLower = new BigDecimal(lowerBound);
BigDecimal decimalUpper = new BigDecimal(upperBound);
BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions), MathContext.DECIMAL64);
BigDecimal splitDecimalLower, splitDecimalUpper;
for (int i = 0; i < numPartitions; i++) {
splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale, RoundingMode.HALF_EVEN);
splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i + 1))).setScale(scale, RoundingMode.HALF_EVEN);
if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) {
intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(), splitDecimalUpper.toPlainString()));
}
}
return intervals;
}
use of org.apache.commons.lang3.tuple.MutablePair in project hive by apache.
the class HiveKVResultCache method next.
public synchronized Tuple2<HiveKey, BytesWritable> next() {
Preconditions.checkState(hasNext());
if (!readBufferUsed) {
try {
if (input == null && output != null) {
// Close output stream if open
output.close();
output = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
input = new Input(fis);
} finally {
if (input == null && fis != null) {
fis.close();
}
}
}
if (input != null) {
// Load next batch from disk
for (int i = 0; i < IN_MEMORY_NUM_ROWS; i++) {
MutablePair<HiveKey, BytesWritable> pair = readBuffer[i];
pair.setLeft(readHiveKey(input));
pair.setRight(readValue(input));
}
if (input.end()) {
input.close();
input = null;
}
rowsInReadBuffer = IN_MEMORY_NUM_ROWS;
readBufferUsed = true;
readCursor = 0;
} else if (writeCursor == 1) {
MutablePair<HiveKey, BytesWritable> pair = writeBuffer[0];
Tuple2<HiveKey, BytesWritable> row = new Tuple2<HiveKey, BytesWritable>(pair.getLeft(), pair.getRight());
pair.setLeft(null);
pair.setRight(null);
writeCursor = 0;
return row;
} else {
// No record on disk, more data in write buffer
switchBufferAndResetCursor();
}
} catch (Exception e) {
// Clean up the cache
clear();
throw new RuntimeException("Failed to load rows from disk", e);
}
}
MutablePair<HiveKey, BytesWritable> pair = readBuffer[readCursor];
Tuple2<HiveKey, BytesWritable> row = new Tuple2<HiveKey, BytesWritable>(pair.getLeft(), pair.getRight());
pair.setLeft(null);
pair.setRight(null);
if (++readCursor >= rowsInReadBuffer) {
readBufferUsed = false;
rowsInReadBuffer = 0;
readCursor = 0;
}
return row;
}
Aggregations