in java/tsfile/src/main/java/org/apache/tsfile/read/common/Chunk.java [218:315]
public Chunk rewrite(TSDataType newType, Chunk timeChunk) throws IOException {
if (newType == null || newType == chunkHeader.getDataType()) {
return this;
}
TSEncoding encoding =
TSEncoding.valueOf(TSFileDescriptor.getInstance().getConfig().getValueEncoder(newType));
IMeasurementSchema schema =
new MeasurementSchema(
chunkHeader.getMeasurementID(), newType, encoding, chunkHeader.getCompressionType());
ValueChunkWriter chunkWriter =
new ValueChunkWriter(
chunkHeader.getMeasurementID(),
chunkHeader.getCompressionType(),
newType,
encoding,
schema.getValueEncoder(),
encryptParam);
List<Chunk> valueChunks = new ArrayList<>();
valueChunks.add(this);
TableChunkReader chunkReader = new TableChunkReader(timeChunk, valueChunks, null);
List<IPageReader> pages = chunkReader.loadPageReaderList();
for (IPageReader page : pages) {
IPointReader pointReader = page.getAllSatisfiedPageData().getBatchDataIterator();
while (pointReader.hasNextTimeValuePair()) {
TimeValuePair point = pointReader.nextTimeValuePair();
Object convertedValue = null;
if (point.getValue().getVector()[0] != null) {
convertedValue =
newType.castFromSingleValue(
chunkHeader.getDataType(), point.getValue().getVector()[0].getValue());
}
long timestamp = point.getTimestamp();
switch (newType) {
case BOOLEAN:
chunkWriter.write(
timestamp,
convertedValue == null ? true : (boolean) convertedValue,
convertedValue == null);
break;
case DATE:
case INT32:
chunkWriter.write(
timestamp,
convertedValue == null ? Integer.MAX_VALUE : (int) convertedValue,
convertedValue == null);
break;
case TIMESTAMP:
case INT64:
chunkWriter.write(
timestamp,
convertedValue == null ? (long) Integer.MAX_VALUE : (long) convertedValue,
convertedValue == null);
break;
case FLOAT:
chunkWriter.write(
timestamp,
convertedValue == null ? (float) Integer.MAX_VALUE : (float) convertedValue,
convertedValue == null);
break;
case DOUBLE:
chunkWriter.write(
timestamp,
convertedValue == null ? (double) Integer.MAX_VALUE : (double) convertedValue,
convertedValue == null);
break;
case TEXT:
case STRING:
case BLOB:
chunkWriter.write(
timestamp,
convertedValue == null ? Binary.EMPTY_VALUE : (Binary) convertedValue,
convertedValue == null);
break;
default:
throw new IOException("Unsupported data type: " + newType);
}
}
chunkWriter.sealCurrentPage();
}
ByteBuffer newChunkData = chunkWriter.getByteBuffer();
ChunkHeader newChunkHeader =
new ChunkHeader(
chunkHeader.getChunkType(),
chunkHeader.getMeasurementID(),
newChunkData.capacity(),
newType,
chunkHeader.getCompressionType(),
encoding);
chunkData.flip();
timeChunk.chunkData.flip();
return new Chunk(
newChunkHeader,
newChunkData,
deleteIntervalList,
chunkWriter.getStatistics(),
encryptParam);
}